見出し画像

RSI Re:Born Value ver1.0本体

//RSI Re:born ver1.0 2024.3.25 @issei0008


//@version=5


indicator(title="RSI Re:Born Value ver1.0", shorttitle="RSI_Re:born", format=format.price,overlay=true, precision=2)//, timeframe="", timeframe_gaps=true

table_view=input.string(defval="右下",title="表示位置",options=["表示なし","右上","右下","左下"])

IptPeriod_table = input.timeframe(defval="1",title="MT テーブル",

  tooltip = '右端にマルチタイムで表示したいRSIを指定)// マルチタイムフレームのデータを取得')


ma(source, length, type) =>

    switch type

        "SMA" => ta.sma(source, length)

        "EMA" => ta.ema(source, length)

        "SMMA (RMA)" =>ta.rma(source, length)

        "WMA" => ta.wma(source, length)

        "VWMA" => ta.vwma(source, length)


rsiSource = input.source(close,"Source",  group="RSI Settings")

rsi_show = input.bool(false, title="",inline="rsi",  group="RSI Settings")

rsiLength = input.int(14, minval=1, title="RSI Length", group="RSI Settings",inline="rsi",

  tooltip = '')

rsi_color=input.color(color.new(color.blue, 20), title="", group="RSI Settings",inline="rsi")

RSIValue2 = input.float(title="RSI 買われ過ぎ警戒ライン", defval=50.0 , minval=50.0, maxval=100 )  // RSI value below which the alarm will trigger

RSIValue1 = input.float(title="RSI 売られ過ぎ警戒ライン", defval=50.0 , minval=1.0, maxval=50,

  tooltip = '集計用の平均もしくは偏差σのRSIを記入するといいかも' + "\n" + "50にしてると集計用標準偏差が割り当てられるよ" + "\n" +

  "IPOとかは集計取れないから、警戒ラインと集計回数を手打ちしてね")


maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")


ma_show = input.bool(true, title="", group="MA Settings",inline="101")

maLength = input.int(10, title="RSI-MA", group="MA Settings",inline="101")

up_color=input.color(color.new(color.blue, 20), title="UP", group="MA Settings",inline="102")

down_color=input.color(color.new(color.blue, 20), title="DOWN", group="MA Settings",inline="102")

center_color=input.color(color.new(color.orange, 20), title="Center", group="MA Settings",inline="102")


over = input(true, title="Over Buy or Sell", inline = "201" )

bullColor = input.color(title="買われ過ぎ ",defval=color.new(color.yellow, 0), inline = "205" )

bearColor = input.color(title="売られ過ぎ",defval=color.new(color.yellow, 0), inline = "205")



aggreg = input.int(20, title="集計回数", group="集計用",inline="301",tooltip = '標準の20だとσ3以上の98%の精度は出せません')

up_threshold = input.float(title="集計用閾値 上値", defval=70.0 , minval=50.0, maxval=100 ,inline="302")  // RSI value below which the alarm will trigger

down_threshold = input.float(title="下値", defval=30.0 , minval=1.0, maxval=50,inline="302")

bb_input = input.float(1.0, minval=0.1, maxval=4, title="標準偏差",tooltip = "偏差 のσ 上値+σ 下値-σ" )


//アラート用

alert_view = input.bool(true,title="RSI アラート用ライン",inline="401", group="アラート ライン")

alert_act = input.bool(true,title="アラート オン",inline="401", group="アラート ライン")

alertRSIValue = input.float(title="買い、売りとのRSI差", defval=1.5 , minval=0.5, maxval=50,step=0.5,inline="402",tooltip = 'RSI 買われ過ぎが75、差が1.5のとき、73.5でアラート')

alert_up_color=input.color(color.new(color.gray, 20), title="UP", inline="102")

alert_down_color=input.color(color.new(color.gray, 20), title="DOWN", inline="102")



var float[] up_array = array.new_float(aggreg,0.0)

var float[] down_array = array.new_float(aggreg,0.0)


// テーブル表示

var view_position = position.bottom_right

