見出し画像

トレンドサイクルカウンターv0.95インジ本体

// @ issei0008 last 2024-03-24

//@version=5

strategy(title='Trend Cycle Counter  V0.95',shorttitle='Trend_Cycle V0.95',overlay=true, max_bars_back = 3000)


cycle_source = input(title = "Source", defval = close, tooltip = '未使用')


length = input.int(25,"感度足数", minval = 5, maxval = 50 , step=1 , tooltip=

  "直近高値のブレイクスルーを見ています"  + "\n" +

  "float _max = ta.highest(感度足数)"  + "\n" +

  "float _min = ta.lowest(感度足数)"  + "\n" +

  "100 * (high - _max) / (_max - _min)" + "\n" +

  "感度足数内の高値安値の更新" + "\n" +

  "日足の場合は20くらい、週足の場合は13くらいに"

  )

length2 = input.int(600,"max_bars_back", minval = 200, maxval = 3000 , step=10 ,tooltip="上昇下降回数が5-10回くらいになるように、平均取ります。"  + "\n" +

  "最初の端数は削除してますので気持ち長めに"  + "\n" +

  "上下周期が平均周期に近づいたら保ち合い拮抗、上昇または下降に偏ったらトレンド転換、50以上の時に前回値出してます" + "\n" +

  "サイクルは平均値を見るのではなく、偏りを見てください、直近高値をトレンド転換にしてるのでRCIサイクルを見ながら逆指値INでもいいかも"+ "\n" +

  "下降トレンドのサイクルが短くなり、上昇トレンドのサイクルが長くなってきたら上昇トレンド、みたいな"+ "\n" +

  "それに合わせて、三尊や高値更新、ダブルボトムや安値更新を確認して、段々と長期トレンドで確認して!"

  )

bool alart_trend = input(true,title='トレンド転換アラート')

bool show_trend_sigma = input(true,"サイクルσ",inline = 'シグマ' )

trend_sigma = input.int(1,"", minval = 1, maxval = 3,inline = 'シグマ'  , tooltip="トレンドサイクルから標準偏差を求めて、次のサイクル期待値を描画" + "\n" +

  "1σ=平均値±標準偏差=68.3%"  + "\n" +

  "2σ=平均値±標準偏差×2=95.4%"  + "\n" +

  "3σ=平均値±標準偏差×3=99.7%"

  )

trend_sigma_up_color = input.color(defval=color.new(color.teal, 85) ,title="",inline = 'シグマ')

trend_sigma_down_color = input.color(defval=color.new(color.purple, 85),inline = 'シグマ' ,title="" , tooltip="トレンドサイクルから標準偏差を求めて、次のサイクル期待値を描画" + "\n" +

  "1σ=平均値±標準偏差=68.3%"  + "\n" +

  "2σ=平均値±標準偏差×2=95.4%"  + "\n" +

  "3σ=平均値±標準偏差×3=99.7%"

  )


bool show_barcolor = input(false,"barcolor")

bool show_back = input(false,"グラフ詳細")



//   ストラテジーテスター

strategy_on = input(false, title="ストラテジーテスター",tooltip = "売買表示。" + "\n" +

  "サイクルは上昇が強いときは段々と上昇サイクルが増えていき、下降サイクルが短くなっていきます。" + "\n" +

  "できるだけ長いサイクルの初動を狙って!" ,

  group = "  ▽ ▽ ▽ ストラテジーテスター ▽ ▽ ▽ ")

label_view = input(true, title = 'Entry,Exit条件ラベルの描画',group = "  ▽ Entry(新規タイミング) ▽ ")

strategy_ave = input(true, title = '上昇、下降平均以下でのみ買い')

strategy_how_to = input.string("成買い", title = '買い方',

  options = [ "成買い" , "指値HLC" , "終値" ] ,

  tooltip = "成買い:指標が出た次の足で成買い" + "\n" + "指値HLC:指標のHLC(高値安値終値平均)で指値" + "\n" + "終値;指標が出た足の終値で指値")

strategy_trend_ent = input(true, title = 'トレンド転換時点でエントリー' , tooltip="Trend_L がロングエントリー、Trend_Sがショートエントリー ")


