YTD ROC Returns Zero First Market Day of Year

Hello,

I have code that gives the year-to-date ROC for a security but it returns zero as the ROC for the first market day of the year:

YearStartClose = ValueWhen ( Year () != Ref( Year (), -1 ), Close); MyROC = (( C - YearStartClose ) / ( YearStartClose ))*100;

MyROC writes zero for the first day of the year. Any suggestions?

Mike Klein

@m, What value are you expecting?

If you walk through your formula and think about it, it has to be Zero.

The close of the first trading day of the year is your YearStartClose value. So, on the first day of the year, your have 0 point move away from the YearStartClose. So it has to be Zero.

If you had the Close of any other day be the same as the Close of the First day of the year, what value would you expect to get? Zero.

1 Like

No it is not zero. It is NULL.

Anyway without much talk... you have to convert NULL to 1 (TRUE) at first year check since Ref(Year(), -1) at first bar of array returns NULL.

new_year = Nz(Year() != Ref(Year(), -1), 1);
YearStartClose = ValueWhen(new_year, Close); 
MyROC = (C - YearStartClose) / YearStartClose*100;
1 Like

@fxshrat, I think you caught the initial case for the very first year set of bars.

Not sure if the OP was looking for that.

I assumed from the way the question was written, that he was shocked with the first day of a new trading year showing a Zero.

OK then in addition to that he might compare to the Open of first bar of each year if he doesn't want it to be zero there.

new_year = Nz(Year() != Ref(Year(), -1), 1);
YearStartClose = ValueWhen(new_year, Close); 
MyROC = iif(new_year, (C-O)/O, (C-YearStartClose) / YearStartClose)*100;
Plot(myRoc, "MyRoc", colorRed);

Or use Roc(C,1) instead of (C-O)/O or whatever...

1 Like

@mklein welcome to the forum. For your simple question we can give you too many answers but hope to make you feel welcome.

Assuming your are using daily bars here are alternate solutions to @fxshrat's.

// Create a variable period version of the Rate-of-Change Indicator
TradeDayOfYear = BarsSince( year() > Ref( year(), -1 ) ) + 1;

function VarROC( Close, TradeDayOfYear )
{
    prev = Ref( Close, -TradeDayOfYear );
    return 100 * ( Close - prev ) / prev;
}

// MultiTimeFrame functionality version
YrClose = TimeFrameGetPrice( "C", inYearly, -1 );
MyROC = ( ( C - YrClose ) / ( YrClose ) ) * 100;

// Explore to debug //
Filter = 1;
AddColumn( C, "Close", 1.3 );
AddColumn( TradeDayOfYear, "TradeDayOfYear", 1, colorBlue, colorDefault );
AddColumn( VarROC( C, TradeDayOfYear ), "YTD roc", 1.3 );
AddColumn( YrClose, "YrClose", 1.3 );
AddColumn( MyROC, "MyROC", 1.3 );

image

3 Likes

He compares to the close of first bar of each year so yours will get different results than his ones.


As aside another ones...

new_year = Nz(Year() != Ref(Year(), -1), 1);
YearStartOpen = ValueWhen(new_year, Open); 
MyROC = (C-YearStartOpen ) / YearStartOpen*100;
Plot(myRoc, "MyRoc", colorRed);

Or

YearStartOpen = TimeFrameGetPrice("O", inYearly); 
MyROC = (C-YearStartOpen ) / YearStartOpen*100;
Plot(myRoc, "MyRoc", colorRed);
3 Likes

@fxshrat Yes I see your point. I was assuming (perhaps a mistake on my part) he made a mistake in his post and what he really wants is the "year-to-date" returns which I believe are normally calculated from the Close of the previous year. Hopefully we have given him enough ideas that he will have his solution.

Thank you all very much for your help and ideas. My mistake was not using the close of the year prior or the open on January 1st. Either approach works!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.