Stop and Take Profit Multiple of ATR

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #10456 quote
    Dymjohn
    Participant
    Senior

    Hi,

    I”ve spent some considerable time trying to set ONCE a profit and Take profit as a multiple of say an ATR(14) but without success. The code runs without error but doesn’t do anything. Can you help please?

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    DEFPARAM FLATBEFORE = 070000 // Flat before this time
    DEFPARAM FLATAFTER = 205000 // Flat after this time
    //use ATR14 to set targets and stops
    
    
    //Set Stop and Targets
    //reset stop and target
    IF NOT ONMARKET THEN
    St= 0
    Tp= 0
    // set Stop and target
    
    //ONCE St =  ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
    //ONCE Tp =  ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
    
    //prices to enter trades
    BuyPrice = High[0]+2*PointSize
    SellPrice = Low[0]-2*PointSize
    
    // Conditions to enter long positions
    indicator1 = Stochastic[14,5](close)
    c1 = (indicator1 < 25)
    
    indicator2 = Average[20](close)
    indicator3 = Average[50](close)
    c2 = (indicator2 > indicator3)
    
    IF c1 AND c2 THEN
    BUY 1 PERPOINT AT BuyPrice STOP
    ENDIF
    // set stop
    IF LONGONMARKET THEN
    St =  BuyPrice - ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
    Tp = BuyPrice + ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
    ENDIF
    // Conditions to exit long positions
    indicator4 = Stochastic[14,5](close)
    c3 = (indicator4 CROSSES UNDER 75)
    
    IF c3 THEN
    SELL  AT MARKET
    ENDIF
    
    // Conditions to enter short positions
    indicator5 = Stochastic[14,5](close)
    c4 = (indicator5 > 75)
    
    indicator6 = Average[50](close)
    indicator7 = Average[20](close)
    c5 = (indicator6 > indicator7)
    
    IF c4 AND c5 THEN
    SELLSHORT 1 PERPOINT AT SellPrice STOP
    ENDIF
    // set stop
    IF SHORTONMARKET THEN
    St =  SellPrice + ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
    Tp = SellPrice - ROUND((AverageTrueRange[14](CLOSE))*2)*PointSize
    ENDIF
    
    // Conditions to exit short positions
    indicator8 = Stochastic[14,5](close)
    c6 = (indicator8 CROSSES OVER 25)
    
    IF c6 THEN
    EXITSHORT  AT MARKET
    ENDIF
    
    // Stops and targets
    // STOP AND TARGET
    SET STOP LOSS  St
    SET TARGET PPROFIT  Tp
    ENDIF
    
    #10460 quote
    Nicolas
    Keymaster
    Master

    You should compute them like this, without making any conversion to POINTS and set them as decimal values, not price ones:

    //stoploss and takeprofit for long positions
    St =  ROUND((AverageTrueRange[14](CLOSE))*2)
    Tp = ROUND((AverageTrueRange[14](CLOSE))*2)

    and use these instructions to set them:

    SET STOP LOSS  St
    SET TARGET PROFIT  Tp

    Without the “P” which is made to set POINTS/PIPS stoploss and takeprofit

    If you are using the strategy on EURUSD for example, you should expect St and Tp to be : 0.0016 , and that’s the kind of value that LOSS and PROFIT need.

    #10467 quote
    Dymjohn
    Participant
    Senior

    Thanks for looking at this Nicolas, however, neither the stops nor take profits are working on the DAX. e.g. on the tester a buy entry at 9:30, yesterday, was made at 9930.3. By 11:50 this was in profit of 72.5 points it was finally closed at 20:50 at 9943.8. Not sure which ATR the program will use but the entry bar was 15.30219 and the previous ATR 16.4528.

    Not sure what actually closed the trade as the stochastic crossed under 75  four times before closing the trade.

    I enclose my revised code for reference would appreciate your consideration.

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    DEFPARAM FLATBEFORE = 070000 // Flat before this time
    DEFPARAM FLATAFTER = 205000 // Flat after this time
    //use ATR14 to set targets and stops
    
    
    //Set Stop and Targets
    //reset stop and target
    IF NOT ONMARKET THEN
    St= 0
    Tp= 0
    
    //prices to enter trades
    BuyPrice = High[0]+2*PointSize
    SellPrice = Low[0]-2*PointSize
    
    // Conditions to enter long positions
    indicator1 = Stochastic[14,5](close)
    c1 = (indicator1 < 25)
    
    indicator2 = Average[20](close)
    indicator3 = Average[50](close)
    c2 = (indicator2 > indicator3)
    
    IF c1 AND c2 THEN
    BUY 1 PERPOINT AT BuyPrice STOP
    ENDIF
    // set stop
    IF LONGONMARKET THEN
    St = ROUND((AverageTrueRange[14](CLOSE))*2)
    Tp = ROUND((AverageTrueRange[14](CLOSE))*2)
    ENDIF
    // Conditions to exit long positions
    indicator4 = Stochastic[14,5](close)
    c3 = (indicator4 CROSSES UNDER 75)
    
    IF c3 THEN
    SELL  AT MARKET
    ENDIF
    
    // Conditions to enter short positions
    indicator5 = Stochastic[14,5](close)
    c4 = (indicator5 > 75)
    
    indicator6 = Average[50](close)
    indicator7 = Average[20](close)
    c5 = (indicator6 > indicator7)
    
    IF c4 AND c5 THEN
    SELLSHORT 1 PERPOINT AT SellPrice STOP
    ENDIF
    // set stop
    IF SHORTONMARKET THEN
    St = ROUND((AverageTrueRange[14](CLOSE))*2)
    Tp = ROUND((AverageTrueRange[14](CLOSE))*2)
    ENDIF
    
    // Conditions to exit short positions
    indicator8 = Stochastic[14,5](close)
    c6 = (indicator8 CROSSES OVER 25)
    
    IF c6 THEN
    EXITSHORT  AT MARKET
    ENDIF
    
    // Stops and targets
    // STOP AND TARGET
    SET STOP LOSS  St
    SET TARGET PROFIT  Tp
    ENDIF
    
    #10471 quote
    Nicolas
    Keymaster
    Master

    On DAX, please try using PLOSS and PPROFIT instead.

    #10472 quote
    Dymjohn
    Participant
    Senior

    Hi Nicolas,

    Same result I’m afraid. Any other suggestions please?

    John

    #10477 quote
    Nicolas
    Keymaster
    Master

    Lines 69 and 70 should be at the end of code, not above your last endif.

    #10563 quote
    Dymjohn
    Participant
    Senior

    Hi Nicolas, been away for a couple of days but when I make this change I get the message

    Syntax error line 72 character 1

    So any other ideas please?

    John

    #10570 quote
    Nicolas
    Keymaster
    Master

    I have not tested your code, but I think that it should end like this:

    ENDIF
    SET STOP LOSS  St
    SET TARGET PROFIT  Tp
    

    Make sure that any IF are correctly ended with an ENDIF.

    #10584 quote
    Dymjohn
    Participant
    Senior

    Thanks Nicolas found an if without an endif

    #203747 quote
    lilo789
    Participant
    New

    Hi everybody,

     

     

    I hope you will appreciate this TV script : https://www.tradingview.com/script/ofwFdl2A-stoch-supertrd-atr-200ma/

    It features:

    – ATR StopLoss using the price + an ATR multiplier

    -ATR TakeProfit using the price + the same ATR mult * 2  so it creates a 1:2  Risk/Reward

    – a trade breakeaven stop

    The sl/tp are fixed once the trade is launched and do not look to update along the trade.

    All these gives a better maxDD, specially the breakeven feature.

     

    Cheers 🙂

    #203763 quote
    GraHal
    Participant
    Master

    I reckon you should open a new / separate Topic and post the full script off Trading View and see if some kind soul might convert to PRT for you / us? 🙂

    #203771 quote
    lilo789
    Participant
    New

    Hi GraHal 🙂

     

    I’ll try to remember to do so within the week but first I will give it a tr by my self maybe I can do it 😀

     

     

    Have a nice week

    GraHal thanked this post
    #203878 quote
    lilo789
    Participant
    New

    Hi again

     

     

    There you are:

    ATR take profit & stop + breakeven

     

     

    Kind Regards

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

Stop and Take Profit Multiple of ATR


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Dymjohn @dymjohn Participant
Summary

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

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 07/13/2016
Status: Active
Attachments: No files
Logo Logo
Loading...