以前みつけた高勝率のzigzagストラテジーを再現する試みです。

インジケーターは理解できました。
次回は、ストラテジーを作成するにあたって、
必要な情報を変数に格納するスキームを作成します。
※ コピペする場合は以下の変更を行ってください
[](全角の角括弧)→(半角の角括弧)
(全角スペース)→(半角スペース)
=====
//version=4
study("Zig Zag", overlay=true)
// Deviation = 偏差
dev_threshold = input(title="Deviation (%)", type=input.float, defval=.1, minval=0.01, maxval=100)
// Depth = 深さ
depth = input(title="Depth", type=input.integer, defval=10, minval=1)
//**
//* depth -> length -> zero
//* lengthが最高値・最安値であるかを確認
//* trueならbar_indexと価格を返す
//*
pivots(src, length, isHigh) =>
p = nz(src[length])
if length == 0
[bar_index, p]
else
isFound = true
// length -> zeroの最高値・最安値を確認
for i = 0 to length - 1
if isHigh and src[i] > p
isFound := false
if not isHigh and src[i] < p
isFound := false
// depth -> lengthの最高値・最安値を確認
for i = length + 1 to 2 * length
if isHigh and src[i] >= p
isFound := false
if not isHigh and src[i] <= p
isFound := false
// lengthが最高値・最安値だった場合は値を返す
if isFound and length * 2 <= bar_index
[bar_index[length], p]
// そうでない場合はnaを返す
else
[int(na), float(na)]
[iH, pH] = pivots(high, floor(depth / 2), true)
[iL, pL] = pivots(low, floor(depth / 2), false)
// データ確認用
// plot( pH )
// plot( pL )
plot( iH )
plot( na(iH) ? 1 : 0 )
plot( iL )
// 変化率を算出する関数
calc_dev(base_price, price) =>
100 * (price - base_price) / base_price
// 直近のpivotの情報を格納
var line lineLast = na // lineのid (実際はidではなく識別している何か)
var int iLast = 0 // bar_index
var float pLast = 0 // price
var bool isHighLast = true // High か Low か
// 描画したラインの数
var int linesCount = 0
// データ確認用
// plot( lineLast )
// plot( iLast )
// plot( pLast )
// plot( linesCount )
//**
//* Lineを描画する関数
//*
pivotFound(dev, isHigh, index, price) =>
// zigzagの頂点を更新する
if isHighLast == isHigh and not na(lineLast)
if isHighLast ? price > pLast : price < pLast
if linesCount <= 1
line.set_xy1(lineLast, index, price)
line.set_xy2(lineLast, index, price)
[lineLast, isHighLast, false]
else
[line(na), bool(na), false]
// zigzagの方向を変える(zigzagの頂点)
else
// 一番最初のLine
if na(lineLast)
id = line.new(index, price, index, price, color=color.red, width=2)
[id, isHigh, true]
else
// 変化率が設定値以上であることを確認
if abs(dev) >= dev_threshold
id = line.new(iLast, pLast, index, price, color=color.red, width=2)
[id, isHigh, true]
else
[line(na), bool(na), false]
//**
//* 描画処理
//*
// iH iL のデータある
// iH と iL の値(bar_index)が同じ
// 高値と安値の描画処理を行う
if not na(iH) and not na(iL) and iH == iL
dev1 = calc_dev(pLast, pH)
[id2, isHigh2, isNew2] = pivotFound(dev1, true, iH, pH)
if isNew2
linesCount := linesCount + 1
if not na(id2)
lineLast := id2
isHighLast := isHigh2
iLast := iH
pLast := pH
dev2 = calc_dev(pLast, pL)
[id1, isHigh1, isNew1] = pivotFound(dev2, false, iL, pL)
if isNew1
linesCount := linesCount + 1
if not na(id1)
lineLast := id1
isHighLast := isHigh1
iLast := iL
pLast := pL
else
// iH の値がある
if not na(iH)
dev1 = calc_dev(pLast, pH)
[id, isHigh, isNew] = pivotFound(dev1, true, iH, pH)
if isNew
linesCount := linesCount + 1
if not na(id)
lineLast := id
isHighLast := isHigh
iLast := iH
pLast := pH
// iL の値がある
else
if not na(iL)
dev2 = calc_dev(pLast, pL)
[id, isHigh, isNew] = pivotFound(dev2, false, iL, pL)
if isNew
linesCount := linesCount + 1
if not na(id)
lineLast := id
isHighLast := isHigh
iLast := iL
pLast := pL
=====

