NA0TA

トレンドラインのブレイク手法をシステム化しました

OANDA:GBPJPY   Livre sterling / Yen japonais
トレンドラインを使ったトレード手法は多くのトレーダーがトレードしている手法だと思いますが
人によってトレンドラインの引き方って全く異なってくる非常に主観的なトレード手法なんですね。

普段システム開発においてはオシレータなどの指標を使ったり、バーのパターンを組み合わせたパターントレード
でシステム作っているのですが、今回は少しコンセプトを変えてトレンドラインを使ってシステムを検証してみました。

繰り返しますがトレンドラインの引き方は人によって違うので、どのように疑似コードでプログラムに落とし込んでいくか
少々悩みました。いろいろと調べた結果、ピボットポイントの3つの点を使ってトレンドラインを引くのが有効そうだったので
まず、ピボットポイントが発生したら頂点の価格をarray関数に格納していきます。
そして、アップトレンドなら直近3つのピボットポイントのうち最後の価格と最も安い価格の2点をline.new関数でラインを引きます。

ただ2点をラインで結んだのではトレンドラインとして使えないので、ラインを未来のほうへ延長する必要があります。
オプションパラメータをextend=extend.rightに設定してラインを延長します。
そしてpineスクリプトのクールな機能にline.newで引いたラインの任意のインデックスの値を取得できるline.get_priceを使って
延長したラインの価格を取得していきます。

これで準備は整いました。

アップトレンドなら上向きのトレンドラインを下にブレイクしたらショートエントリー
ダウントレンドなら下向きのトレンドラインを上にブレイクしたらロングエントリーします。
ちゃぶつきを抑えるためにフィルターとして10期間RSIが60以上の時のみロング、40未満の時だけショートエントリーできる条件とします。

手仕舞いは仕掛けから6ATRのところにターゲットオーダーを置き、利益が3ATR以上で直近10本のバーの10パーセンタイルで
トレイリングストップ。仕掛け値から1.5ATRのところにロスカットを置きます。

ポンド円の4時間足で検証しましたが、結果はどうでしょう?プロフィットファクターは1.4を越え、期待値は23%と
思った以上の結果がでました。これでトレンドラインをシステム化した手法は機能するとみていいと思います。

サンプルコードを掲載しておきます。
BCount = input(2, title="右PIVOT", step=1, minval=1, maxval=5)
PCount = input(2, title="左PIVOT", step=1, minval=1, maxval=5)
sub_param = input(true, type=input.bool, title="ライン2")

PH0 = pivothigh(BCount,PCount)
Ph = not na(PH0)
phset = false
phset := Ph ? true : false

PL0 = pivotlow(BCount,PCount)
PLL = not na(PL0)
plset = false
plset := PLL ? true : false

var PHlist = array.new_float(3,PH0)
var PLlist = array.new_float(3,PL0)
var indexH = array.new_int(3,bar_index)
var indexL = array.new_int(3,bar_index)

if phset == true
array.remove(PHlist,0)
array.push(PHlist,PH0)
array.remove(indexH,0)
array.push(indexH,bar_index)

if plset == true
array.push(PLlist,PL0)
array.remove(PLlist,0)
array.push(indexL,bar_index)
array.remove(indexL,0)


PH_1 = array.get(PHlist,0)
HH = array.get(PHlist,1)
PH_2 = array.get(PHlist,2)
PH1index = array.get(indexH,0)
HHindex = array.get(indexH,1)
PH2index = array.get(indexH,2)

PL_1 = array.get(PLlist,0)
LL = array.get(PLlist,1)
PL_2 = array.get(PLlist,2)
PL1index = array.get(indexL,0)
LLindex = array.get(indexL,1)
PL2index = array.get(indexL,2)


highpoint = if sub_param == true
PH_1 > HH ? PH_1 : HH
else
HH

high_index = if sub_param == true
PH_1 > HH ? PH1index : HHindex
else
HHindex

var line upper_line = na
if phset == true and strategy.position_size == 0 and highpoint > PH_2
line.delete(upper_line)
upper_line := line.new(high_index-PCount, highpoint, PH2index-PCount , PH_2, extend=extend.right, width=2, color=color.blue)
upper_price = line.get_price(upper_line, bar_index)

//-------------------------------
lowpoint = if sub_param == true
PL_1 < LL ? PL_1 : LL
else
LL

low_index = if sub_param == true
PL_1 < LL ? PL1index : LLindex
else
LLindex