rcis_view = input(false, "",inline = 'RCI')

rcis = input.int(9, "RCI 短期線描画",inline = 'RCI')

rcis_color_p = input.color(defval=color.new(color.teal, 20) ,title="",inline = 'RCI')

rcis_color_m = input.color(defval=color.new(color.red, 20) ,title="",inline = 'RCI')

rcis_color = input.color(defval=color.new(color.gray, 15) ,title="",inline = 'RCI')



rcis_ent = input(true, "",inline = 'RCI2')

rcis_int = input.int(defval=75 ,title="RCI短期 Entry基準値",inline = 'RCI2')

src_rci = input(close, "",inline = 'RCI2',

  tooltip = 'RCI短期の基準値割れでIN位置の確認 RCI_L がロングエントリー、RCI_Sがショートエントリー ' + "\n" +

  "トレンド目標に近かったら転換するかも気を付けて")




// EXIT(決済タイミング)

stop_loss = input.string(defval="トレンド転換ポイント", title="逆指値", group = "  ▽ EXIT(決済タイミング) ▽ ",

  options=["トレンド転換ポイント","前の足の高安・安値","10pips","25pips","50pips","100pips"] ,tooltip = 'トレンド転換ですべて強制決済')

rcim_view = input(false, "",inline = 'RCIm')

rcim = input.int(26, "RCI 中期線描画",inline = 'RCIm' )

rcim_color_p = input.color(defval=color.new(color.yellow, 20) ,title="",inline = 'RCIm')

rcim_color_m = input.color(defval=color.new(color.orange, 20) ,title="",inline = 'RCIm')

rcim_color = input.color(defval=color.new(color.gray, 15) ,title="",inline = 'RCIm')

rcim_exit = input(true, "",inline = 'RCIm2')

rcim_int = input.int(defval=65 ,title="RCI中期 Exit基準値" ,inline = 'RCIm2', minval=-100 , maxval=100 ,

  tooltip = 'RCI中期の基準値割れで決済' + "\n" +

  "ゼロラインで決済が標準" + "\n" + "買いポジの反対売買を基準に設定" + "\n" +

  "50の場合は50で買いポジ決済・-50で売りポジ決済"+ "\n" +

  "-50の場合は-50で買いポジ決済・50で売りポジ決済")



macd_exit = input(true, " MACD EXIT",inline = 'macd3')

src_macd = input(close, "MACD entry",inline = 'macd3',

  tooltip = "上昇トレンド中のMACD-DCでロングexit、下降トレンド中のMACD-GCでショートexit")

macd_sig = input.int(defval=9 ,title="MACD Sign",inline = 'macd2')

macd_short = input.int(defval=12 ,title="Short",inline = 'macd2')

macd_long = input.int(defval=26 ,title="Long",inline = 'macd2')



bool alart_vol = input(true,title='出来高急騰アラート',group="_/ _/ _/  ここから出来高 _/ _/ _/")

bool show_stddev = input(true,"出来高偏差値",  inline = 'volome_style1')

data_stddev = input.int(60,"閾値", minval = 0, maxval = 100 , step=1,  inline = 'volome_style1' ,tooltip="出来高の偏差値をグラフ化して、偏差値を閾値以下を0、閾値以上を2乗して顕著にしてます。" + "\n" +

  "平均周期以上で出来高あるときに転換する場合があるかも。日足の場合は閾値を下げないと反応ないかも" + "\n" +

  "ザラ場開始、引けも評価対象なので、開始引けは出来高急騰とは違うので注意" + "\n" +  "\n" +

  "グラフの都合上、出来高急騰はサイクルカウンターの2倍までしか表示されません。実際には急騰してるかも、要確認")  

stddev_color = input(title='Color', defval=color.new(color.blue, 25),  inline = 'volome_style1')

bool session = input(false,"セッション開始足の出来高を含めない",  inline = 'volome_style-1',tooltip="***未実装 ***この場合のセッションは、ローソク足の前後で時間が連続していないこと" + "\n" +

  "セッション開始足を含めない場合、第2ローソク足の出来高を開始足の出来高とします"

  )

