均線交差戦略


作成日: 2023-10-10 10:44:25 最終変更日: 2023-10-10 10:44:25
コピー: 0 クリック数: 751
1
フォロー
1617
フォロワー

概要

均線交差策略は,移動均線を基にした一般的な取引策である.この策略は,高速移動均線と遅い移動均線の交差を買入と売却の信号として使用する.高速均線が,下から遅い平均線を横切るときは,買入の信号とみなされ,高速均線が,上から下から遅い平均線を横切るときは,売却の信号とみなされる.この策略は,50日平均線を高速平均線として使用し,日平均線を遅い平均線として使用する.

戦略原則

この戦略の核心的な論理は均線理論に基づいている。移動均線は価格変動を効率的に平らげ,価格トレンドを反映する。急速均線は価格変化により敏感で,トレンドの転換点を捉える;ゆっくり均線は価格変化にあまり敏感ではなく,短期的な変動をフィルターする。急速均線の上を通過すると,短期的な価格が上昇し,多頭トレンドに入る;急速均線の下を通過すると,短期的な価格が低下し,空頭トレンドに入る.

具体的には,この戦略は,まず50日平均線と200日平均線を定義する.それから,多頭入場条件を快速平均線の上をゆっくり平均線に,空頭入場条件を快速平均線下をゆっくり平均線に設定する.重複取引を避けるために,戦略は,isEntryとisExitの標識位を使用して制御する.入場条件が満たされたときに,isEntryをTrueに設定し,出場条件が満たされたときに,isExitをTrueに設定する.isEntryがfalseであり,買入シグナルが発生した場合にのみ,多頭開場が行われ,isExitがfalseであり,販売シグナルが発生した場合にのみ,空頭開場が行われる.

また,ストップ・ストラップ・ポイントが設定されています. ユーザーは,ストップ・ストラップの距離を,パーセントを入力することで設定できます. ストップ・ストラップとストップ・プリースは,入場価格のパーセントの変化に応じて計算されます. 持仓量が0以上である場合,設定されたストップ・ストラップとストップ・プリースのパーセントに基づいてストップ・プリースを実行します. 持仓量が0未満である場合,設定された空頭ストップ・プリースのパーセントに基づいてストップ・プリースを実行します.

優位分析

この戦略の利点は以下の通りです.

  1. 操作がシンプルで実現しやすい. 均線交差のみで取引が可能で,取引経験のない初心者にとって非常に適しています.

  2. 撤回は制御可能で,ある種のリスク管理機構がある.移動均線は,短期的な価格変動を効果的にフィルターし,ブレーキを避ける.

  3. カスタマイズ可能なパラメータ,適応性強. ユーザーは平均線パラメータとストップ・ストップ・損失基準を自分で設定し,戦略を最適化するパラメータを設定できます.

  4. 戦略はグラフに直接,重要な平均線,入場点,ストップ・ストップ・ストープの点を描きます.

  5. 拡張性強 戦略の枠組みは完備しており,重要な取引シグナルの変更,指標の追加などで戦略を改良することができます.

リスク分析

この戦略にはいくつかのリスクがあります.

  1. 市場の突発は,巨大なトレードオフを引き起こし,損益を止めることはできません.急速平均線は価格の変化に敏感であり,突発に効果的に対応することはできません.

  2. 長期にわたる変動は,損失を繰り返す.

  3. 取引コストは考慮されていない.実際の取引における手数料と滑り場損失は,利益に深刻な影響を及ぼしている.

  4. 復元データ適合リスク。実況状況は複雑で変動し,復元結果は実戦パフォーマンスを表さない。

対応方法:

  1. より緩やかな止損基準が設定され,追加の突破止損が追加されることもできます.

  2. 平均線距離を適当に拡大し,取引頻度を低下させ,または他の指標でフィルター信号を変更する.

  3. 実際の取引コストを考慮して,より寛大な停止スペースを設定してください.

  4. 市場環境の変化を十分に考慮し,パラメータを最適化し,徐々に適合を減らす.

最適化の方向

この戦略は以下の点で最適化できます.

  1. 異なるパラメータの組み合わせをテストし,最適のパラメータを探します. ゆっくりと平均する日数,パラメータの組み合わせなどをテストできます.

  2. 他の指標にフィルタリングを加え,震動の際に誤取引を避ける.例えばMACD,KDなどの指標.

  3. ストップ・ストップ・ストップ戦略を最適化し,より効率的なリスク管理を実現する.例えば,ストップを追跡し,ストップを掛けるなどである.

  4. 持仓の規模を拡大し,レバレッジを活用して拡大し,利益の余地を増やす。しかし,リスクを制御する。

  5. 实体取引コストを考慮し,反測パラメータを調整して最適化し,戦略パラメータを実戦に適したものにする.

  6. 統計的手法と組み合わせたパラメータの安定性を評価し,データ適合のリスクを軽減し,安定性を向上させる.

要約する

概して,この均線交差策の整体構想は明確で,実現しやすい.量化取引の入門策として適している.しかし,この策には一定のリスクと欠陥がある.パラメータとフィルタを細かく最適化し,実盤取引リスクを制御することに注意し,安定した収益を得る必要がある.この策の枠組みは,強力な拡張性を持つ.ユーザーは,この基礎で革新と最適化を行い,自分のスタイルに適した取引戦略を開発することができる.

