Defining SL and TP target with ATR

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #174954 quote
    phanzphanz
    Participant
    Average

    Hi all,

    I would like to define my SL and TP target using AverageTrueRange (ATR).  So for example, if I open a trade to go long at price = 100, and ATR = 10, I would like to define the SL = ATR =10; and the TP = 2*ATR = 20.  I would like the SL and TP target to be fixed throughout the life of the trade.

    However, it seems my PRT code below reset a new target for the SL and TP everytime a new candle is formed.  How do I go about modifying it so the SL and TP are the one that are originally defined during the inception of the trade.  And once the trade is closed, and upon opening a new trade, the SL and TP will be reset accordingly.

    Thank you and looking forward to hearing from you soon.

     

    // Defining SL and TP properties
    mmyATR = AverageTrueRange[10](close)
    mySL = myATR
    myTP = myATR * 2
    
    // Stops and targets
    SET STOP pLOSS mySL
    SET TARGET pPROFIT myTP
    #174961 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    Firstly, you should use LOSS and PROFIT, at lines 7-8, without a leading “p“, because they require a difference (or range) expressed as a price which ATR is. You could keep using PLOSS and PPROFIT but in this case you should append “*PipSize” at the end pf those two lines, to make code portable across different instruments (as it is now it will work with indices, not with currency pairs).

    Secondly, to make SL and TP unchangeable, you have to embed their calculation within the IF…ENDIF that enters at market:

    IF MyLongConditions AND Not OnMarket THEN
       mySL = myATR
       myTP = myATR * 2
       BUY AT Market
    ENDIF

    in order to prevent them to be calculated again each new candle.

    phanz thanked this post
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Defining SL and TP target with ATR


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
phanz @phanz Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by robertogozzirobertogozzi
5 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/09/2021
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...