bool session2 = input(false,"下の時間もセッション開始として、開始足の出来高を含めない", tooltip="先物のセッション開始が8:45、16:30としたら、9:00もセッション開始とする場合")

session2_h = input.int(9,"セッション足 □時 □□分",   inline = 'volome_style-2-1')

session2_m = input.int(0,"時  ",   inline = 'volome_style-2-1')

session3_h = input.int(9,"セッション足 □時 □□分",   inline = 'volome_style-2-2')

session3_m = input.int(0,"時  ",   inline = 'volome_style-2-2')


// ・・ ・・ 関数定義

f_ph(_length) =>

    float _max = ta.highest(_length)

    float _min = ta.lowest(_length)

    100 * (high - _max) / (_max - _min)


f_pl(_length) =>

    float _max = ta.highest(_length)

    float _min = ta.lowest(_length)

    100 * (low - _max) / (_max - _min)


ord(seq, idx, itv) =>

    p = seq[idx]

    o = 1

    s = 0

    for i = 0 to itv - 1

        if i != idx

            if p < seq[i]

                o := o + 1

            else

                if p == seq[i]

                    s := s + 1

                    o + (s - 1) / 2.0

    o


d(itv) =>

    sum = 0.0

    for i = 0 to itv - 1

        sum := sum + math.pow((i + 1) - ord(src_rci, i, itv), 2)

    sum


rci(itv) => (1.0 - 6.0 * d(itv) / (itv * (itv * itv - 1.0))) * 100.0


float ph = f_ph(length)

float pl = f_pl(length)

float avg = math.avg(ph, pl)

float mean = ta.cum(avg) / (bar_index + 1)

var int cycle_counter = 0

cycle_counter := nz(cycle_counter) + 1

var int cycle_trend = -1


var int count_one = 0 // 最初の波数を削除 array.shift

var int count_zero = 0 // 最初の波数を削除 array.shift

var int cycle_count = 0

var int cycle_count_p = 0

var int cycle_count_m = 0

var int[] cycles = array.new_int(0)

var int[] cycles_p = array.new_int(0)

var int[] cycles_m = array.new_int(0)

var int[] vol_stddev = array.new_int(0)

var int[] slice_vol_stddev = array.new_int(0)

var int[] trend_sigma_a = array.new_int(0)

var float[] highest = array.new_float(0)

var float[] lowest = array.new_float(0)

float highest_high = ta.highest(length)

float lowest_low = ta.lowest(length)

float stop_loss_hh = na

float stop_loss_ll = na

float limit_loss_hh = na

float limit_loss_ll = na

string loss_text = na

if stop_loss =="トレンド転換ポイント"

    stop_loss_hh := highest_high

    stop_loss_ll := lowest_low

    loss_text := "stoploss"

else if stop_loss =="前の足の高安・安値"

    stop_loss_hh := high[1]

    stop_loss_ll := low[1]

    loss_text := "stoploss"

else if stop_loss =="10pips"

    limit_loss_hh := 10

    limit_loss_ll := -10

    loss_text := "limit_loss10"

else if stop_loss =="25pips"

    limit_loss_hh := 25

    limit_loss_ll := -25

    loss_text := "limit_loss25"

else if stop_loss =="50pips"

    limit_loss_hh := 50

    limit_loss_ll := -50

    loss_text := "limit_loss50"

else if stop_loss =="100pips"

    limit_loss_hh := 100

    limit_loss_ll := -100

    loss_text := "limit_loss100"


array.push(highest,high)

array.push(lowest,low)


bar=last_bar_index-length2    // 最後の足から500手前

var label cycle_label = na  // cycle_label をラベル型の変数として宣言


if bar_index == bar

    cycle_counter := 0


// セッション開始時刻確認用

int timeframe_period=-1

if timeframe.period == "1"

    timeframe_period := 1

else if timeframe.period == "3"

    timeframe_period := 3

else if timeframe.period == "5"

    timeframe_period := 5

else if timeframe.period == "15"

    timeframe_period := 15

