Different trade conditions for first and next trades during the same day

I have just started my AFL adventure and would appreciate some help on the following problem.

This is a dummy reversal system, where buy or short signals are generated if price differs 15 or more points from daily open.

I would like to test a situation, when such signal appears 2 or more times during the same day and apply a different reversal value to all signals beginning from 2nd. dailyOpen instead of dailyOpen - pointsFromOpen would be used.

Unfortunately I can't make the code work. If I try to calculate number of Buy and Short trades before defining conditions for them, I get uninitialized variable error. Even if I defaulted number of trades to 0, so that I can force firstBuy and firstShort, script finishes, but there are no trades done in backtest.

//max number of futures traded at one time
SetPositionSize(1,spsShares);

//identify end of data which is exit trade condition
bi = BarIndex();
exitLastBar = bi == LastValue(bi);

dailyOpen = TimeFrameGetPrice( "O", inDaily, 0 );
pointsfromOpen=15;

//totalDailyTrades = 0;
//totalDailyTrades = Sum( Buy, BarsSince( newDay ) + 1 ) + Sum( Short, BarsSince( newDay ) + 1 );

firstBuy = high >= dailyOpen + pointsFromOpen AND totalDailyTrades = 0;
nextBuy =  high >= dailyOpen AND totalDailyTrades > 0;
firstShort = low <= dailyOpen - pointsFromOpen AND totalDailyTrades = 0;
nextShort =  low <= dailyOpen AND totalDailyTrades > 0;

Buy = firstBuy OR nextBuy AND bi!=LastValue(bi);
Short = firstShort OR nextShort AND bi!=LastValue(bi);
Sell = ExitLastBar;
Cover = ExitLastBar;

In your code you only sell or cover at the lastbar of the day.

Do you want to scale in every time you get a Buy or Short ?

Start point

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", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

//SetOption("CommissionMode",3); 
SetOption("FuturesMode",1); 
SetOption("MaxOpenPositions",1); 
//SetOption("InitialEquity",2000);
//SetOption("CommissionAmount",1); 
//max number of futures traded at one time
SetPositionSize(1,spsShares);

//identify end of data which is exit trade condition
bi = BarIndex();
LastBarOfDay = Day() != Ref(Day(), 1);
exitLastBar = LastBarOfDay OR bi == LastValue(bi);

dailyOpen = TimeFrameGetPrice( "O", inDaily, 0 );
pointsfromOpen=Optimize("pointsfromOpen", Param("pointsfromOpen", 15, 5, 30, 5), 5, 30, 5);

xBuy = Cross(High, dailyOpen + pointsFromOpen);
xShort = Cross(dailyOpen - pointsFromOpen, Low);
xSell = ExitLastBar;
xCover = ExitLastBar;

Buy = ExRem(xBuy, xSell OR xShort);
Short = ExRem(xShort, xCover OR xBuy);
Sell = ExRem(ExitLastBar OR Short, Buy);
Cover = ExRem(ExitLastBar OR Buy, Short);

QtyOfTrades = SumSince(LastBarOfDay, Buy or Short);

_N(Title = Title + StrFormat("\nQtyOfTrades %g", QtyOfTrades)); 
 
PlotShapes( IIf(Buy, shapeUpArrow, shapeNone), IIf(Buy==sigScaleIn, colorGold, colorGreen), 0, Low, -29 ); 
PlotShapes( IIf(Short, shapeDownArrow, shapeNone), IIf(Short==sigScaleIn, colorGold, colorRed), 0, High, -29); 
PlotShapes( IIf(Sell AND NOT Short, shapeDownArrow, shapeNone), colorBlue, 0, High, -29); 
PlotShapes( IIf(Cover AND NOT Buy, shapeUpArrow, shapeNone), colorBlue, 0, Low, -29); 

1 Like

Thank You for the reply, however with above code I'm getting an error "Error 30.
Syntax error, identifier 'sumsince' is undefined."

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", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

SetOption("CommissionMode",3); 
SetOption("FuturesMode",1); 
SetOption("MaxOpenPositions",1); 
SetOption("InitialEquity",2000);
SetOption("CommissionAmount",1); 
//max number of futures traded at one time
SetPositionSize(1,spsShares);

//identify end of data which is exit trade condition
bi = BarIndex();
LastBarOfDay = Day() != Ref(Day(), 1);
exitLastBar = LastBarOfDay OR bi == LastValue(bi);

dailyOpen = TimeFrameGetPrice( "O", inDaily, 0 );
pointsfromOpen=Optimize("pointsfromOpen", Param("pointsfromOpen", 25, 5, 300, 5), 5, 300, 5);

xBuy = BarsSince(LastBarOfDay) > 1 AND Cross(High, dailyOpen + pointsFromOpen);
xShort = BarsSince(LastBarOfDay) > 1 AND Cross(dailyOpen - pointsFromOpen, Low);
xSell = ExitLastBar;
xCover = ExitLastBar;
//xBuy = IIf( xBuy AND xShort AND Close >= Open, False, xBuy);
//xShort = IIf( xBuy AND xShort AND Close < Open, False, xShort);

Buy = ExRem(xBuy, xSell OR xShort);
Short = ExRem(xShort, xCover OR xBuy);
Sell = ExRem(ExitLastBar OR Short, Buy);
Cover = ExRem(ExitLastBar OR Buy, Short);

//GainStop = Optimize("GainStop", Param("GainStop", 120, 100, 300, 50), 100, 300, 50); 
//ApplyStop(stopTypeProfit, stopModePoint, GainStop);
//LossStop = Optimize("LossStop", Param("LossStop", 120, 100, 300, 50), 100, 300, 50); 
//ApplyStop(stopTypeLoss, stopModePoint, LossStop);

//QtyOfTrades = SumSince(LastBarOfDay, Buy or Short);
QtyOfTrades = Sum(Buy or Short, BarsSince(LastBarOfDay));

_N(Title = Title + StrFormat("\nQtyOfTrades %g", QtyOfTrades)); 
 
PlotShapes( IIf(Buy, shapeUpArrow, shapeNone), IIf(Buy==sigScaleIn, colorGold, colorGreen), 0, Low, -29 ); 
PlotShapes( IIf(Short, shapeDownArrow, shapeNone), IIf(Short==sigScaleIn, colorGold, colorRed), 0, High, -29); 
PlotShapes( IIf(Sell AND NOT Short, shapeDownArrow, shapeNone), colorBlue, 0, High, -29); 
PlotShapes( IIf(Cover AND NOT Buy, shapeUpArrow, shapeNone), colorBlue, 0, Low, -29); 

1 Like