
この戦略は,直角図の破裂原理を利用し,移動平均のトレンド判断と組み合わせて,トレンド方向の突破取引を実現する.価格が直角図の境界を突破すると取引シグナルが生じる.同時に,速いと遅い移動平均の位置関係を判断して,全体的なトレンド方向を決定し,整盤中に誤ったシグナルを生じさせないようにする.
急速移動平均 ((20周期) と遅い移動平均 ((50周期) を計算する.
K線に基づいて,上を向く長方形 ((close>open) または下を向く長方形 ((close
長方形が前K線の最高値または最低値を突破したかどうかを判断する. 上昇長方形が前K線の最高値を突破した場合は,多頭突破信号を生成する. 下降長方形が前K線の最低値を突破した場合は,空飛ぶ突破信号を生成する.
速動平均線が遅動平均線上にあるかどうかを判断し,もしそうなら多頭トレンドと判断し,逆に空頭トレンドと判断する.
速慢平均線が多頭トレンドと判断された場合にのみ,多頭突破信号は有効である.速慢平均線が空頭トレンドと判断された場合にのみ,空頭突破信号は有効である.これは,整理時に誤った信号を生じさせることを避ける。
有効な多頭突破信号が生成されたとき,一定の止損と止止の基準で多頭開く. 有効な空頭突破信号が生成されたとき,一定の止損と止止の基準で空頭開く.
急速移動平均と遅い移動平均の逆転が起こると,現在のポジションを平らにする.
直角図の境界を突破口として使用し,これはより強い突破信号を表す.
また,トレンドの方向を考慮し,計算に誤った信号を発生させないようにし,精度を向上させる.
トレンドとブレイクを考慮して,トレンドの状況で戦略をうまく実行する.
パラメータの最適化により,異なる品種と時間周期に対応できる.
突破失敗の危険. 解決策は,より大きな突破口を選び,突破動力がより強くなるようにすることです.
トレンド判断の不正確さのリスク. 解決方法は,平均線パラメータを調整すること,または他の補助指標をトレンド判断に追加することもできます.
止損設定が小さすぎると止損の頻度が高くなるリスクがある. 解決方法は,異なる品種と時間周期の動向に応じて止損幅を調整することです.
利得空間設定のリスクが小さすぎる. 解決方法は,異なる品種と時間周期の動態に基づいて異なる利回り設定である.
全体として,移動平均線パラメータ,突破口パラメータ,止損幅,損比のこれらのパラメータは,異なる品種と時間周期に応じてテストされ,最適化され,戦略パラメータをカスタマイズする必要があります.
異なるタイプの移動平均 (EMA,SMAなど) をテストして,より適切な平均線指標を探します.
トレンド判断の正確さを高めるために,Momentumなどの他の補助判断指標を添加することができます.
機械学習などの方法によって動的に最適化することができる.
突破の成功率に合わせて統計学的に学習し,突破口パラメータを調整することができる.
この戦略は,トレンド特征とブレイク特性を統合し,理論的には無効な信号を大量にフィルターすることができます.重要なことは,パラメータのテストと最適化に注意を払い,戦略を異なる品種と時間周期に適用してカスタマイズすることで,実際の取引で優れた効果を得ることができます.さらに,補助指標と機械学習技術は,戦略の改善に方向性を提供します.継続的な最適化により,この戦略は,安定した信頼できるトレンドブレイク取引戦略になることができます.
/*backtest
start: 2023-10-15 00:00:00
end: 2023-11-14 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
//Backtested Time Frame: H1
//Default Settings: Are meant to run successfully on all currency pairs to reduce over-fitting.
//Risk Warning: This is a forex trading robot, backtest performance will not equal future performance, USE AT YOUR OWN RISK.
//Code Warning: Although every effort has been made for robustness, this code has not been vetted by independent 3rd parties.
strategy("Pin Bar Strategy v1", overlay=true)
// User Input
usr_risk = input(title="Equity Risk (%)",type=input.integer,minval=1,maxval=100,step=1,defval=3,confirm=false)
atr_mult = input(title="Stop Loss (x*ATR, Float)",type=input.float,minval=0.1,maxval=100,step=0.1,defval=1.9,confirm=false)
trd_rewd = input(title="Risk : Reward (1 : x*SL, Float)",type=input.float,minval=0.1,maxval=100,step=0.1,defval=3.1,confirm=false)
sma_fast = input(title="Fast MA (Period)",type=input.integer,minval=1,maxval=500,step=1,defval=20,confirm=false)
sma_slow = input(title="Slow MA (Period)",type=input.integer,minval=1,maxval=500,step=1,defval=50,confirm=false)
atr_valu = input(title="ATR (Period)",type=input.integer,minval=1,maxval=500,step=1,defval=14,confirm=false)
use_slpe = input(title="Use MA Slope (Boolean)",type=input.bool,defval=true,confirm=false)
slp_long = input(title="Bull Slope Angle (Deg)",type=input.integer,minval=-90,maxval=90,step=1,defval=1,confirm=false)
slp_shrt = input(title="Bear Slope Angle (Deg)",type=input.integer,minval=-90,maxval=90,step=1,defval=-1,confirm=false)
emg_exit = input(title="Exit When MA Re-Cross (Boolean)",type=input.bool,defval=true,confirm=false)
ent_canc = input(title="Cancel Entry After X Bars (Period)",type=input.integer,minval=1,maxval=500,step=1,defval=3,confirm=false)
// Create Indicators
fastSMA = sma(close, sma_fast)
slowSMA = sma(close, sma_slow)
bullishPinBar = ((close > open) and ((open - low) > 0.66 * (high - low))) or ((close < open) and ((close - low) > 0.66 * (high - low)))
bearishPinBar = ((close > open) and ((high - close) > 0.66 * (high - low))) or ((close < open) and ((high - open) > 0.66 * (high - low)))
atr = atr(atr_valu)
// Specify Trend Conditions
smaUpTrend = (fastSMA > slowSMA) and (fastSMA[1] > slowSMA[1]) and (fastSMA[2] > slowSMA[2]) and (fastSMA[3] > slowSMA[3]) and (fastSMA[4] > slowSMA[4])
smaDnTrend = (fastSMA < slowSMA) and (fastSMA[1] < slowSMA[1]) and (fastSMA[2] < slowSMA[2]) and (fastSMA[3] < slowSMA[3]) and (fastSMA[4] < slowSMA[4])
candleUpTrend = (close[5] > fastSMA[5]) and (open[5] > fastSMA[5]) and (close[6] > fastSMA[6]) and (open[6] > fastSMA[6]) and (close[7] > fastSMA[7]) and (open[7] > fastSMA[7]) and (close[8] > fastSMA[8]) and (open[8] > fastSMA[8]) and (close[9] > fastSMA[9]) and (open[9] > fastSMA[9]) and (close[10] > fastSMA[10]) and (open[10] > fastSMA[10])
candleDnTrend = (close[5] < fastSMA[5]) and (open[5] < fastSMA[5]) and (close[6] < fastSMA[6]) and (open[6] < fastSMA[6]) and (close[7] < fastSMA[7]) and (open[7] < fastSMA[7]) and (close[8] < fastSMA[8]) and (open[8] < fastSMA[8]) and (close[9] < fastSMA[9]) and (open[9] < fastSMA[9]) and (close[10] < fastSMA[10]) and (open[10] < fastSMA[10])
// Specify Piercing Conditions
bullPierce = ((low < fastSMA) and (open > fastSMA) and (close > fastSMA)) or ((low < slowSMA) and (open > slowSMA) and (close > slowSMA))
bearPierce = ((high > fastSMA) and (open < fastSMA) and (close < fastSMA)) or ((high > slowSMA) and (open < slowSMA) and (close < slowSMA))
// MA Slope Function
angle(_source) =>
rad2degree=180/3.14159265359
ang=rad2degree*atan((_source[0] - _source[1])/atr(atr_valu))
// Calculate MA Slope
fastSlope=angle(fastSMA)
slowSlope=angle(slowSMA)
slopingUp = fastSlope > slp_long
slopingDn = fastSlope < slp_shrt
// Specify Entry Conditions
longEntry = smaUpTrend and bullishPinBar and bullPierce
shortEntry = smaDnTrend and bearishPinBar and bearPierce
longEntryWithSlope = smaUpTrend and bullishPinBar and bullPierce and slopingUp
shortEntryWithSlope = smaDnTrend and bearishPinBar and bearPierce and slopingDn
// Specify Secondary Exit Conditions
longExit = crossunder(fastSMA, slowSMA)
shortExit = crossover(fastSMA, slowSMA)
// Long Entry Function
enterlong() =>
risk = usr_risk * 0.01 * strategy.equity
stopLoss = low[1] - atr[1] * atr_mult
entryPrice = high[1]
units = risk / (entryPrice - stopLoss)
takeProfit = entryPrice + trd_rewd * (entryPrice - stopLoss)
strategy.entry("long", strategy.long, units, stop=entryPrice)
strategy.exit("exit long", "long", stop=stopLoss, limit=takeProfit)
// Short Entry Function
entershort() =>
risk = usr_risk * 0.01 * strategy.equity
stopLoss = high[1] + atr[1] * atr_mult
entryPrice = low[1]
units = risk / (stopLoss - entryPrice)
takeProfit = entryPrice - trd_rewd * (stopLoss - entryPrice)
strategy.entry("short", strategy.short, units, stop=entryPrice)
strategy.exit("exit short", "short", stop=stopLoss, limit=takeProfit)
// Execute Long Entry w/o Slope
if (longEntry and use_slpe == false)
enterlong()
// Execute Long Entry w/ Slope
if (longEntryWithSlope and use_slpe == true)
enterlong()
// Exit Long Due to Re-Cross
if(longExit and strategy.position_size > 0 and emg_exit)
strategy.order("exit long, re-cross", strategy.short, abs(strategy.position_size))
// Cancel the Long Entry
strategy.cancel("long", barssince(longEntry) > ent_canc)
// Execute Short Entry w/o Slope
if (shortEntry and use_slpe == false)
entershort()
// Execute Short Entry w/ Slope
if (shortEntryWithSlope and use_slpe == true)
entershort()
// Exit Short Due to Re-Cross
if(shortExit and strategy.position_size < 0 and emg_exit)
strategy.order("exit short, re-cross", strategy.long, abs(strategy.position_size))
// Cancel the Short Entry
strategy.cancel("short", barssince(shortEntry) > ent_canc)
// Plot Moving Averages to Chart
plot(fastSMA, color=color.red)
plot(slowSMA, color=color.blue)
// Plot Pin Bars to Chart
plotshape(bullishPinBar, style=shape.arrowup, location=location.abovebar, color=#FF0000, text='')
plotshape(bearishPinBar, style=shape.arrowdown, location=location.belowbar, color=#0000FF, text='')