Fixed Stop Loss

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #221578 quote
    Brad
    Participant
    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
    #221625 quote
    robertogozzi
    Moderator
    Master

    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
    #221667 quote
    Brad
    Participant
    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.

    1.-My-original-code.png 1.-My-original-code.png 2.-The-adjusted-code-with-your-changes.png 2.-The-adjusted-code-with-your-changes.png 3.-Replacing-the-code-with-a-simple-fixed-20-point-SL.png 3.-Replacing-the-code-with-a-simple-fixed-20-point-SL.png
    #221672 quote
    robertogozzi
    Moderator
    Master

    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
    Brad thanked this post
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Fixed Stop Loss


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Brad @bradkinsey-2 Participant
Summary

This topic contains 3 replies,
has 2 voices, and was last updated by robertogozzi
2 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/25/2023
Status: Active
Attachments: 3 files
Logo Logo
Loading...