if table_view == "右上"

    view_position:=position.top_right

else if table_view == "右下"

    view_position:=position.bottom_right

else if table_view == "左下"

    view_position:=position.bottom_left

else

    table_view:=""


up = ta.rma(math.max(ta.change(rsiSource), 0), rsiLength)

down = ta.rma(-math.min(ta.change(rsiSource), 0), rsiLength)

rsi =  100 - (100 / (1 + up / down))




// 集計用

if rsi[1] > up_threshold and rsi[2] < rsi[1] and rsi[1] > rsi

    array.insert(up_array, 0, rsi[1])

    array.remove(up_array, aggreg)

if rsi[1] < down_threshold and rsi[2] > rsi[1] and rsi[1] < rsi

    array.insert(down_array, 0, rsi[1])

    array.remove(down_array, aggreg)


// 平均

float up_ave = 0.0

float down_ave = 0.0

float up_div = 0.0

float down_div = 0.0


if array.get(up_array, aggreg - 1) > 0.0

    up_ave := array.avg(up_array)

    up_div := up_ave + array.stdev(up_array) * bb_input

if array.get(down_array, aggreg - 1) > 0.0

    down_ave := array.avg(down_array)

    down_div := down_ave - array.stdev(down_array) * bb_input


RSIValue2 := RSIValue2 == 50 ? up_div :RSIValue2

RSIValue1 := RSIValue1 == 50 ? down_div :RSIValue1


value_up =  rsi <= RSIValue2 ?

       close + (rsiLength - 1) * (RSIValue2 * down - (100 - RSIValue2) * up) / (100 - RSIValue2) :

       close - (rsiLength - 1) * ((100 - RSIValue2) * up - RSIValue2 * down) / RSIValue2


value_down = rsi >= RSIValue1 ?

       close - (rsiLength - 1) * ((100 - RSIValue1) * up - RSIValue1 * down ) / RSIValue1 :

       close + (rsiLength - 1) * (RSIValue1 * down - (100 - RSIValue1) * up) / (100 - RSIValue1)


// アラート用

float alert_up =0.0

float alert_down =0.0


if alert_view or alert_act

    alert_up :=  rsi <= RSIValue2 - alertRSIValue ?

          close + (rsiLength - 1) * ((RSIValue2 - alertRSIValue) * down - (100 - (RSIValue2 - alertRSIValue)) * up) / (100 - (RSIValue2 - alertRSIValue)) :

          close - (rsiLength - 1) * ((100 - (RSIValue2 - alertRSIValue)) * up - (RSIValue2 - alertRSIValue) * down) / (RSIValue2 - alertRSIValue)


    alert_down := rsi >= RSIValue1 + alertRSIValue ?

          close - (rsiLength - 1) * ((100 - (RSIValue1 + alertRSIValue)) * up - (RSIValue1 + alertRSIValue) * down ) / (RSIValue1 + alertRSIValue) :

          close + (rsiLength - 1) * ((RSIValue1 + alertRSIValue) * down - (100 - (RSIValue1 + alertRSIValue)) * up) / (100 - (RSIValue1 + alertRSIValue))


rsi_ma = (ma(value_up,maLength,maTypeInput) + ma(value_down,maLength,maTypeInput))*0.5

plot(rsi_show ? value_up : na, color=rsi_color,offset = 1)

plot(rsi_show ? value_down : na, color=rsi_color,offset = 1)

value_up_ma = plot(ma_show ? ma(value_up,maLength,maTypeInput) : na, color=high > ma(value_up,maLength,maTypeInput) and over ? bullColor : up_color,offset = 1)

value_down_ma = plot(ma_show ? ma(value_down,maLength,maTypeInput) : na, color=low < ma(value_down,maLength,maTypeInput) and over ? bearColor : down_color,offset = 1)

alert_up_ma = plot(alert_view ? ma(alert_up,maLength,maTypeInput) : na, color=high > ma(alert_up,maLength,maTypeInput) and over ? bullColor : alert_up_color,offset = 1)

