ZigZag解説
はじめに
ZigZagのインジケーターを見つけたので、自分で作れるレベルまで理解を進めたいと思います。
PineScript
//@version=3
study("ZigZag!",overlay=true)
use_current_res=input(true,title="Use Current Resolution?")
length = input(title="Length", type=integer, defval=10)
phase = input(title="Phase", type=integer, defval=50)
power = input(title="Power", type=integer, defval=2)
line_widht = input(title="Line Width", type=integer, defval=1)
do_col = input(true,title="color up and down moves?")
//UseFixed = input(title="Use Fixed Timeframes?", type=bool, defval=false)
htf = input(title="Higher Timeframe", defval="005", options=["M", "W", "3D", "D", "720", "360", "240", "180", "120", "060", "030", "015", "010", "005", "003", "001"])
o = security(tickerid, htf, open)
h = security(tickerid, htf, high)
l = security(tickerid, htf, low)
c = security(tickerid, htf, close)
src = use_current_res ? ohlc4 : (o + h + l + c)/4
// definition of "Jurik Moving Average", by Everget
jma(_src,_length,_phase,_power) =>
phaseRatio = _phase < -100 ? 0.5 : _phase > 100 ? 2.5 : _phase / 100 + 1.5
beta = 0.45 * (_length - 1) / (0.45 * (_length - 1) + 2)
alpha = pow(beta, _power)
jma = 0.0
e0 = 0.0
e0 := (1 - alpha) * _src + alpha * nz(e0[1])
e1 = 0.0
e1 := (_src - e0) * (1 - beta) + beta * nz(e1[1])
e2 = 0.0
e2 := (e0 + phaseRatio * e1 - nz(jma[1])) * pow(1 - alpha, 2) + pow(alpha, 2) * nz(e2[1])
jma := e2 + nz(jma[1])
//calculate jma turning point
jma_price = jma(src,length,phase,power)
turn_down = rising(jma_price[1],1) and not rising(jma_price,1)
turn_up = falling(jma_price[1],1) and not falling(jma_price,1)
pivot=na
pivot:=
turn_down?highest(high,10):
turn_up?lowest(low,10):na
pivot_color=
do_col?
pivot<fixnan(pivot)[1]?red:lime:
yellow
plot(pivot,linewidth=line_widht,color=pivot_color,offset=-2,transp=0)
// plotshape(turn_down?fixnan(pivot):na,title="top",style=shape.triangledown,location=location.absolute,size=size.tiny,color=red,transp=0)
// plotshape(turn_up?fixnan(pivot):na,title="bottom",style=shape.triangleup,location=location.absolute,size=size.tiny,color=lime,transp=0)
解説
インジケーターの定義
//@version=3
study("ZigZag!",overlay=true)
//@version=3: バージョンを指定しており、現時点の最新は4ですstudy: インジケーターを意味しています。strategyを指定すると売買ソフトを意味します。"ZigZag!": インジケーターのタイトルを指定しますoverlay=true: チャートに重ねるタイプのインジケーターにします
設定値
use_current_res=input(true,title="Use Current Resolution?")
length = input(title="Length", type=integer, defval=10)
phase = input(title="Phase", type=integer, defval=50)
power = input(title="Power", type=integer, defval=2)
line_widht = input(title="Line Width", type=integer, defval=1)
do_col = input(true,title="color up and down moves?")
//UseFixed = input(title="Use Fixed Timeframes?", type=bool, defval=false)
htf = input(title="Higher Timeframe", defval="005", options=["M", "W", "3D", "D", "720", "360", "240", "180", "120", "060", "030", "015", "010", "005", "003", "001"])
input: インジケーターの設定値として定義しますtype: 設定値の種類ですtitle: 設定値のタイトルですdefval: 設定値のデフォルト値です
別の時間足を取得
o = security(tickerid, htf, open)
h = security(tickerid, htf, high)
l = security(tickerid, htf, low)
c = security(tickerid, htf, close)
- security
: 別のシンボル/時間足をリクエスト
security(symbol, resolution, expression) → series: の形式で使用symbol (string): シンボルresolution (string): 時間足。空の文字列はチャートの現在の時間足になる。expression: 系列または系列に型変換できる要素を含むタプル
- tickerid : 現在選択している通貨のシンボル
元になるシリーズを指定
use_current_res なtrueなら現在の時間足を使用し、falseなら指定された時間足のデータを使用します。
src = use_current_res ? ohlc4 : (o + h + l + c)/4
XXX ? YYY : ZZZ: 三項演算子- ohlc4 : (始値+高値+安値+終値)/4
独自関数を定義
Jurik Moving Average(ジュリック移動平均)を求めている。
jma(_src,_length,_phase,_power) =>
phaseRatio = _phase < -100 ? 0.5 : _phase > 100 ? 2.5 : _phase / 100 + 1.5
beta = 0.45 * (_length - 1) / (0.45 * (_length - 1) + 2)
alpha = pow(beta, _power)
jma = 0.0
e0 = 0.0
e0 := (1 - alpha) * _src + alpha * nz(e0[1])
e1 = 0.0
e1 := (_src - e0) * (1 - beta) + beta * nz(e1[1])
e2 = 0.0
e2 := (e0 + phaseRatio * e1 - nz(jma[1])) * pow(1 - alpha, 2) + pow(alpha, 2) * nz(e2[1])
jma := e2 + nz(jma[1])
phaseRatio = _phase < -100 ? 0.5 : _phase > 100 ? 2.5 : _phase / 100 + 1.5_phaseが100より小さければ0.5_phaseが100より大きければ2.5_phaseがそれ以外であれば、_phaseを100で割って1.5を足す
alpha = pow(beta, _power):betaの_power乗をalphaに代入
交差の判定
//calculate jma turning point
jma_price = jma(src,length,phase,power)
turn_down = rising(jma_price[1],1) and not rising(jma_price,1)
turn_up = falling(jma_price[1],1) and not falling(jma_price,1)
jma_price = jma(src,length,phase,power): JMAを計算- rising
:
rising(source, length) → series[bool]
現在のxが過去yバーに対する前のxよりも大きい場合はtrue、そうでない場合はfalse。 - falling
:
falling(source, length) → series[bool]
現在のxが過去yバーに対する前のxよりも小さい場合はtrue、そうでない場合はfalse。 turn_down = rising(jma_price[1],1) and not rising(jma_price,1)
「1つ前のバーが上昇しており、現在のバーが上昇していない」の判定結果をturn_downに代入。turn_up = falling(jma_price[1],1) and not falling(jma_price,1)
「1つ前のバーが下降しており、現在のバーが下降していない」の判定結果をturn_upに代入。
描画する元データを計算
turn_downがtrueなら高値を、turn_upがtrueなら底値を、それ以外はnaを入れる。
pivot=na
pivot:=
turn_down?highest(high,10):
turn_up?lowest(low,10):na
na: 無効な値のシリーズ
描画
pivotを元にZigZagを描画
plot(pivot,linewidth=line_widht,color=pivot_color,offset=-2,transp=0)
Seriesとは
はじめに PineScriptを使用する上で、最も重要なのがSeriesの理解です。 Seriesは若干クセがあるので、しっかりと理解する必要があります。 Pineスクリプトの型システム Pineスクリプトには以下の基本的な9つの型が存在します。 int float bool color string line label plot hline これらの型はいくつかの形式で存在します。 形式には5種類あります。 literal const input simple series 型と形式は合わせて参照されます。 例えばliteral bool型、input bool型…のようにコンパイラによって識別されます。 また、以下のような型も存在します。 array void na (not available) tuple type 形式 Literal Literalは固定された値を表すための特別な表記法です。 Literalは常に以下のどれかの型形式になります。 literal float (3.14, 6.02E-23, 3e8) literal int (42) literal bool (true, false) literal string (“A text literal”) literal color (#FF55C6) ビルトインのopen,high,low,…などはLiterarlではありません。 これらはserires formです。 Const Const形式の値は次の特徴があります。...
BinaryOption用インジケーター
はじめに TradingViewでBinaryOption用のインジケーターを研究中です。 今回は以下のインジケーターを見てみます。 https://jp.tradingview.com/script/s48QJlfi-Vdub-Binary-Options-SniperVX-v1/ PineScript //@version=4 study("Vdub Binary Options SniperVX v1", overlay=true, shorttitle="Vdub_SniperBX_v1") // //====================channel====================== len = input(8, minval=1) src = input(close, title="Source") out = sma(src, len) last8h = highest(close, 13) lastl8 = lowest(close, 13) bearish = cross(close,out) == 1 and close[1] > close bullish = cross(close,out) == 1 and close[1] < close channel2=input(false, title="Bar Channel On/Off") ul2=plot(channel2?last8h:last8h==nz(last8h[1])?last8h:na, color=color.black, linewidth=1, style=plot.style_linebr, title="Candle body resistance level top", offset=0) ll2=plot(channel2?lastl8:lastl8==nz(lastl8[1])?lastl8:na, color=color.black, linewidth=1, style=plot....