First Monday every month, was: Not Understanding Flip()

I wish to trade a system only on the first Monday of every month. Basic idea is to set variable 'aNewMonth' whenever Month() changes.
Set 'tradeDay' on the first day after DayOfYear() is incremented by more than two days. And then reset 'aNewMonth' till the next 'newMonth'.
Tried unsuccessfully to implement above using 'Flip() function.
Please see code, chart image, and watch images.
In chart, 10/5/2020, tradeDay is 1, OK! 10/12/2020, tradeDay is also 1 in all Mondays, newMonth or not, because 'aNewMonth' is never reset.
I obviously don't understand how to use Flip().
Any and all help is appreciated. Thank you.

*/
newMonth = false; // init, state, enables tradeDay
aNewMonth = False; // sticky newMonth
tradeDay = false; // init, state, enables buy and resets monthOn
newMonth = Month() != Ref(Month(), -1);
  str_month = "Month: " + numtostr(Month());
  str_newMonth = "newMonth: " + NumToStr(newMonth);
// tried Flip() below also
aNewMonth = Flip(newMonth, Ref(tradeDay, -1));
//  str_aNewMonth = "aNewMonth: " + NumToStr(aNewMonth);

//*// Try making it first Monday of the month
DOW = DayOfWeek(); // for debuggin only
  str_DOW = "DOW: " + numtostr(DOW, 1.0);
  str_DOWm1 = "DOWm1: " + numtostr(Ref(DOW, -1), 1.0);
DOY = DayOfYear();
  str_DOY = "DOY: " + numtostr(DOY, 1.0);
aNewMonth = Flip(newMonth, Ref(tradeDay, -1));
  str_aNewMonth = "aNewMonth: " + NumToStr(aNewMonth);

 //newMonth and after weekend  
tradeDay = aNewMonth AND DOY > (Ref(DOY, -1) + 2);
  str_tradeDay = "tradeDay: " + numtostr(tradeDay, 1.0);
tradeDay = Flip(tradeDay, aNewMonth);
//aNewMonth = Flip(newMonth, Ref(tradeDay, -1));
//  str_aNewMonth = "aNewMonth: " + NumToStr(aNewMonth);

tradecolor = 0;
tradecolor = IIf(tradeDay == 1, colorRed, colorgreen);

Plot(DOW, "DOW", tradecolor, styleStaircase);
theMonth = Month();
Plot(theMonth, "Month", colorgreen, styleStaircase);
Plot(tradeDay, "tradeDay", colorbrown);

210218 Flip test chart
210218 Flip test watch window

You don't need so many lines of code. One line is enough.

FirstMondayEveryMonth = ExRem( DayOfWeek() == 1, Month() != Ref( Month(), 1 ) );

Note: that the code references one bar ahead but only to detect last day of the month so it is harmless

4 Likes

Thank you so much. Your suggestion worked perfectly for Mondays. However in retrospect I decided that I wanted trade, not only on Mondays, but on my first opportunity if the first Monday fell on a weekend or holiday. Therefore this minor modification:

DOW = DayOfWeek(); // for debuggin only
  str_DOW = "DOW: " + numtostr(DOW, 1.0);
  str_DOWm1 = "DOWm1: " + numtostr(Ref(DOW, -1), 1.0);
DOY = DayOfYear();
  str_DOY = "DOY: " + numtostr(DOY, 1.0);

tradeDay = ExRem(DOY > Ref(DOY, -1) + 2 AND  DOW < 3, Month() != Ref(Month(), 1 ));
  str_tradeDay = "tradeDay: " + numtostr(tradeDay, 1.0);

The 'str_' variables are just to make testing easier.

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