alert_down_ma = plot(alert_view ? ma(alert_down,maLength,maTypeInput) : na, color=low < ma(alert_down,maLength,maTypeInput) and over ? bearColor : alert_down_color,offset = 1)


fill(value_up_ma,alert_up_ma,color=high > ma(alert_up,maLength,maTypeInput) and over ? bullColor : alert_up_color)

fill(value_down_ma,alert_down_ma,color=low < ma(alert_down,maLength,maTypeInput) and over ? bearColor : alert_down_color)

plot(ma_show ? rsi_ma : na, color=center_color,offset = 1)


// アラート

alertcondition(high > ma(alert_up,maLength,maTypeInput) and alert_act,  title="RSI Over", message="{{ticker}} {{interval}}分足/ {{time}} " + "\n" + "終値:{{close}} , 出来高:{{volume}} ")

alertcondition(low < ma(alert_down,maLength,maTypeInput) and alert_act,  title="RSI UNDER", message="{{ticker}} {{interval}}分足/ {{time}} " + "\n" + "終値:{{close}} , 出来高:{{volume}} ")


if high > ma(alert_up,maLength,maTypeInput) and alert_act

    alert("RSI Over :" + str.tostring(ma(alert_up,maLength,maTypeInput), "#.#"), alert.freq_once_per_bar)

if low < ma(alert_down,maLength,maTypeInput) and alert_act

    alert("RSI UNDER :" + str.tostring(ma(alert_down,maLength,maTypeInput), "#.#"), alert.freq_once_per_bar)


// ▲▲▲▲ アラート


//右上にマルチタイムの表示

rsi01=request.security(syminfo.tickerid, IptPeriod_table,ta.rsi(rsiSource,rsiLength),barmerge.gaps_on,barmerge.lookahead_on)

if table_view != ""

    var table mainTrending = table.new(view_position, 2, 4, border_width=1)

    table.cell(mainTrending, 0, 0,"RSI : " + str.tostring(rsi, "#.#") + " / " +

      (IptPeriod_table=="1D" ? "1Day":

      IptPeriod_table=="1W"? "1Week":

      IptPeriod_table=="1M" ? "1mon":

      IptPeriod_table=="" ? "チャート": IptPeriod_table +"min" ) + " : "+

      str.tostring(rsi01, "#.#") ,

      text_color=rsi > RSIValue2 ? color.new(color.yellow,  transp = 10) :

      rsi < RSIValue1 ? color.new(color.red,  transp = 10) :

      color.new(color.white,  transp = 30),

      bgcolor=color.new(color.black, 50),

      text_halign=text.align_left,

      text_size=size.small

      )


    table.cell(mainTrending, 0, 1,

      "RSI if " + str.tostring(RSIValue2, "#.#") + " : " + str.tostring(value_up, "#,###.#") + " / if " + str.tostring(RSIValue1, "#.#") + " : " + str.tostring(value_down, "#,###.#"),

      text_color=rsi > RSIValue2 ? color.new(color.yellow,  transp = 10) :

      rsi < RSIValue1 ? color.new(color.red,  transp = 10) :

      color.new(color.white,  transp = 30),

      bgcolor=color.new(color.black, 50),

      text_halign=text.align_left,

      text_size=size.small

      )

   

    table.cell(mainTrending, 0, 2,

      "RSI 上端平均 : " + str.tostring(up_ave, "#.#") + " / 下端平均 " + str.tostring(down_ave, "#.#") ,

      text_color= color.new(color.white,  transp = 30),

      bgcolor=color.new(color.black, 50),

      text_halign=text.align_left,

      text_size=size.small

      )


    table.cell(mainTrending, 0, 3,

      "RSI 上端 " + str.tostring(bb_input, "#.#") + "σ : " + str.tostring(up_div, "#.#") + " / 下端 -" + str.tostring(bb_input, "#.#") + "σ : " + str.tostring(down_div, "#.#") ,

      text_color= color.new(color.white,  transp = 30),

      bgcolor=color.new(color.black, 50),

      text_halign=text.align_left,

      text_size=size.small

      )

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