var line lower_line = na
if plset == true and strategy.position_size == 0 and lowpoint < PL_2
line.delete(lower_line)
lower_line := line.new(low_index-PCount,lowpoint,PL2index-PCount,PL_2,extend=extend.right, width=2, color=color.purple)
lower_price = line.get_price(lower_line,bar_index)

//RSIフィルタ
RSI = rsi(hlc3, 10)
buyover = RSI > 60
sellover = RSI < 40
warm = input(false, "RSIフィルタH", input.bool)
cool = input(false, "RSIフィルタL", input.bool)
rs_fL = warm == true ? buyover : cool == true ? sellover : RSI > 0
rs_fS = warm == true ? sellover : cool == true ? buyover : RSI > 0

//買いシグナル△
BUY = highpoint > PH_2 and crossover(close,upper_price) and strategy.position_size == 0

//売りシグナル▽
SELL = lowpoint < PL_2 and crossunder(close,lower_price) and strategy.position_size == 0

LONG_SIGNAL = BUY and rs_fL
SHORT_SIGNAL = SELL and rs_fS

strategy.entry("買い", strategy.long, qty = Risk_sizin == true ? amount : Flat_sizin == true ? amount2 :
Unit_sizin == true ? amount3 : lot*1 ,when = LONG_SIGNAL and iswork, comment="買い="+tostring(closeTR))

strategy.entry("売り", strategy.short, qty = Risk_sizin == true ? amount : Flat_sizin == true ? amount2 :
Unit_sizin==true ? amount3 : lot*1 ,when = SHORT_SIGNAL and iswork, comment="売り="+tostring(closeTR))

//ロスカットルール
qty_set = abs(strategy.position_size) * currency
loss_cutL = strategy.position_avg_price - unit_val / qty_set
loss_cutS = strategy.position_avg_price + unit_val / qty_set
Unit_lossL = strategy.position_avg_price - ((unit_val_src - margin) / Unit / qty_set)
Unit_lossS = strategy.position_avg_price + ((unit_val_src - margin) / Unit / qty_set)
strategy.exit("LC","買い", stop = so_bool_1 == true ? loss_cutL : na, comment="買い-損切り")
strategy.exit("LC","売り", stop = so_bool_1 == true ? loss_cutS : na, comment="売り-損切り")
strategy.cancel("LC", strategy.openprofit > 0 or so_bool_1 ==false or Unit_sizin == true)

strategy.exit("LC2","買い", stop = Unit_sizin == true ? Unit_lossL : na, comment="買い-ユニットロス")
strategy.exit("LC2","売り", stop = Unit_sizin == true ? Unit_lossS : na, comment="売り-ユニットロス")
strategy.cancel("LC2", strategy.openprofit > 0 or so_bool_1 ==true or Unit_sizin == false)

//プロテクトストップ
var protect_switch_L = false
var protect_switch_S = false
if strategy.position_size > 0
if crossover(close, strategy.position_avg_price + atr * 3.0)
protect_switch_L := true
protect_L = if protect_switch_L == true
strategy.position_avg_price + atr * 0.7
strategy.exit("PT","買い", stop = so_bool_1 == true or Unit_sizin == true ? protect_L : na, comment="買い-プロテクト")

if strategy.position_size < 0
if crossunder(close, strategy.position_avg_price - atr * 3.0)
protect_switch_S := true
protect_S = if protect_switch_S == true
strategy.position_avg_price - atr * 0.7
strategy.exit("PT","売り", stop = so_bool_1 == true or Unit_sizin == true ? protect_S : na, comment="売り-プロテクト")

if strategy.position_size == 0
protect_switch_L := false
protect_switch_S := false
strategy.cancel("PT", strategy.openprofit < 0 or so_bool_1 ==false)

//パーセンタイルストップルール
strategy.close("買い", when = parcentile == true and close > strategy.position_avg_price + atr * 2 ?
close <= percentile_nearest_rank(close, 10, stop_pa) and close < close : na, comment="買い-%トレイル")
strategy.close("売り", when = parcentile == true and close < strategy.position_avg_price - atr * 2 ?
close >= percentile_nearest_rank(close, 10, 100 - stop_pa) and close > close : na, comment="売り-%トレイル")

//Profit-%ターゲット-ルール
Profit = strategy.openprofit
p_size = abs(strategy.position_size) * currency * atr
if strategy.position_size > 0
strategy.close("買い", when = profit_per == true and crossover(Profit, p_size * pro_pa*0.1) , comment="買い-Profit%ターゲット")
if strategy.position_size < 0
strategy.close("売り", when = profit_per == true and crossover(Profit, p_size * pro_pa*0.1) , comment="売り-Profit%ターゲット")

Clause de non-responsabilité

Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.