ストラテジーソースコード
/*backtest
start: 2023-10-02 00:00:00
end: 2023-10-09 00:00:00
period: 3m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gjfsdrtytru

//@version=4
strategy("Backtest Engine", "Backtest", overlay=true, commission_type=strategy.commission.percent, commission_value=0.07, initial_capital = 1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, currency = currency.USD)


// Start code here...
fastMA = sma(close,50)
slowMA = sma(close,200)

plot(fastMA, "Fast MA",  color.blue)
plot(slowMA, "Slow MA",  color.red)

// Long Enrty/Exit
longCondition = crossover(fastMA,slowMA)
closeLong = crossover(slowMA,fastMA)

// Short Enrty/Exit
shortCondition = crossover(slowMA,fastMA)
closeShort = crossover(fastMA,slowMA)


// Bot web-link alert - {{strategy.order.comment}}
botLONG = "ENTRY LONG ALERT"
botCLOSELONG = "CLOSE LONG ALERT"
botSHORT = "ENTRY SHORT ALERT"
botCLOSESHORT = "CLOSE SHORT ALERT"

//////////////////////////////////////////////////////////////////
//////////////////////// BACKTEST ENGINE \\\\\\\\\\\\\\\\\\\\\\\\\
/////////////////// [NO USER INPUT REQUIRED] /////////////////////
//////////////////////////////////////////////////////////////////

// Time period
testStartYear = input(2020, "Backtest Start Year")
testStartMonth = input(5, "Backtest Start Month")
testStartDay = input(11, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

periodLength = input(3650, "Backtest Period (days)", minval=0,tooltip="Days until strategy ends") * 86400000 // convert days into UNIX time
testPeriodStop = testPeriodStart + periodLength

testPeriod() =>
    time >= testPeriodStart and time <= testPeriodStop ? true : false

// Convert Take profit and Stop loss to percentage
longTP = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=0) * 0.01 // Set levels with input options
longSL = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=0) * 0.01 // Set levels with input options
shortTP = input(title="Short Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=0) * 0.01 // Set levels with input options
shortSL = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=0) * 0.01 // Set levels with input options

// 0% TP/SL = OFF (a value of 0 turns off TP/SL feature)
longProfitPerc = longTP == 0 ? 1000 : longTP
longStopPerc = longSL == 0 ? 1 : longSL
shortProfitPerc = shortTP == 0 ? 1 : shortTP
shortStopPerc = shortSL == 0 ? 1000 : shortSL

// Determine TP/SL price based on percentage given
longProfitPrice  = strategy.position_avg_price * (1 + longProfitPerc)
longStopPrice  = strategy.position_avg_price * (1 - longStopPerc)
shortProfitPrice  = strategy.position_avg_price * (1 - shortProfitPerc)
shortStopPrice  = strategy.position_avg_price * (1 + shortStopPerc)

// Anti-overlap
isEntry_Long = false
isEntry_Long := nz(isEntry_Long[1], false)
isExit_Long = false
isExit_Long := nz(isExit_Long[1], false)
isEntry_Short = false
isEntry_Short := nz(isEntry_Short[1], false)
isExit_Short = false
isExit_Short := nz(isExit_Short[1], false)

entryLong = not isEntry_Long and longCondition
exitLong = not isExit_Long and closeLong
entryShort = not isEntry_Short and  shortCondition
exitShort = not isExit_Short and closeShort

if (entryLong)
    isEntry_Long := true
    isExit_Long := false
if (exitLong)
    isEntry_Long := false
    isExit_Long := true
if (entryShort)
    isEntry_Short := true
    isExit_Short := false
if (exitShort)
    isEntry_Short := false
    isExit_Short := true

// Order Execution
if testPeriod() 
    if entryLong
        strategy.entry(id="Long", long=true, when = entryLong, comment=botLONG) // {{strategy.order.comment}}
    if entryShort
        strategy.entry(id="Short", long=false, when = entryShort, comment=botSHORT) // {{strategy.order.comment}}


// TP/SL Execution
if (strategy.position_size > 0)
    strategy.exit(id="Long SL/TP", from_entry="Long", limit=longProfitPrice, stop=longStopPrice)
    strategy.close(id="Long", when=exitLong, comment=botCLOSELONG) // {{strategy.order.comment}}

if (strategy.position_size < 0)
    strategy.exit(id="Short TP/SL", from_entry="Short", limit=shortProfitPrice, stop=shortStopPrice)
    strategy.close(id="Short", when=exitShort, comment=botCLOSESHORT) // {{strategy.order.comment}}
    
// Draw Entry, TP and SL Levels for Long Positions
plot(strategy.position_size > 0 ? longTP == 0 ? na : longProfitPrice : na, style=plot.style_linebr, color=color.green, title="Long TP")
plot(strategy.position_size > 0 ? strategy.position_avg_price : na, style=plot.style_linebr, color=color.blue, title="Long Entry")
plot(strategy.position_size > 0 ? longSL == 0 ? na : longStopPrice : na, style=plot.style_linebr, color=color.red, title="Long SL")
// Draw Entry, TP and SL Levels for Short Positions
plot(strategy.position_size < 0 ? shortTP == 0 ? na : shortProfitPrice : na, style=plot.style_linebr, color=color.green, title="Short TP")
plot(strategy.position_size < 0 ? strategy.position_avg_price : na, style=plot.style_linebr, color=color.blue, title="Short Entry")
plot(strategy.position_size < 0 ? shortSL == 0 ? na : shortStopPrice : na, style=plot.style_linebr, color=color.red, title="Short SL")