Hi Mike,
This looks really good. I wish I had your skills in AFL programming.
Resetting each year to near zero is better idea than trying to use ROC for 
comparison.
One comment I would like to make is that, with this approach we are not able to 
look into the future which is desired outcome for seasonality plot. How would 
you modify this code to plot seasonality average, say three months forward?

Kind Regards
Richard


--- In [email protected], "Mike" <sfclimb...@...> wrote:
>
> 
> Wow, formatting of that was terrible. Reposting from a different
> browser...
> 
> years = Param("Years", 3, 1, 15, 1);
> newYear = Year() != Ref(Year(), -1);
> avgROC = 0;
> 
> // Create dynamic variables for each year (excluding current)
> for (y = 1; y <= years; y++) {
>     initialValue = IIf(newYear, ValueWhen(newYear, Close, y),
> ValueWhen(newYear, Close, y + 1));
>     barValue = Ref(Close, -252 * y);
>     runningROC = (barValue / initialValue) - 1;
>     VarSet("Year" + y, runningROC);
>     avgROC += runningROC;
> }
> 
> avgROC /= years;
> 
> // Plot zero line
> Plot(0, "", colorLightGrey, styleLine | StyleThick | StyleNoLabel);
> 
> // Plot vertical lines for new year markers
> Plot(newYear, "", colorLightGrey, styleHistogram | styleThick |
> styleOwnScale | styleNoLabel);
> 
> // Plot current year with thick emphasis
> initialValue = Iif(newYear, ValueWhen(newYear, Close, 0),
> ValueWhen(newYear, Close, 1));
> barValue = Close;
> runningROC = (barValue / initialValue) - 1;
> Plot(runningROC, "ROC", colorDarkRed, styleThick);
> 
> // Plot previous years with incrementing colors
> for (y = 1; y <= years; y++) {
>     Plot(VarGet("Year" + y), "" + y + " Years Ago", colorDarkRed + y);
> }
> 
> // Plot average as solid area
> Plot(avgROC, "Avg.", colorDarkRed + y, styleArea);
> 
> Mike
> 
> 
> --- In [email protected], "Mike" <sfclimbers@> wrote:
> >
> > Richard, I suspect that ROC(C, 1) would not reveal much. It might be
> > more interesting to plot a running ROC since the start of the
> respective
> > years. I've altered the code to show running year to date rate of
> > return, including vertical bars delineating the first bar of each new
> > year. You will notice that at each year goal post, all values start
> over
> > at zero (plus or minus a bar or two due to 252 being an imprecise
> > measure of a year).
> > Hope that helps.
> > years = Param("Years", 3, 1, 15, 1); newYear = Year() != Ref(Year(),
> > -1); avgROC = 0;
> > // Create dynamic variables for each year (excluding current) for (y =
> > 1; y <= years; y++) { initialValue = IIf(newYear, ValueWhen(newYear,
> > Close, y), ValueWhen(newYear, Close, y + 1)); barValue = Ref(Close,
> > -252 * y); runningROC = (barValue / initialValue) - 1;
> > VarSet("Year" + y, runningROC);
> > avgROC += runningROC; }
> > avgROC /= years;
> > // Plot zero line Plot(0, "", colorLightGrey, styleLine | StyleThick |
> > StyleNoLabel);
> > // Plot vertical lines for new year markers Plot(newYear, "",
> > colorLightGrey, styleHistogram | styleThick | styleOwnScale |
> > styleNoLabel);
> > // Plot current year with thick emphasis initialValue = Iif(newYear,
> > ValueWhen(newYear, Close, 0), ValueWhen(newYear, Close, 1)); barValue
> =
> > Close; runningROC = (barValue / initialValue) - 1; Plot(runningROC,
> > "ROC", colorDarkRed, styleThick);
> > // Plot previous years with incrementing colors for (y = 1; y <=
> years;
> > y++) { Plot(VarGet("Year" + y), "" + y + " Years Ago", colorDarkRed
> > + y); }
> > // Plot average as solid area Plot(avgROC, "Avg.", colorDarkRed + y,
> > styleArea);
> > Mike --- In [email protected], "Richard" richpach2@ wrote:
> > >
> > > Hello Mike,
> > >
> > > This is very clever piece of code. Thank you for sharing it with us.
> > > I would have used "for" loop to create a new "close" array for each
> > year with 1-252 index. I didn't know you can use build-in "Close"
> array
> > in that way. Now that we can see the result for each year, it looks to
> > me like we need to "normalize" the results so they can be compared on
> > relative basis. Calculating an average from absolute values does not
> > look right. Do you think that using ROC(C,1) would be an effective way
> > to compare all individual year's results?
> > >
> > > Regards
> > > Richard
> > >
> > > --- In [email protected], "Mike" sfclimbers@ wrote:
> > > >
> > > >
> > > > If "quick and dirty" is good enough for you, you can just assume
> 252
> > > > trading days per trading year and then use Ref() with multiples of
> > that
> > > > number.
> > > >
> > > > e.g.
> > > > years = Param("Years", 3, 1, 15, 1);
> > > > avgClose = 0;
> > > >
> > > > // Create dynamic variables for each year (excluding current)
> > > > for (y = 1; y <= years; y++) {
> > > > pastClose = Ref(Close, -252 * y);
> > > > VarSet("Year" + y, pastClose);
> > > > avgClose += pastClose;
> > > > }
> > > >
> > > > avgClose /= years;
> > > >
> > > > // Plot current year with thick emphasis
> > > > Plot(Close, "Close", colorDarkRed, styleThick);
> > > >
> > > > // Plot previous years with incrementing colors
> > > > for (y = 1; y <= years; y++) {
> > > > Plot(VarGet("Year" + y), "" + y + " Years Ago", colorDarkRed +
> > y);
> > > > }
> > > >
> > > > // Plot average as solid area
> > > > Plot(avgClose, "Avg.", colorDarkRed + y, styleArea);
> > > >
> > > > Note that the example assumes that the number of backyears (i.e.
> > years
> > > > variable) plus the base color (e.g. colorDarkRed) will not exceed
> > the
> > > > maximum color index found here:
> > > > http://www.amibroker.com/guide/a_language.html
> > > > <http://www.amibroker.com/guide/a_language.html>
> > > >
> > > > Mike
> > > >
> > > >
> > > > --- In [email protected], "Rick_Miller_98" <rick300m@>
> > wrote:
> > > > >
> > > > > I removed the nested loops line and defined MonthPer=1 and
> managed
> > to
> > > > get a plot.
> > > > >
> > > > > However this is looking at a month to month ROC with only 12
> > > > datapoints. A seasonality chart needs to be daily, weekly at most.
> > > > >
> > > > > Ideally the chart would have each year's performnce plotted in
> the
> > > > background and the composite chart of all years plotted in the
> > > > foreground on a daily basis.
> > > > >
> > > > > This seams like quite complicated coding. I would be willing to
> > > > contribute towards a properly functioning chart Afl if anyone has
> it
> > or
> > > > is willing to code it.
> > > > >
> > > > >
> > > > >
> > > > > --- In [email protected], "Richard" richpach2@ wrote:
> > > > > >
> > > > > > Thank you for posting this code.
> > > > > > Whitout <Nested_Includes.AFL> some values are not defined.
> > > > > > can you please advise what is the default value for MonthPer
> and
> > > > what is the ListMonths represent?
> > > > > >
> > > > > > Regards
> > > > > > Richard
> > > > > >
> > > > > > --- In [email protected], "vlanschot" <vlanschot@>
> > wrote:
> > > > > > >
> > > > > > > Below is some old code of mine. May get you going though.
> > > > > > >
> > > > > > > PS
> > > > > > >
> > > > > > > #include_once <Nested_Includes.AFL>
> > > > > > >
> > > > > > > Maand = Month();
> > > > > > >
> > > > > > > JanRet = ValueWhen(Maand == 1 AND
> > > > Ref(Maand,1)==2,ROC(C,MonthPer));
> > > > > > > FebRet = ValueWhen(Maand == 2 AND
> > > > Ref(Maand,1)==3,ROC(C,MonthPer));
> > > > > > > MarRet = ValueWhen(Maand == 3 AND
> > > > Ref(Maand,1)==4,ROC(C,MonthPer));
> > > > > > > AprRet = ValueWhen(Maand == 4 AND
> > > > Ref(Maand,1)==5,ROC(C,MonthPer));
> > > > > > > MayRet = ValueWhen(Maand == 5 AND
> > > > Ref(Maand,1)==6,ROC(C,MonthPer));
> > > > > > > JunRet = ValueWhen(Maand == 6 AND
> > > > Ref(Maand,1)==7,ROC(C,MonthPer));
> > > > > > > JulRet = ValueWhen(Maand == 7 AND
> > > > Ref(Maand,1)==8,ROC(C,MonthPer));
> > > > > > > AugRet = ValueWhen(Maand == 8 AND
> > > > Ref(Maand,1)==9,ROC(C,MonthPer));
> > > > > > > SepRet = ValueWhen(Maand == 9 AND
> > > > Ref(Maand,1)==10,ROC(C,MonthPer));
> > > > > > > OctRet = ValueWhen(Maand == 10 AND
> > > > Ref(Maand,1)==11,ROC(C,MonthPer));
> > > > > > > NovRet = ValueWhen(Maand == 11 AND
> > > > Ref(Maand,1)==12,ROC(C,MonthPer));
> > > > > > > DecRet = ValueWhen(Maand == 12 AND
> > > > Ref(Maand,1)==1,ROC(C,MonthPer));
> > > > > > >
> > > > > > >
> > > > > > > SeasRet =
> > > > > > > IIf(Maand == 1, LastValue(Cum(JanRet)),
> > > > > > > IIf(Maand == 2, LastValue(Cum(FebRet)),
> > > > > > > IIf(Maand == 3, LastValue(Cum(MarRet)),
> > > > > > > IIf(Maand == 4, LastValue(Cum(AprRet)),
> > > > > > > IIf(Maand == 5, LastValue(Cum(MayRet)),
> > > > > > > IIf(Maand == 6, LastValue(Cum(JunRet)),
> > > > > > > IIf(Maand == 7, LastValue(Cum(JulRet)),
> > > > > > > IIf(Maand == 8, LastValue(Cum(AugRet)),
> > > > > > > IIf(Maand == 9, LastValue(Cum(SepRet)),
> > > > > > > IIf(Maand == 10,LastValue(Cum(OctRet)),
> > > > > > > IIf(Maand == 11,LastValue(Cum(NovRet)),
> > > > > > > IIf(Maand == 12,LastValue(Cum(DecRet)),0))))))))))));
> > > > > > >
> > > > > > > SeasRet = SeasRet/(BarCount-1);
> > > > > > >
> > > > > > > KleurHist = IIf(SeasRet<0,
> > > > colorRed,IIf(SeasRet>0,colorGreen,colorLightGrey));
> > > > > > >
> > > > > > > Plot(SeasRet,FullName()+" Seasonal Return for
> > > > "+StrExtract(ListMonths,SelectedValue(Month()-1)),KleurHist,2+4);
> > > > > > > //Plot(OctRet,"ret",1,1);
> > > > > > > Title = "{{DATE}} -- {{INTERVAL}}\n{{VALUES}} ";
> > > > > > >
> > > > > > > --- In [email protected], Rick Osborn <ricko@>
> wrote:
> > > > > > > >
> > > > > > > > I would be interested in that too
> > > > > > > >
> > > > > > > > Best Regards
> > > > > > > > Rick Osborn
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > > ________________________________
> > > > > > > > From: Rick_Miller_98 <rick300m@>
> > > > > > > > To: [email protected]
> > > > > > > > Sent: Mon, December 14, 2009 9:53:23 PM
> > > > > > > > Subject: [amibroker] Seasonality Plot
> > > > > > > >
> > > > > > > >
> > > > > > > > I am looking for some code to overlay multiple years on
> the
> > same
> > > > Jan-Dec axis and to create a composite plot of all of those years
> to
> > > > identify seasonality trends.
> > > > > > > >
> > > > > > > > Please E-Mail me if you have it. I am willing to
> contribute
> > to
> > > > the cause.
> > > > > > > >
> > > > > > > > Thanks!
> > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>


Reply via email to