Convert Pine Script V5 Strategy to Study/ Indicator

Viewed 16

I'm a beginner playing around with Pine Script on Trading View and I'm looking to convert the below V5 Pine script strategy into an study/ indicator so I can use alerts to make trades and also show the alerts on the chart. I only want one buy/sell alert at a time

Buy Alert if rsi > 75 and (rsiMA > rsiMA[1]) but I don't want another buy alert while I'm still in a long trade, so the price would need to have increased by 0.8% (TP), dropped by 2% (SL) or the RSI dropped below 55 for a long to close

Similar with Short trade

Sell alert if rsi < 35 and (rsiMA < rsiMA[1]) but I don't want another sell alert while I'm still in a short trade, so the price would need to have decreased by 0.8% (TP), increased by 2% (SL) or the RSI gone above 55 for a short to close

I think I can use alertcondition() for a buy and sell alert but I don't know how to tie in the other conditions so it wont issue another buy/sell alert on the next candle

Any help would be great

     if rsi > 75 and (rsiMA > rsiMA[1])
        strategy.entry("Long Order", strategy.long, comment="ENTER-LONG")
    if rsi < 55
        strategy.close("Long Order", alert_message="L-CL")

strategy.exit("L-TP1", from_entry="Long Order", limit=high * 1.004, qty_percent=50, alert_message="L-TP1" + str.tostring(high * 1.004))
strategy.exit("L-TP2", from_entry="Long Order", limit=high * 1.008, qty_percent=100, alert_message="L-TP2" + str.tostring(high * 1.008))
strategy.exit("Exit Long", from_entry="Long Order", stop=low * 0.98, alert_message="L-SL" + str.tostring(low * 0.98))        


    if rsi < 35 and (rsiMA < rsiMA[1])
        strategy.entry("Short Order", strategy.short, comment="ENTER-SHORT")
    if rsi > 55
        strategy.close("Short Order", alert_message="S-CL")    

strategy.exit("S-TP1", from_entry="Short Order", limit=low * 0.996, qty_percent=50, alert_message="S-TP1" + str.tostring(low * 0.996))
strategy.exit("S-TP2", from_entry="Short Order", limit=low * 0.992, qty_percent=100, alert_message="S-TP2" + str.tostring(low * 0.992))
strategy.exit("Exit Short", from_entry="Short Order", stop=high * 1.02, alert_message="S-SL" + str.tostring(high * 1.02))
```
    
0 Answers
Related