
双均線交差策略は,典型的なトレンドを追跡する取引策である。この策略は,2つの移動平均を用いており,一つは急速な移動平均,もう一つは遅い移動平均である。急速な移動平均が上下からゆっくりな移動平均を横切るときは”,黄金交差”と呼ばれ,上昇傾向が形成される可能性を示し,その時にポジションを多く開く。急速な移動平均が上下からゆっくりな移動平均を横切るときは”,死交差”と呼ばれ,下降傾向が形成される可能性を示し,その時にポジションを空にする。この策略のコードは,シンプルな移動平均 ((SMA) 指数と数値移動平均 ((EMA) を使用することをサポートし,ストップロスを設定することができます。
この戦略の核心は,移動平均のトレンド特性と交差信号を利用してトレンドの方向と開店のタイミングを判断することである.まずは,パラメータを設定して,高速移動平均 ((デフォルト50) と遅い移動平均 ((デフォルト200) の周期,およびSMAまたはEMAを使用する選択を行うこと.それから,二つの移動平均を計算して,それらの交差を判断する.
双均線交差策略は,2つの異なる周期の移動平均の交差によってトレンド方向と開拓タイミングを判断する簡単な古典的なトレンド追跡策略であり,中長期のトレンドを把握するのに適しています.しかし,固定パラメータは,変化する市場環境で不安定なパフォーマンスを発揮し,さらなる最適化が必要になります.
/*backtest
start: 2023-05-11 00:00:00
end: 2024-05-16 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
//==============================================================================
// A baseline strategy with a well known concept, golden cross & death cross.
// Support for both Simple & Exponential moving averages.
// Support for long & short stop losses as a percentage.:well
//==============================================================================
strategy("Basic Moving Average Crosses", overlay=true)
//------------------------------------------------------------------------------
// configuration
//------------------------------------------------------------------------------
maQuickLength = input(50, title="Quick MA Length")
maSlowLength = input(200, title="Quick MA Length")
useSma = input(true, title="Use SMA? If false, EMA is used.")
maQuick = useSma ? ta.sma(close, maQuickLength) : ta.ema(close, maQuickLength)
maSlow = useSma ? ta.sma(close, maSlowLength) : ta.ema(close, maSlowLength)
stop_loss_percentage = input(2.0, title="Stop Loss (%)")
var float longStopLevel = na
var float shortStopLevel = na
bool isGoldenCross = ta.crossover(maQuick, maSlow)
bool isDeathCross = ta.crossunder(maQuick, maSlow)
//------------------------------------------------------------------------------
// position opening logic
//------------------------------------------------------------------------------
if(strategy.position_size == 0)
// Golden cross, enter a long position
if(isGoldenCross)
strategy.entry("Buy", strategy.long)
longStopLevel := close - close * stop_loss_percentage/100.0
strategy.exit("StopLossLong", "Buy", stop=longStopLevel)
// Death cross, enter short position
else if(isDeathCross)
strategy.entry("Sell", strategy.short)
shortStopLevel := close + close * stop_loss_percentage/100.0
strategy.exit("StopLossShort", "Sell", stop=shortStopLevel)
//------------------------------------------------------------------------------
// position closing logic
//------------------------------------------------------------------------------
else
// Close long position on death cross
if(strategy.position_size > 0 and isDeathCross)
strategy.close("Buy")
// Close short position on golden cross
else if(strategy.position_size < 0 and isGoldenCross)
strategy.close("Sell")
//------------------------------------------------------------------------------
// ploting
//------------------------------------------------------------------------------
plot(maQuick, color=color.yellow)
plot(maSlow, color=color.blue)