ATR Stop Loss query

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #116821 quote
    sulimaster
    Participant
    Average

    Hi

    In the below code, is the ATR related stop loss level re-calculated after close of every bar when trade is on market? I want to use the ATR of the bar just before the trade was executed. Any ideas how this can be coded please?

    Thanks

    Sachin

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    x = AroonUp[10]
    y = AroonDown[10]
    
    // Conditions to enter long positions
    
    IF (x > y) THEN
    BUY 1 PERPOINT AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    
    IF (y > x) THEN
    SELL AT MARKET
    ENDIF
    
    // Conditions to enter short positions
    //
    IF (y > x) THEN
    SELLSHORT 1 PERPOINT AT MARKET
    ENDIF
    
    // Conditions to exit short positions
    
    IF (x > y) THEN
    EXITSHORT AT MARKET
    ENDIF
    
    SET STOP LOSS AverageTrueRange[14](close)
    
    #116825 quote
    robertogozzi
    Moderator
    Master
    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    IF OnMarket THEN
       SL = 0
    ELSE
       SL = AverageTrueRange[14](close)
    ENDIF
    
    x = AroonUp[10]
    y = AroonDown[10]
     
    // Conditions to enter long positions
     
    IF (x > y) THEN
       BUY 1 PERPOINT AT MARKET
    ENDIF
     
    // Conditions to exit long positions
     
    IF (y > x) THEN
       SELL AT MARKET
    ENDIF
     
    // Conditions to enter short positions
    //
    IF (y > x) THEN
       SELLSHORT 1 PERPOINT AT MARKET
    ENDIF
     
    // Conditions to exit short positions
     
    IF (x > y) THEN
       EXITSHORT AT MARKET
    ENDIF
    
    IF SL > 0 THEN
       SET STOP LOSS SL
    ENDIF

    you need to calculate and set SL only when not Onmarket, once a trade is opened it must no be set anymore.

    sulimaster thanked this post
    #116826 quote
    Vonasi
    Moderator
    Master

    Try this (not tested). The ATR stop is fixed at the ATR value at the close of the decision bar which is the one before the trade is actually executed. Not sure if you mean this bar or the one before the decision bar so just change the [0] to [1] in AverageTrueRange[14](close[0]) if it is the the ATR of the bar before the decision bar that you mean.

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    x = AroonUp[10]
    y = AroonDown[10]
    
    // Conditions to enter long positions
    
    IF not longonmarket and(x > y) THEN
    BUY 1 PERPOINT AT MARKET
    SET STOP LOSS AverageTrueRange[14](close[0])
    ENDIF
    
    // Conditions to exit long positions
    
    IF (y > x) THEN
    SELL AT MARKET
    ENDIF
    
    // Conditions to enter short positions
    //
    IF not shortonmarket and (y > x) THEN
    SELLSHORT 1 PERPOINT AT MARKET
    SET STOP LOSS AverageTrueRange[14](close[0])
    ENDIF
    
    // Conditions to exit short positions
    
    IF (x > y) THEN
    EXITSHORT AT MARKET
    ENDIF
    
    
    sulimaster thanked this post
    #116827 quote
    Vonasi
    Moderator
    Master

    Roberto – you beat me by three minutes but your code will not set the stop loss until the trade has been open for one bar.

    sulimaster and robertogozzi thanked this post
    #116829 quote
    sulimaster
    Participant
    Average

    Thanks Vonasi

    Yes, ATR at close of the decision bar. Your code will execute both rows 10 and 11? No need for AND or anything else after MARKET on row 10 and before SET on row 11?

    #116831 quote
    Vonasi
    Moderator
    Master

    No. Any time SET is read in the code it sends an order to your broker. That order stays on the market till it is either filled or it is cancelled or changed by another SET instruction being read in the code.

    sulimaster thanked this post
    #116833 quote
    sulimaster
    Participant
    Average

    Thank you Vonasi. Learnt something very useful today.

    FYI – I’ve done a quick test and it seems to work

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

ATR Stop Loss query


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
sulimaster @sulimaster Participant
Summary

This topic contains 6 replies,
has 3 voices, and was last updated by sulimaster
6 years, 1 month ago.

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