Bar Index (barindex) of the bar at which the Nth last order was executed.
Syntax:
1 |
TRADEINDEX(N) |
Example :
Exit all positions if at least 5 bars elapsed since the last trade has launched.
1 2 3 4 |
if onmarket and BARINDEX-TRADEINDEX(1)>=5 then SELL AT MARKET EXITSHORT AT MARKET endif |
Example :
Example of a simple trading strategy using MACD crossing above and below the zero line with adding orders while the price continue to get higher with condition on number of bars since the last open.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
defparam cumulateorders=true myMACD = MACD[12,26,9](close) long = myMACD crosses over 0 exit = myMACD crosses under 0 //first order IF NOT LongOnMarket AND long THEN BUY 1 CONTRACTS AT MARKET ENDIF If LongOnMarket AND exit THEN SELL AT MARKET ENDIF //let's add another order while price continue to get higher (more than 10 points) than the last order taken with a condition of 5 bars elapsed since then IF BARINDEX-TRADEINDEX(1)>5 AND Close-TRADEPRICE(1)>10 AND LongOnMarket THEN BUY 1 CONTRACTS AT MARKET ENDIF //trailing stop for the whole orders SET STOP %TRAILING 1.5 |
Remember to set
DEFPARAM CumulateOrders=true
or it dosn’t work
Code of the example fixed, thanks a lot.