どんな記事
TradingView(トレーディングビュー)の Pineスクリプトでつくる「Study(インジケーター)」のよく使う組み合わせをまとめてみました(主に自分用)。どれもコピペで機能をつけ加えることができる簡単なものです。
今後も、新たに見つけたり思いついたものがあれば、ここに加えていく予定です!
追記
2020年6月18日 更新
「timezoneと夏時間の認識」の最適化を行いました。
2019年8月17日 追記
timezone
label
line
に関するコードを追加しました。その他、version4の公開に伴い加筆修正を行いました。
2019年6月8日 追記
VWAPの累積平均を算出する を追加しました。
2019年5月4日 更新・追記
チャート以外の価格データを取得する のコードを最新のものに更新、Quandlから金融データを取得するを追加しました。
条件に応じて背景色を変更する
bool1 = input(true ,title="色分け")
condition = __YOUR_PROGRAM__
bgcolor(color
= bool1==false ? na
: condition==1 ? #64B5F6
: condition==2 ? #2196F3
: condition==3 ? #64B5F6
: na
,transp=70 ,title="背景色")
2本のラインの間を塗りつぶす
//@version=3
p1 = plot(emaA ,color=red ,title="EMA A" ,style=line ,linewidth=2, transp=0)
p2 = plot(emaB ,color=blue ,title="EMA B" ,style=line ,linewidth=2, transp=0)
fill(p1 ,p2 ,color=emaA > emaB ? red : blue ,title="fill" ,transp=60)
複数のシグナルを表示する
//@version=3
a = ema(close ,5)
b = ema(close ,25)
c = ema(close ,50)
sig1 = cross(a ,b)
sig2 = cross(a ,c)
sig3 = cross(b ,c)
p1 = plot(4 ,color=black ,editable=false ,trackprice=false)
p2 = plot(3 ,color=black ,editable=false ,trackprice=false)
p3 = plot(2 ,color=black ,editable=false ,trackprice=false)
p4 = plot(1 ,color=black ,editable=false ,trackprice=false)
fill(p1 ,p2 ,color = sig1 ? red : gray ,title="sig1" ,transp=50)
fill(p2 ,p3 ,color = sig2 ? red : gray ,title="sig2" ,transp=50)
fill(p3 ,p4 ,color = sig3 ? red : gray ,title="sig3" ,transp=50)
メインチャート以外の価格データを取得する
Security()
で別の銘柄のデータを取得
security()
で、チャート以外の銘柄のデータを取得できる。security("usdjpy" ,"D" ,close)
のclose
を、ema(close ,20)
などと指定することもできる。Security()
を複数記述すると、すべて取りにいってしまうのか重くなる。要注意。
以下のコードは、メインチャートの価格を指定した通貨で為替換算するもの。
currency = input("None" ,title="為替換算" ,options=["None" ,"USD" ,"EUR" ,"GBP" ,"AUD" ,"CAD" ,"CHF"])
get_root(currency) =>
currency=="USD" ? "usdjpy" :
currency=="EUR" ? "eurjpy" :
currency=="GBP" ? "gbpjpy" :
currency=="AUD" ? "audjpy" :
currency=="CAD" ? "cadjpy" :
currency=="CHF" ? "chfjpy" :
"usdjpy"
sc1(currency ,root) =>
currency=="None" ? 1 :
security(root ,"D" ,close)
root = get_root(currency)
value = close * sc1(currency ,root)
※ 銘柄コード「4005」など、複数の取引所で同じコードが使われている場合、「TSE:4005」のように取引所コードで渡さないと取得することができない。組込み変数なら、ticker
ではなくtickerid
を使うこと。
Security()
で別の時間軸のデータを取得
メインチャートより上の時間軸の価格を取得することもできる。
//1時間足を取得
Security( syminfo.tickerid ,"60" ,close )
//4時間足を取得
Security( syminfo.tickerid ,"240" ,close )
//日足を取得
Security( syminfo.tickerid ,"D" ,close )
//週足を取得
Security( syminfo.tickerid ,"W" ,close )
//月足を取得
Security( syminfo.tickerid ,"M" ,close )
日足の四本値を正確に取得
OSE:NK225
の金~月曜日にかけてなど、ごく稀にSecurity()
で取得する価格データがズレることがある。正確な日足などの四本値が欲しい場合は、以下のように対応する。
//@version=4
sessionStart = input( "Morning" ,options=[ "Night" ,"Morning" ] )
is_nkx = syminfo.root=="NK225" or syminfo.root=="NK225M"
morning_open = hour[1] == 05 and hour == 08 and minute >= 30
night_open = hour[1] == 15 and hour == 16 and minute >= 30
is_morning_open = sessionStart=="Morning" and morning_open
is_night_open = sessionStart=="Night" and night_open
is_open = is_nkx and ( is_morning_open or is_night_open )
var float dayOpen = na
dayOpen := is_open ? open : dayOpen[1]
直近のデータだけ描画する
plotshape(close, show_last=7)
他にも、plot
plotbar
plotchar
fill
などでもshow_last
を使うことができる。
チャートに直線を引く
na
で間を空けて、style=line
で点と点をつなぐ
//@version=3
isRegularFractal(mode) =>
ret = mode == 1 ? high[4] < high[3] and high[3] < high[2] and high[2] > high[1] and high[1] > high[0] : mode == -1 ? low[4] > low[3] and low[3] > low[2] and low[2] < low[1] and low[1] < low[0] : false
topFractal = isRegularFractal(1) ? high[2] : isRegularFractal(-1) ? low[2] : na
plot(topFractal ,color=red ,offset=-2)
na
が入ると、有効なデータの点と点を直線でつないでくれる。
上昇幅を算出して
//@version=3
shortl = input(10)
longl = input(100)
offset_f = input(2)
offset_b = input(20)
// タイムラインを作成しておく
timeline = 0
timeline:= nz(timeline[1]) + 1
// ボトムを探す
a = valuewhen(low==lowest(low ,shortl) ,low ,0)
b = valuewhen(low==lowest(low ,longl) ,low ,0)
whena = valuewhen(low==lowest(low ,shortl) ,timeline ,0)
whenb = valuewhen(low==lowest(low ,longl) ,timeline ,0)
m = (b-a) / (whenb-whena)
bot_price = m * (timeline-whena) + a
back_bot_price = m * (timeline-whena-offset_b) + a
forward_bot_price = m * (timeline-whena+offset_f) + a
bot_price := b-a==0 ? nz(bot_price[1]) : bot_price
same_bot = a==a[1] and b==b[1] and a!=0 and b!=0 and whena!=whenb
plot(same_top ? top_price : na ,style=linebr ,color=red ,linewidth=2 ,offset= 0 ,transp=50 ,title="_top")
plot(same_top ? back_top_price : na ,style=linebr ,color=red ,linewidth=1 ,offset=-offset_b ,transp=50 ,title="back_top")
plot(same_top ? forward_top_price : na ,style=linebr ,color=red ,linewidth=1 ,offset= offset_f ,transp=70 ,title="forward_top")
タイムラインと上昇幅を算出して、力技で直線を引く。
version4のline.new()
を使って
圧倒的に使いやすい。
bar_index
で位置を指定する。以下のコードは、4本過去にタテ線を描画するもの。
※ bar_index
で将来を指定することはできない
//@version=4
var line vl01 = na
line.delete( vl01 )
vl01 := line.new( bar_index[4] ,high+tr ,bar_index[4] ,low-tr )
time
で位置を指定する
※ 将来を指定することはできるが、ズレる場合がある
//@version=4
showHL = input( true ,title="当日クローズまでの ヨコ線 を描画 (30分足以下)")
showVL = input( true ,title="当日クローズまでの タテ線 の描画 (30分足以下)" )
is_intraday_smaller30 = timeframe.isintraday and timeframe.multiplier <= 30
b4high5 = security( syminfo.tickerid ,"D" ,highest( high ,5 )[1] ,lookahead=barmerge.lookahead_on )
b4low5 = security( syminfo.tickerid ,"D" ,lowest( low ,5 )[1] ,lookahead=barmerge.lookahead_on )
b4mid5 = ( b4high5 + b4low5 ) / 2
is_nan = b4atr[1]!=b4atr
var start = time
var end = time
if is_nan and is_intraday_smaller30
start := timestamp( year,month,dayofmonth,hour,minute )
end := timestamp( year,month, dayofmonth + 1 ,hour,minute )
var line ln_high5 = na
var line ln_mid5 = na
var line ln_low5 = na
var line ln_01 = na
var line ln_02 = na
if showHL
line.delete( ln_high5 )
line.delete( ln_mid5 )
line.delete( ln_low5 )
ln_high5 := line.new( start ,b4high5 ,end ,b4high5 ,xloc.bar_time )
ln_mid5 := line.new( start ,b4mid5 ,end ,b4mid5 ,xloc.bar_time )
ln_low5 := line.new( start ,b4low5 ,end ,b4low5 ,xloc.bar_time )
if showVL
line.delete( ln_01 )
line.delete( ln_02 )
ln_01 := line.new( start ,b4high5 ,end ,b4low5 ,xloc.bar_time )
ln_02 := line.new( start ,b4high5 ,end ,b4low5 ,xloc.bar_time )
TradingViewの時間軸のクセに注意
- TradingViewの時間軸には以下の特徴がある
- 過去のセッション時間外は表示されない
- 未来の時間外は表示される(24時間すべて表示されている)
- セッションが始まるタイミングで時間外の目盛りが削除される
- timeでラインを詳細に描画
- そのまま③で省略されるとズレる
- ローソク足とラインの間隔が優先される(時間ではない)
- 省略された分、未来に描画されたことになる
指標発表の時刻にタテ線を引くようなコードを作成した場合、大変不便な思いをする。(「F5」で更新すれば良いだけなのだが)
チャートに文字を出力する
plotshape()
を使って
version3まではこれしかできなかった。textの内容をtostring()
で動的に変えるようなことはできない。
//@version=4
data = close >= open
plotshape( data ,text="UP" ,textcolor=color.black ,style=shape.labeldown )
version4のlabel.new()
を使って
bar_index
で位置を指定する。
//@version=4
label.new( bar_index ,high ,"x="+tostring(bar_index)+"\ny="+tostring(high) ,style=label.style_none )
time
で位置を指定する。
//@version=4
dt = time - time[1]
var label xy = na
label.delete( xy )
xy := label.new( time+3 * dt ,hlc3 ,"x="+tostring(bar_index)+"\ny="+tostring(high) ,xloc.bar_time ,style=label.style_none )
前々回の安値・高値更新を元にしたトレイリングストップ
//@version=3
vw_low = valuewhen(low > low[1] ,low[1] ,1)
vw_high = valuewhen(high < high[1] ,high[1] ,1)
low_active = close > vw_low
high_active = close < vw_high
p1 = plot(low_active ? vw_low : na ,linewidth=1 ,color=purple ,style=linebr ,title="前々回の安値更新")
p2 = plot(high_active ? vw_high : na ,linewidth=1 ,color=purple ,style=linebr ,title="前々回の高値更新")
当然だが、有効なときとそうでないときがある。
数秘を算出する
「Astrology(占星)が有効なら、Numerology(数秘)も有効なんじゃないか」と、ちょっと気になって作成してみたコード。実用性はあまりなさそう。サクッと試せるのはTradingViewの良いところ。
calc_n(target) =>
n4 = target >= 2000 ? 2 : target >= 1000 ? 1 : 0
n3_ = target - n4*1000
n3 = n3_ >= 900 ? 9 : n3_ >= 800 ? 8 : n3_ >= 700 ? 7 : n3_ >= 600 ? 6 : n3_ >= 500 ? 5 : n3_ >= 400 ? 4 : n3_ >= 300 ? 3 : n3_ >= 200 ? 2 : n3_ >= 100 ? 1 : 0
n2_ = n3_ - n3*100
n2 = n2_ >= 90 ? 9 : n2_ >= 80 ? 8 : n2_ >= 70 ? 7 : n2_ >= 60 ? 6 : n2_ >= 50 ? 5 : n2_ >= 40 ? 4 : n2_ >= 30 ? 3 : n2_ >= 20 ? 2 : n2_ >= 10 ? 1 : 0
n1 = n2_ - n2*10
[n4 ,n3 ,n2 ,n1]
[y4 ,y3 ,y2 ,y1] = calc_n(year)
[_ ,_ ,m2 ,m1] = calc_n(month)
[_ ,_ ,d2 ,d1] = calc_n(dayofmonth)
num__ = y4 + y3 + y2 + y1 + m2 + m1 + d2 + d1
[_ ,_ ,n2_ ,n1_] = calc_n(num__)
num_ = n2_ + n1_
[_ ,_ ,n2 ,n1] = calc_n(num_)
num = n2 + n1
変化率の対数を算出する
roc()
が変化率。1%を、0.01
ではなく1
と返すため、100で割っている。log()
は対数を返す。これを必要なだけ足していくと Ku-Chart ができあがる。
calc_log(target) => nz( log( 1 + roc(target ,1) / 100 ))
強弱の順位を算出する
同程度の数値があって順位が欲しい場合に使う。
target1 = __YOUR_PROGRAM__
target2 = __YOUR_PROGRAM__
target3 = __YOUR_PROGRAM__
target4 = __YOUR_PROGRAM__
target5 = __YOUR_PROGRAM__
target6 = __YOUR_PROGRAM__
target7 = __YOUR_PROGRAM__
target8 = __YOUR_PROGRAM__
rank(target) =>
_1 = target > target1 ? 0 : 1
_2 = target > target2 ? 0 : 1
_3 = target > target3 ? 0 : 1
_4 = target > target4 ? 0 : 1
_5 = target > target5 ? 0 : 1
_6 = target > target6 ? 0 : 1
_7 = target > target7 ? 0 : 1
_8 = target > target8 ? 0 : 1
_1 + _2 + _3 + _4 + _5 + _6 + _7 + _8
rank1 = rank(target1)
Quandlから金融データを取得する
金融データと言えば Quandl。人口やGDPの推移、機関投資家の取引状況等を確認することができます。なんと、TradingView は Quandl からデータを取得することができます。すごい。最近では、quande()
という専用の関数も追加されています。
以下のコードは、CFTCの建玉明細を表示するもの。
//@vertion=3
force_root = input("", title="Override Product")
is_includeoptions = input(false, type=bool, title="Include Options")
fxroot =
ticker == "USDCAD" ? "CD" :
ticker == "USDCAD" ? "CD" :
ticker == "USDCHF" ? "SF" :
ticker == "USDCZK" ? "CZ" :
ticker == "USDHUF" ? "FR" :
ticker == "USDILS" ? "IS" :
ticker == "USDJPY" ? "JY" :
ticker == "USDMXN" ? "MP" :
ticker == "USDNOK" ? "UN" :
ticker == "USDPLN" ? "PZ" :
ticker == "USDRUB" ? "RU" :
ticker == "USDSEK" ? "SE" :
ticker == "USDZAR" ? "RA" :
ticker == "EURUSD" ? "EC" :
ticker == "AUDUSD" ? "AD" :
ticker == "GBPUSD" ? "BP" :
ticker == "NZDUSD" ? "NE" :
ticker == "BRLUSD" ? "BR" :
""
root = force_root == "" ? fxroot == "" ? syminfo.root : fxroot : force_root
code = root + (is_includeoptions ? "_FO" : "_F") + "_L_ALL"
long = security("QUANDL:CFTC/"+code+"|1", "D", close)
short = security("QUANDL:CFTC/"+code+"|2", "D", close)
plot(long, color = red, title="Long" ,style=stepline)
plot(short, color = blue, title="Short" ,style=stepline)
plot(long-short, color = orange, title="Net", style=columns ,transp=50)
VWAPの累積平均を算出する
VWAPというのは、「Volume Weighted Average Price(出来高加重平均価格)」のこと。多くの機関投資家が参考にするものらしく、このラインよりも価格が上にあれば、起点とする日より後に買った投資家の損益合計はプラスになるとのこと。VWAP の移動平均はvwma()
という関数が用意されている。
isWork = timestamp( fromY ,fromM ,fromD ,00 ,00 ) <= time
volume_sum = volume
volume_sum := isWork ? isWork[1] != isWork ? volume : volume_sum[1] + volume : 0
vwap_sum = vwap
vwap_sum := isWork ? isWork[1] != isWork ? vwap
: (vwap_sum[1] * volume_sum[1] + vwap * volume) / volume_sum
: na
注意点
起点となる足のvwap
をna
などで削ると正確に計算されない。想像するに、vwap
は組み込み変数のようで、ウラは関数。以下のコードは上手くいかない。
//@version=4
dayOpen = security( syminfo.tickerid ,"D" ,open ,lookahead=barmerge.lookahead_on )
is_open = dayOpen[1]!=dayOpen
plot( is_open ? na : vwap ,color=color.red )
次のようにすると上手くいく。
//@version=4
dayOpen = security( syminfo.tickerid ,"D" ,open ,lookahead=barmerge.lookahead_on )
is_open = dayOpen[1]!=dayOpen
VWAP = vwap
plot( is_open ? na : VWAP ,color=color.blue )
timezoneと夏時間の認識
各国のOpen/Closeや、中値・経済指標等の発表時刻を描画するコードです。夏時間や各取引所の時差を認識し調整します。過去~現在では背景色で表し、当日の変化点に縦線を引きます。
上図では、それぞれ「灰色:日本」「赤:香港」「青:ロンドン」「オレンジ:米国」を表しています。
//@version=4
study( "Sessions" ,overlay=true )
showVL = input( true ,title="縦線 の描画 (30分足以下)" )
showVLBG = input( true ,title="市場ごとの背景色を入れる" )
_colorVL = input( "市場ごと" ,title="縦線 の色" ,options=[ "黒" ,"グレー" ,"白" ,"緑" ,"市場ごと" ] )
_styleVL = input( "破線" ,title="縦線 の形式" ,options=[ "直線" ,"破線" ,"点線" ] )
widthVL = input( 1 ,title="縦線 の太さ (1~3)" ,type=input.integer ,minval=1 ,maxval=3 )
_extendVL = input( true ,title="縦線 を上下に延長する" )
is_intraday = timeframe.isintraday
b4h20 = security( syminfo.tickerid ,"D" ,highest( high ,5 )[1] ,lookahead=barmerge.lookahead_on )
b4l20 = security( syminfo.tickerid ,"D" ,lowest( low ,5 )[1] ,lookahead=barmerge.lookahead_on )
var adjustGMT = syminfo.timezone=="America/New_York" ? -4
: syminfo.timezone=="Asia/Tokyo" ? 9
: syminfo.timezone=="Etc/UTC" ? 0
: syminfo.timezone=="America/Los_Angeles" ? -7
: syminfo.timezone=="America/Chicago" ? -5
: syminfo.timezone=="America/Phoenix" ? -7
: syminfo.timezone=="America/Toronto" ? -4
: syminfo.timezone=="America/Vancouver" ? -7
: syminfo.timezone=="America/Argentina/Buenos_Aires" ? -3
: syminfo.timezone=="America/El_Salvador" ? -6
: syminfo.timezone=="America/Sao_Paulo" ? -3
: syminfo.timezone=="America/Bogota" ? -5
: syminfo.timezone=="Europe/Moscow" ? 3
: syminfo.timezone=="Europe/Athens" ? 3
: syminfo.timezone=="Europe/Berlin" ? 2
: syminfo.timezone=="Europe/London" ? 1
: syminfo.timezone=="Europe/Madrid" ? 2
: syminfo.timezone=="Europe/Paris" ? 2
: syminfo.timezone=="Europe/Warsaw" ? 2
: syminfo.timezone=="Australia/Sydney" ? 10
: syminfo.timezone=="Australia/Brisbane" ? 10
: syminfo.timezone=="Australia/ACT" ? 10
: syminfo.timezone=="Asia/Almaty" ? 6
: syminfo.timezone=="Asia/Ashkhabad" ? 5
: syminfo.timezone=="Asia/Taipei" ? 8
: syminfo.timezone=="Asia/Singapore" ? 8
: syminfo.timezone=="Asia/Shanghai" ? 8
: syminfo.timezone=="Asia/Seoul" ? 9
: syminfo.timezone=="Asia/Dubai" ? 4
: syminfo.timezone=="Asia/Hong_Kong" ? 8
: syminfo.timezone=="Asia/Bangkok" ? 7
: syminfo.timezone=="Pacific/Auckland" ? 12
: syminfo.timezone=="Pacific/Honolulu" ? -10 : 0
var s_ny_st = time
var s_uk_st = time
var e_ny_st = time
var e_uk_st = time
if( month[1]==2 and month==3 )
remainder_s = ( 31 - dayofmonth ) % 7
calc_pointday_s = 31 - remainder_s
target_date_s_uk = 8 - dayofweek <= remainder_s ? calc_pointday_s + ( 8 - dayofweek ) : calc_pointday_s - ( dayofweek - 1 )
s_uk_st := timestamp( "GMT",year,3,target_date_s_uk,1,0 )
target_date_s_ny = dayofweek <= dayofmonth ? dayofmonth - ( dayofweek -1 ) : dayofmonth + ( 8 - dayofweek )
s_ny_st := timestamp( "GMT-4",year,3, target_date_s_ny + 7 ,2,0 )
if( month[1]==9 and month==10 )
remiander_e = ( 31 - dayofmonth ) % 7
calc_pointday_e = 31 - remiander_e
target_date_e_uk = 8 - dayofweek <= remiander_e ? calc_pointday_e + ( 8 - dayofweek ) : calc_pointday_e - ( dayofweek - 1 )
e_uk_st := timestamp( "GMT",year,10,target_date_e_uk,1,0 )
if( month[1]==10 and month==11 )
target_date_e_ny = dayofweek <= dayofmonth ? dayofmonth - ( dayofweek -1 ) : dayofmonth + ( 8 - dayofweek )
e_ny_st := timestamp( "GMT-4",year,11,target_date_e_ny,2,0 )
toggle_ny_st = ( 3 < month and month < 11 ) or ( month==3 and s_ny_st <= time ) or ( month==11 and time <= e_ny_st )
toggle_uk_st = ( 3 < month and month < 10 ) or ( month==3 and s_uk_st <= time ) or ( month==10 and time <= e_uk_st )
var jp1_open = time
var jp_fix = time
var jp1_close = time
var jp2_open = time
var jp2_close = time
var hk1_open = time
var hk1_close = time
var hk2_open = time
var hk2_close = time
var uk_open = time
var uk_fix = time
var uk_close = time
var us_econ = time
var ny_open = time
var ny_close = time
checkGMT( gtm ,h ,m ) =>
gap = hour - adjustGMT + gtm
gap_day1 = gap >= 24 ? 1 : gap < 0 ? -1 : 0
gap_day2 = h * 60 + m >= 24 * 60 ? 1 : 0
gap_day1 + gap_day2
if is_intraday
jp1_open := timestamp("GMT+9",year,month, dayofmonth + checkGMT( 9 ,08 ,00 ) ,09,00)
jp_fix := timestamp("GMT+9",year,month, dayofmonth + checkGMT( 9 ,09 ,55 ) ,09,55)
jp1_close := timestamp("GMT+9",year,month, dayofmonth + checkGMT( 9 ,11 ,00 ) ,11,00)
jp2_open := timestamp("GMT+9",year,month, dayofmonth + checkGMT( 9 ,12 ,30 ) ,12,30)
jp2_close := timestamp("GMT+9",year,month, dayofmonth + checkGMT( 9 ,17 ,00 ) ,15,00)
hk1_open := timestamp("GMT+8",year,month, dayofmonth + checkGMT( 8 ,09 ,30 ) ,09,30)
hk1_close := timestamp("GMT+8",year,month, dayofmonth + checkGMT( 8 ,12 ,00 ) ,12,00)
hk2_open := timestamp("GMT+8",year,month, dayofmonth + checkGMT( 8 ,13 ,30 ) ,13,30)
hk2_close := timestamp("GMT+8",year,month, dayofmonth + checkGMT( 8 ,16 ,00 ) ,16,00)
uk_open := toggle_uk_st ? timestamp("GMT+1",year,month, dayofmonth + checkGMT( 1 ,08 ,00 ) ,08,00) : timestamp("GMT",year,month, dayofmonth + checkGMT( 0 ,08 ,00 ) ,08,00)
uk_fix := toggle_uk_st ? timestamp("GMT+1",year,month, dayofmonth + checkGMT( 1 ,16 ,00 ) ,16,00) : timestamp("GMT",year,month, dayofmonth + checkGMT( 0 ,16 ,00 ) ,16,00)
uk_close := toggle_uk_st ? timestamp("GMT+1",year,month, dayofmonth + checkGMT( 1 ,17 ,00 ) ,17,00) : timestamp("GMT",year,month, dayofmonth + checkGMT( 0 ,17 ,00 ) ,17,00)
ny_open := toggle_ny_st ? timestamp("GMT-4",year,month, dayofmonth + checkGMT( -4 ,08 ,00 ) ,08,00) : timestamp("GMT-5",year,month, dayofmonth + checkGMT( -5 ,08 ,00 ) ,08,00)
us_econ := toggle_ny_st ? timestamp("GMT-4",year,month, dayofmonth + checkGMT( -4 ,08 ,30 ) ,08,30) : timestamp("GMT-5",year,month, dayofmonth + checkGMT( -5 ,08 ,30 ) ,08,30)
ny_close := toggle_ny_st ? timestamp("GMT-4",year,month, dayofmonth + checkGMT( -4 ,17 ,00 ) ,17,00) : timestamp("GMT-5",year,month, dayofmonth + checkGMT( -5 ,17 ,00 ) ,17,00)
var line ln_01 = na
var line ln_02 = na
var line ln_03 = na
var line ln_04 = na
var line ln_05 = na
var line ln_06 = na
var line ln_07 = na
var line ln_08 = na
var line ln_09 = na
var line ln_10 = na
var line ln_11 = na
var line ln_12 = na
var line ln_13 = na
var line ln_14 = na
var line ln_15 = na
colorVL = _colorVL=="黒" ? color.black : _colorVL=="グレー" ? #cccccc : _colorVL=="緑" ? #4CAF50 : color.white
styleVL = _styleVL=="直線" ? line.style_solid : _styleVL=="破線" ? line.style_dashed : line.style_dotted
extendVL = _extendVL ? extend.both : extend.none
if is_intraday and showVL
line.delete( ln_01 )
line.delete( ln_02 )
line.delete( ln_03 )
line.delete( ln_04 )
line.delete( ln_05 )
line.delete( ln_06 )
line.delete( ln_07 )
line.delete( ln_08 )
line.delete( ln_09 )
line.delete( ln_10 )
line.delete( ln_11 )
line.delete( ln_12 )
line.delete( ln_13 )
line.delete( ln_14 )
line.delete( ln_15 )
ln_01 := line.new( jp1_open ,b4h20 ,jp1_open ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.gray : colorVL ,styleVL ,widthVL )
ln_02 := line.new( jp_fix ,b4h20 ,jp_fix ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.gray : colorVL ,styleVL ,widthVL )
ln_03 := line.new( jp1_close ,b4h20 ,jp1_close ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.gray : colorVL ,styleVL ,widthVL )
ln_04 := line.new( jp2_open ,b4h20 ,jp2_open ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.gray : colorVL ,styleVL ,widthVL )
ln_05 := line.new( jp2_close ,b4h20 ,jp2_close ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.gray : colorVL ,styleVL ,widthVL )
ln_06 := line.new( hk1_open ,b4h20 ,hk1_open ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.red : colorVL ,styleVL ,widthVL )
ln_07 := line.new( hk1_close ,b4h20 ,hk1_close ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.red : colorVL ,styleVL ,widthVL )
ln_08 := line.new( hk2_open ,b4h20 ,hk2_open ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.red : colorVL ,styleVL ,widthVL )
ln_09 := line.new( hk2_close ,b4h20 ,hk2_close ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.red : colorVL ,styleVL ,widthVL )
ln_10 := line.new( uk_open ,b4h20 ,uk_open ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.blue : colorVL ,styleVL ,widthVL )
ln_11 := line.new( uk_fix ,b4h20 ,uk_fix ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.blue : colorVL ,styleVL ,widthVL )
ln_12 := line.new( uk_close ,b4h20 ,uk_close ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.blue : colorVL ,styleVL ,widthVL )
ln_13 := line.new( ny_open ,b4h20 ,ny_open ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.orange : colorVL ,styleVL ,widthVL )
ln_14 := line.new( us_econ ,b4h20 ,us_econ ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.orange : colorVL ,styleVL ,widthVL )
ln_15 := line.new( ny_close ,b4h20 ,ny_close ,b4l20 ,xloc.bar_time ,extendVL ,_colorVL=="市場ごと" ? color.orange : colorVL ,styleVL ,widthVL )
var is_weekend = false
is_saturday = dayofweek == dayofweek.saturday
is_end_of_sunday = ( dayofweek == dayofweek.sunday and dayofmonth[1] != dayofmonth ) or ( dayofweek[1] == dayofweek.sunday and dayofweek == dayofweek.monday )
if ( is_saturday )
is_weekend := true
if ( is_end_of_sunday )
is_weekend := false
JP = not showVLBG ? color.white : not is_weekend and ( ( jp1_open < time and time < jp1_close ) or ( jp2_open < time and time < jp2_close ) ) ? color.gray : color.white
HK = not showVLBG ? color.white : not is_weekend and ( ( hk1_open < time and time < hk1_close ) or ( hk2_open < time and time < hk2_close ) ) ? color.red : color.white
UK = not showVLBG ? color.white : not is_weekend and uk_open < time and time < uk_close ? color.blue : color.white
NY = not showVLBG ? color.white : not is_weekend and ny_open < time and time < ny_close ? color.orange : color.white
bgcolor( JP ,title="日本" )
bgcolor( HK ,title="香港" )
bgcolor( UK ,title="ロンドン" )
bgcolor( NY ,title="ニューヨーク" )
デバック
plot()
を使って
確認したい値が「数値」の場合は、plot()
でデバックする。なんか上手くいっていないときは、とにかくまずplot()
。
label.new()
を使って
確認したい項目が「文字列」の場合は、label.new()
で確認する。
//@version=4
study("マイスクリプト" ,overlay=true )
dayOpen = security( syminfo.tickerid ,"D" ,open ,lookahead=barmerge.lookahead_on )
is_open = dayOpen[1]!=dayOpen
if( is_open )
label.new( bar_index ,high+tr/2 ,syminfo.timezone ,style=label.style_none )
tostring()
を組み合わせて数値を確認することもできる。
//@version=4
study("マイスクリプト" ,overlay=true )
dayOpen = security( syminfo.tickerid ,"D" ,open ,lookahead=barmerge.lookahead_on )
is_open = dayOpen[1]!=dayOpen
if( is_open )
label.new( bar_index ,high+tr/2 ,tostrign( open ) ,style=label.style_none )
タカハシ / 8年目の兼業トレーダー
元・日本料理の板前。現在は、投資やプログラミング、動画コンテンツの撮影・制作・編集などを。更新のお知らせは、各SNSやLINEで。LINEだと1対1でお話することもできます!
- 記事をシェア