// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © trading-setups
//@version=5
strategy(title="Silver Setup Strategy", shorttitle="Silver Strategy", overlay=true,
default_qty_type=strategy.percent_of_equity, default_qty_value=100,
commission_type=strategy.commission.percent, commission_value=0.1,
slippage=2, process_orders_on_close=true)
// ═══════════════════════════════════════════════════════════════════════════════════════════
// INPUT PARAMETERS
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Time session filters
time_session_1 = input.session("0700-0900", "Morning Session (7-9 AM)", group="Time Windows")
time_session_2 = input.session("1000-1200", "Mid-Day Session (10 AM-12 PM)", group="Time Windows")
time_session_3 = input.session("", "Optional Third Session", group="Time Windows")
// Risk management parameters
risk_multiplier = input.float(4.6, "Take Profit Multiplier", minval=1.0, maxval=10.0, step=0.1, group="Risk Management")
stop_offset = input.int(3, "Stop Loss Offset (bars)", minval=1, maxval=5, group="Risk Management")
risk_per_trade = input.float(2.0, "Risk Per Trade (%)", minval=0.5, maxval=10.0, step=0.5, group="Risk Management")
// Strategy settings
allow_long = input.bool(true, "Allow Long Positions", group="Strategy Settings")
allow_short = input.bool(true, "Allow Short Positions", group="Strategy Settings")
close_daily = input.bool(true, "Close Positions at 1 PM PT", group="Strategy Settings")
// Visual settings
show_paint_levels = input.bool(true, "Show Paint Bar Levels", group="Display Options")
show_stop_target = input.bool(true, "Show Stop/Target Levels", group="Display Options")
show_entry_signals = input.bool(true, "Show Entry Signals", group="Display Options")
// ═══════════════════════════════════════════════════════════════════════════════════════════
// PAINT BAR DETECTION
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Paint bar conditions
bull_paint_condition = low > high[2]
bear_paint_condition = high < low[2]
// ═══════════════════════════════════════════════════════════════════════════════════════════
// TIME SESSION MANAGEMENT
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Time session detection
is_time_1 = time(timeframe.period, time_session_1, "America/Los_Angeles")
is_time_2 = time(timeframe.period, time_session_2, "America/Los_Angeles")
is_time_3 = time(timeframe.period, time_session_3, "America/Los_Angeles")
// Combined time window filter
in_time_window = (not na(is_time_1) or not na(is_time_2) or not na(is_time_3)) and
dayofweek != dayofweek.saturday and dayofweek != dayofweek.sunday
// Daily close detection (1 PM Pacific)
close_time = time(timeframe.period, "1300-1301", "America/Los_Angeles")
should_close = close_daily and not na(close_time) and dayofweek != dayofweek.saturday and dayofweek != dayofweek.sunday
// ═══════════════════════════════════════════════════════════════════════════════════════════
// STATE MANAGEMENT VARIABLES
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Bull paint state variables
var float bull_paint_high = na
var float bull_paint_low = na
var float bull_stop_level = na
var float bull_target_level = na
var bool bull_paint_active = false
// Bear paint state variables
var float bear_paint_low = na
var float bear_paint_high = na
var float bear_stop_level = na
var float bear_target_level = na
var bool bear_paint_active = false
// ═══════════════════════════════════════════════════════════════════════════════════════════
// PAINT BAR DETECTION AND LEVEL CALCULATION
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Detect new bull paint bar
if bull_paint_condition and not bull_paint_active and strategy.position_size == 0
bull_paint_high := high
bull_paint_low := low
bull_stop_level := low[stop_offset]
// Calculate take profit level (risk multiplier)
risk_distance = bull_paint_low - bull_stop_level
bull_target_level := bull_paint_low + (risk_distance * risk_multiplier)
bull_paint_active := true
bear_paint_active := false // Cancel opposing paint
// Detect new bear paint bar
if bear_paint_condition and not bear_paint_active and strategy.position_size == 0
bear_paint_low := low
bear_paint_high := high
bear_stop_level := high[stop_offset]
// Calculate take profit level (risk multiplier)
risk_distance = bear_stop_level - bear_paint_high
bear_target_level := bear_paint_high - (risk_distance * risk_multiplier)
bear_paint_active := true
bull_paint_active := false // Cancel opposing paint
// ═══════════════════════════════════════════════════════════════════════════════════════════
// ENTRY CONDITIONS (3 REASONS: LOCATION, MOMENTUM, STRUCTURE)
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Long entry conditions
long_location = in_time_window // Reason 1: Location (time window)
long_momentum = bull_paint_active // Reason 2: Momentum (paint bar directional bias)
long_structure = close > bull_paint_low and close < bull_paint_high and low <= bull_paint_low // Reason 3: Structure (price re-entry)
long_filter = strategy.position_size == 0 and allow_long // Position filter
long_entry_condition = long_location and long_momentum and long_structure and long_filter
// Short entry conditions
short_location = in_time_window // Reason 1: Location (time window)
short_momentum = bear_paint_active // Reason 2: Momentum (paint bar directional bias)
short_structure = close < bear_paint_high and close > bear_paint_low and high >= bear_paint_high // Reason 3: Structure (price re-entry)
short_filter = strategy.position_size == 0 and allow_short // Position filter
short_entry_condition = short_location and short_momentum and short_structure and short_filter
// ═══════════════════════════════════════════════════════════════════════════════════════════
// POSITION SIZING CALCULATION
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Calculate position size based on risk per trade
calculate_position_size(entry_price, stop_price) =>
account_value = strategy.equity
risk_amount = account_value * (risk_per_trade / 100)
stop_distance = math.abs(entry_price - stop_price)
if stop_distance > 0
position_size = risk_amount / stop_distance
// Limit position size to account equity
math.min(position_size, account_value)
else
0
// ═══════════════════════════════════════════════════════════════════════════════════════════
// STRATEGY EXECUTION
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Long entry execution
if long_entry_condition
qty = calculate_position_size(close, bull_stop_level)
strategy.entry("Long", strategy.long, qty=qty, comment="LONG Entry")
bull_paint_active := false
// Short entry execution
if short_entry_condition
qty = calculate_position_size(close, bear_stop_level)
strategy.entry("Short", strategy.short, qty=qty, comment="SHORT Entry")
bear_paint_active := false
// ═══════════════════════════════════════════════════════════════════════════════════════════
// EXIT CONDITIONS AND EXECUTION
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Long position exits
if strategy.position_size > 0
// Stop loss
strategy.exit("Long Stop", "Long", stop=bull_stop_level, comment="LONG Stop")
// Take profit
strategy.exit("Long Target", "Long", limit=bull_target_level, comment="LONG Target")
// Daily close
if should_close
strategy.close("Long", comment="LONG Daily Close")
// Short position exits
if strategy.position_size < 0
// Stop loss
strategy.exit("Short Stop", "Short", stop=bear_stop_level, comment="SHORT Stop")
// Take profit
strategy.exit("Short Target", "Short", limit=bear_target_level, comment="SHORT Target")
// Daily close
if should_close
strategy.close("Short", comment="SHORT Daily Close")
// ═══════════════════════════════════════════════════════════════════════════════════════════
// PAINT BAR CLEANUP (REMOVE STALE PAINT BARS)
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Reset bull paint if price moves too far away or position is closed
if bull_paint_active and (high > bull_paint_high * 1.1 or low < bull_stop_level or strategy.position_size != 0)
bull_paint_active := false
// Reset bear paint if price moves too far away or position is closed
if bear_paint_active and (low < bear_paint_low * 0.9 or high > bear_stop_level or strategy.position_size != 0)
bear_paint_active := false
// ═══════════════════════════════════════════════════════════════════════════════════════════
// VISUAL ELEMENTS
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Paint bar coloring
barcolor(bull_paint_condition ? color.green : bear_paint_condition ? color.red : na, offset=-1)
// Entry signal shapes
plotshape(show_entry_signals and long_entry_condition, title="Long Entry Signal",
style=shape.triangleup, location=location.belowbar, color=color.lime,
size=size.normal, text="LONG")
plotshape(show_entry_signals and short_entry_condition, title="Short Entry Signal",
style=shape.triangledown, location=location.abovebar, color=color.red,
size=size.normal, text="SHORT")
// Paint bar levels
plot(show_paint_levels and bull_paint_active ? bull_paint_high : na,
color=color.new(color.green, 50), style=plot.style_line, linewidth=2, title="Bull Paint High")
plot(show_paint_levels and bull_paint_active ? bull_paint_low : na,
color=color.new(color.green, 50), style=plot.style_line, linewidth=2, title="Bull Paint Low")
plot(show_paint_levels and bear_paint_active ? bear_paint_high : na,
color=color.new(color.red, 50), style=plot.style_line, linewidth=2, title="Bear Paint High")
plot(show_paint_levels and bear_paint_active ? bear_paint_low : na,
color=color.new(color.red, 50), style=plot.style_line, linewidth=2, title="Bear Paint Low")
// Stop and target levels for active positions
plot(show_stop_target and strategy.position_size > 0 ? bull_stop_level : na,
color=color.new(color.red, 20), style=plot.style_line, linewidth=3, title="Long Stop")
plot(show_stop_target and strategy.position_size > 0 ? bull_target_level : na,
color=color.new(color.blue, 20), style=plot.style_line, linewidth=3, title="Long Target")
plot(show_stop_target and strategy.position_size < 0 ? bear_stop_level : na,
color=color.new(color.red, 20), style=plot.style_line, linewidth=3, title="Short Stop")
plot(show_stop_target and strategy.position_size < 0 ? bear_target_level : na,
color=color.new(color.blue, 20), style=plot.style_line, linewidth=3, title="Short Target")
// ═══════════════════════════════════════════════════════════════════════════════════════════
// PERFORMANCE METRICS TABLE
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Display strategy performance information
if barstate.islast
var table perf_table = table.new(position.top_right, 2, 10, bgcolor=color.white, border_width=1)
// Strategy information
table.cell(perf_table, 0, 0, "Silver Setup Strategy", text_color=color.black, text_size=size.small)
table.cell(perf_table, 1, 0, "", text_color=color.black, text_size=size.small)
// Current position
pos_size = strategy.position_size
pos_text = pos_size > 0 ? "LONG" : pos_size < 0 ? "SHORT" : "FLAT"
pos_color = pos_size > 0 ? color.green : pos_size < 0 ? color.red : color.gray
table.cell(perf_table, 0, 1, "Position", text_color=color.black, text_size=size.small)
table.cell(perf_table, 1, 1, pos_text, text_color=pos_color, text_size=size.small)
// Paint bar status
table.cell(perf_table, 0, 2, "Bull Paint Active", text_color=color.black, text_size=size.small)
table.cell(perf_table, 1, 2, bull_paint_active ? "YES" : "NO",
text_color=bull_paint_active ? color.green : color.red, text_size=size.small)
table.cell(perf_table, 0, 3, "Bear Paint Active", text_color=color.black, text_size=size.small)
table.cell(perf_table, 1, 3, bear_paint_active ? "YES" : "NO",
text_color=bear_paint_active ? color.green : color.red, text_size=size.small)
// Time window status
table.cell(perf_table, 0, 4, "In Time Window", text_color=color.black, text_size=size.small)
table.cell(perf_table, 1, 4, in_time_window ? "YES" : "NO",
text_color=in_time_window ? color.green : color.red, text_size=size.small)
// Performance metrics
table.cell(perf_table, 0, 5, "Total Trades", text_color=color.black, text_size=size.small)
table.cell(perf_table, 1, 5, str.tostring(strategy.closedtrades, "#"), text_color=color.black, text_size=size.small)
table.cell(perf_table, 0, 6, "Win Rate", text_color=color.black, text_size=size.small)
win_rate = strategy.closedtrades > 0 ? (strategy.wintrades / strategy.closedtrades) * 100 : 0
table.cell(perf_table, 1, 6, str.tostring(win_rate, "#.#") + "%", text_color=color.black, text_size=size.small)
table.cell(perf_table, 0, 7, "Profit Factor", text_color=color.black, text_size=size.small)
pf_text = strategy.grossloss != 0 ? str.tostring(strategy.grossprofit / math.abs(strategy.grossloss), "#.##") : "N/A"
table.cell(perf_table, 1, 7, pf_text, text_color=color.black, text_size=size.small)
table.cell(perf_table, 0, 8, "Net Profit", text_color=color.black, text_size=size.small)
net_profit_color = strategy.netprofit > 0 ? color.green : strategy.netprofit < 0 ? color.red : color.gray
table.cell(perf_table, 1, 8, str.tostring(strategy.netprofit, "#.##"), text_color=net_profit_color, text_size=size.small)
table.cell(perf_table, 0, 9, "Max Drawdown", text_color=color.black, text_size=size.small)
table.cell(perf_table, 1, 9, str.tostring(strategy.max_drawdown, "#.##"), text_color=color.red, text_size=size.small)
// ═══════════════════════════════════════════════════════════════════════════════════════════
// TRADE ANNOTATION (OPTIONAL)
// ═══════════════════════════════════════════════════════════════════════════════════════════
// Add trade labels with entry/exit information
if strategy.position_size > 0 and strategy.position_size[1] == 0
label.new(bar_index, low, "LONG\nEntry: " + str.tostring(close, "#.##") +
"\nStop: " + str.tostring(bull_stop_level, "#.##") +
"\nTarget: " + str.tostring(bull_target_level, "#.##"),
color=color.green, style=label.style_label_up, size=size.small)
if strategy.position_size < 0 and strategy.position_size[1] == 0
label.new(bar_index, high, "SHORT\nEntry: " + str.tostring(close, "#.##") +
"\nStop: " + str.tostring(bear_stop_level, "#.##") +
"\nTarget: " + str.tostring(bear_target_level, "#.##"),
color=color.red, style=label.style_label_down, size=size.small)
// Exit labels
if strategy.position_size == 0 and strategy.position_size[1] > 0
exit_reason = should_close[1] ? "Daily Close" : high[1] >= bull_target_level ? "Target Hit" : "Stop Hit"
label.new(bar_index, high, "LONG EXIT\n" + exit_reason + "\nPrice: " + str.tostring(close, "#.##"),
color=color.gray, style=label.style_label_down, size=size.small)
if strategy.position_size == 0 and strategy.position_size[1] < 0
exit_reason = should_close[1] ? "Daily Close" : low[1] <= bear_target_level ? "Target Hit" : "Stop Hit"
label.new(bar_index, low, "SHORT EXIT\n" + exit_reason + "\nPrice: " + str.tostring(close, "#.##"),
color=color.gray, style=label.style_label_up, size=size.small)
Show all 316 lines