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

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

If you found this post useful, consider leaving a ❤️ or a nice comment!

Got it