Although many of you supplied answers for my earlier
inquiry regarding finding the price of 1 year ago and setting it as a constant
(and I appreciate the response very much), I elected to use Roy Larsen's.
For reasons that I will spare you, I now have decided to use instead the last
closing price of last year as the constant. In other words, I will be
calculating the rate of return for the current calendar year both forward
(this is normal practice) and backward (not very common) based upon the
closing price of the last trade day of last year. I modified Roy's formula
as follows:
Sm:=12;
Sd:=28;
Sy:=LastValue(Year()-1);
StartDate:=(DayOfMonth()>=Sd AND
Month()=Sm
AND
Year()=Sy);
BeginYearPx:=LastValue(ValueWhen(1,StartDate,C));
100+(((C-BeginYearPx)/BeginYearPx)*100);
I know it is more than a little modified, but Roy's original formula introduced me to some
MetaStock functions I had never used before. THAT is the true value of
this forum. Yes, you may get the exact answer you need, but you can learn
from every answer (from guys like Roy, Jose, Richard and many more I should be
mentioning). By the way, I still plan
on using Roy's original formula for other purposes (with full credit, of
course). And, Jose, thank you for getting my brain kick-started by
pointing out the problem with simply accumulating percentage
gains/losses.
When I use the above as
the formula for the custom indicator, it generates the correct values. You
may notice that I also modified the last line (my addition) so that the
year always starts with a value of 100. The reason for this is that I
plan on additionally generating OHLC MetaStock format price files outside of
MetaStock and then converting those files to MetaStock (that code will be based
on the same formula). That way, I can display bar or candlestick charts
with the new "relative" OHLC prices. OHLC price files won't plot
negative values, so starting at 100 always keeps the value above 0 even when
going back previous to the beginning of the year or if the current price is down for the
year. In other
words, if the security is down 3% from the current year's
beginning price, the displayed value for that day will be 97. It
really seems to do a good job of keeping the displayed value
properly aligned with the actual price and current rate of
return without drift of any kind.
I
think I mentioned this before, but in case you are tuning in late,
where I am really going with this is to recalculate all the indexes and ETFs I
work with to equal weight composites. Most indexes are either cap weighted
like the S&P 500 Index or price weighted like the Dow Jones Industrial
Average. To do that, I need to apply this formula to each of 3400 components I
follow then recalculate the historical data
on the composites using the values given by the above formula and
then simply total them and divide by the number of components in the index or
ETF. Voila, an equal weighted composite (I think or hope).
Again,
I appreciate everyone's input on this.
Thank
you.
Best,
Larry Carhartt
MasterDATA Composite
Plug-in For MetaStock
The
Only Source for Index & ETF Composite/Breadth
Historical MetaStock Data
Roy,
Thank you again for the code on finding the price of a specific time
period ago and setting it as a constant. I added one more
simple line of code so this is what it looks like:
{Price 1 Year
OLD}
Sm:=LastValue(Month());
Sd:=LastValue(DayOfMonth());
Sy:=LastValue(Year()-1);
Start:=(DayOfMonth()>=Sd
AND Month()=Sm AND
Year()=Sy) OR Year()>Sy OR (Year()=Sy AND
Month()>Sm);
Date:=Cum(Start)=1;
{Date}
YearAgoPx:=LastValue(ValueWhen(1,Date,C));
((C-YearAgoPx)/YearAgoPx)*100
It
provides a perfectly aligned rate of change (absolutely no drift) for the
last one year of trading (updated daily). Furthermore, implementing
MetaStock's "Security" function now allows MetaStock to display charts of as
many indexes and ETFs as I wish all in one chart with values that are
comparable to one another (apples to apples). Prior to the one year
ago date, prices are still comparable, the one year ago price being zero for
any index or ETF compared (of course this would work with individual
securities also). This comparability opens up numerous
possibilities.
I
try to put as much calculation into MetaStock as possible (as opposed to my
plug-in) and your code did the trick in this case. You never cease to
amaze me. I will make certain that you (and your site) receive credit
for your assistance on my web site. Thank you again.
Best,
Larry
Carhartt
MasterDATA
The
Only Source for Index & ETF Composite/Breadth
Reports, Charts & Data
Here it is in a format that recognises 1 year
ago. The compare statement in the valuewhen also resolves the problem
of one year ago falling on a non-trading day.
{do not change
these}
daynum := Cum(1);
totaldays
:= LastValue(daynum);
fulldate :=
Year()+Month()/12+DayOfMonth()/365;
{config section}
constantneeded := C;
dateneeded :=
LastValue(fulldate)-1;
priceneeded := ValueWhen(1,yearagodate >
Ref(fulldate,-1) AND yearagodate <= fulldate,constantneeded
);
{Plot
priceneeded}
priceneeded;
Best regards,
Richard
Dale.
Norgate Investor Services
- Premium quality Stock, Futures and
Foreign Exchange Data for
markets in Australia, Asia, Canada,
Europe, UK & USA -
Is
there a way to set a constant value representing the price of a specific
date? For instance, ref(c,-250) would give you the close price from
approximately one year ago. With this formula, when
MetaStock calculates the indicator, the close of one year ago will be
calculated on each bar of data. What I need is the price one year ago
from today and that price remains a constant for all previous bars to
use in a calculation. I realize I could enter the constant manually,
but that does not allow for automation and certainly leaves room for
error.
Can this be done in MetaStock? I hope I am
being clear on what I need.