Jerry,
There is an error in your calculation of a new week. DayOfWeek() will
always return a non zero, so you will be calculating as though every day
is a new week. Instead, check whether the the previous DayOfWeek is
greater than the current one (i.e. Friday > Monday).
Also, there was a bug in my earlier post. No need for special case
handling when current bar is a boundary bar. I had thought that
ValueWhen using zero meant the current value when the condition was true
for the current bar. But, value zero actually means look into the future
for the next incidence even when current bar is true.
Correcting for both your error and mine, and adding easier to read
title, the complete revised code becomes the following (which should be
accurate when there have not been any trading holidays):
baseColor = colorDarkRed;
boundaryColor = colorLightGrey;
weeks = Param( "Weeks", 3, 1, 15, 1 );
weekDays = DayOfWeek();
newWeek = Ref( weekDays, -1 ) > weekDays;
avgROC = 0;
// Create dynamic variables for each week (excluding current)
for ( w = 1; w <= weeks; w++ )
{
initialValue = ValueWhen( newWeek, Close, w + 1 );
barValue = Ref( Close, -5 * w );
runningROC = ( ( barValue / initialValue ) - 1 ) * 100;
VarSet( "Week" + w, runningROC );
avgROC += runningROC;
}
avgROC /= weeks;
// Plot zero line
Plot( 0, "", boundaryColor, styleLine | StyleThick | StyleNoLabel );
// Plot vertical lines for new week markers
Plot( newWeek, "", boundaryColor, styleHistogram | styleThick |
styleOwnScale | styleNoLabel );
// Plot current week with thick emphasis
initialValue = ValueWhen( newWeek, Close, 1 );
barValue = Close;
runningROC = ( ( barValue / initialValue ) - 1 ) * 100;
Plot( runningROC, "ROC", baseColor, styleThick );
// Plot previous weeks with incrementing colors
titleStr = StrFormat( ", Bar %g, Current: %g, Initial: %g", BarIndex(),
barValue, initialValue );
titleStr += StrFormat( "\n" + EncodeColor( baseColor ) + "This week:
%2.1f%%", runningROC );
for ( w = 1; w <= weeks; w++ )
{
value = VarGet( "Week" + w );
titleStr += StrFormat( "\n" + EncodeColor( baseColor + w ) + w + "
week" + WriteIf( w > 1, "s", "" ) + " ago: %2.1f%%", value );
Plot( value, "" + w + " weeks ago", baseColor + w );
}
// Plot average as solid area
Plot( avgROC, "Avg.", baseColor + w, styleArea | styleNoLabel );
Title = NumToStr( DateTime(), formatDateTime ) + titleStr + StrFormat(
"\n" + EncodeColor( baseColor + w ) + "Average: %2.1f%%", avgROC );
Mike
--- In [email protected], "Jerry Gress" <pleasenospample...@...>
wrote:
>
> Hi Mike,
>
> Thanks for the tips,
>
> Below are my changes (weekly) to your (yearly) code. Assuming it is
correct
> than why on a chart with 50 days I can get a plot of average solid
area on
> all days except the very first 2 days? Seems to me if one, two weeks
are
> selected then the first one to two weeks should not have any plots?
>
> Or does something need to be change in the section just after Avgroc =
0?
>
>
> Regards,
>
> JG
>
>
>
>
> ///////////////////////////////////////////////////////////////////
>
> weeks = Param("Weeks",3,1,15,1);
> newweek = DayOfWeek();
> Avgroc = 0;
> // Create dynamic variables for each year (excluding current)
> for (y = 1; y <= weeks; y++) {
> initialValue = IIf(newweek, ValueWhen(newweek, Close, y),
> ValueWhen(newweek, Close, y + 1));
> barValue = Ref(Close, -5 * y);
> runningroc = (barValue / initialValue) - 1;
> VarSet("Week" + y, runningroc);
> Avgroc += runningroc;
> }
> Avgroc /= weeks;
>
> // Plot previous years with incrementing colors
> for (y = 1; y <= weeks; y++) {
> Plot(VarGet("Week" + y), "" + y + "Weeks Ago", colorDarkRed + y );
> }
> // Plot average as solid area
> Plot(Avgroc, "Avg.", colorDarkRed + y, styleArea | styleNoLabel);
>
>
> ///////////////////////////////////////////////////////////////////
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On
Behalf
> Of Mike
> Sent: Wednesday, December 23, 2009 2:56 PM
> To: [email protected]
> Subject: [amibroker] Re: Winter project, Correlation Trading
>
> Jerry,
>
> Sorry, sent you the link to the poor formatted version. The better
version
> is here:
http://finance.groups.yahoo.com/group/amibroker/message/144970
>
> And, yes, if you want every Tuesday to be your criteria for a new
period,
> then what you have done should work. But, you must also change the
constant
> used in the calculations. Instead of 252 (i.e. trading days in a
year), you
> would need to use 5 (i.e. trading days in a week).
>
> Note also that when dealing with a year, minor errors in the alignment
don't
> make much difference. But, when using just a week, as you are doing,
the
> alignment will likely be more important and you will probably have to
work
> out some more precise code than simply using multiples of 5 bars/week.
For
> example, when the markets were closed due to holidays, using a hard
coded
> multiple of 5 days will misalign the data.
>
> Mike
>
> --- In [email protected], "Jerry Gress" pleasenospamplease@
> wrote:
> >
> > Thanks Mike,
> >
> > Will work on changing Year() to week???
> >
> >
> > In fact when I change the one line as below I get something that may
> work!!
> >
> >
> > newYear = DayOfWeek()==2;//Year() != Ref(Year(), -1);
> >
> > JG
> >
> >
> > -----Original Message-----
> > From: [email protected] [mailto:[email protected]]
On
> Behalf
> > Of Mike
> > Sent: Wednesday, December 23, 2009 11:10 AM
> > To: [email protected]
> > Subject: [amibroker] Re: Winter project, Correlation Trading
> >
> > Jerry,
> >
> > I was involved in a similar thread recently, excecpt covering 1 year
at a
> > time. Perhaps you can alter the code for your needs.
> >
> > http://finance.groups.yahoo.com/group/amibroker/message/144969
> >
> > Mike
> >
> > --- In [email protected], "Jerry Gress"
<pleasenospamplease@>
> > wrote:
> > >
> > > Hello All,
> > >
> > > Would like to take the 1 minute data from three weeks ago and two
weeks
> > ago
> > > then super impose it on the following week. For example: Monday 7
Dec,
> > > Monday 14 all on Monday Dec 21.
> > >
> > >
> > > My feeble attempt that does not work for one week:
> > >
> > > yut = DayOfWeek()==2;
> > > ytr = Ref(yut,-1);
> > > Plot(ytr,"",colorGreen,styleBar);
> > >
> > >
> > > So do I have to do the timeframe stuff?
> > >
> > > Any help will be appreciated.
> > >
> > > JG
> > >
> >
> >
> >
> >
> > ------------------------------------
> >
> > **** IMPORTANT PLEASE READ ****
> > This group is for the discussion between users only.
> > This is *NOT* technical support channel.
> >
> > TO GET TECHNICAL SUPPORT send an e-mail directly to
> > SUPPORT {at} amibroker.com
> >
> > TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
> > http://www.amibroker.com/feedback/
> > (submissions sent via other channels won't be considered)
> >
> > For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
> > http://www.amibroker.com/devlog/
> >
> > Yahoo! Groups Links
> >
>
>
>
>
> ------------------------------------
>
> **** IMPORTANT PLEASE READ ****
> This group is for the discussion between users only.
> This is *NOT* technical support channel.
>
> TO GET TECHNICAL SUPPORT send an e-mail directly to
> SUPPORT {at} amibroker.com
>
> TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
> http://www.amibroker.com/feedback/
> (submissions sent via other channels won't be considered)
>
> For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
> http://www.amibroker.com/devlog/
>
> Yahoo! Groups Links
>