else if timeframe.period == "30"

    timeframe_period := 30

else if timeframe.period == "45"

    timeframe_period := 45

else if timeframe.period == "60"

    timeframe_period := 60

else

    timeframe_period := -1

v=0


var max_bar = 0

var max_high = 0.0

var str_max_high = ""

var min_bar = 0

var min_low = 99999999.9

var str_min_low = ""


if bar_index > bar

    if cycle_trend > 0

        if high > max_high

            max_high := high

            max_bar := bar_index

            str_max_high := high >= 10000 ? str.tostring(max_high, "#,###") : high >= 1000 ? str.tostring(max_high, "#,###.#") : str.tostring(max_high, "###.##")

    else

        if low < min_low

            min_low := low

            min_bar := bar_index

            str_min_low := low >= 10000 ? str.tostring(min_low, "#,###") : low >= 1000 ? str.tostring(min_low, "#,###.#") : str.tostring(min_low, "###.##")


if cycle_trend < 0 and ph >= 0.0 and bar_index > bar

    if show_back

        label.new(bar_index, 0.0, '', color=color.green, style=label.style_arrowdown)

    if  cycle_counter > int(length/2)

        label.new(bar_index[1], cycle_counter, text=str.tostring(cycle_counter) , color=color.maroon, style=label.style_none ,textcolor=color.red)

        label.new(bar_index[bar_index - min_bar], cycle_counter -bar_index + min_bar, text=str_min_low , color=color.maroon, style=label.style_arrowdown, size = size.small  ,textcolor=color.red)

        label.new(bar_index[bar_index - min_bar], cycle_counter -bar_index + min_bar + 20, text="min:" + str.tostring(cycle_counter -bar_index + min_bar) ,

          color=color.maroon, style=label.style_none, size = size.small  ,textcolor=color.red)

        min_low := 99999999.9


    if array.size(cycles_m) > 2 and cycle_counter > 50

        label.new(bar_index[1], cycle_counter+30, text="(前回:" + str.tostring(array.get(cycles_m,array.size(cycles_m)-1)) + ")", color=color.red, style=label.style_none,  textcolor=color.red)

    array.push(cycles, cycle_counter)

    array.push(cycles_m, cycle_counter)

    if session and timeframe_period > 0 and time[1] - time[2]  > timeframe_period * 60 * 1000 and volume[1] > volume

        array.pop(vol_stddev)

        array.push(vol_stddev, int(volume))

        array.push(vol_stddev, int(volume))

        v:=(time[0] - time[1] ) /( timeframe_period * 60 * 1000)

    else

        array.push(vol_stddev, int(volume))

        v:=(time[0] - time[1] ) /( timeframe_period * 60 * 1000)

    array.push(trend_sigma_a, cycle_counter)

    cycle_counter := 0

    array.clear(highest)

    array.clear(lowest)

    cycle_trend := +1

    cycle_count_p  += 1

    cycle_count += 1

    if count_one == 0

        count_one := 1

    if alart_trend

        alert("Trend Cycle Counter トレンド上昇 :" + str.tostring(close, "#.#"), alert.freq_once_per_bar)

    cycle_count

   

