gogo2/Niki/GPT/short_1m.pine
Dobromir Popov 219cfa3569 new pine
2025-02-08 21:03:55 +02:00

122 lines
4.9 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//@version=6
strategy("Aggressive Bear Market Short Strategy - V6 Improved", overlay=true, initial_capital=10000, currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// === INPUTS ===
// Trend Confirmation: SMA
smaPeriod = input.int(title="SMA Period", defval=50, minval=1)
// RSI Inputs
rsiPeriod = input.int(title="RSI Period", defval=14, minval=1)
rsiAggThreshold = input.int(title="Aggressive RSI Threshold", defval=60, minval=1, maxval=100)
// MACD Inputs
macdFast = input.int(title="MACD Fast Length", defval=12, minval=1)
macdSlow = input.int(title="MACD Slow Length", defval=26, minval=1)
macdSignalL = input.int(title="MACD Signal Length", defval=9, minval=1)
// Bollinger Bands Inputs
bbLength = input.int(title="Bollinger Bands Length", defval=20, minval=1)
bbStdDev = input.float(title="BB StdDev Multiplier", defval=2.0, step=0.1)
// Stochastic Inputs
stochLength = input.int(title="Stochastic %K Length", defval=14, minval=1)
stochSmooth = input.int(title="Stochastic %D Smoothing", defval=3, minval=1)
stochAggThreshold = input.int(title="Aggressive Stochastic Threshold", defval=75, minval=1, maxval=100)
// ADX Inputs (Manual Calculation)
adxPeriod = input.int(title="ADX Period", defval=14, minval=1)
adxAggThreshold = input.float(title="Aggressive ADX Threshold", defval=20.0, step=0.1)
// Risk Management Inputs
stopLossPercent = input.float(title="Stop Loss (%)", defval=0.5, step=0.1)
takeProfitPercent = input.float(title="Take Profit (%)", defval=1.0, step=0.1)
// Trailing Stop Option
useTrailingStop = input.bool(title="Use Trailing Stop", defval=true)
trailStopPercent = input.float(title="Trailing Stop (%)", defval=0.5, step=0.1)
trailOffset = useTrailingStop ? trailStopPercent / 100 * close : na
// === INDICATOR CALCULATIONS ===
// 1. SMA for trend confirmation.
smaValue = ta.sma(close, smaPeriod)
// 2. RSI measurement.
rsiValue = ta.rsi(close, rsiPeriod)
// 3. MACD Calculation.
[macdLine, signalLine, _] = ta.macd(close, macdFast, macdSlow, macdSignalL)
// 4. Bollinger Bands Calculation.
bbBasis = ta.sma(close, bbLength)
bbDev = bbStdDev * ta.stdev(close, bbLength)
bbUpper = bbBasis + bbDev
bbLower = bbBasis - bbDev
// 5. Stochastic Oscillator.
k = ta.stoch(close, high, low, stochLength)
d = ta.sma(k, stochSmooth)
// 6. Manual ADX Calculation (using Wilders smoothing):
prevClose = nz(close[1], close)
tr = math.max(high - low, math.max(math.abs(high - prevClose), math.abs(low - prevClose)))
upMove = high - nz(high[1])
downMove = nz(low[1]) - low
plusDM = (upMove > downMove and upMove > 0) ? upMove : 0
minusDM = (downMove > upMove and downMove > 0) ? downMove : 0
atr = ta.rma(tr, adxPeriod)
smPlusDM = ta.rma(plusDM, adxPeriod)
smMinusDM = ta.rma(minusDM, adxPeriod)
plusDI = 100 * (smPlusDM / atr)
minusDI = 100 * (smMinusDM / atr)
dx = 100 * math.abs(plusDI - minusDI) / (plusDI + minusDI)
adxValue = ta.rma(dx, adxPeriod)
// === AGGRESSIVE SIGNAL CONDITIONS ===
// Mandatory Bearish Trend Condition: Price must be below the SMA.
bearTrend = close < smaValue
// MACD Condition: Enter if MACD is below its signal line.
macdSignalFlag = macdLine < signalLine
// RSI Condition: Enter if RSI is above the aggressive threshold.
rsiSignalFlag = rsiValue > rsiAggThreshold
// Bollinger Bands Condition: Enter if price is above the upper band (overextended rally).
bbSignalFlag = close > bbUpper
// Stochastic Condition: Trigger if %K crosses under the aggressive threshold.
stochSignalFlag = ta.crossunder(k, stochAggThreshold)
// ADX Condition: Confirm that trend strength is above the threshold.
adxSignalFlag = adxValue > adxAggThreshold
// Count the number of indicator signals present.
signalCount = (macdSignalFlag ? 1 : 0) +
(rsiSignalFlag ? 1 : 0) +
(bbSignalFlag ? 1 : 0) +
(stochSignalFlag ? 1 : 0) +
(adxSignalFlag ? 1 : 0)
// Require the bearish trend plus at least 3 indicator signals before entering.
entryCondition = bearTrend and (signalCount >= 3)
if entryCondition
strategy.entry("Short", strategy.short)
// === EXIT CONDITIONS ===
// For open short positions, set a defined stop loss and take profit.
// The stop loss is placed above the entry price, and the take profit is below.
// If enabled, a trailing stop is added.
if strategy.position_size < 0
entryPrice = strategy.position_avg_price
stopPrice = entryPrice * (1 + stopLossPercent / 100)
targetPrice = entryPrice * (1 - takeProfitPercent / 100)
strategy.exit("Exit Short", from_entry="Short", stop=stopPrice, limit=targetPrice, trail_offset=trailOffset)
// === PLOTTING ===
plot(smaValue, color=color.orange, title="SMA")
plot(bbUpper, color=color.blue, title="Bollinger Upper")
plot(bbBasis, color=color.gray, title="Bollinger Basis")
plot(bbLower, color=color.blue, title="Bollinger Lower")
plot(adxValue, title="ADX", color=color.fuchsia)
plot(rsiValue, title="RSI", color=color.purple)
hline(rsiAggThreshold, title="Aggressive RSI Threshold", color=color.red)