Hi everyone,
I am defining my stop loss as a function of the ATR and I would like this ATR to be, not the ATR value at the current candle, but the ATR value as it was at the opening of the trade.
I thought it would be as simple as writing
ATR=AverageTrueRange[10](close[BarIndex-TradeIndex])
but instead it throws everything off. The backtest just accumulates massive losses on the very first trade of the period. And since the formula includes “TradeIndex”, I can’t program an indicator to see what values it produces.
Am I missing something obvious?
Thanks a lot !
Hi there,
You could start with a command
Graph TradeIndex
in order to see whether that remains the same throughout the open position. This is not necessarily so ! (depending on what you further do when the position is running).
If that remains the same, then consider whether the logic is OK (I can’t tell but it looks weird ?).
Otherwise it seems to me that you could have something like this :
Once ATR = 0
If OnMarket and not OnMarket[1] then // Just opened a new position ?
ATR=AverageTrueRange[10](close)
endif
// And from here on use the ATR variable.
JSParticipant
Senior
Hi,
What is important for such calculations is that your ATR -calculation is in the “IF-Then” statement:
If myConditions then
Buy 1 contract at Market
ATR=AvarageTrueRange[10](Close[BarIndex-TradeIndex])
EndIf
Set Stop PLoss ATR
Set Take Profit (2*ATR)
If your calculation is outside the “IF-Then” then the ATR values will change every time the code is recalculated … (at the Close of the Bar)
If you define ATR as :
ATR = AverageTrueRange[10](close)
then, ATR value at TradeIndex is :
MyStopLoss = ATR[BarIndex-TradeIndex]
Anyway, the best option is to define the stop loss just after th buy instruction with “Set Stop PLoss ATR”