if cycle_trend > 0 and pl <= -100.0 and bar_index > bar

    if show_back

        label.new(bar_index, -100.0, '', color=color.maroon, style=label.style_arrowup)

    if  cycle_counter > int(length/2)

        label.new(bar_index[1], cycle_counter, text=str.tostring(cycle_counter), color=color.green, style=label.style_none,  textcolor=color.green)

        label.new(bar_index[bar_index - max_bar], cycle_counter -bar_index + max_bar, text=str_max_high , color=color.green, style=label.style_arrowdown, size = size.small  ,textcolor=color.green)

        label.new(bar_index[bar_index - max_bar], cycle_counter -bar_index + max_bar + 20, text="max:" + str.tostring(cycle_counter -bar_index + max_bar) ,

          color=color.green, style=label.style_none, size = size.small  ,textcolor=color.green)

        max_high := 0


    if array.size(cycles_p) > 2  and cycle_counter > 50

        label.new(bar_index[1], cycle_counter+30, text="(前回:" + str.tostring(array.get(cycles_p,array.size(cycles_p)-1)) + ")", color=color.green, style=label.style_none,  textcolor=color.green)

    array.push(cycles, cycle_counter)

    array.push(cycles_p, cycle_counter)

    if session and timeframe_period > 0 and time[1] - time[2]  > timeframe_period * 60 * 1000 and volume[1] > volume

        array.pop(vol_stddev)

        array.push(vol_stddev, int(volume))

        array.push(vol_stddev, int(volume))

        //v:=(time[0] - time[1] ) /( timeframe_period * 60 * 1000)

    else

        array.push(vol_stddev, int(volume))

        //v:=(time[0] - time[1] ) /( timeframe_period * 60 * 1000)

    array.push(trend_sigma_a, cycle_counter)

    cycle_counter := 0

    array.clear(highest)

    array.clear(lowest)

    cycle_trend := -1

    cycle_count_m  += 1

    cycle_count += 1

    if count_one == 0

        count_one := -1

    if alart_trend

        alert("Trend Cycle Counter トレンド下降 :" + str.tostring(close, "#,##0.#")  , alert.freq_once_per_bar)

    cycle_count

   

//上昇下降

up=cycle_trend > 0 ? (array.avg(cycles_p)-cycle_counter):(array.avg(cycles_m)-cycle_counter)


if array.avg(cycles)-cycle_counter > 5 and cycle_counter > int(length/2) and barstate.islast

    cycle_label:=label.new(bar_index+up, cycle_trend > 0 ? array.avg(cycles_p):array.avg(cycles_m),

      "トレンド目標" ,size=size.tiny,  textcolor=cycle_trend > 0 ? color.lime : color.red,  

      color=cycle_trend > 0 ? color.lime : color.red, style=label.style_xcross )


// ラベルを削除

label.delete(cycle_label[1])



// 転換点の表示

var label switch_label = na

if cycle_counter >= length and  bar_index > bar

    for i =0 to length

        if (cycle_trend < 0 and high[i] == highest_high) or (cycle_trend > 0 and low[i] == lowest_low )

            label.delete(switch_label)

            str_switch_label = low[i] >= 10000 ? str.tostring(cycle_trend > 0 ? low[i] : high[i], "#,###") :

               low[i] >= 1000 ? str.tostring(cycle_trend > 0 ? low[i] : high[i], "#,###.#") : str.tostring(cycle_trend > 0 ? low[i] : high[i], "###.##")

            switch_label := label.new(bar_index[i],cycle_counter[i],str_switch_label ,size=size.small,  textcolor=color.yellow ,  

               color = color.yellow, style=label.style_arrowdown )

            // ラベルを削除

           

        if i == cycle_counter

            break



// 最初の波数を削除

if array.size(cycles) == 1 and  count_one == 1

    count_zero := array.shift(cycles)

    array.clear(cycles_p)

    array.clear(trend_sigma_a)

    cycle_count_p := cycle_count_p - 1

    count_one:= 2


if array.size(cycles) ==1 and  count_one ==-1

    count_zero :=  array.shift(cycles)

    array.clear(cycles_m)

    array.clear(trend_sigma_a)

    cycle_count_m := cycle_count_m - 1

    count_one:= -2

   


plot(series=array.avg(cycles), title='Cycle AVG', color=color.new(color.orange, 0) , offset=up) //周期線の延長

plot(series=cycle_counter, title='Cycle Counter', color=cycle_trend > 0 ? color.lime : color.red)

plot(show_back ? ph : na, title='%Wd high')

plot(show_back ? pl : na, title='%Wd low')

plot(show_back ? avg : na, title='HL AVG', color=color.new(color.black, 0))

plot(show_back ? mean : na, title='Mean', color=color.new(color.navy, 0))



// ▽▽▽▽ トレンド転換 EXIT ストラテジー

if strategy.position_size > 0 and bar_index > bar and cycle_trend[1] > 0 and cycle_trend < 0

    strategy.cancel_all()

    strategy.close_all("close_All",alert_message="トレンド転換 決済")


if strategy.position_size < 0 and bar_index > bar and cycle_trend[1] < 0 and cycle_trend > 0

    strategy.cancel_all()

    strategy.close_all("Close_All", alert_message="トレンド転換 決済")



// ▽▽▽▽ トレンド転換 エントリー ストラテジー

strategy.entry("Trend_L", strategy.long, when=strategy_on  and strategy_trend_ent and bar_index > bar and cycle_trend[1] < 0 and cycle_trend > 0 , alert_message="トレンド転換 買いENT")

strategy.entry("Trend_S", strategy.short, when=strategy_on  and strategy_trend_ent and bar_index > bar and cycle_trend[1] > 0 and cycle_trend < 0 , alert_message="トレンド転換 売りENT")

strategy.exit(loss_text,  when=strategy_on  and strategy_trend_ent and bar_index > bar and cycle_trend[1] < 0 and cycle_trend > 0 ,stop=stop_loss_ll,limit=limit_loss_ll)

strategy.exit(loss_text,  when=strategy_on  and strategy_trend_ent and bar_index > bar and cycle_trend[1] > 0 and cycle_trend < 0 ,stop=stop_loss_hh,limit=limit_loss_hh)



// ▽▽▽▽ RCI短期 エントリー ストラテジー

plot(rcis_view ? rci(rcis) : na, title='RCI_S', color=rci(rcis) >= math.abs(rcis_int) ? rcis_color_p : rci(rcis) <= -1 * math.abs(rcis_int) ? rcis_color_m : rcis_color)


bool rcis_gcdc = na

if cycle_trend > 0 and ta.crossover(rci(rcis),-1 * math.abs(rcis_int))

    rcis_gcdc:= true

else if cycle_trend < 0 and ta.crossunder(rci(rcis),math.abs(rcis_int))

    rcis_gcdc:= true

else

    rcis_gcdc:= false

if strategy_ave == true

    if cycle_trend > 0 and cycle_counter <= array.avg(cycles_p) and rcis_gcdc == true

        rcis_gcdc:= true

    else if cycle_trend < 0 and cycle_counter <= array.avg(cycles_m) and rcis_gcdc == true

        rcis_gcdc:= true

    else

        rcis_gcdc:= false


plotshape(label_view and rcis_ent and rcis_gcdc ? cycle_counter : na,

  location=location.absolute, color=cycle_trend > 0 ? rcis_color_p : rcis_color_m , size=size.tiny,

  title="RCI_S", text="RCI_S" , textcolor=cycle_trend > 0 ? rcis_color_p : rcis_color_m , style=shape.arrowdown )


if alart_trend and rcis_ent and

  (cycle_trend > 0 and rci(rcis)[1] < -1 * rcis_int and  rci(rcis)[0] >= -1 * rcis_int and cycle_counter < array.avg(cycles_p) )

    alert("Trend Cycle Counter 上昇トレンドRCI短期 :" + str.tostring(close, "#,##0.#")  , alert.freq_once_per_bar)

if alart_trend and rcis_ent and

  (cycle_trend < 0 and rci(rcis)[1] > rcis_int and  rci(rcis)[0] <=  rcis_int  and cycle_counter < array.avg(cycles_m))

    alert("Trend Cycle Counter 下降トレンドRCI短期 :" + str.tostring(close, "#,##0.#")  , alert.freq_once_per_bar)


rcis_long_entry_condition = (cycle_trend > 0 and rcis_gcdc and rcis_ent)

rcis_short_entry_condition = (cycle_trend < 0 and rcis_gcdc and rcis_ent)


strategy.entry("RCI_L", strategy.long, when=strategy_on  and rcis_long_entry_condition and bar_index > bar, stop=stop_loss_ll , alert_message="RCI短期 買いENT")

strategy.entry("RCI_S", strategy.short, when=strategy_on  and rcis_short_entry_condition and bar_index > bar, stop=stop_loss_hh , alert_message="RCI短期 売りENT")

