本策略基于月相周期作为交易信号,结合RSI、MACD、OBV等多个指标来识别比特币等数字货币的交易机会。该策略主要优势是利用月相阶段这一外部因素作为交易启发信号,与大多数只依赖技术指标的策略不同,可以在一定程度上避免被市场操纵。
该策略的核心逻辑是根据月相周期的不同阶段来判断是否符合做多或做空的条件。月相计算公式为:
月相周期长度=29.5305882天 已知某次满月时间,可以计算从该满月开始到当前时间的天数 月相年龄=距离已知满月的天数%月相周期长度 月相值=(1 + cos(月相年龄/月相周期长度*2*π))/2
根据月相值的大小可以判断当前是什么月相。月相在0到1之间变化,值越大表示离满月越近,值越小表示离新月越近。
策略根据月相阈值判断是否符合做多或做空条件。如果月相值大于做多阈值(默认0.51),则有机会做多;如果月相值小于做空阈值(默认0.49),则有机会做空。
此外,策略还结合交易量、RSI、MACD等指标来避免在非理想情况下发出交易信号。只有在交易量放大、RSI和MACD符合条件时,才会打开仓位。
该策略主要有以下几个优势:
总的来说,该策略充分利用月相的独特优势,并辅以多种技术指标来识别高概率交易机会,通过风险控制手段有效控制交易风险。
该策略主要存在以下风险:
为控制这些风险,可以采取以下措施:
通过参数优化和综合指标应用,可以在很大程度上规避交易风险。
该策略仍有进一步优化的空间:
本策略通过月相独特的交易信号,配合主流技术指标,实现了高效的比特币交易。相比单一指标策略,本策略可以更好抵御市场操纵风险,具有独特优势。通过止损预防风险和优化参数,可以稳定获取较好收益。该策略仍可进一步提升,具有很大的应用前景。
/*backtest
start: 2023-01-08 00:00:00
end: 2024-01-14 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Lunar Phase Strategy by Symphoenix", overlay=true)
// Input parameters
start_year = input(2023, title="Start year")
end_year = input(2023, title="End year")
longPhaseThreshold = input(0.51, title="Long Phase Threshold")
shortPhaseThreshold = input(0.49, title="Short Phase Threshold")
riskPerTrade = input(0.05, title="Risk Per Trade (as a % of Equity)")
stopLossPerc = input(0.01, title="Stop Loss Percentage")
atrLength = input(21, title="ATR Length for Volatility")
trailPerc = input(0.1, title="Trailing Stop Percentage")
maxDrawdownPerc = input(0.1, title="Maximum Drawdown Percentage")
volumeLength = input(7, 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
lunarPhase = calculateLunarPhase()
// Advanced Volume Analysis
priceChange = ta.change(close)
obv = ta.cum(priceChange > 0 ? volume : priceChange < 0 ? -volume : 0)
// Additional Technical Indicators
rsi = ta.rsi(close, 14)
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
// 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 >= start_year and year <= end_year
// Entry and Exit Conditions
// Adjusted Entry and Exit Conditions
longCondition = lunarPhase > longPhaseThreshold and lunarPhase < 0.999 and isVolumeConfirmed and obv > obv[1] and rsi < 70 and macdLine > signalLine and isWithinBacktestPeriod
shortCondition = lunarPhase < shortPhaseThreshold and lunarPhase > 0.001 and isVolumeConfirmed and obv < obv[1] and rsi > 30 and macdLine < signalLine and isWithinBacktestPeriod
if longCondition
if strategy.position_size < 0
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 shortCondition
if strategy.position_size > 0
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")