
この戦略は,移動平均の交差に基づく量的な取引戦略である.これは,2つの異なる周期の移動平均 ((快線と慢線) を計算することによって,Fast線が下から上へ慢線を横切るときに買い信号を生じ,Fast線が上から下へ慢線を横切るときに売り信号を生じさせる.また,この戦略は,ダイナミックなポジション管理の概念を導入し,アカウントの損益状況に応じて,リスクを管理するために,各取引のポジションサイズを動的に調整する.
移動平均線交差策略 (英語: Moving average line crossing strategy) は,二つの異なる周期の移動平均線の交差信号によって価格トレンドを捕捉し,同時にリスクを制御するために動的ポジション管理ルールを導入する,シンプルで実用的な量化取引策略である.この策略の論理は明確で,実行しやすい,適用範囲は広い.しかし,実用的なアプリケーションでは,頻繁な取引,不安定な市場のパフォーマンス,パラメータの最適化などの潜在的なリスクに注意し,必要に応じて戦略を最適化および改善し,トレンド確認指標を導入し,ポジションルール管理を最適化し,ストップダストのメカニズムやパラメータ自律最適化などに加入する.継続的な最適化と改善により,戦略の安定性と収益性をさらに向上させることを期待する.
/*backtest
start: 2024-06-06 00:00:00
end: 2024-06-13 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © okolienicholas
//@version=5
strategy("Moving Average Crossover Strategy", overlay=true)
// Input parameters
fast_length = input(9, title="Fast MA Length")
slow_length = input(21, title="Slow MA Length")
source = close
account_balance = input(100, title="Account Balance") // Add your account balance here
// Calculate moving averages
fast_ma = ta.sma(source, fast_length)
slow_ma = ta.sma(source, slow_length)
// Plot moving averages
plot(fast_ma, color=color.blue, title="Fast MA")
plot(slow_ma, color=color.red, title="Slow MA")
// Generate buy/sell signals
buy_signal = ta.crossover(fast_ma, slow_ma)
sell_signal = ta.crossunder(fast_ma, slow_ma)
// Plot buy/sell signals
plotshape(buy_signal, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, title="Buy Signal")
plotshape(sell_signal, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, title="Sell Signal")
// Calculate the risk per trade
risk_per_trade = account_balance * 0.01
// Calculate the number of shares to buy
shares_to_buy = risk_per_trade / (high - low)
// Calculate the profit or loss
profit_or_loss = strategy.netprofit
// Adjust the position size based on the profit or loss
if (profit_or_loss > 0)
shares_to_buy = shares_to_buy * 1.1 // Increase the position size by 10% when in profit
else
shares_to_buy = shares_to_buy * 0.9 // Decrease the position size by 10% when in loss
// Execute orders
if (buy_signal)
strategy.entry("Buy", strategy.long, qty=shares_to_buy)
if (sell_signal)
strategy.entry("Sell", strategy.short, qty=shares_to_buy)