strategy.exit(loss_text,  when=strategy_on  and rcis_long_entry_condition and bar_index > bar and cycle_trend[1] < 0 and cycle_trend > 0 ,stop=stop_loss_ll,limit=limit_loss_ll)

strategy.exit(loss_text,  when=strategy_on  and rcis_short_entry_condition and bar_index > bar ,stop=stop_loss_hh,limit=limit_loss_hh)


// ▲▲▲▲▲ RCI エントリー ストラテジー



// ▽▽▽▽ RCI中期 exit ストラテジー

plot(rcim_view ? rci(rcim) : na, title='RCI_m', color=rci(rcim) >= math.abs(rcim_int) ? rcim_color_p : rci(rcim) <= -1 * math.abs(rcim_int) ? rcim_color_m : rcim_color)


bool rcim_gcdc = na

if cycle_trend > 0 and ta.crossunder(rci(rcim),math.abs(rcim_int))

    rcim_gcdc:= true

else if cycle_trend < 0 and ta.crossover(rci(rcim),-1 * math.abs(rcim_int))

    rcim_gcdc:= true

else

    rcim_gcdc:= false


plotshape(label_view and rcim_exit and rcim_gcdc ? cycle_counter : na,

  location=location.absolute, color=cycle_trend > 0 ? rcim_color_p : rcim_color_m , size=size.tiny,

  title="RCI_m", text="RCI_m" , textcolor=cycle_trend > 0 ? rcim_color_p : rcim_color_m , style=shape.arrowdown )


if alart_trend and rcim_exit and cycle_trend > 0 and rcim_gcdc

    alert("Trend Cycle Counter 上昇トレンドRCI中期線決済 :" + str.tostring(close, "#,##0.#")  , alert.freq_once_per_bar)

if alart_trend and rcim_exit and cycle_trend < 0 and rcim_gcdc

    alert("Trend Cycle Counter 下降トレンドRCI中期線決済 :" + str.tostring(close, "#,##0.#")  , alert.freq_once_per_bar)


long_exit_condition = (cycle_trend > 0 and rcim_gcdc and rcim_exit)

short_exit_condition = (cycle_trend < 0 and rcim_gcdc and rcim_exit)


if strategy_on  and bar_index > bar

    if long_exit_condition

        strategy.close_all("RCI_L_exit",  alert_message="RCI中期 買いEXIT")

    if short_exit_condition

        strategy.close_all("RCI_S_exit",  alert_message="RCI中期 売りEXIT")


// ▲▲▲▲▲ RCI EXIT ストラテジー


// ▽▽▽▽ macd exit ストラテジー

macdLine = ta.ema(src_macd, macd_short) - ta.ema(src_macd, macd_long)

macdsignal = ta.ema(macdLine, macd_sig)


bool macd_dc = na

bool macd_gc = na

macd_dc := ta.crossunder(macdLine, macdsignal)

macd_gc := ta.crossover(macdLine, macdsignal)

macd_l_exit_condition = (cycle_trend > 0 and macd_dc and macd_exit)

macd_s_exit_condition = (cycle_trend < 0 and macd_gc and macd_exit)

if strategy_on  and bar_index > bar

    if macd_l_exit_condition

        strategy.close_all("macd_exit",  alert_message="MACD 買いEXIT")

    if macd_s_exit_condition

        strategy.close_all("macd_exit",  alert_message="MACD 売りEXIT")


plotshape(label_view and macd_exit and (macd_l_exit_condition or macd_s_exit_condition) and strategy.position_size != 0 ? cycle_counter : na,

  location=location.absolute, color=color.orange , size=size.tiny,

  title="macd", text="macd" , textcolor=color.orange , style=shape.arrowdown )


// △△△△ macd exit ストラテジー


color col = na

if show_barcolor

    if avg > -40

        col := color.green

        col

    else if avg < -60

        col := color.maroon

        col

    else

        col := color.silver

        col

barcolor(col)


//陽線回数、陰線回数

var bull_count = 0

var bear_count = 0

var equal_count = 0


// 期間内のローソク足の色を確認し、陽線と陰線の回数を数える

