doing my best to learn pinescript. im currently working with a Market Structure shift Strategy and CANNOT understan why these trades are closing immediatly after placing them.
at this stage i simply want to take trades in the direction of the identified MSS and close them on an opposing one.
I have previously grappled with trades opening and closing on every bar and solved it by adding a criteria reset after trade entry. im wondering if that is what has cause it.
code is outlined below.
//@version=5
strategy("Market Structure (working copy)", "Market Structure (working copy)", true, max_labels_count = 500, max_lines_count = 500)
pivot_strength = input.int(3, "Pivot Strength", 1)
show_pivots = input.bool(false, "Show Pivots", inline = "PIV")
highs = input.color(color.blue, "", inline = "PIV")
lows = input.color(#f23645, "", inline = "PIV")
show_bos = input.bool(false, "Show BOS/MSS")
style = input.string("Dashed", "Line Style", options = ['Solid', 'Dashed', 'Dotted'])
enableFilter = input(true, "Enable Backtesting Range Filtering")
fromDate = input.time(timestamp("20 Jul 2023 00:00 +0300"), "Start Date")
toDate = input.time(timestamp("20 Jul 2099 00:00 +0300"), "End Date")
tradeDateIsAllowed = not enableFilter or (time >= fromDate and time <= toDate)
ticks_target = input.int(60, "Ticks Target")
// Times are in UTC
type swing
int _index = na
float _val = na
method set_val(swing s, int i, float v) =>
s._index := i
s._val := v
method set_na(swing s) =>
s._index := na
s._val := na
var swing_high = swing.new()
var swing_low = swing.new()
var temp_hi = swing.new()
var temp_lo = swing.new()
var bool bull = na
sh = false
sl = false
bull_bos = false
bull_mss = false
bear_bos = false
bear_mss = false
var line_style = switch style
"Solid" => line.style_solid
"Dashed" => line.style_dashed
"Dotted" => line.style_dotted
// drawing boxes on the sh and sl's
if not na(ta.pivothigh(high, pivot_strength, pivot_strength))
swing_high.set_val(bar_index - pivot_strength, high[pivot_strength])
sh := true // Sh and SL are used for the shapes drawn above and below sl and sh's
if not na(ta.pivotlow(low, pivot_strength, pivot_strength))
swing_low.set_val(bar_index - pivot_strength, low[pivot_strength])
sl := true // Sh and SL are used for the shapes drawn above and below sl and sh's
// ID MSS section
if not na(swing_high._val)
if close > swing_high._val
mss = (bull == false or na(bull))
if mss
bull_mss := true
temp_hi.set_val(swing_high._index, swing_high._val)
bear_mss := false // Reset bear_mss when bull_mss is true
else
bull_bos := true
temp_hi.set_val(swing_high._index, swing_high._val)
bull := true
swing_high.set_na()
if not na(swing_low._val)
if close < swing_low._val
mss = (bull == true or na(bull))
if mss
bear_mss := true
temp_lo.set_val(swing_low._index, swing_low._val)
bull_mss := false // Reset bull_mss when bear_mss is true
else
bear_bos := true
temp_lo.set_val(swing_low._index, swing_low._val)
bull := false
swing_low.set_na()
// MSS direction ascertained in here.
if show_bos
if bull_mss[1] or bull_bos[1]
line.new(temp_hi._index, temp_hi._val, bar_index - 1, temp_hi._val, style = line_style, color = highs)
label.new(math.floor(math.avg(temp_hi._index, bar_index - 1)), temp_hi._val, bull_mss[1] ? "MSS" : "BOS", textcolor = highs, color = #ffffff00, style = label.style_label_down)
if bear_mss[1] or bear_bos[1]
line.new(temp_lo._index, temp_lo._val, bar_index - 1, temp_lo._val, style = line_style, color = lows)
label.new(math.floor(math.avg(temp_lo._index, bar_index - 1)), temp_lo._val, bear_mss[1] ? "MSS" : "BOS", textcolor = lows, color = #ffffff00, style = label.style_label_up)
plotshape(show_pivots and sh, style = shape.triangledown, location = location.abovebar, color = highs, offset = -pivot_strength, size = size.tiny)
plotshape(show_pivots and sl, style = shape.triangleup, location = location.belowbar, color = lows, offset = -pivot_strength, size = size.tiny)
// Entry conditions
var entry_condition_long = false
var entry_condition_short = false
// Variables to reset MSS conditions one bar later
var bool bull_mss_reset = false
var bool bear_mss_reset = false
// Check for entry based on MSS
if bull_mss and tradeDateIsAllowed and strategy.opentrades < 1
entry_condition_long := true
if bear_mss and tradeDateIsAllowed and strategy.opentrades < 1
entry_condition_short := true
// Entry
if entry_condition_long and bull_mss
strategy.entry("Buy", strategy.long, 1)
if entry_condition_short and bear_mss
strategy.entry("Sell", strategy.short, 1)
// Exit conditions
var exit_condition_long = false
var exit_condition_short = false
// Check for exit based on opposing MSS
if bear_mss and tradeDateIsAllowed and strategy.opentrades > 0
exit_condition_long:= true
if bull_mss and tradeDateIsAllowed and strategy.opentrades > 0
exit_condition_short:= true
// Exit
if exit_condition_long
strategy.close("Buy")
if exit_condition_short
strategy.close("Sell")
Adam Seeber is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.