Stop loss, Scale out and Trailing stop using the ATR

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #121459 quote
    19robhfx60
    Participant
    Average

    Hello everybody first time on this forum I hope this is the right place for this discussion.

    I am having trouble coding a strategy to back test a number of different  algorithms ( combination of indicators). Below is what I have so far and it is not doing what I expect or more importantly what I want. I have commented where I think it is needed to help explain what I am trying to do.

    In summary I wish to have a stop loss 1.5 X the ATR at time of trade entry and when break even is reached I wish to close ½ the position and let the other half run (still with the same stop loss). When the trade reaches 2 X the ATR figure at trade entry in favour of my trade I wish to apply a trailing stop to move with  the trade as profit increases, that trailing stop is the same as the original stop loss (1.5 X ATR at trade entry).

    The trade is closed when either my stop loss is hit or my exit conditions (indicators) signal me to exit. You will see there is an entry condition I have named “continuation trade” this is simply to allow for re-traces (to exit the trade and then get back in if conditions are favourable).

    Any help is welcome however time is getting away from me and I really need to solve this quickly so I can get testing algo’s. Thanks in advance.

    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Prevents the system from placing new orders on specified days of the week
    daysForbiddenEntry = OpenDayOfWeek = 0
    
    // --- settings
    amount = 2 // amount of contract/lots/shares to open for each order
    Lot2Close = 1 // amount of contract/lots/shares to close when breakeven occurs
    breakeven = averagetruerange*1.5 // multiple of ATR at time of trade entry to reach no loss trade
    stoploss = averagetruerange*1.5 // multiple of ATR at time of trade entry to stop further loss
    //trailing stop function
    trailingstart = averagetruerange*2 //trailingstop will start at mulitple of ATR points profit at time of trade entry
    //trailing step is moving the "stoploss" as trade moves in your favour
    // end settings
    
    // Conditions to enter long positions
    indicator1 = HullAverage[50](close)
    c1 = (close > indicator1)
    indicator2, indicator3, ignored, indicator4 = CALL "Aroon oscillator"[14]
    c2 = (indicator2 CROSSES OVER indicator3)
    indicator5 = Average[5](close)-Average[25](close)
    c3 = (indicator5 > 0)
    c4 = (indicator2 CROSSES OVER indicator4)
    //  original entry signal Aroon zero cross
    IF (c1 AND c2 AND c3) AND not daysForbiddenEntry THEN
    BUY amount CONTRACT AT MARKET
    // "continuation trade" signal (not opening 2nd position) Aroon line crosses upper line c4 and correct side of moving average c1 2nd confirmation indicator long c3 (long again signal)
    ELSIF NOT LONGONMARKET AND c4 AND c1 AND c3 AND not daysForbiddenEntry THEN
    BUY amount CONTRACT AT MARKET
    stoploss = averagetruerange[14]*1.5
    breakeven = averagetruerange[14]*1.5
    trailingstart = averagetruerange[14]*2
    ENDIF
    
    // Conditions to exit long positions
    indicator6 = HullAverage[50](close)
    c5 = (close < indicator6)
    indicator7, indicator8, ignored, indicator9 = CALL "Aroon oscillator"[14]
    c6 = (indicator7 CROSSES UNDER indicator8)
    indicator10 = Average[5](close)-Average[25](close)
    c7 = (indicator10 < 0)
    c8 = (indicator7 CROSSES UNDER indicator9)
    
    // Manage trade "scale out" at breakeven (half off the table)
    IF NOT ONMARKET THEN
    breakeven =0
    stoploss =0
    trailingstart =0
    newSL =0
    ENDIF
    IF LONGONMARKET AND countoflongshares= amount AND close-tradeprice[1]=> breakeven THEN
    SELL Lot2Close CONTRACT AT MARKET
    ENDIF
    
    // Move to "trailing stop" (trailing start) then apply trailing step (difference in close of trade price in favour of trade) to new stoploss (trailing stop)
    IF LONGONMARKET AND newSL=0 AND close-tradeprice(1)=> trailingstart THEN
    newSL = close-tradeprice(1) - stoploss
    ENDIF
    //next moves
    IF LONGONMARKET AND newSL>0 AND close-tradeprice(1)> close-tradeprice (-1)  THEN
    newSL = newSL+(close-tradeprice(1)-close-tradeprice(-1))
    ENDIF
    // New Stoploss
    IF LONGONMARKET AND close-tradeprice(1)=< newSL THEN
    SELL AT MARKET
    
    // Normal exit indicator signals
    ELSIF c5 OR c6 OR c7 OR c8 THEN
    SELL AT MARKET
    stoploss = averagetruerange[14]*1.5
    breakeven = averagetruerange[14]*1.5
    
    ENDIF
    
    // Conditions to enter short positions
    indicator11 = HullAverage[50](close)
    c9 = (close < indicator11)
    indicator12, indicator13, indicator14, ignored = CALL "Aroon oscillator"[14]
    c10 = (indicator12 CROSSES UNDER indicator13)
    indicator15 = Average[5](close)-Average[25](close)
    c11 = (indicator15 < 0)
    c12 = (indicator12 CROSSES UNDER indicator14)
    
    //  original entry signal Aroon zero cross
    IF (c9 AND c10 AND c11) AND not daysForbiddenEntry THEN
    SELLSHORT amount CONTRACT AT MARKET
    
    // "continuation trade" signal(not opening 2nd position) Aroon line crosses lower line c12 and correct side of moving average c9 2nd confirmation indicator short c11 (short again signal)
    ELSIF NOT SHORTONMARKET AND c12 AND c9 AND c11 AND not daysForbiddenEntry THEN
    SELLSHORT amount CONTRACT AT MARKET
    stoploss = averagetruerange[14]*1.5
    breakeven = averagetruerange[14]*1.5
    trailingstart = averagetruerange[14]*2
    
    ENDIF
    
    // Conditions to exit short positions
    indicator16 = HullAverage[50](close)
    c13 = (close > indicator16)
    indicator17, indicator18, indicator19, ignored = CALL "Aroon oscillator"[14]
    c14 = (indicator17 CROSSES OVER indicator18)
    indicator20 = Average[5](close)-Average[25](close)
    c15 = (indicator20 CROSSES OVER 0)
    c16 = (indicator17 CROSSES OVER indicator19)
    
    // Manage trade "scale out" at breakeven (half off the table)
    IF NOT ONMARKET THEN
    breakeven =0
    stoploss =0
    trailingstart =0
    newSL =0
    ENDIF
    IF SHORTONMARKET AND countofshortshares= amount AND close-tradeprice[1]=> breakeven THEN
    EXITSHORT Lot2Close CONTRACT AT MARKET
    ENDIF
    
    // Move to "trailing stop" (trailing start) then apply trailing step (difference in close of trade price in favour of trade) to new stoploss (trailing stop)
    IF SHORTONMARKET AND newSL=0 AND close-tradeprice(1)=> trailingstart THEN
    newSL= close-tradeprice(1)+ stoploss
    ENDIF
    //next moves
    IF SHORTONMARKET AND newSL>0 AND close-tradeprice(1)< close-tradeprice(-1) THEN
    newSL = newSL-(close-tradeprice(-1)-close-tradeprice(1))
    ENDIF
    // New Stoploss
    IF SHORTONMARKET AND close-tradeprice(1)=> newSL THEN
    EXITSHORT AT MARKET
    
    // Normal exit indicator signals
    ELSIF c13 OR c14 OR c15 OR c16 THEN
    EXITSHORT  AT MARKET
    stoploss = averagetruerange[14]*1.5
    breakeven = averagetruerange[14]*1.5
    
    ENDIF
    
    
    // Stops and targets
    SET STOP pLOSS stoploss
     
    

    P.S. from time to time I get an error message when I click the back

    test my system button. The message reads.

    Line 141 (the line number varies)

    One of the following characters would be more suitable than “End of code” : null

    However there is no code at this line even the line number does not appear, the code finishes two lines earlier and then the next lines number appears 1 line below where the code ends with again no code written.

    Once again thanks in advance.

    Regards 19robhfx60

    Moda thanked this post
    #121461 quote
    robertogozzi
    Moderator
    Master

    Lines 30-32 should be moved just below line 26, which is the first entry (if I correctly understood your code) and lines 91-93 should be moved to line 87.
    Lines 107-112 can be removed, since they are a duplicate of lines 45-50.
    Lines 51 and 113 have [1] next to keyword TRADEPRICE, replace it by (1).
    Negative values (-1) are NOT allowed anywhere as an index reference. If you want to refer to the value prior to (1) then you have to write (2). What do you want to do  with those lines?
    What use are lines 70-71 and 132-133?
    You never exit upon NEWSL, as it is in the original code where you read about that trailing stop. At the end you should append these lines:
    [scode]
    IF NewSL > 0 THEN
    SELL AT NewSL STOP
    EXITSHORT AT NewSL STOP
    ENDIF
    [/scode]
    Partial positions CANNOT be closed in strategies. I know this can be done only when backtesting though I have never used it, thus I cannot tell you how to tell your code to close, say, only one out of two positions opened. Perhaps it’s just:
    [scode]
    SELL 1 CONTRACT AT MARKET
    EXITSHORT 1 CONTRACT AT MARKET
    [/scode]

    #121522 quote
    19robhfx60
    Participant
    Average

    Hello Roberto. Thank you for the quick reply I was not expecting to get a reply so quickly. As you can tell by my code I’m new to this coding thing and that string of code was a combination of several other bits of coding from similar topics so there is some repeating myself going on there. I am aware about the closing of half positions situation, however I will  be trading manually to begin with so there many ways to solve that issue . I will implement the changes you have suggested and let you know the result Thank you for your knowledge and time and again thank you for the quick reply. oh I nearly forgot any ideas about the error message thing at the end of my post?

    Regards 19robhfx60

    #121535 quote
    robertogozzi
    Moderator
    Master

    Tehere are so many errors… when you modify the code many of them will be corrected.

    I’ve been returned an error about the HullAverage since I have v10.3 which has not a built-in HULL average. If you have v11 this will not be the error you are talking about.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Stop loss, Scale out and Trailing stop using the ATR


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
19robhfx60 @19robhfx60 Participant
Summary

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

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/08/2020
Status: Active
Attachments: No files
Logo Logo
Loading...