I'm new to AML loops and this is my 1st attempt to use them, based on documentation and a lot of trial and error, unfortunately I'm stuck and despite of Explore part of the code, I can't figure out why variables don't take proper values.
This code is used on intraday data and is supposed to:
- Create an array of daily opens (dailyOpen).
- Pre-define an array which tells how many points from daily open trigger buy or short signal, I made this value hardcoded 15 (pointsFromOpen).
- For each bar check whether there is a fresh buy or short signal and populate Buy / Short array based on it.
- Sum fresh Buy and Short signals for trading session (reset counter for 1st bar of day), when trade counter is an even number (checked through modulo calculation), don't do anything, pointsFromOpen should stay 15 (this part doesn't seem to work, only 1st bar has value of 15 in Explore and it's the reason Buy and Short signals in Explore are incorrect as well) and if trade counter is an odd number, pointsFromOpen array element should change to 0.
Variable dayTrades in Explore has the same value for each bar, I assume this is fine and last existing value of this variable is shown.
Any advice is greatly appreciated!
//max number of futures traded at one time
SetPositionSize(1,spsShares);
SetOption("FuturesMode",1);
SetOption("MaxOpenPositions",1);
dailyOpen = TimeFrameGetPrice("O", inDaily, 0 );
pointsFromOpen = 15;
Buy = 0;
Short = 0;
Sell = 0;
Cover = 0;
barBuy = 0;
barShort = 0;
barSell = 0;
barCover = 0;
barPointsFromOpen = 0;
dayTrades = 0;
newDay = Day() != Ref( Day(), -1 );
for( i = 1; i < BarCount; i++ )
{
if (NewDay[i] == 1) //new trading session (1st bar)
{
dayTrades = 0;
}
// for odd number (1st, 3rd, 5th etc.) of already done trades this session -> 0 points from open
barPointsFromOpen[i] = iif (dayTrades % 2 == 1,0,1);
if (barPointsFromOpen[i] == 0);
{
pointsFromOpen[i] = 0;
}
if( High[i] >= dailyOpen[i] + pointsFromOpen[i] ) // AND pointsFromOpen[i] <= volatilityLimit[i] AND bi!=LastValue(bi)
{
dayTrades = IIf(barBuy == 0, dayTrades + 1, dayTrades);
barBuy = 1;
barShort = 0;
}
if( Low[i] <= dailyOpen[i] - pointsFromOpen[i] ) // AND pointsFromOpen[i] <= volatilityLimit[i] AND bi!=LastValue(bi)
{
dayTrades = IIf(barShort == 0, dayTrades + 1, dayTrades);
barBuy = 0;
barShort = 1;
}
Buy[i] = barBuy;
Short[i] = barShort;
}
Filter = 1; //all symbols and quotes accepted in Explore tab
//add column to Explore tab
addcolumn( Low, "Low", 1);
addcolumn( High, "High", 1);
addcolumn( Buy, "Buy", 1);
addcolumn( Short, "Short", 1);
addcolumn( dailyOpen, "daily open", 1);
addcolumn( pointsFromOpen, "points from open", 1);
addcolumn( dayTrades, "day trades", 1);
addcolumn( newDay, "new day", 1);
addcolumn( barPointsFromOpen, "bar points from open", 1);