Welcome to the BEST forum for traders

Did you know we're sharing tons of exclusive content here?
Our community is growing quickly. Take a look around and say "Hello".

SL and TP according to open and close of previous bar

Aug 26, 2021 - 5:50 PM

Viewed 1735 times

https://best-trading-indicator.com/community/bti/forums/4180/topics/25727 COPY
  • Hello guys, I am new here and I would need yout help
    I spent my whole day on internet looking for an answer but nothing is helping me

    I have a scalping algorithm. This algorithm tells me when to Long or Short. However I want to have limits and loss
    When I am going Long, the previous bar is always red. I want to put my limit at the same price than Open of the previous bar and my Loss at the time worth two times the lenght of the previous bar.

    Eg : The previous candle opened at 50 and closed at 48. I buy when the next candle open (at 48) and i want the Limit to be at 50 and Loss at 44 (= 48-((50-48)*2)))

    I tried different thing including ticks but nothing works
    I have no idea how to get the value of open and close

    If anyone can help me, I would gladly listen to his explanation

    Thank you very much

    1
  • Hi @kahei1801

    The way to get the current open, close variables are the open and close keywords
    Documentation: https://www.tradingview.com/pine-script-reference/v4

    The previous open though has to be accessed with the [1] as open[1] and the previous close as cloe[1]

    0
  • Dave (@dave):
    Hi @kahei1801
    The way to get the current open, close variables are the open and close keywords
    Documentation: https://www.tradingview.com/pine-script-reference/v4
    The previous open though has to be accessed with the [1] as open[1] and the previous close as cloe[1]

    Hi Dave and thank you for your help :)
    I think the [1] helped me a bit but I am now stuck on a new problem and i don't even know where the problem is from

    I calculate the ticks between the open and close of the previous candle stick :
    TakeProfit = (- close[1] + open[1]) / syminfo.mintick
    StopLoss = TakeProfit * 2
    (I am 100% sure that the previous candle is red, so I put a minus before close[1] to get a positive number of ticks)

    I want my position to close when I reach one of those level, thus :
    strategy.entry(Enter Long, true, when=TradeLong)
    strategy.exit(Exit Long, from_entry=Enter Long, profit=TakeProfit , stop=StopLoss )

    However the trade is clotured at the same price than it is opened. Right after it is opened. I tried this to detect where the problem is from

    strategy.exit(Leaving long, Enter long, profit=50, stop=20)
    But the situation remains the same

    Do you have any idea please ?

    Thank you again

    0
CONTENTS