Stop trading if a monthly percentage is hit?

I'm looking for a way to stop trading for the month if I have reached a profit of 10% or a loss of 8% in the current month. I have coded a simple moving average cross with my version of the code below. It doesn't work though, sometimes it stops trading when hit 10% and sometimes in continues. Hope someone can help, thanks!

///////////////////////////////////////////////////////PLOT PRICE/////////////////////////////////////
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();
//////////////////////////////////////////////////////BUY SELL/////////////////////////////////////////////
SetOption("MaxOpenPositions", 1);
SetPositionSize(100, spsPercentOfEquity);
PositionScore = adx(15);

ma1=MA(C,10);
ma2=MA(C,20);

Buy=Cross(ma2,ma1);
BuyPrice=C;

Sell= Cross(ma1,ma2);
SellPrice=C;
/////////////////////////////////////this is where i tried to code teh 10% / 8% profit/loss. IT DOES NOT WORK///////////////////////////////////////////////

Firstday= Month()!= Ref(Month(),-1) AND Ref(Month(),-1)==Ref(Month(),-2);
EQ1= ValueWhen(Firstday,Equity());
EQ= Equity();

Target= EQ < (EQ1*1.1) AND EQ> (EQ1*0.92);

Buy= IIf(NOT Target,0,Buy);//////only buy if profit this month in below 10% and losses are above 8%

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Sell= ExRem(Sell, Buy);
Buy= ExRem(Buy,Sell);

PlotShapes( Buy*shapeUptriangle, colorGreen, 0, Buyprice);
PlotShapes( Sell*shapeDownTriangle, colorRed, 0, Sellprice);
//////////////////////////////////////////////////////PLOT PRICE/////////////////////////////////////
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
//////////////////////////////////////////////////////BUY SELL/////////////////////////////////////////////
SetOption("MaxOpenPositions", 1);
SetPositionSize(100, spsPercentOfEquity);
PositionScore = adx(15);

ma1=MA(C,10);
ma2=MA(C,20);

Buy=Cross(ma1,ma2);
BuyPrice=C;

Sell= 0;
SellPrice=C;

ApplyStop(stopTypeLoss,stopModePercent,2);
ApplyStop(stopTypeProfit,stopModePercent,4);
/////////////////////////////////////this is where i tried to code teh 10% / 8% profit/loss. IT DOES NOT WORK///////////////////////////////////////////////

Firstday= Month()!= Ref(Month(),-1) AND Ref(Month(),-1)==Ref(Month(),-2);
EQ1= ValueWhen(Firstday,Equity());
EQ= Equity();

Target= EQ < (EQ1*1.1) AND EQ> (EQ1*0.92);

Buy= IIf(NOT Target,0,Buy);//////only buy if profit this month in below 10% and losses are above 8%

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PlotShapes( Buy<em>shapeUptriangle, colorGreen, 0, Buyprice);
PlotShapes( Sell</em>shapeDownTriangle, colorRed, 0, Sellprice);`

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