I want to backtest a strategy that does a SELL_TO_CLOSE within a given number of bars if a target price hasn't been reached. Is there a function that tells the number of bars a trade has been opened? Or a method one can follow to keep a count that can be used in the SELL_TO_CLOSE test?
The following snippet describes what I've tried (that isn't working):
The following snippet describes what I've tried (that isn't working):
Code:
def longOpenRule = aboveBars > minLongBarsSinceMACross
and rsiCrossBelow
and price > trendDef;
def longTargetReached = price >= (EntryPrice() * (1 + (longExitPct / 100)));
def barCount = if !EntryPrice() then 0 else barCount[1] + 1;
AddOrder(OrderType.BUY_TO_OPEN, longOpenRule, price, tradeSize);
AddOrder(OrderType.SELL_TO_CLOSE,longTargetReached or barCount > maxLongBars, price, tradeSize);