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;