BB SSL

//version=5
strategy("Prompt michael Demo GPT - Drift Bollinger Bands Strategy", commission_type=strategy.commission.percent, commission_value=0.05, slippage=3, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// -------------------------------------------
// Original Bollinger Bands logic
// -------------------------------------------
bb_length = input.int(14, title="Length", group="Bollinger Bands", inline="BB")
bb_stddev = input.float(3.0, title="StdDev", group="Bollinger Bands", inline="BB")
[_, upper, lower] = ta.bb(close, bb_length, bb_stddev)
plot(upper, "Upper", color=color.aqua)
plot(lower, "Lower", color=color.aqua)

buy_condition_bb = ta.crossover(close, upper)
sell_condition_bb = ta.crossunder(close, lower)

// Trend filter
trend_period = input.int(365, title="Period", group="Trend Filter", inline="Trend")
buy_condition_trend = close > ta.ema(close, trend_period)
sell_condition_trend = close < ta.ema(close, trend_period)

// Volatility Filter
vol_filter_length = input.int(15, title="Length", group="Volatility Filter", inline="Vol")
vol_stddev = ta.stdev(close, vol_filter_length)
vol_ma_length = input.int(28, title=">MA", group="Volatility Filter", inline="Vol")
buy_sell_condition_vol = vol_stddev > ta.sma(vol_stddev, vol_ma_length)

// Original final conditions (not removed, but we will improve strategy using SSL conditions)
buy_condition_final = buy_condition_bb and buy_condition_trend and buy_sell_condition_vol
sell_condition_final = sell_condition_bb and sell_condition_trend and buy_sell_condition_vol

// Close conditions
close_condition_long = sell_condition_bb
close_condition_short = buy_condition_bb

// Stop loss and Trailing Profit
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="LSLTP")
tp_pct = input.float(9, title="TP", group="SL and TP", inline="LSLTP")

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)

plotshape(buy_condition_final, style=shape.labelup, location=location.belowbar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Buy', textcolor=color.new(color.white, 0))
plotshape(sell_condition_final, style=shape.labeldown, location=location.abovebar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Sell', textcolor=color.new(color.white, 0))

// -------------------------------------------
// SSL channel logic (filling gaps and ensuring correctness)
// -------------------------------------------
len = input.int(10, "SSL Length")
smaHigh = ta.sma(high, len)
smaLow = ta.sma(low, len)

var float Hlv = 0.0
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]

sslDown = Hlv < 0 ? smaHigh : smaLow
sslUp = Hlv < 0 ? smaLow : smaHigh

plot(sslDown, linewidth=2, color=color.red)
plot(sslUp, linewidth=2, color=color.lime)

// -------------------------------------------
// New improved entry/exit logic using SSL crosses
// -------------------------------------------
longEntryCondition = ta.crossover(sslUp, sslDown)
longExitCondition = ta.crossunder(sslUp, sslDown)
shortEntryCondition = ta.crossover(sslDown, sslUp)
shortExitCondition = ta.crossunder(sslDown, sslUp)

// Entries and exits with SSL conditions (no qty specified, uses 10% of capital by default settings)
if longEntryCondition
strategy.entry('Long', strategy.long)
alert('78304410994772,buy,BTCUSD,risk=0.1', alert.freq_once_per_bar_close)

if longExitCondition
strategy.close("Long")
alert('78304410994772,close_condition_long,BTCUSD,risk=0.1', alert.freq_once_per_bar_close)

if shortEntryCondition
strategy.entry('Short', strategy.short)
alert('78304410994772,sell,BTCUSD,risk=0.1', alert.freq_once_per_bar_close)

if shortExitCondition
strategy.close("Short")
alert('78304410994772,close_condition_short,BTCUSD,risk=0.1', alert.freq_once_per_bar_close)
Trend Analysis

Отказ от ответственности