インジケーターは理解できました。
次回は、ストラテジーを作成するにあたって、
必要な情報を変数に格納するスキームを作成します。
※ コピペする場合は以下の変更を行ってください
[](全角の角括弧)→(半角の角括弧)
(全角スペース)→(半角スペース)
=====
//version=4
study("Zig Zag", overlay=true)
// Deviation = 偏差
dev_threshold = input(title="Deviation (%)", type=input.float, defval=.1, minval=0.01, maxval=100)
// Depth = 深さ
depth = input(title="Depth", type=input.integer, defval=10, minval=1)
//**
//* depth -> length -> zero
//* lengthが最高値・最安値であるかを確認
//* trueならbar_indexと価格を返す
//*
pivots(src, length, isHigh) =>
p = nz(src[length])
if length == 0
[bar_index, p]
else
isFound = true
// length -> zeroの最高値・最安値を確認
for i = 0 to length - 1
if isHigh and src[i] > p
isFound := false
if not isHigh and src[i] < p
isFound := false
// depth -> lengthの最高値・最安値を確認
for i = length + 1 to 2 * length
if isHigh and src[i] >= p
isFound := false
if not isHigh and src[i] <= p
isFound := false
// lengthが最高値・最安値だった場合は値を返す
if isFound and length * 2 <= bar_index
[bar_index[length], p]
// そうでない場合はnaを返す
else
[int(na), float(na)]
[iH, pH] = pivots(high, floor(depth / 2), true)
[iL, pL] = pivots(low, floor(depth / 2), false)
// データ確認用
// plot( pH )
// plot( pL )
plot( iH )
plot( na(iH) ? 1 : 0 )
plot( iL )
// 変化率を算出する関数
calc_dev(base_price, price) =>
100 * (price - base_price) / base_price
// 直近のpivotの情報を格納
var line lineLast = na // lineのid (実際はidではなく識別している何か)
var int iLast = 0 // bar_index
var float pLast = 0 // price
var bool isHighLast = true // High か Low か
// 描画したラインの数
var int linesCount = 0
// データ確認用
// plot( lineLast )
// plot( iLast )
// plot( pLast )
// plot( linesCount )
//**
//* Lineを描画する関数
//*
pivotFound(dev, isHigh, index, price) =>
// zigzagの頂点を更新する
if isHighLast == isHigh and not na(lineLast)
if isHighLast ? price > pLast : price < pLast
if linesCount <= 1
line.set_xy1(lineLast, index, price)
line.set_xy2(lineLast, index, price)
[lineLast, isHighLast, false]
else
[line(na), bool(na), false]
// zigzagの方向を変える(zigzagの頂点)
else
// 一番最初のLine
if na(lineLast)
id = line.new(index, price, index, price, color=color.red, width=2)
[id, isHigh, true]
else
// 変化率が設定値以上であることを確認
if abs(dev) >= dev_threshold
id = line.new(iLast, pLast, index, price, color=color.red, width=2)
[id, isHigh, true]
else
[line(na), bool(na), false]
//**
//* 描画処理
//*
// iH iL のデータある
// iH と iL の値(bar_index)が同じ
// 高値と安値の描画処理を行う
if not na(iH) and not na(iL) and iH == iL
dev1 = calc_dev(pLast, pH)
[id2, isHigh2, isNew2] = pivotFound(dev1, true, iH, pH)
if isNew2
linesCount := linesCount + 1
if not na(id2)
lineLast := id2
isHighLast := isHigh2
iLast := iH
pLast := pH
dev2 = calc_dev(pLast, pL)
[id1, isHigh1, isNew1] = pivotFound(dev2, false, iL, pL)
if isNew1
linesCount := linesCount + 1
if not na(id1)
lineLast := id1
isHighLast := isHigh1
iLast := iL
pLast := pL
else
// iH の値がある
if not na(iH)
dev1 = calc_dev(pLast, pH)
[id, isHigh, isNew] = pivotFound(dev1, true, iH, pH)
if isNew
linesCount := linesCount + 1
if not na(id)
lineLast := id
isHighLast := isHigh
iLast := iH
pLast := pH
// iL の値がある
else
if not na(iL)
dev2 = calc_dev(pLast, pL)
[id, isHigh, isNew] = pivotFound(dev2, false, iL, pL)
if isNew
linesCount := linesCount + 1
if not na(id)
lineLast := id
isHighLast := isHigh
iLast := iL
pLast := pL
=====
小次郎講師公式インジケーターのお申込
bit.ly/2vdSV4Q
小次郎講師のLINE@
bit.ly/2VZQFu3
小次郎講師のチャート情報局
bit.ly/2GvLAEp
bit.ly/2vdSV4Q
小次郎講師のLINE@
bit.ly/2VZQFu3
小次郎講師のチャート情報局
bit.ly/2GvLAEp
İlgili yayınlar
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.
小次郎講師公式インジケーターのお申込
bit.ly/2vdSV4Q
小次郎講師のLINE@
bit.ly/2VZQFu3
小次郎講師のチャート情報局
bit.ly/2GvLAEp
bit.ly/2vdSV4Q
小次郎講師のLINE@
bit.ly/2VZQFu3
小次郎講師のチャート情報局
bit.ly/2GvLAEp
İlgili yayınlar
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.