BradParticipant
Average
Hi All
I am struggling to see what I am doing wrong with my SL setting.
I am trying to set a FIXED SL price according to the data when a trade is triggered using the highest price of the previous 5 bars + half the ATR.
Am I coding it correctly? Are the codes in the correct place? The SL does not take effect when back-testing.
Any assistance is appreciated.
defparam cumulateOrders = false
// Set the parameters for the moving average
MAPeriod = 20
// Calculate the moving average
MA = average[MAPeriod](close)
// Sell short when close crosses below the moving average
if close crosses under MA then
sellshort at market
endif
// Set the profit target and stop loss
set target pprofit 50 // 50 points profit target
IF TradeIndex[0] THEN
LookBack = 5
HighestHigh = Highest[LookBack](high) + (AverageTrueRange[14](close)[0] *0.5)
OriginalSLPrice = HighestHigh
ENDIF
SET STOP LOSS OriginalSLPrice
Tradeindex must be referenced using PARENTHESES, not brackets. The last operation’s bar is TRADEINDEX or TRADEINDEX(1).
Be warned that TRADEINDEX will be aware of the newly opened (or closed) trade at the closing of the candle, so lines 19-21 will only be executed from the second bar on…
Bear in mind that TRADEINDEX is the bar when the last operation (be it Entry or Exit) is executed.
I suggest replacing the last 6 lines as follows:
myBAR = TradeIndex
IF OnMarket AND myBAR <> myBAR[1] THEN
LookBack = 5
HighestHigh = Highest[LookBack](high) + (AverageTrueRange[14](close)[0] *0.5)
OriginalSLPrice = HighestHigh
SET STOP LOSS OriginalSLPrice
ENDIF
BradParticipant
Average
Thanks for the feedback Roberto.
I seem to get the same results. No SL is set up.
I’ve added the following screenshots with results where a trade is triggered and where a stop should be placed:
1. My original code.
2. The adjusted code with your changes.
3. Replacing the code with a simple fixed 20-point SL.
When I replace the code with a simple SET STOP LOSS 20, I get the required entry and exit.
My fault, I didn’t realize that you are setting your Stop Loss at a PRICE level, not a PRICE DIFFERENCE. Replace SET STOP LOSS with SET STOP PRICE as follows:
SET STOP PRICE OriginalSLPrice