Symphoenix

SyMoon

Dec 24th, 2023
357
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | Money | 0 0
  1. //@version=5
  2. strategy("Lunar Phase Strategy by Symphoenix", overlay=true)
  3.  
  4. // Input parameters
  5. longPhaseThreshold = input(0.94, title="Long Phase Threshold")
  6. shortPhaseThreshold = input(0.07, title="Short Phase Threshold")
  7. riskPerTrade = input(0.1, title="Risk Per Trade (as a % of Equity)")
  8. stopLossPerc = input(0.25, title="Stop Loss Percentage")
  9. atrLength = input(14, title="ATR Length for Volatility")
  10. trailPerc = input(0.1, title="Trailing Stop Percentage")
  11. maxDrawdownPerc = input(0.25, title="Maximum Drawdown Percentage")
  12. volumeLength = input(21, title="Volume MA Length")
  13.  
  14. // Constants for lunar phase calculation and ATR
  15. atr = ta.atr(atrLength)
  16. volMA = ta.sma(volume, volumeLength) // Volume moving average
  17.  
  18. // Improved Lunar Phase Calculation
  19. calculateLunarPhase() =>
  20. moonCycleLength = 29.5305882
  21. daysSinceKnownFullMoon = (time - timestamp("2019-12-12T05:12:00")) / (24 * 60 * 60 * 1000)
  22. lunarAge = daysSinceKnownFullMoon % moonCycleLength
  23. phase = ((1 + math.cos(lunarAge / moonCycleLength * 2 * math.pi)) / 2)
  24. phase
  25.  
  26. // Main strategy logic
  27. lunarPhase = calculateLunarPhase()
  28.  
  29. // Calculate Position Size based on Volatility and Account Equity
  30. calculatePositionSize() =>
  31. equity = strategy.equity
  32. riskAmount = equity * riskPerTrade
  33. positionSize = riskAmount / atr
  34. if positionSize > 1000000000000
  35. positionSize := 1000000000000
  36. positionSize
  37.  
  38. positionSize = calculatePositionSize()
  39.  
  40. // Maximum Drawdown Tracking
  41. var float maxPortfolioValue = na
  42. maxPortfolioValue := math.max(maxPortfolioValue, strategy.equity)
  43. drawdown = (maxPortfolioValue - strategy.equity) / maxPortfolioValue
  44.  
  45. // Check for maximum drawdown
  46. if drawdown > maxDrawdownPerc
  47. strategy.close_all()
  48. strategy.cancel_all()
  49.  
  50. // Volume Analysis
  51. isVolumeConfirmed = volume > volMA
  52.  
  53. // Date Check for Backtesting Period
  54. isWithinBacktestPeriod = year >= 2023 and year <= 2023
  55.  
  56. // Entry and Exit Conditions
  57. if lunarPhase > longPhaseThreshold and lunarPhase < 0.999 and isVolumeConfirmed and isWithinBacktestPeriod
  58. if strategy.position_size <= 0 // Close short positions before going long
  59. strategy.close_all()
  60. if strategy.position_size < positionSize
  61. strategy.entry("Long", strategy.long, qty=positionSize)
  62. strategy.exit("Exit Long", "Long", trail_offset=atr * trailPerc, trail_points=atr)
  63.  
  64. if lunarPhase < shortPhaseThreshold and lunarPhase > 0.001 and isVolumeConfirmed and isWithinBacktestPeriod
  65. if strategy.position_size >= 0 // Close long positions before going short
  66. strategy.close_all()
  67. if strategy.position_size > -positionSize
  68. strategy.entry("Short", strategy.short, qty=positionSize)
  69. strategy.exit("Exit Short", "Short", trail_offset=atr * trailPerc, trail_points=atr)
  70.  
  71. // Implementing Stop-Loss Logic
  72. longStopLoss = strategy.position_avg_price * (1 - stopLossPerc)
  73. shortStopLoss = strategy.position_avg_price * (1 + stopLossPerc)
  74.  
  75. if strategy.position_size > 0 and close < longStopLoss
  76. strategy.close("Long")
  77.  
  78. if strategy.position_size < 0 and close > shortStopLoss
  79. strategy.close("Short")
  80.  
Advertisement
Comments
  • # text 0.12 KB | 0 0
    1. 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