On historical bars the stop works fine but on a live chart the stop doesnt work correctly. If the stop level is met it will close on bar close. The take profit exits at the correct limit price, but its like the stop is acting like a market on bar close instead of a limit. I have a large code section to give as much detail on the relevant section of the code as possible.
// calc_on_every_tick = true
// Strategy {
// global vars {
bool in_long = strategy.position_size > 0
bool in_short = strategy.position_size < 0
bool in_trade = strategy.position_size != 0
bool no_trade = strategy.position_size == 0
bool state_up = base_signal == 1
bool state_down = base_signal == 2
bool state_neutral = base_signal == 3
bool up_or_neutral = state_up or state_neutral
bool down_or_neutral = state_down or state_neutral
float take_profit_price = syminfo.mintick * take_profit_value
float stop_loss_price = syminfo.mintick * stop_loss_value
float break_even_price = syminfo.mintick * break_even_value
float breaK_even_ticks = syminfo.mintick * break_even_level
// end global vars }
// trade vars {
var int cue_state = 0
var bool long_cued = false
var bool short_cued = false
var bool neutral_cued = false
var float tp_price = na
var float entry_price = na
var float stop_price = na
var bool broke_even = false
bool stop_continuation = false
// end trade vars }
// strategy execution {
if barstate.isconfirmed
if no_trade
broke_even := false
tp_price := na
entry_price := na
stop_price := na
strategy.cancel_all()
if enable_reversal
if reversal_up and (base_signal == 2 or base_signal == 3) and enable_long
cue_state := base_signal
long_cued := true
short_cued := false
neutral_cued := false
if reversal_down and (base_signal == 1 or base_signal == 3) and enable_short
cue_state := base_signal
long_cued := false
short_cued := true
neutral_cued := false
if reversal_neutral and enable_neutral and no_trade
cue_state := base_signal
if state_neutral
long_cued := false
short_cued := false
neutral_cued := true
if state_up and enable_short
long_cued := false
short_cued := true
neutral_cued := false
if state_down and enable_long
long_cued := true
short_cued := false
neutral_cued := false
if cue_state == 3
if long_cued and state_down
long_cued := false
cue_state := 0
if short_cued and state_up
short_cued := false
cue_state := 0
if cancel_on_continue
if long_cued and continuation_down
stop_continuation := true
long_cued := false
neutral_cued := false
cue_state := 0
if short_cued and continuation_up
stop_continuation := true
short_cued := false
neutral_cued := false
cue_state := 0
if reset_on_repaint and repaint_siginal
if long_cued and trend == -1 and continuation
long_cued := false
cue_state := 0
if short_cued and trend == 1 and continuation
short_cued := false
cue_state := 0
if long_cued
bool cue_condition = cue_state == 2 ? up_or_neutral : state_up
if cue_condition
tp_price := close + take_profit_price
entry_price := close
stop_price := close - stop_loss_price
strategy.entry("Long", strategy.long, alert_message = buy_string)
strategy.exit("Long Exit", "Long", stop = stop_price, limit = tp_price, alert_profit = close_trade, alert_loss = close_trade)
cue_state := 0
long_cued := false
if short_cued
bool cue_condition = cue_state == 1 ? down_or_neutral : state_down
if cue_condition
tp_price := close - take_profit_price
entry_price := close
stop_price := close + stop_loss_price
strategy.entry("Short", strategy.short, alert_message = sell_string)
strategy.exit("Short Exit", "Short", stop = stop_price, limit = tp_price, alert_profit = close_trade, alert_loss = close_trade)
cue_state := 0
short_cued := false
if neutral_cued
if state_up
tp_price := close + take_profit_price
entry_price := close
stop_price := close - stop_loss_price
strategy.entry("Long", strategy.long, alert_message = buy_string)
strategy.exit("Long Exit", "Long", stop = stop_price, limit = tp_price, alert_profit = close_trade, alert_loss = close_trade)
cue_state := 0
neutral_cued := false
if state_down
tp_price := close - take_profit_price
entry_price := close
stop_price := close + stop_loss_price
strategy.entry("Short", strategy.short, alert_message = sell_string)
strategy.exit("Short Exit", "Short", stop = stop_price, limit = tp_price, alert_profit = close_trade, alert_loss = close_trade)
cue_state := 0
neutral_cued := false
if enable_continuation and not stop_continuation
if continuation_up and state_up and enable_long
tp_price := close + take_profit_price
entry_price := close
stop_price := close - stop_loss_price
strategy.entry("Long", strategy.long, alert_message = buy_string)
strategy.exit("Long Exit", "Long", stop = stop_price, limit = tp_price, alert_profit = close_trade, alert_loss = close_trade)
cue_state := 0
long_cued := false
short_cued := false
neutral_cued := false
if continuation_down and state_down and enable_short
tp_price := close - take_profit_price
entry_price := close
stop_price := close + stop_loss_price
strategy.entry("Short", strategy.short, alert_message = sell_string)
strategy.exit("Short Exit", "Short", stop = stop_price, limit = tp_price, alert_profit = close_trade, alert_loss = close_trade)
cue_state := 0
long_cued := false
short_cued := false
neutral_cued := false
if in_trade and enable_break_even and not broke_even
if in_long and high >= (entry_price + break_even_price)
stop_price := entry_price + breaK_even_ticks
strategy.exit("Long Exit", "Long", stop = stop_price, limit = tp_price, alert_profit = close_trade, alert_loss = close_trade)
broke_even := true
if in_short and low <= (entry_price - break_even_price)
stop_price := entry_price - breaK_even_ticks
strategy.exit("Short Exit", "Short", stop = stop_price, limit = tp_price, alert_profit = close_trade, alert_loss = close_trade)
broke_even := true
// end strategy execution }```
I was trying to have a stop loss limit order but what I got was a stop loss level that acted like a market order on bar close.
New contributor
Jesse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.