DEV Community

karen & kavan
karen & kavan

Posted on

How can I convert Pinescript trailing tp&sl to pybit?

Hey guys,
I tried to convert below sample code from pinescript to pybit that is about trailing stop loss and take profit but I confused
please help me.

trail_stop_pct = input.float(0.5, title = "trailing stop activation (%)", group = "Exit Long", inline = "LTS", tooltip = "Trailing Threshold %")
trail_offset_pct = input.float(0.5, title = "trailing offset (%)", group = "Exit Long", inline = "LTS", tooltip = "Trailing offset %")
trail_stop_tick = trail_stop_pct * close / 100
trail_offset_tick = trail_offset_pct * close / 100

sl_pct = input.float(2, title = "SL", group = "SL and TP", inline = "SLTP")
tp_pct = input.float(9, title = "TP", group = "SL and TP", inline = "SLTP")

long_sl_price = strategy.position_avg_price * (1-sl_pct/100)
long_tp_price = strategy.position_avg_price * (1+tp_pct/100)
short_sl_price = strategy.position_avg_price * (1+sl_pct/100)
short_tp_price = strategy.position_avg_price * (1-tp_pct/100)

strategy.exit("Long Exit", "Long", stop = long_sl_price, limit = long_tp_price, trail_points = trail_stop_tick, trail_offset = trail_offset_tick)
strategy.exit("Short Exit", "Short", stop = short_sl_price, limit = short_tp_price, trail_points = trail_stop_tick, trail_offset = trail_offset_tick)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)