Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("Lunar Phase Strategy by Symphoenix", overlay=true)
- // Input parameters
- longPhaseThreshold = input(0.94, title="Long Phase Threshold")
- shortPhaseThreshold = input(0.07, title="Short Phase Threshold")
- riskPerTrade = input(0.1, title="Risk Per Trade (as a % of Equity)")
- stopLossPerc = input(0.25, title="Stop Loss Percentage")
- atrLength = input(14, title="ATR Length for Volatility")
- trailPerc = input(0.1, title="Trailing Stop Percentage")
- maxDrawdownPerc = input(0.25, title="Maximum Drawdown Percentage")
- volumeLength = input(21, title="Volume MA Length")
- // Constants for lunar phase calculation and ATR
- atr = ta.atr(atrLength)
- volMA = ta.sma(volume, volumeLength) // Volume moving average
- // Improved Lunar Phase Calculation
- calculateLunarPhase() =>
- moonCycleLength = 29.5305882
- daysSinceKnownFullMoon = (time - timestamp("2019-12-12T05:12:00")) / (24 * 60 * 60 * 1000)
- lunarAge = daysSinceKnownFullMoon % moonCycleLength
- phase = ((1 + math.cos(lunarAge / moonCycleLength * 2 * math.pi)) / 2)
- phase
- // Main strategy logic
- lunarPhase = calculateLunarPhase()
- // Calculate Position Size based on Volatility and Account Equity
- calculatePositionSize() =>
- equity = strategy.equity
- riskAmount = equity * riskPerTrade
- positionSize = riskAmount / atr
- if positionSize > 1000000000000
- positionSize := 1000000000000
- positionSize
- positionSize = calculatePositionSize()
- // Maximum Drawdown Tracking
- var float maxPortfolioValue = na
- maxPortfolioValue := math.max(maxPortfolioValue, strategy.equity)
- drawdown = (maxPortfolioValue - strategy.equity) / maxPortfolioValue
- // Check for maximum drawdown
- if drawdown > maxDrawdownPerc
- strategy.close_all()
- strategy.cancel_all()
- // Volume Analysis
- isVolumeConfirmed = volume > volMA
- // Date Check for Backtesting Period
- isWithinBacktestPeriod = year >= 2023 and year <= 2023
- // Entry and Exit Conditions
- if lunarPhase > longPhaseThreshold and lunarPhase < 0.999 and isVolumeConfirmed and isWithinBacktestPeriod
- if strategy.position_size <= 0 // Close short positions before going long
- strategy.close_all()
- if strategy.position_size < positionSize
- strategy.entry("Long", strategy.long, qty=positionSize)
- strategy.exit("Exit Long", "Long", trail_offset=atr * trailPerc, trail_points=atr)
- if lunarPhase < shortPhaseThreshold and lunarPhase > 0.001 and isVolumeConfirmed and isWithinBacktestPeriod
- if strategy.position_size >= 0 // Close long positions before going short
- strategy.close_all()
- if strategy.position_size > -positionSize
- strategy.entry("Short", strategy.short, qty=positionSize)
- strategy.exit("Exit Short", "Short", trail_offset=atr * trailPerc, trail_points=atr)
- // Implementing Stop-Loss Logic
- longStopLoss = strategy.position_avg_price * (1 - stopLossPerc)
- shortStopLoss = strategy.position_avg_price * (1 + stopLossPerc)
- if strategy.position_size > 0 and close < longStopLoss
- strategy.close("Long")
- if strategy.position_size < 0 and close > shortStopLoss
- strategy.close("Short")
Advertisement
Comments
-
- download all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment