3

I want to add some code to my strategy on TV that will update the stop loss level when the price moves up by X%. Basically, I am trying to update the variable multiple times.

Everything I've tried creates a stop that moves up and down. I want it to only go up.

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
  • Welcome to stackoverflow. Could you please update your post to include the code that you've tried? – HaC Mar 17 '19 at 04:31

2 Answers2

2

I figured it out.

//////

stop = input(10.0, title='Stop Loss %', type=float)/100

first_stop = strategy.position_avg_price * (1 - stop)
second_stop = strategy.position_avg_price 
third_stop = strategy.position_avg_price * 1.05
fourth_stop = strategy.position_avg_price * 1.1
fifth_stop = strategy.position_avg_price * 1.15
sixth_stop = strategy.position_avg_price * 1.2
seventh_stop = strategy.position_avg_price * 1.25
eighth_stop = strategy.position_avg_price * 1.3

move_trigger = lowest(low,7)

first_check = na
first_check := move_trigger > second_stop ? second_stop : first_stop

second_check = na
second_check := move_trigger > third_stop ? third_stop : first_check

third_check = na
third_check := move_trigger > fourth_stop ? fourth_stop : second_check

fourth_check = na
fourth_check := move_trigger > fifth_stop ? fifth_stop : third_check

fifth_check = na
fifth_check := move_trigger > sixth_stop ? sixth_stop : fourth_check

sixth_check = na
sixth_check := move_trigger > seventh_stop ? seventh_stop : fifth_check

stop_level = na
stop_level := move_trigger > eighth_stop ? eighth_stop : sixth_check

/////

  • Hi thanks for this! Could you possibly show how you incorporated it with entries and closes/exits?? That would be great. – Drewdavid Oct 18 '21 at 16:58
0

Use any entry trigger you want. I use this in combination with a simple take profit trigger that exits at position avg price X a set percentage. It doesn't seem to work with other exits.

David
  • 1