Search code examples
pine-scriptpine-script-v5tradingview-api

How to use the current price of the candle and not the close


I want to know if the current price of a candle can be used, not when it closes, which is using the close, but at the exact moment. I asked ChatGPT and they told me it can't be done. But I wonder, for example, in the RSI the line changes as the price fluctuates at the moment the candle is formed, I want to do something similar.

Greetings.

if(strategy.opentrades > 0 and close >= pricelimitlong or strategy.opentrades > 0 and close <= pricestoplong)
    alert("Operacion Long Cerrada")
    strategy.exit("Buy Exit", from_entry="Buy", limit = pricelimitlong, stop = pricestoplong)

// IF SHORT
if(strategy.opentrades > 0 and close <= pricelimitshort or strategy.opentrades > 0 and close >= pricestopshort)
    alert("Operacion Short Cerrada")
    strategy.exit("Sell Exit", from_entry="Sell", limit = pricelimitshort, stop = pricestopshort)

I want the trade to close when the price reaches a specific point, not when the candle closes.


Solution

  • By default, strategy() is not executed on every tick. So, even if you do;

    //@version=5
    strategy("My script", overlay=true)
    
    plot(close)
    

    This will not plot the current (close) price.

    However, you can add calc_on_every_tick=true to your strategy() and make it update on every tick. This way, you can use close as current price. This will only work in real-time and not on the historical bars.