Pine Script™ (v5) の覚書 - 作ってみる:MTF移動平均線を描くインジケータの作成 (2)
前回までの話
MTF移動平均線に必要なパーツの作成
MA用label
labelを常に移動平均線の右に表示するために、labelのY座標は常に最新のMA値、X座標は最新のローソク足の2本分右になるようなlabelを再定義する。
ma_label(y,textline="",textcolor=color.blue,offset=2)=>
var l=label.new(na,na,textline,textcolor=textcolor,style=label.style_none)
l.set_xy(bar_index+offset,y)
l
自動的にひとつ上のTFを取得
現在表示しているチャートのTFは以下のように取得できる。
tf=timeframe.period
(1, 5, 15, 60, 240, D, W, M, 3M, 12M)というステップと比較して、一段上のTFを取得するためには3項演算子を用いて計算する。小さい値から比較して、今の値より大きなTFを取得する。
//HTFを計算する関数
tfhigher(tf)=>
tfsec=timeframe.in_seconds(tf)
htf=timeframe.in_seconds("1")>tfsec?"1":
timeframe.in_seconds("5")>tfsec?"5":
timeframe.in_seconds("15")>tfsec?"15":
timeframe.in_seconds("60")>tfsec?"60":
timeframe.in_seconds("240")>tfsec?"240":
timeframe.in_seconds("D")>tfsec?"D":
timeframe.in_seconds("W")>tfsec?"W":
timeframe.in_seconds("M")>tfsec?"M":
timeframe.in_seconds("3M")>tfsec?"3M":
timeframe.in_seconds("12M")>tfsec?"12M":na
htf
これを組み合わせて1段上と2段上のTFを取得する。
tf=timeframe.period//今のTF
htf1=tfhigher(tf)//1段上のTF
htf2=tfhigher(htf1)//2段上のTF(1段上の1段上)
続く
この記事が気に入ったらサポートをしてみませんか?