
이 전략은 다양한 주기의 MA 이동 평균을 계산하여 시장의 경향 방향을 판단하고, 추세가 상승할 때 더 많이 하고, 추세가 하락할 때 공백을 하고, 추세 추적을 구현한다.
이 전략은 전체적인 아이디어가 명확하며 MA를 사용하여 트렌드를 판단하는 것은 매우 고전적이며, 매개 변수 최적화 및 지표 최적화 후 매우 실용적인 트렌드 추적 전략이 될 수 있다.
/*backtest
start: 2023-02-15 00:00:00
end: 2024-02-21 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("MA60上多下空", overlay=true)
// 计算MA20/60/120
ma20 = ta.sma(close, 20)
ma60 = ta.sma(close, 60)
ma120 = ta.sma(close, 120)
// 判断MA的趋势
maUpTrend = ma20 > ma60 and ma60 > ma120
maDownTrend = ma20 < ma60 and ma60 < ma120
// 画竖直线标记MA趋势转折点
plotshape(maUpTrend and ta.crossover(ma20, ma60), style=shape.triangledown, location=location.abovebar, color=color.green, size=size.small)
plotshape(maDownTrend and ta.crossunder(ma20, ma60), style=shape.triangleup, location=location.belowbar, color=color.red, size=size.small)
// 画背景标记MA趋势
bgcolor(maUpTrend ? color.new(color.green, 90) : na)
bgcolor(maDownTrend ? color.new(color.red, 90) : na)
// 建立多头仓位的条件
longCondition = ta.crossover(close, ma60)
// 建立空头仓位的条件
shortCondition = ta.crossunder(close, ma60)
// 在穿过MA60时,根据条件建立相应的多头或空头仓位
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
// 止盈止损规则
calculateReturns() =>
close / strategy.position_avg_price - 1
takeProfitCondition = calculateReturns() >= 3 // 仓位盈利达到300%
stopLossCondition = calculateReturns() <= -0.1 // 仓位亏损达到10%
if (takeProfitCondition)
strategy.close("Long", comment="Take Profit")
strategy.close("Short", comment="Take Profit")
if (stopLossCondition)
strategy.close("Long", comment="Stop Loss")
strategy.close("Short", comment="Stop Loss")