I’m trying to code a simple strategy using pine script.
The problems are regarding the stop loss.
I would like once the position is open a SL at 0.2% under the open price.
Once the price reaches +0.1% it closes the SL and open a trail stop loss following at 0.02% under the highest price since the position is open.
The important point is to keep the initial SL active until the price reaches the +0.1% and then switch to the trail SL.
Below my current script.
I hope my explanation is clear and someone can help me.
Thanks a lot,
Alex
//@version=5
strategy('RSI Crossover Strategy', overlay=true)
rsi_length = input(14, title='RSI Length')
rsi_cross_under = input(50, title='RSI Cross Under Level')
rsi_cross_over = input(67, title='RSI Cross Over Level')
stop_loss_percentage = input(0.2, title='Stop Loss Percentage')
take_profit_percentage = input(0.1, title='Take Profit Percentage')
rsi_value = ta.rsi(close, rsi_length)
lowest_rsi_20 = ta.lowest(rsi_value, 64)
strategy.entry('Buy', strategy.long, when=(ta.crossover(rsi_value, rsi_cross_under)) and (lowest_rsi_20 < 40))
stop_loss_price = strategy.position_avg_price * (1 - stop_loss_percentage / 100)
take_profit_price = strategy.position_avg_price * (1 + take_profit_percentage / 100)
Entry = strategy.position_avg_price
strategy.exit('Stop Loss/Take Profit', 'Buy', stop=stop_loss_price, limit=take_profit_price)
// Plot stop loss and take profit levels with labels
plot(strategy.position_size > 0 ? stop_loss_price : na, color=color.red, linewidth=1, title="SL",style=plot.style_line)
plot(strategy.position_size > 0 ? take_profit_price : na, color=color.green, linewidth=1, title="TP",style=plot.style_line)
plot(strategy.position_size > 0 ? Entry : na, color=color.blue, linewidth=1, title="TP",style=plot.style_line)
I already tried multiple approaches but it obviously not working so far
alexandre huve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.