黄金交叉策略是一种简单的市场指标,可以帮助长期投资者确定入场时机。该策略基于短期和长期移动平均线的交叉来产生交易信号。当短期移动平均线上穿长期移动平均线时,形成黄金交叉,表示市场进入牛市,可以做多;当短期移动平均线下穿长期移动平均线时,形成死亡交叉,表示市场进入熊市,应平仓。
该策略使用sma函数计算短期和长期的简单移动平均线。短期移动平均线长度设置为50天,长期移动平均线长度设置为200天。策略通过crossover和crossunder函数判断短期线是否上穿或下穿长期线,以产生交易信号。
当短期线上穿长期线时,表明市场趋势由下跌转为上涨,产生黄金交叉,这是做多的信号。策略会通过strategy.entry函数开仓做多。当短期线下穿长期线时,表明市场趋势由上涨转为下跌,形成死亡交叉,这是平仓的信号。策略会通过strategy.close_all函数平掉所有头寸。
通过抓住市场趋势转折点的黄金/死亡交叉来确定入场时机和平仓时机,可以有效过滤市场 noise,是一个简单实用的趋势跟踪策略。
可以设置止损来控制风险,优化移动平均线参数来减少假信号,结合其他指标来确认信号可靠性,开发突发事件处理机制来应对重大新闻。
该策略可以从以下几个方面进行优化:
优化移动平均线参数,调整短期和长期均线的长度,使其更好地匹配不同市场的特点;
增加成交量的条件判断,只在成交量放大的情况下才产生信号;
结合其它指标,如MACD、RSI等来确认黄金/死亡交叉信号,避免假信号;
增加止损策略,如跟踪止损、比例止损等,控制单笔亏损;
增加仓位管理策略,如固定仓位、指数增仓等,控制整体风险;
优化入场时机,当交叉发生后再观察一段时间,过滤假交叉。
通过以上优化,可以使策略参数更符合市场统计特性,过滤假信号,控制风险,在保持简单的同时,进一步提高策略的稳定性和盈利能力。
黄金交叉策略是一个既简单又实用的趋势跟踪策略。它直观地通过移动平均线交叉来捕捉市场趋势,可以有效为长线投资者识别入场和退出的时机。该策略容易实现,适合初学者学习,也可以进行多方面的拓展与优化,使其成为一个灵活可靠的量化交易策略。总体来说,黄金交叉策略结合简洁性和实用性,是量化交易体系中很有价值的一员。
/*backtest
start: 2023-08-27 00:00:00
end: 2023-09-26 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
strategy("Dumb strategy 2 - Golden Cross", shorttitle="Golden Cross", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
lShort = input(50, title="short length")
lLong = input(200, title="long length")
src = input(close, title="Source")
smaShort = sma(src, lShort)
smaLong = sma(src, lLong)
plot(smaShort, title="SMA Short", style=line, linewidth=3, color=lime)
plot(smaLong, title="SMA Long", style=line, linewidth=3, color=red)
//
//Backtest Time Inputs
//
testStartYear = input(2009, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
testStopYear = input(2019, "Backtest Stop Year")
testStopMonth = input(1, "Backtest Stop Month")
testStopDay = input(01, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
testPeriodBackground = input(title="Color Background?", type=bool, defval=true)
testPeriodBackgroundColor = testPeriodBackground and (time >= testPeriodStart) and (time <= testPeriodStop) ? blue : na
bgcolor(testPeriodBackgroundColor, transp=80)
testPeriod() => true
if testPeriod()
longCondition = crossover(smaShort, smaLong)
if (longCondition)
strategy.entry("Long Entry", strategy.long)
shortCondition = crossunder(smaShort, smaLong)
if (shortCondition)
strategy.close_all(true)