移動平均差の戦略

作者: リン・ハーンチャオチャン,日付: 2024-01-24 11:43:41
タグ:

img

概要

この戦略は,価格と移動平均線との間の差異を検出し,それらを購入・販売信号として使用します.これは差異を見つけるために任意の振動器に適用できます.これはバックテストおよびライブ取引に使用できる貴重なツールです.

戦略の論理

  1. 長さの移動平均 (MA) を計算する Len
  2. MAのピボット低点 (PL) とピボット高点 (PH) を検出する
  3. 上昇差の確認:価格が新低に達するが,MAは達成しないか,MAが新低に達するが,価格は達成しない.
  4. 低迷差の確認:価格が新高を上げてもMAは上がらないか,またはMAが新高を上げても価格が上がらないか
  5. 差異に基づいて購入・販売

利点分析

  1. 価格とMAの差異を自動的に検出し,手動判断の誤りを避ける
  2. どんなオシレーターにも適用可能で 拡張性が高い
  3. バックテストと収益性の検証に使用できます
  4. 敏感性を調整し,偽信号を避けるための設定可能なパラメータ
  5. 正確で包括的な判断のために複数の差異型を提供

リスク分析

  1. 不正なオシレーター設定が過剰な誤った信号を生成する可能性があります.
  2. 差が起こる前に必要な有効なピボットポイント,信号は不十分かもしれない
  3. センシビリティをバランスさせ 偽信号をフィルタリングするためにパラメータを調整する必要があります
  4. 他の要因と組み合わせるとうまく作用し,単独で使用すると比較的低い信頼度

オプティマイゼーションの方向性

  1. 最適なパラメータ組み合わせを見つけるために MA パラメータを最適化
  2. 誤った信号を避けるために音量などの他の指標と組み合わせる
  3. 差異の信頼性を判断するための機械学習モデルを追加する
  4. リスク管理メカニズムを追加して,各取引損失を制御する

概要

この戦略は,主観的な誤りを避けるために価格とMAの間の差異を自動判断のための取引信号として使用する. 拡張性が強い任意の振動器に広く適用できる. 信号の信頼性とシステム安定性を著しく改善するために,パラメータの最適化と他の指標との使用を必要とする.


/*backtest
start: 2023-12-24 00:00:00
end: 2024-01-12 00:00:00
period: 1h
basePeriod: 15m
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/
// © tista 
//https://www.tradingview.com/u/tista/#published-scripts

//@version=4

strategy(title="MA Divergences", format=format.price)

//* Backtesting Period Selector | Component *//
//* https://www.tradingview.com/script/eCC1cvxQ-Backtesting-Period-Selector-Component *//
//* https://www.tradingview.com/u/pbergden/ *//
//* Modifications made *//
testStartYear = input(2021, "Backtest Start Year") 
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

testStopYear = input(999999, "Backtest Stop Year")
testStopMonth = input(9, "Backtest Stop Month")
testStopDay = input(26, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)

testPeriod() => true
/////////////// END - Backtesting Period Selector | Component ///////////////
len = input(title="MA Period", minval=1, defval=14)
src = input(title="MA Source", defval=close)
lbR = input(title="Pivot Lookback Right", defval=5)
lbL = input(title="Pivot Lookback Left", defval=5)
rangeUpper = input(title="Max of Lookback Range", defval=600)
rangeLower = input(title="Min of Lookback Range", defval=2)
plotBull = input(title="Plot Bullish", defval=true)
plotHiddenBull = input(title="Plot Hidden Bullish", defval=true)
plotBear = input(title="Plot Bearish", defval=true)
plotHiddenBear = input(title="Plot Hidden Bearish", defval=true)

bearColor = color.red
bullColor = color.green
hiddenBullColor = color.green
hiddenBearColor = color.red
textColor = color.white
noneColor = color.new(color.white, 100)

osc = wma(src, len)

plot(osc, title="MA", linewidth=2, color=color.yellow)

plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
phFound = na(pivothigh(osc, lbL, lbR)) ? false : true

_inRange(cond) =>
    bars = barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper

alertcondition(osc[1] > 100.0 and osc[2] < 100.0, title="MA value crosses over 100.0", message="Check charts for a MA cross over 100.0")
alertcondition(osc[1] < 100.0 and osc[2] > 100.0, title="MA value crosses under 100.0", message="Check charts for a MA cross under 100.0")
alertcondition(osc[1] > -100. and osc[2] < -100.0, title="MA value crosses over -100.0", message="Check charts for a MA cross over -100.0")
alertcondition(osc[1] < -100.0 and osc[2] > -100.0, title="MA value crosses under -100.0", message="Check charts for a MA cross under -100.0")

//------------------------------------------------------------------------------
// Regular Bullish

// Osc: Higher Low
oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Lower Low
priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)

bullCond = plotBull and priceLL and oscHL and plFound

plot(
	 plFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish",
	 linewidth=2,
	 color=(bullCond ? bullColor : noneColor),
	 transp=0
	 )

plotshape(
	 bullCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish Label",
	 text=" Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor,
	 transp=0
	 )

alertcondition(bullCond, title="Regular bullish divergence in MA found", message="Check charts for a regular bullish divergence found with MA")

//------------------------------------------------------------------------------
// Hidden Bullish

// Osc: Lower Low
oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Higher Low
priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)

hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound

plot(
	 plFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish",
	 linewidth=2,
	 color=(hiddenBullCond ? hiddenBullColor : noneColor),
	 transp=0
	 )

plotshape(
	 hiddenBullCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish Label",
	 text=" H Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor,
	 transp=0
	 )

alertcondition(hiddenBullCond, title="Hidden bullish divergence in MA found", message="Check charts for a hidden bullish divergence found with MA")

//------------------------------------------------------------------------------
// Regular Bearish

// Osc: Lower High
oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Higher High
priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)

bearCond = plotBear and priceHH and oscLH and phFound

plot(
	 phFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish",
	 linewidth=2,
	 color=(bearCond ? bearColor : noneColor),
	 transp=0
	 )

plotshape(
	 bearCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish Label",
	 text=" Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor,
	 transp=0
	 )

alertcondition(bearCond, title="Regular bearish divergence in MA found", message="Check charts for a regular bearish divergence found with MA")

//------------------------------------------------------------------------------
// Hidden Bearish

// Osc: Higher High
oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Lower High
priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)

hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound

plot(
	 phFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish",
	 linewidth=2,
	 color=(hiddenBearCond ? hiddenBearColor : noneColor),
	 transp=0
	 )

plotshape(
	 hiddenBearCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish Label",
	 text=" H Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor,
	 transp=0
	 )

// Alerts
//alertcondition(bearCond or hiddenBearCond, title='Bear div', message='Bear div')
//alertcondition(bullCond or hiddenBullCond, title='Bull div', message='Bull div')
//alertcondition(bearCond or bullCond, title='Bull or beal div', message='Bull or bear div') 
//alertcondition(hiddenBearCond or hiddenBullCond, title='Bull or beal div', message='Hidden Bull or bear div') 
//alertcondition(hiddenBearCond or hiddenBullCond or bearCond or bullCond, title='Bull or beal div', message='Any Bull or bear div') 

if testPeriod()
    if bullCond or hiddenBullCond
        strategy.entry("Buy", strategy.long)
    if bearCond or hiddenBearCond
        strategy.entry("Sell", strategy.short)

もっと