How can I write a pinescript strategy to buy and sell with ATR based TP and SL value?

Discussion in 'Strategy Building' started by MrAgi1, Dec 11, 2023.

  1. MrAgi1

    MrAgi1

    I want to write a pinescript strategy that places a simultaneous buy and sell order at the open price of each candle on my selected chart. The buy and sell orders have TP of ATR value/3. And they both have SL of the ATR value.

    The results from ChatGPT and Bard are error prone as usual and don’t work in the pinescript editor.
     
  2. Sergio123

    Sergio123

    schizo and MrAgi1 like this.
  3. tsfx

    tsfx

    I don't understand your request. What do you mean place buy AND sell orders at open price? You mean you want to execute market order when new candle opens?
     
    MrAgi1 likes this.
  4. MrAgi1

    MrAgi1

    Yes
     
  5. MrAgi1

    MrAgi1

    Yeah I did. But need to make some adjustments. But no errors
     
  6. tsfx

    tsfx

    but if you sell market and buy market at the same time then you don't have a position?! :confused:
     
  7. MrAgi1

    MrAgi1

    Yeah I know. Just want to know how many times the TP is executed more than the stop loss. When the SL is 3 time greater than the TP. And to try it both directions.
    And also if neither is touched before the end of the closing day, then the close price is exit for both the buy and sell market orders.
     
  8. tsfx

    tsfx

    If your stop loss is higher than the TP and you do it both directions simultaneously then how can stop loss ever be touched before first TP?

    Or you mean that when the first TP is executed you will open another position at the open price should the price trade back there and then have a new at 0.3ATR again? So, basically you want to know how much price ranges before back and forth 0.3ATR before reaching first SL?
     
  9. MrAgi1

    MrAgi1

    Exactly. Want to know how much price ranges. How often does it go back and forth before trending for a long time in one direction(and hitting the SL). I am not interested in the trades PNL values. Just want to count the number of TP hit instead of SL
     
    tsfx likes this.
  10. R1234

    R1234

    Lots of guys that can code this on fiverr
     
    #10     Dec 12, 2023