Elegant way to trade every nth week

Posted By: VHX

Elegant way to trade every nth week - 12/27/21 10:44

Hi,
is there an elegant way to trade e.g. every 5th week and on different days (e.g. Tuesday in week 1, Thursday in week 6,...). I tried it with something like

if(week(0)%5 == 1) {...}

which works for one year, but if the script moves on to the next year this does not work propperly, as it trades in week 51 of the current year and then again in week 1 of the next year. Using the Bar number directly also does not work properly, because of missing trading day (holidays etc.).

Also, I only want to trade once per week, so if I am in the right week I can select Tuesday with dow(0)==2, but if I also have a condition for Thursday, it would also trad Thursday that week. Do I then have to flag it with something like

boolean tradedThisWeek = True;

and toggle that accordingly? Or is there an easier, more elegant way to do this?
Posted By: Petra

Re: Elegant way to trade every nth week - 12/27/21 11:04

static int Weeknum = 0;
if(week(0) != week(1)) Weeknum = (Weeknum+1) % 5;
if(Weeknum == 0 && dow(0) == TUESDAY) ...
...
Posted By: VHX

Re: Elegant way to trade every nth week - 12/27/21 16:25

Thanks Petra,
that piece of code does work well during the year, but at the end of the year you'll get shorter periods (as there are sometimes weeks with only 1 day, i.e. Mon 18-12-31 is only one day in week 53, so on Wed 19-01-02 you'll increase Weeknum too early). And you'll get longer periods if you are in the right week, but if the stock markets were closed that TUESDAY.
Posted By: Zheka

Re: Elegant way to trade every nth week - 12/27/21 17:53

To solve the problem of holidays/'missing data', try playing with BR_FLAT.
© 2024 lite-C Forums