DEV Community

karen & kavan
karen & kavan

Posted on

2

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay