見出し画像

上位足 MA を一括表示する TradingView インジケータ(Tocorin_MTF_MA)のメモ


1.はじめに


皆さん、チャート見る時に下位足を見ているとうっかり上位足の大事なポイントを見失うことはないですか?

MTF(マルチタイムフレーム)で物事を見ようとしていてもうっかり忘れる… そんなことを防ぐために、TradingView で上位足の MA を同時表示するインジケータを作成しました。(⇒ インジケータリンク
さくっと作ったものなのでそのまま公開してみました。変なところがあったらゴメンナサイ。

あと、TradingViewで類似のインジケータが見つからなかったので作ったのですが、同様のインジで公開済のものがあると非公開になるようです。もし非公開になったら誰かのインジが既にあるということなので探してください。

2.機能説明

基本機能としては表示中の足の MA (SMA もしくは EMA)を 7種類表示できるものとなっています。それに加えて、限定した条件の中で MTF の MA を同時に表示します。

※ 同時に計算できる MA の本数制限やプログラム・設定の都合上、以下の足のみを表示しているとき、上位足の MA 表示を行うインジケータになっています。

 5分足表示時  ⇒  1時間足 と 4時間足 と 日足
 1時間足表示時 ⇒  4時間足 と 日足
 4時間足表示時 ⇒  日足

ソースコードに全て書いてありますが、表示中の足で表示する 7本分の MA の初期設定は(10 - 25 - 50 - 75 - 100 - 200 - 900)となっています。上位足の MA 表示対象となるのは 7本分の MA のうち 2番目、4番目、5番目 の 3種類のみです。デフォルト設定の場合、25MA、75MA、100MA、ですね。

また、上位足の表示について、以下の足は下位足のデフォルト設定と重複するために非表示になっています。
 1時間足 75本 (= 5分足 900本 = 4500分 なので)
 4時間足25本  (= 1時間足 100本 = 100時間 なので)
これは、MA設定を変えると変わってくる部分のため必要な場合は表示設定を行ってください。

この記事の最後には、ソースコードを貼っておきます。違うMAを表示したい、表示中の足で参照している期間が気に食わない、等あったら適宜変更してみてください。

3.画面イメージ

キャプチャ

このインジケータの画面イメージを説明しておきます。上の画面は5分足画面を表示した時に各移動平均線がどのように描画されているのかを吹き出しで補足してあります。

他インジを同時表示していると何かとチャートが見にくくなるので、このインジの基本設定では移動平均線の描画を「線」ではなく「ギザギザな線」と「ドット」で描くようにしています。線は多いけれど、、思ったよりも見やすいと思いませんか?

もちろん、インジケータのオプションで線のスタイルも変更可能なので気に入らなかったら変更してください。

また、移動平均の計算元の時間足単位でギザギザ、ドットが描画されるので、どこからどこまでが 1h, 4h, 1D なのか、というのが一目でわかる、、のが地味に便利だと気付きました。(黄色い四角が描画対象の時間足 1本分)

参考までに、1時間足、4時間足表示時のサンプルも載せておきます。


まずは1時間足。
 細かなギザギザ: 1h MA
 短いドット: 4h MA
 長いドット: 1D MA  です。

キャプチャ


次に 4時間足
 細かなギザギザ: 4h MA
 短いドット: 1D MA です。

キャプチャ

4.設定画面イメージ

ここで、設定画面の説明もしておきます。特に難しい設定はないのですが、以下のような設定が可能です。

パラメータ画面:
 MA Type ⇒ MAの種類(SMA/EMA)を指定
 Period1~7 ⇒ 対象時間足何本分で MA を計算するか指定
 Time Frame 〇〇 (is display?)
  ⇒ MA を表示するか
    (A1-A7 は表示中の時間足、B2/4/5 は一つ上位、
     C2/4/5 は二つ上位、D2/4/5 は三つ上位の MA表示設定)

スタイル画面:
 各 MA の表示色、描画スタイルを指定

キャプチャ

5.ソースコード(Pineスクリプト)

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tocorin

//@version=4


study(title="Tocorin_MTF_MA", shorttitle="Tocorin_MTF_MA", overlay=true)
emaOrSma = input(title="MA Type", defval="SMA", options=["SMA","EMA"])

getSma(timeFrame, period) =>
   retsma = security(syminfo.tickerid, timeFrame, sma(close, period))

getEma(timeFrame, period) =>
   retsma = security(syminfo.tickerid, timeFrame, ema(close, period))

getMa(maType, timeFrame, period) =>
   retMa = maType!="SMA" ? getEma(timeFrame, period) : getSma(timeFrame, period)


is5m = if timeframe.period == '5'
   true
else
   false

is60m = if timeframe.period == '60'
   true
else
   false

is240m = if timeframe.period == '240'
   true
else
   false

timeframe_A = timeframe.period
timeframe_B = '60'
timeframe_C = '240'
timeframe_D = 'D'

maPeriod_A_1 = input(title="period 1", type=input.integer, defval=10, minval=1, maxval=10000)
maPeriod_A_2 = input(title="period 2", type=input.integer, defval=25, minval=1, maxval=10000)
maPeriod_A_3 = input(title="period 3", type=input.integer, defval=50, minval=1, maxval=10000)
maPeriod_A_4 = input(title="period 4", type=input.integer, defval=75, minval=1, maxval=10000)
maPeriod_A_5 = input(title="period 5", type=input.integer, defval=100, minval=1, maxval=10000)
maPeriod_A_6 = input(title="period 6", type=input.integer, defval=200, minval=1, maxval=10000)
maPeriod_A_7 = input(title="period 7", type=input.integer, defval=900, minval=1, maxval=10000)

ma_A_1 = getMa(emaOrSma, timeframe_A, maPeriod_A_1)
ma_A_2 = getMa(emaOrSma, timeframe_A, maPeriod_A_2)
ma_A_3 = getMa(emaOrSma, timeframe_A, maPeriod_A_3)
ma_A_4 = getMa(emaOrSma, timeframe_A, maPeriod_A_4)
ma_A_5 = getMa(emaOrSma, timeframe_A, maPeriod_A_5)
ma_A_6 = getMa(emaOrSma, timeframe_A, maPeriod_A_6)
ma_A_7 = getMa(emaOrSma, timeframe_A, maPeriod_A_7)
ma_B_2 = getMa(emaOrSma, timeframe_B, maPeriod_A_2)
ma_B_4 = getMa(emaOrSma, timeframe_B, maPeriod_A_4)
ma_B_5 = getMa(emaOrSma, timeframe_B, maPeriod_A_5)
ma_C_2 = getMa(emaOrSma, timeframe_C, maPeriod_A_2)
ma_C_4 = getMa(emaOrSma, timeframe_C, maPeriod_A_4)
ma_C_5 = getMa(emaOrSma, timeframe_C, maPeriod_A_5)
ma_D_2 = getMa(emaOrSma, timeframe_D, maPeriod_A_2)
ma_D_4 = getMa(emaOrSma, timeframe_D, maPeriod_A_4)
ma_D_5 = getMa(emaOrSma, timeframe_D, maPeriod_A_5)

isDisplayma_A_1 = input(type=input.bool, title = "Time Frame A1 (is display?)", defval = true)
isDisplayma_A_2 = input(type=input.bool, title = "Time Frame A2 (is display?)", defval = true)
isDisplayma_A_3 = input(type=input.bool, title = "Time Frame A3 (is display?)", defval = true)
isDisplayma_A_4 = input(type=input.bool, title = "Time Frame A4 (is display?)", defval = true)
isDisplayma_A_5 = input(type=input.bool, title = "Time Frame A5 (is display?)", defval = true)
isDisplayma_A_6 = input(type=input.bool, title = "Time Frame A6 (is display?)", defval = true)
isDisplayma_A_7 = input(type=input.bool, title = "Time Frame A6 (is display?)", defval = true)
isDisplayma_B_2 = input(type=input.bool, title = "Time Frame B2 (is display?)", defval = true)
isDisplayma_B_4 = input(type=input.bool, title = "Time Frame B4 (is display?)", defval = false)
isDisplayma_B_5 = input(type=input.bool, title = "Time Frame B5 (is display?)", defval = true)
isDisplayma_C_2 = input(type=input.bool, title = "Time Frame C2 (is display?)", defval = false)
isDisplayma_C_4 = input(type=input.bool, title = "Time Frame C4 (is display?)", defval = true)
isDisplayma_C_5 = input(type=input.bool, title = "Time Frame C5 (is display?)", defval = true)
isDisplayma_D_2 = input(type=input.bool, title = "Time Frame D2 (is display?)", defval = true)
isDisplayma_D_4 = input(type=input.bool, title = "Time Frame D4 (is display?)", defval = true)
isDisplayma_D_5 = input(type=input.bool, title = "Time Frame D5 (is display?)", defval = true)

plot(isDisplayma_A_1 ? ma_A_1: na, title="A1", color=color.orange, linewidth=1, style=plot.style_stepline)
plot(isDisplayma_A_2 ? ma_A_2: na, title="A2", color=color.green, linewidth=1, style=plot.style_stepline)
plot(isDisplayma_A_3 ? ma_A_3: na, title="A3", color=color.aqua, linewidth=1, style=plot.style_stepline)
plot(isDisplayma_A_4 ? ma_A_4: na, title="A4", color=color.purple, linewidth=1, style=plot.style_stepline)
plot(isDisplayma_A_5 ? ma_A_5: na, title="A5", color=color.blue, linewidth=1, style=plot.style_stepline)
plot(isDisplayma_A_6 ? ma_A_6: na, title="A6", color=color.gray, linewidth=1, style=plot.style_stepline)
plot(isDisplayma_A_7 ? ma_A_7: na, title="A7", color=color.silver, linewidth=1, style=plot.style_stepline)

plot(is5m and isDisplayma_B_2 ? ma_B_2: na, title="B2", color=color.green, linewidth=1, style=plot.style_stepline)
plot(is5m and isDisplayma_B_4 ? ma_B_4: na, title="B4", color=color.purple, linewidth=1, style=plot.style_stepline)
plot(is5m and isDisplayma_B_5 ? ma_B_5: na, title="B5", color=color.blue, linewidth=1, style=plot.style_stepline)

plot((is5m or is60m) and isDisplayma_C_2 ? ma_C_2: na, title="C2", color=color.green, linewidth=1, style=plot.style_circles)
plot((is5m or is60m) and isDisplayma_C_4 ? ma_C_4: na, title="C4", color=color.purple, linewidth=1, style=plot.style_circles)
plot((is5m or is60m) and isDisplayma_C_5 ? ma_C_5: na, title="C5", color=color.blue, linewidth=1, style=plot.style_circles)

plot((is5m or is60m or is240m) and isDisplayma_D_2 ? ma_D_2: na, title="D2", color=color.red, linewidth=1, style=plot.style_circles)
plot((is5m or is60m or is240m) and isDisplayma_D_4 ? ma_D_4: na, title="D4", color=color.red, linewidth=1, style=plot.style_circles)
plot((is5m or is60m or is240m) and isDisplayma_D_5 ? ma_D_5: na, title="D5", color=color.red, linewidth=1, style=plot.style_circles)


この記事が気に入ったらサポートをしてみませんか?