该策略是一种适用于加密货币市场的简单移动平均线(SMA)交叉策略。它利用快速、中速和慢速三组SMA来识别潜在的入市和出场信号。当快速SMA上穿中速SMA时,产生买入信号;当快速SMA下穿中速SMA时,产生卖出信号。
策略允许交易者设置以下关键参数:
根据用户设置的SMA长度,分别计算出快速SMA、中速SMA和慢速SMA。
当快速SMA上穿中速SMA时,产生买入信号;当快速SMA下穿中速SMA时,产生卖出信号。
策略结合账户资金和每笔交易承受的风险比例,计算出每笔交易的名义本金。再结合ATR来计算止损幅度,最终确定每笔交易的具体仓位。
可以通过适当缩短SMA周期、辅助其他指标等方式来优化。
本策略整合了SMA交叉判断、风险管理和仓位优化多项功能,是一种适合加密市场的趋势跟踪策略。交易者可以根据自己的交易风格、市场环境等因素调整参数,实施优化。
/*backtest
start: 2024-01-05 00:00:00
end: 2024-02-04 00:00:00
period: 3h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("Onchain Edge Trend SMA Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// Configuration Parameters
priceSource = input(close, title="Price Source")
includeIncompleteBars = input(true, title="Consider Incomplete Bars")
maForecastMethod = input(defval="flat", options=["flat", "linreg"], title="Moving Average Prediction Method")
linearRegressionLength = input(3, title="Linear Regression Length")
fastMALength = input(7, title="Fast Moving Average Length")
mediumMALength = input(30, title="Medium Moving Average Length")
slowMALength = input(50, title="Slow Moving Average Length")
tradingCapital = input(100000, title="Trading Capital")
tradeRisk = input(1, title="Trade Risk (%)")
// Calculation of Moving Averages
calculateMA(source, period) => sma(source, period)
predictMA(source, forecastLength, regressionLength) =>
maForecastMethod == "flat" ? source : linreg(source, regressionLength, forecastLength)
offset = includeIncompleteBars ? 0 : 1
actualSource = priceSource[offset]
fastMA = calculateMA(actualSource, fastMALength)
mediumMA = calculateMA(actualSource, mediumMALength)
slowMA = calculateMA(actualSource, slowMALength)
// Trading Logic
enterLong = crossover(fastMA, mediumMA)
exitLong = crossunder(fastMA, mediumMA)
// Risk and Position Sizing
riskCapital = tradingCapital * tradeRisk / 100
lossThreshold = atr(14) * 2
tradeSize = riskCapital / lossThreshold
if (enterLong)
strategy.entry("Enter Long", strategy.long, qty=tradeSize)
if (exitLong)
strategy.close("Enter Long")
// Display Moving Averages
plot(fastMA, color=color.blue, linewidth=2, title="Fast Moving Average")
plot(mediumMA, color=color.purple, linewidth=2, title="Medium Moving Average")
plot(slowMA, color=color.red, linewidth=2, title="Slow Moving Average")