pine short v2
This commit is contained in:
parent
8a64d833f3
commit
fdf3d8fd85
@ -1,31 +1,34 @@
|
|||||||
//@version=6
|
//@version=6
|
||||||
strategy("Versatile Bear Market Short Strategy - V6 (Manual ADX)", overlay=true, initial_capital=10000, currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
|
strategy("Aggressive Bear Market Short Strategy - V6 (Aggressive Conditions)", overlay=true, initial_capital=10000, currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
|
||||||
|
|
||||||
// === INPUTS ===
|
// === INPUTS ===
|
||||||
// Trend Confirmation: Simple Moving Average
|
// Trend Confirmation: Simple Moving Average
|
||||||
smaPeriod = input.int(title="SMA Period", defval=50, minval=1)
|
smaPeriod = input.int(title="SMA Period", defval=50, minval=1)
|
||||||
|
|
||||||
// Overbought Condition: RSI Parameters
|
// RSI Parameters
|
||||||
rsiPeriod = input.int(title="RSI Period", defval=14, minval=1)
|
rsiPeriod = input.int(title="RSI Period", defval=14, minval=1)
|
||||||
rsiOverbought = input.int(title="RSI Overbought Level", defval=60, minval=1, maxval=100)
|
// Lower the overbought threshold to be more aggressive.
|
||||||
|
rsiAggThreshold = input.int(title="Aggressive RSI Threshold", defval=50, minval=1, maxval=100)
|
||||||
|
|
||||||
// MACD Parameters
|
// MACD Parameters
|
||||||
macdFast = input.int(title="MACD Fast Length", defval=12, minval=1)
|
macdFast = input.int(title="MACD Fast Length", defval=12, minval=1)
|
||||||
macdSlow = input.int(title="MACD Slow Length", defval=26, minval=1)
|
macdSlow = input.int(title="MACD Slow Length", defval=26, minval=1)
|
||||||
macdSignal = input.int(title="MACD Signal Length", defval=9, minval=1)
|
macdSignalL = input.int(title="MACD Signal Length", defval=9, minval=1)
|
||||||
|
|
||||||
// Bollinger Bands Parameters
|
// Bollinger Bands Parameters
|
||||||
bbLength = input.int(title="Bollinger Bands Length", defval=20, minval=1)
|
bbLength = input.int(title="Bollinger Bands Length", defval=20, minval=1)
|
||||||
bbStdDev = input.float(title="BB StdDev Multiplier", defval=2.0, step=0.1)
|
bbStdDev = input.float(title="BB StdDev Multiplier", defval=2.0, step=0.1)
|
||||||
|
|
||||||
// Stochastic Oscillator Parameters
|
// Stochastic Oscillator Parameters
|
||||||
stochLength = input.int(title="Stochastic %K Length", defval=14, minval=1)
|
stochLength = input.int(title="Stochastic %K Length", defval=14, minval=1)
|
||||||
stochSmooth = input.int(title="Stochastic %D Smoothing", defval=3, minval=1)
|
stochSmooth = input.int(title="Stochastic %D Smoothing", defval=3, minval=1)
|
||||||
stochOverbought = input.int(title="Stochastic Overbought Level", defval=80, minval=1, maxval=100)
|
// Lower overbought threshold to catch more rallies.
|
||||||
|
stochAggThreshold = input.int(title="Aggressive Stochastic Threshold", defval=70, minval=1, maxval=100)
|
||||||
|
|
||||||
// ADX Parameters (Manual Calculation)
|
// ADX Parameters (Manual Calculation)
|
||||||
adxPeriod = input.int(title="ADX Period", defval=14, minval=1)
|
adxPeriod = input.int(title="ADX Period", defval=14, minval=1)
|
||||||
adxThreshold = input.float(title="ADX Trend Strength Threshold", defval=25.0, step=0.1)
|
// Lower the ADX threshold for trend strength.
|
||||||
|
adxAggThreshold = input.float(title="Aggressive ADX Threshold", defval=20.0, step=0.1)
|
||||||
|
|
||||||
// Risk Management
|
// Risk Management
|
||||||
stopLossPercent = input.float(title="Stop Loss (%)", defval=0.5, step=0.1)
|
stopLossPercent = input.float(title="Stop Loss (%)", defval=0.5, step=0.1)
|
||||||
@ -34,95 +37,101 @@ takeProfitPercent = input.float(title="Take Profit (%)", defval=0.3, step=0.1)
|
|||||||
|
|
||||||
// === INDICATOR CALCULATIONS ===
|
// === INDICATOR CALCULATIONS ===
|
||||||
|
|
||||||
// 1. SMA for trend confirmation; price below this confirms a bearish market.
|
// 1. SMA for overall trend determination.
|
||||||
smaValue = ta.sma(close, smaPeriod)
|
smaValue = ta.sma(close, smaPeriod)
|
||||||
|
|
||||||
// 2. RSI to check for overbought conditions.
|
// 2. RSI calculation.
|
||||||
rsiValue = ta.rsi(close, rsiPeriod)
|
rsiValue = ta.rsi(close, rsiPeriod)
|
||||||
|
|
||||||
// 3. MACD to catch momentum shifts.
|
// 3. MACD calculation.
|
||||||
[macdLine, signalLine, _] = ta.macd(close, macdFast, macdSlow, macdSignal)
|
[macdLine, signalLine, _] = ta.macd(close, macdFast, macdSlow, macdSignalL)
|
||||||
|
|
||||||
// 4. Bollinger Bands to identify potential exhaustion in rallies.
|
// 4. Bollinger Bands calculation.
|
||||||
bbBasis = ta.sma(close, bbLength)
|
bbBasis = ta.sma(close, bbLength)
|
||||||
bbDev = bbStdDev * ta.stdev(close, bbLength)
|
bbDev = bbStdDev * ta.stdev(close, bbLength)
|
||||||
bbUpper = bbBasis + bbDev
|
bbUpper = bbBasis + bbDev
|
||||||
bbLower = bbBasis - bbDev
|
bbLower = bbBasis - bbDev
|
||||||
|
|
||||||
// 5. Stochastic Oscillator (using %K) for additional overbought signals.
|
// 5. Stochastic Oscillator calculation.
|
||||||
k = ta.stoch(close, high, low, stochLength)
|
k = ta.stoch(close, high, low, stochLength)
|
||||||
d = ta.sma(k, stochSmooth)
|
d = ta.sma(k, stochSmooth)
|
||||||
|
|
||||||
|
|
||||||
// === MANUAL ADX CALCULATION ===
|
// === MANUAL ADX CALCULATION ===
|
||||||
// Step 1: Calculate the True Range (TR)
|
// Step 1: True Range (TR)
|
||||||
prevClose = nz(close[1], close)
|
prevClose = nz(close[1], close)
|
||||||
tr = math.max(high - low, math.max(math.abs(high - prevClose), math.abs(low - prevClose)))
|
tr = math.max(high - low, math.max(math.abs(high - prevClose), math.abs(low - prevClose)))
|
||||||
|
|
||||||
// Step 2: Calculate directional movements (DM)
|
// Step 2: Directional Movements (DM)
|
||||||
upMove = high - nz(high[1])
|
upMove = high - nz(high[1])
|
||||||
downMove = nz(low[1]) - low
|
downMove = nz(low[1]) - low
|
||||||
plusDM = (upMove > downMove and upMove > 0) ? upMove : 0
|
plusDM = (upMove > downMove and upMove > 0) ? upMove : 0
|
||||||
minusDM = (downMove > upMove and downMove > 0) ? downMove : 0
|
minusDM = (downMove > upMove and downMove > 0) ? downMove : 0
|
||||||
|
|
||||||
// Step 3: Smooth the values using Wilder's RMA (smoothed moving average)
|
// Step 3: Wilder's RMA smoothing
|
||||||
atr = ta.rma(tr, adxPeriod)
|
atr = ta.rma(tr, adxPeriod)
|
||||||
smPlusDM = ta.rma(plusDM, adxPeriod)
|
smPlusDM = ta.rma(plusDM, adxPeriod)
|
||||||
smMinusDM = ta.rma(minusDM, adxPeriod)
|
smMinusDM = ta.rma(minusDM, adxPeriod)
|
||||||
|
|
||||||
// Step 4: Calculate the Directional Indicators (DI+ and DI-)
|
// Step 4: Directional Indicators
|
||||||
plusDI = 100 * (smPlusDM / atr)
|
plusDI = 100 * (smPlusDM / atr)
|
||||||
minusDI = 100 * (smMinusDM / atr)
|
minusDI = 100 * (smMinusDM / atr)
|
||||||
|
|
||||||
// Step 5: Compute the Directional Index (DX)
|
// Step 5: Directional Index (DX)
|
||||||
dx = 100 * math.abs(plusDI - minusDI) / (plusDI + minusDI)
|
dx = 100 * math.abs(plusDI - minusDI) / (plusDI + minusDI)
|
||||||
|
|
||||||
// Step 6: Smooth DX to get ADX
|
// Step 6: ADX from smoothed DX values.
|
||||||
adxValue = ta.rma(dx, adxPeriod)
|
adxValue = ta.rma(dx, adxPeriod)
|
||||||
|
|
||||||
|
|
||||||
// === ENTRY CONDITIONS FOR A SHORT POSITION ===
|
// === AGGRESSIVE SIGNAL CONDITIONS ===
|
||||||
|
|
||||||
// Condition 1: Bear Trend Confirmation - Price is below the SMA.
|
// Mandatory Bearish Condition: Price must be below the SMA.
|
||||||
bearTrend = close < smaValue
|
bearTrend = close < smaValue
|
||||||
|
|
||||||
// Condition 2: Bollinger Bands - Price crosses under the upper band.
|
// Aggressive MACD Condition: Instead of a cross event, use a simple condition.
|
||||||
bbCondition = ta.crossunder(close, bbUpper)
|
macdSignalFlag = macdLine < signalLine
|
||||||
|
|
||||||
// Condition 3: MACD - MACD line crosses below the signal line.
|
// Aggressive RSI Condition: RSI above a lower overbought threshold.
|
||||||
macdCondition = ta.crossunder(macdLine, signalLine)
|
rsiSignalFlag = rsiValue > rsiAggThreshold
|
||||||
|
|
||||||
// Condition 4: RSI - Check if RSI is overbought.
|
// Aggressive Bollinger Bands Condition: Price is above the upper band (suggesting an overextended rally).
|
||||||
rsiCondition = rsiValue > rsiOverbought
|
bbSignalFlag = close > bbUpper
|
||||||
|
|
||||||
// Condition 5: Stochastic - %K crosses under the overbought threshold.
|
// Aggressive Stochastic Condition: Look for a crossunder of the aggressive threshold.
|
||||||
stochCondition = ta.crossunder(k, stochOverbought)
|
stochSignalFlag = ta.crossunder(k, stochAggThreshold)
|
||||||
|
|
||||||
// Condition 6: ADX - The market must have a strong trend.
|
// Aggressive ADX Condition: Market shows trend strength above the lower threshold.
|
||||||
adxCondition = adxValue > adxThreshold
|
adxSignalFlag = adxValue > adxAggThreshold
|
||||||
|
|
||||||
// Combine all conditions for a short entry signal.
|
// Count the number of indicator signals that are true.
|
||||||
shortEntryCondition = bearTrend and bbCondition and macdCondition and rsiCondition and stochCondition and adxCondition
|
signalCount = (macdSignalFlag ? 1 : 0) +
|
||||||
|
(rsiSignalFlag ? 1 : 0) +
|
||||||
|
(bbSignalFlag ? 1 : 0) +
|
||||||
|
(stochSignalFlag ? 1 : 0) +
|
||||||
|
(adxSignalFlag ? 1 : 0)
|
||||||
|
|
||||||
if shortEntryCondition
|
// Take a short position if the bear market condition is met and at least 2 indicator signals fire.
|
||||||
|
if bearTrend and (signalCount >= 2)
|
||||||
strategy.entry("Short", strategy.short)
|
strategy.entry("Short", strategy.short)
|
||||||
|
|
||||||
|
|
||||||
// === EXIT CONDITIONS ===
|
// === EXIT CONDITIONS ===
|
||||||
|
// For open short positions, define a percentage‑based stop loss (above entry) and take profit (below entry).
|
||||||
// For open short positions, define a stop loss above and a take profit below the entry price.
|
|
||||||
if strategy.position_size < 0
|
if strategy.position_size < 0
|
||||||
entryPrice = strategy.position_avg_price
|
entryPrice = strategy.position_avg_price
|
||||||
stopPrice = entryPrice * (1 + stopLossPercent / 100)
|
stopPrice = entryPrice * (1 + stopLossPercent / 100)
|
||||||
targetPrice = entryPrice * (1 - takeProfitPercent / 100)
|
targetPrice = entryPrice * (1 - takeProfitPercent / 100)
|
||||||
strategy.exit("Exit Short", from_entry="Short", stop=stopPrice, limit=targetPrice)
|
strategy.exit("Exit Short", from_entry="Short", stop=stopPrice, limit=targetPrice)
|
||||||
|
|
||||||
|
|
||||||
// === PLOTTING ===
|
// === PLOTTING ===
|
||||||
plot(smaValue, color=color.orange, title="SMA")
|
plot(smaValue, color=color.orange, title="SMA")
|
||||||
plot(bbUpper, color=color.blue, title="Bollinger Upper")
|
plot(bbUpper, color=color.blue, title="Bollinger Upper Band")
|
||||||
plot(bbBasis, color=color.gray, title="Bollinger Basis")
|
plot(bbBasis, color=color.gray, title="Bollinger Basis")
|
||||||
plot(bbLower, color=color.blue, title="Bollinger Lower")
|
plot(bbLower, color=color.blue, title="Bollinger Lower Band")
|
||||||
|
|
||||||
// Optional: Plot the manually calculated ADX.
|
|
||||||
plot(adxValue, title="ADX", color=color.fuchsia)
|
plot(adxValue, title="ADX", color=color.fuchsia)
|
||||||
|
|
||||||
|
// Optional: Plot RSI and a horizontal line at the aggressive RSI threshold.
|
||||||
|
plot(rsiValue, title="RSI", color=color.purple)
|
||||||
|
hline(rsiAggThreshold, title="Aggressive RSI Threshold", color=color.red)
|
Loading…
x
Reference in New Issue
Block a user