How to trigger buy signal once a month only

Hi, below is my code to trigger buy signal when rsi(25) is less than 45 and price is less than monthly support*1.01 . This code keeps generating buy signals when the rsi(25) is less than 45 and again it crosses above 45 and crosses back below 45. I want to generate just one signal when it first crosses below 45 and that signal to be active for ever and should not generate same signal until next month. Next month it should generate fresh signal because the monthly support will be different. that means one signal per month. in the attached BANKINDIA-I chart signals are generated on 16,17,18,19, 22, 30, 31st Jan and 2nd Feb. it should generate only on 16th Jan and next on 2nd feb. Appreciate you help. BANKINDIA-I


_SECTION_BEGIN("");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorWhite ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

GraphXSpace = 5 ;
SetChartOptions(0,chartShowArrows|chartShowDates);
 
ppl = ParamToggle("Plot Pivot Levels","Off|On",1);
 
numbars = LastValue(Cum(Status("barvisible")));
fraction= IIf(StrRight(Name(),3) == "", 3.2, 3.2);
hts = -33.5;

/* This code calculates the previous month's high, low and close */

MHi1 = IIf(Month()!=Ref(Month(),-1),Ref(HighestSince(Month()!=Ref(Month(),-1),H,1),-1),0);
MHi = ValueWhen(Month()!=Ref(Month(),-1),MHi1,1);
MLo1 = IIf(Month()!=Ref(Month(),-1),Ref(LowestSince(Month()!=Ref(Month(),-1),L,1),-1),0);
MLo = ValueWhen(Month()!=Ref(Month(),-1),MLo1,1);
MCl1 = IIf(Month()!=Ref(Month(),-1),Ref(C,-1),0);
MCl = ValueWhen(Month()!=Ref(Month(),-1),MCl1,1); 
 
/* This code calculates Monthly Piovts */
 

pm = (MHi + MLo + MCl)/3; pmI = LastValue (pm,1);
rm1 = (pm*2)-MLo; rm1I = LastValue (rm1,1);
sm1 = (pm*2)-MHi; sm1I = LastValue (sm1,1);

if((ppl==1) ) 
{

Plot(pm,"",colorBlue,styleLine|styleNoRescale);
Plot(sm1,"",colorRed,styleLine|styleNoRescale);
Plot(rm1,"",colorGreen,styleLine|styleNoRescale);
}

if(ppl==1)  {
PlotText("MPiv=" + WriteVal(pm,fraction), LastValue(BarIndex())-(numbars/Hts), pmI +0.05, colorBlue);
PlotText("rm1=" + WriteVal(rm1,fraction), LastValue(BarIndex())-(numbars/Hts), rm1I +0.05, colorGreen);
PlotText("sm1=" + WriteVal(sm1,fraction), LastValue(BarIndex())-(numbars/Hts), sm1I +0.05, colorRed);
}

rsi45=RSI(25)<45;
Buy=L<=SM1*1.01 AND rsi45 ;

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-10);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0, L, Offset=-20);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-15); 

_SECTION_END();

If you need the signal to be active until the following month, check the flip function: http://www.amibroker.com/guide/afl/flip.html

NewMonth=Month()!=ref(Month(),-1);
BuyMode=Flip(Buy,NewMonth);
3 Likes

Thanks dear. Your new month logic helped me.
I used below and it worked.

NewMonth=Month()!=ref(Month(),-1);
Buy=ExRem(Buy, NewMonth);

I am sorry to say that I did not check it properly. Though it removed successive signals till the next month it did not solve the problem of ON/OFF buy signals; the flip function seems to be not working. I am using 64bit version 6.20.1. See below my code, appreciate if you can correct it.

_SECTION_BEGIN("");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorWhite ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
 
/* This code calculates the previous month's high, low and close */

MHi1 = IIf(Month()!=Ref(Month(),-1),Ref(HighestSince(Month()!=Ref(Month(),-1),H,1),-1),0);
MHi = ValueWhen(Month()!=Ref(Month(),-1),MHi1,1);
MLo1 = IIf(Month()!=Ref(Month(),-1),Ref(LowestSince(Month()!=Ref(Month(),-1),L,1),-1),0);
MLo = ValueWhen(Month()!=Ref(Month(),-1),MLo1,1);
MCl1 = IIf(Month()!=Ref(Month(),-1),Ref(C,-1),0);
MCl = ValueWhen(Month()!=Ref(Month(),-1),MCl1,1); 
 
/* This code calculates Monthly Piovts */
rg = (MHi - MLo);
pm = (MHi + MLo + MCl)/3; pmI = LastValue (pm,1);
rm1 = (pm*2)-MLo; rm1I = LastValue (rm1,1);
sm1 = (pm*2)-MHi; sm1I = LastValue (sm1,1);
rm2 = pm + rm1 - sm1; rm2I = LastValue (rm2,1);
sm2 = pm - rm1 + sm1; sm2I = LastValue (sm2,1);

NewMonth=Month()!=ref(Month(),-1);

Buy=L<=SM2 AND RSI(25)<35;

Buymode=Flip(Buy,NewMonth);

Sell=H>=RM1 AND RSI(25)>67;

PlotShapes(IIf(Buymode, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-15);
PlotShapes(IIf(Buymode, shapeSquare, shapeNone),colorLime, 0, L, Offset=-25);                      
PlotShapes(IIf(Buymode, shapeUpArrow, shapeNone),colorBlue, 0,L, Offset=-20); 

_SECTION_END();