移動平均線のパネル表示@TradingView
目次
概要
移動平均線の表示(6種)と移動平均の価格を高値順にパネル表示を行うスクリプトです。
作成意図
- たくさん出しすぎて色が分からくなるので少しでも関係性(順番)が見えるように。
- 急いで指値注文等をする際にぱっと数字が分かるように。
機能
- 移動平均は6種input入力で登録可能
- 移動平均の種類は"sma""ema""wma"の3種類から選択
- ラベルを用いて各移動平均の価格を表示する
- 現在位置(”--now--”)と6種の移動平均(計7個)を価格順で並べ替え(バブルソート)
- ラベルの各種設定が可能
- 色をtradingviewの標準色から選択可能
- 横位置は現在の前後で設定可能(ただし分足の場合は機能しない)
- 縦位置は指定期間での高値に表示
工夫点
- パネルの表示計算は”barstate.islast”を条件とし、最終足のみで計算とし無駄カット
- ただし上記の場合"highest( high, 20)"の計算が現在足でしか計算しなかったためif文の外にだした。(これには結構ハマった)
- パネルの色表示を標準色から選べる様にした
改善点
- 分足を使用した際に未来足の位置に配置する方法が不明
- 移動平均に合わせてパネル内の文字色も変更したいが、inputの色を取得する方法がない。(と思う)
設定項目
コード
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © kabuist001 //@version=4 study("WR_Moving Average Panel", shorttitle="WR_MV_Panel", overlay=true ) // // MA 移動平均 // MA_type = input(defval="sma", title="MAtype" , type=input.string,options=["sma","ema","wma"]) len1 = input(5, title="Length_1") len2 = input(10, title="Length_2") len3 = input(25, title="Length_3") len4 = input(75, title="Length_4") len5 = input(100, title="Length_5") len6 = input(200, title="Length_6") ma1 = sma(close ,len1) ma2 = sma(close ,len2) ma3 = sma(close ,len3) ma4 = sma(close ,len4) ma5 = sma(close ,len5) ma6 = sma(close ,len6) if MA_type=="ema" ma1 := ema(close ,len1) ma2 := ema(close ,len2) ma3 := ema(close ,len3) ma4 := ema(close ,len4) ma5 := ema(close ,len5) ma6 := ema(close ,len6) else if MA_type=="wma" ma1 := wma(close ,len1) ma2 := wma(close ,len2) ma3 := wma(close ,len3) ma4 := wma(close ,len4) ma5 := wma(close ,len5) ma6 := wma(close ,len6) plot(ma1, color=color.red, linewidth=2, title="Length1平均線") plot(ma2, color=color.yellow, linewidth=2, title="Length2平均線") plot(ma3, color=color.blue, linewidth=2, title="Length3平均線") plot(ma4, color=color.purple, linewidth=2, title="Length4平均線") plot(ma5, color=color.lime, linewidth=2, title="Length5平均線") plot(ma6, color=color.green, linewidth=2, title="Length6平均線") // // パネル作成 // info_on = input(type = input.bool, title = "infoパネルon-off", defval=true) info_x = input(type = input.integer, title = "infoパネル横位置(標準:5本先)", defval = 5) info_y = input(type = input.integer, title = "infoパネル縦位置(標準:20足中の高値)", defval = 20) user_color = input(title="infoパネル色", defval="lime", options=["white", "black", "aqua", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "orange", "purple", "red", "silver", "teal", "yellow"]) var Panel_color = user_color == "aqua" ? color.new(color.aqua,30) : user_color == "black" ? color.new(color.black,30) : user_color == "blue" ? color.new(color.blue,30) : user_color == "fuchsia" ? color.new(color.fuchsia,30) : user_color == "gray" ? color.new(color.gray,30) : user_color == "green" ? color.new(color.green,30) : user_color == "lime" ? color.new(color.lime,30) : user_color == "maroon" ? color.new(color.maroon,30) : user_color == "navy" ? color.new(color.navy,30) : user_color == "olive" ? color.new(color.olive,30) : user_color == "orange" ? color.new(color.orange,30) : user_color == "purple" ? color.new(color.purple,30) : user_color == "red" ? color.new(color.red,30) : user_color == "silver" ? color.new(color.silver,30) : user_color == "teal" ? color.new(color.teal,30) : user_color == "white" ? color.new(color.white,30) : user_color == "yellow" ? color.new(color.yellow,30) : color.new(color.black,30) //パネルの表示高さ yloc_value=highest(high,info_y) //最終足のみで並べ替え実施。 if barstate.islast //初期設定 no1=ma1 no2=ma2 no3=ma3 no4=ma4 no5=ma5 no6=ma6 no7=close temp=0.0 // no1決定 if no7 > no6 temp:=no6 no6:=no7 no7:=temp if no6 > no5 temp:=no5 no5:=no6 no6:=temp if no5 > no4 temp:=no4 no4:=no5 no5:=temp if no4 > no3 temp:=no3 no3:=no4 no4:=temp if no3 > no2 temp:=no2 no2:=no3 no3:=temp if no2 > no1 temp:=no1 no1:=no2 no2:=temp //no2 if no7 > no6 temp:=no6 no6:=no7 no7:=temp if no6 > no5 temp:=no5 no5:=no6 no6:=temp if no5 > no4 temp:=no4 no4:=no5 no5:=temp if no4 > no3 temp:=no3 no3:=no4 no4:=temp if no3 > no2 temp:=no2 no2:=no3 no3:=temp //no3 if no7 > no6 temp:=no6 no6:=no7 no7:=temp if no6 > no5 temp:=no5 no5:=no6 no6:=temp if no5 > no4 temp:=no4 no4:=no5 no5:=temp if no4 > no3 temp:=no3 no3:=no4 no4:=temp //no4 if no7 > no6 temp:=no6 no6:=no7 no7:=temp if no6 > no5 temp:=no5 no5:=no6 no6:=temp if no5 > no4 temp:=no4 no4:=no5 no5:=temp //no5 if no7 > no6 temp:=no6 no6:=no7 no7:=temp if no6 > no5 temp:=no5 no5:=no6 no6:=temp // no6-no7 if no7 > no6 temp:=no6 no6:=no7 no7:=temp no1_name= no1 == ma1 ? tostring(round(len1))+"MA : "+tostring(round(no1)) :no1 == ma2 ? tostring(round(len2))+"MA : "+tostring(round(no1)) : no1 == ma3 ? tostring(round(len3))+"MA : "+tostring(round(no1)) : no1 == ma4 ? tostring(round(len4))+"MA : "+tostring(round(no1)) : no1 == ma5 ? tostring(round(len5))+"MA : "+tostring(round(no1)) : no1 == ma6 ? tostring(round(len6))+"MA : "+tostring(round(no1)) : no1 == close ? "--now--" : "?" no2_name= no2 == ma1 ? tostring(round(len1))+"MA : "+tostring(round(no2)) :no2 == ma2 ? tostring(round(len2))+"MA : "+tostring(round(no2)) : no2 == ma3 ? tostring(round(len3))+"MA : "+tostring(round(no2)) : no2 == ma4 ? tostring(round(len4))+"MA : "+tostring(round(no2)) : no2 == ma5 ? tostring(round(len5))+"MA : "+tostring(round(no2)) : no2 == ma6 ? tostring(round(len6))+"MA : "+tostring(round(no2)) : no2 == close ? "--now--" : "?" no3_name= no3 == ma1 ? tostring(round(len1))+"MA : "+tostring(round(no3)) :no3 == ma2 ? tostring(round(len2))+"MA : "+tostring(round(no3)) : no3 == ma3 ? tostring(round(len3))+"MA : "+tostring(round(no3)) : no3 == ma4 ? tostring(round(len4))+"MA : "+tostring(round(no3)) : no3 == ma5 ? tostring(round(len5))+"MA : "+tostring(round(no3)) : no3 == ma6 ? tostring(round(len6))+"MA : "+tostring(round(no3)) : no3 == close ? "--now--" : "?" no4_name= no4 == ma1 ? tostring(round(len1))+"MA : "+tostring(round(no4)) :no4 == ma2 ? tostring(round(len2))+"MA : "+tostring(round(no4)) : no4 == ma3 ? tostring(round(len3))+"MA : "+tostring(round(no4)) : no4 == ma4 ? tostring(round(len4))+"MA : "+tostring(round(no4)) : no4 == ma5 ? tostring(round(len5))+"MA : "+tostring(round(no4)) : no4 == ma6 ? tostring(round(len6))+"MA : "+tostring(round(no4)) : no4 == close ? "--now--" : "?" no5_name= no5 == ma1 ? tostring(round(len1))+"MA : "+tostring(round(no5)) :no5 == ma2 ? tostring(round(len2))+"MA : "+tostring(round(no5)) : no5 == ma3 ? tostring(round(len3))+"MA : "+tostring(round(no5)) : no5 == ma4 ? tostring(round(len4))+"MA : "+tostring(round(no5)) : no5 == ma5 ? tostring(round(len5))+"MA : "+tostring(round(no5)) : no5 == ma6 ? tostring(round(len6))+"MA : "+tostring(round(no5)) : no5 == close ? "--now--" : "?" no6_name= no6 == ma1 ? tostring(round(len1))+"MA : "+tostring(round(no6)) :no6 == ma2 ? tostring(round(len2))+"MA : "+tostring(round(no6)) : no6 == ma3 ? tostring(round(len3))+"MA : "+tostring(round(no6)) : no6 == ma4 ? tostring(round(len4))+"MA : "+tostring(round(no6)) : no6 == ma5 ? tostring(round(len5))+"MA : "+tostring(round(no6)) : no6 == ma6 ? tostring(round(len6))+"MA : "+tostring(round(no6)) : no6 == close ? "--now--" : "?" no7_name= no7 == ma1 ? tostring(round(len1))+"MA : "+tostring(round(no7)) :no7 == ma2 ? tostring(round(len2))+"MA : "+tostring(round(no7)) : no7 == ma3 ? tostring(round(len3))+"MA : "+tostring(round(no7)) : no7 == ma4 ? tostring(round(len4))+"MA : "+tostring(round(no7)) : no7 == ma5 ? tostring(round(len5))+"MA : "+tostring(round(no7)) : no7 == ma6 ? tostring(round(len6))+"MA : "+tostring(round(no7)) : no7 == close ? "--now--" : "?" dt = time - time[1] var label l = na label.delete(l) if info_on==true l := label.new( time+info_x*dt, yloc_value, xloc=xloc.bar_time, yloc=yloc.price, style=label.style_labeldown, color=Panel_color,textcolor=color.white, text="MV Panel\n" +no1_name+"\n" +no2_name+"\n" +no3_name+"\n" +no4_name+"\n" +no5_name+"\n" +no6_name+"\n" +no7_name )