if bar_index > bar

    bull_count := 0

    bear_count := 0

    equal_count := 0

    for i = 0 to cycle_counter - 1

        if close[i+1] > open[i+1]

            bull_count := bull_count + 1

        else if close[i+1] < open[i+1]

            bear_count := bear_count + 1

        else

            equal_count := equal_count + 1


//カウント表示

var table mainTrending = table.new(position.top_right   , 1, 4, border_width=1)


table.cell(mainTrending, 0, 1,

  text="現在カウント: " + str.tostring(cycle_counter),

  text_color=color.new(color=cycle_trend > 0 ? color.lime : color.red,  transp = 10),

  text_size=size.small,

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

  text_halign=text.align_right

  )


table.cell(mainTrending, 0, 2,

  text=cycle_trend > 0 ? "上昇平均: " + str.tostring(array.avg(cycles_p), "#.#") + "足/" + str.tostring(cycle_count_p) + "回中":

   "下降平均: " + str.tostring(array.avg(cycles_m), "#.#") + "/" + str.tostring(cycle_count_m) + "回中",

  text_color=color.new(color=cycle_trend > 0 ? color.lime : color.red,  transp = 10),

  text_size=size.small,

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

  text_halign=text.align_right,

  tooltip="陽線回数:"+str.tostring(bull_count) + " 陰線回数:"+str.tostring(bear_count) + " 同値:"+str.tostring(equal_count)  + "\n" +

     "トレンド内高値:" + str.tostring(array.max(highest)) + " 安値:" + str.tostring(array.min(lowest))  + "\n" +

     "トレンド転換予想ライン:" + str.tostring(cycle_trend > 0 ? lowest_low : highest_high)  + str.tostring(cycle_trend > 0 ? "▼" : "△")

  )



table.cell(mainTrending, 0, 3,

  text="平均周期: " + str.tostring(array.avg(cycles), "#.#")+ "/" + str.tostring(last_bar_index - bar - count_zero)+ "足中",

  text_color=color.new(color.orange,  transp = 20),

  text_size=size.small,

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

  text_halign=text.align_right,

  tooltip=

      str.tostring(str.format_time(time[last_bar_index - bar - count_zero], "M/d H:mm (E)", syminfo.timezone)) + " からカウント"+ "\n" +

      "トレンド予想カウント σ-" + str.tostring(trend_sigma) + ":" + str.tostring(array.avg(trend_sigma_a) - array.stdev(trend_sigma_a)  * trend_sigma, "#.#") + " ~ " +

      " σ+" + str.tostring(trend_sigma) + ":" + str.tostring(array.avg(trend_sigma_a) + array.stdev(trend_sigma_a)  * trend_sigma, "#.#")

      )    



// 標準偏差からシグマ範囲を先行塗りつぶし バックグラウンドカラー

if show_trend_sigma and bar_index > bar

    int sss = array.avg(trend_sigma_a) > array.stdev(trend_sigma_a) * trend_sigma ? int(array.avg(trend_sigma_a)) - int(array.stdev(trend_sigma_a)) * trend_sigma : 1

    var sigma_box = box.new(na,na,na,na,na, bgcolor = cycle_trend > 0 ? trend_sigma_up_color : trend_sigma_down_color )

    sigma_box.set_lefttop(last_bar_index  + sss - cycle_counter , array.max(cycles) *1.2)

    sigma_box.set_rightbottom(last_bar_index  + array.avg(trend_sigma_a) + array.stdev(trend_sigma_a) * trend_sigma - cycle_counter, -10)



// vplume偏差値

stddev=(volume - array.avg(vol_stddev)) / array.stdev(vol_stddev)*10+50

// 出来高の偏差値をプロット

if alart_vol and stddev > data_stddev

    alert("Trend Cycle Counter 出来高急騰 :" + str.tostring( math.pow((stddev - data_stddev) ,2))  , alert.freq_once_per_bar)

plot(show_stddev ? stddev < data_stddev ? 0 : math.pow((stddev - data_stddev) ,2) > array.max(cycles) * 2 ? array.max(cycles) *2 : math.pow((stddev - data_stddev) ,2) : na, color=stddev_color, title="出来高の偏差値")


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