見出し画像

フォーキャストエコーV0.0 本体(思考実験中)扱い注意

// Echo Forecast [LuxAlgo] 改造@issei0008 2023.09.17

//@version=5
indicator("Echo Forecast V0.0","Echo Forecast", overlay = true, max_bars_back = 1000, max_lines_count = 500)//, format=format.price, precision=2, timeframe="", timeframe_gaps=true

//Settings
torf=input(true,"短期評価",inline="line_00")
length = input.int(50, '評価範囲', minval = 10, maxval = 200,inline="line_00" , tooltip = "4派合計で500以下にすること、オーバーフローして表示されなくなります")
fcast = input.int(50, '予想範囲', minval = 10, maxval = 200,inline="line_00")

torf1=input(true,"中期評価",inline="line_01")
length1 = input.int(100, '評価範囲', minval = 20, maxval = 200,inline="line_01",step=5)
fcast1 = input.int(100, '予想範囲', minval = 20, maxval = 200,inline="line_01",step=5)

torf2=input(false,"長期評価",inline="line_02")
length2 = input.int(150, '評価範囲', minval = 30, maxval = 300,inline="line_02",step=10)
fcast2 = input.int(150, '予想範囲', minval = 30, maxval = 300,inline="line_02",step=10)

torf3=input(false,"最大評価",inline="line_03")
length3 = input.int(200, '評価範囲', minval = 100, maxval = 500,inline="line_03",step=10)
fcast3 = input.int(200, '予想範囲', minval = 100, maxval = 500,inline="line_03",step=10)

fmode = input.string('Similarity', 'Forecast Mode', options = ['Similarity','Dissimilarity'])
cmode = input.string('Cumulative', 'Forecast Construction', options = ['Cumulative','Mean','Linreg'])
src = input(close)

//Style
fcastCss = input(color.blue, '予想線短期', inline = 'fcast_style', group = 'Style')
fcastStyle = input.string('──', '', options = ['──','- - -','? ? ?'], inline = 'fcast_style', group = 'Style')

//Style1
fcastCss1 = input(color.purple, '予想線中期', inline = 'fcast_style1')
fcastStyle1 = input.string('──', '', options = ['──','- - -','? ? ?'], inline = 'fcast_style1')

//Style2
fcastCss2 = input(color.new(color.yellow, 20), '予想線長期', inline = 'fcast_style2')
fcastStyle2 = input.string('- - -', '', options = ['──','- - -','? ? ?'], inline = 'fcast_style2')

//Style3
fcastCss3 = input(color.new(color.red, 20), '予想線最大', inline = 'fcast_style3')
fcastStyle3 = input.string('- - -', '', options = ['──','- - -','? ? ?'], inline = 'fcast_style3')

showArea = input(true,'一番長い評価範囲', inline = 'areas')
refArea = input(color.new(#ff5d00, 80), '参照', inline = 'areas')
corrArea = input(color.new(#089981, 80), '相関', inline = 'areas')
evalArea = input(color.new(color.gray, 80),'評価', inline = 'areas')

// Populate line arrays
var lines = array.new_line(0)
var lines1 = array.new_line(0)
var lines2 = array.new_line(0)
var lines3 = array.new_line(0)

if torf == false
length:=0
fcast:=0
if torf1 == false
length1:=0
fcast1:=0
if torf2 == false
length2:=0
fcast2:=0
if torf3 == false
length3:=0
fcast3:=0

if barstate.isfirst
for i = 0 to math.max(fcast,fcast1,fcast2,fcast3)-1
if torf and i<fcast
array.push(lines, line.new(na,na,na,na, style = fcastStyle == '- - -' ? line.style_dashed : fcastStyle == '? ? ?' ? line.style_dotted : line.style_solid, color = fcastCss))
if torf1 and i<fcast1
array.push(lines1, line.new(na,na,na,na, style = fcastStyle1 == '- - -' ? line.style_dashed : fcastStyle1 == '? ? ?' ? line.style_dotted : line.style_solid, color = fcastCss1))
if torf2 and i<fcast2
array.push(lines2, line.new(na,na,na,na, style = fcastStyle2 == '- - -' ? line.style_dashed : fcastStyle2 == '? ? ?' ? line.style_dotted : line.style_solid, color = fcastCss2))
if torf3 and i<fcast3
array.push(lines3, line.new(na,na,na,na, style = fcastStyle3 == '- - -' ? line.style_dashed : fcastStyle3 == '? ? ?' ? line.style_dotted : line.style_solid, color = fcastCss3))

//-----------------------------------------------------------------------------}
//Calculations
//-----------------------------------------------------------------------------{

n = bar_index
d = src - src[1]

//Get range maximum/minimum
top = ta.highest(src, math.max(length,length1,length2,length3) + math.max(fcast,fcast1,fcast2,fcast3) * 2)
btm = ta.lowest(src, math.max(length,length1,length2,length3) + math.max(fcast,fcast1,fcast2,fcast3) * 2)
//Set forecast
if barstate.islast and torf
k = 0
float val = na
A = array.new_float(0) //Calculation window
X = array.new_int(0) //Linreg independant variable

//Populate calculation window
for i = 0 to fcast*2+length
    A.push(src[i])

    //Populate independant variable array
    if cmode == 'Linreg'
        X.push(n[i]) 

a = A.slice(0, fcast-1) //Reference window

//Find window to produce forecast
for i = 0 to length-1
    b = A.slice(fcast + i, fcast * 2 + i - 1)     //Evaluation window
    r = a.covariance(b) / (a.stdev() * b.stdev()) //Correlation

    //Maximization or minimization problem
    if fmode == 'Similarity'
        val := r >= nz(val, r) ? r : val
    else
        val := r <= nz(val, r) ? r : val

    k := val == r ? i : k
    
//Set ECHO
prev = src
current = src
for i = 0 to fcast-1
    e = d[fcast + k + (fcast-i-1)]

    //Get forecast point
    if cmode == 'Mean'
        current := array.avg(a) + e
    else if cmode == 'Linreg'
        a := A.slice(0, fcast)
        x = X.slice(0, fcast)

        alpha = a.covariance(x) / x.variance()
        beta = a.avg() - alpha * x.avg()
        current := alpha * (n + i + 1) + beta + e
    else
        current += e
    
    l = lines.get(i)
    l.set_xy1(n+i, prev)
    l.set_xy2(n+i+1, current)
    
    prev := current

// Set forecast01
if barstate.islast and torf1
k = 0
float val = na
A = array.new_float(0) //Calculation window
X = array.new_int(0) //Linreg independant variable

//Populate calculation window
for i = 0 to fcast1*2+length1
    A.push(src[i])

    //Populate independant variable array
    if cmode == 'Linreg'
        X.push(n[i]) 

a = A.slice(0, length1-1) //Reference window

//Find window to produce forecast
for i = 0 to length1-1
    b = A.slice(fcast1 + i, fcast1 * 2 + i - 1)     //Evaluation window
    r = a.covariance(b) / (a.stdev() * b.stdev()) //Correlation

    //Maximization or minimization problem
    if fmode == 'Similarity'
        val := r >= nz(val, r) ? r : val
    else
        val := r <= nz(val, r) ? r : val

    k := val == r ? i : k
    
//Set ECHO
prev = src
current = src
for i = 0 to fcast1-1
    e = d[fcast1 + k + (fcast1-i-1)]

    //Get forecast point
    if cmode == 'Mean'
        current := array.avg(a) + e
    else if cmode == 'Linreg'
        a := A.slice(0, fcast1)
        x = X.slice(0, fcast1)

        alpha = a.covariance(x) / x.variance()
        beta = a.avg() - alpha * x.avg()
        current := alpha * (n + i + 1) + beta + e
    else
        current += e
    
    l = lines1.get(i)
    l.set_xy1(n+i, prev)
    l.set_xy2(n+i+1, current)
    
    prev := current

// Set forecast02
if barstate.islast and torf2
k = 0
float val = na
A = array.new_float(0) //Calculation window
X = array.new_int(0) //Linreg independant variable

//Populate calculation window
for i = 0 to fcast2*2+length2
    A.push(src[i])

    //Populate independant variable array
    if cmode == 'Linreg'
        X.push(n[i]) 

a = A.slice(0, length2-1) //Reference window

//Find window to produce forecast
for i = 0 to length2-1
    b = A.slice(fcast2 + i, fcast2 * 2 + i - 1)     //Evaluation window
    r = a.covariance(b) / (a.stdev() * b.stdev()) //Correlation

    //Maximization or minimization problem
    if fmode == 'Similarity'
        val := r >= nz(val, r) ? r : val
    else
        val := r <= nz(val, r) ? r : val

    k := val == r ? i : k
    
//Set ECHO
prev = src
current = src
for i = 0 to fcast2-1
    e = d[fcast2 + k + (fcast2-i-1)]

    //Get forecast point
    if cmode == 'Mean'
        current := array.avg(a) + e
    else if cmode == 'Linreg'
        a := A.slice(0, fcast2)
        x = X.slice(0, fcast2)

        alpha = a.covariance(x) / x.variance()
        beta = a.avg() - alpha * x.avg()
        current := alpha * (n + i + 1) + beta + e
    else
        current += e
    
    l = lines2.get(i)
    l.set_xy1(n+i, prev)
    l.set_xy2(n+i+1, current)
    
    prev := current

// Set forecast03

if barstate.islast and torf3
k = 0
float val = na
A = array.new_float(0) //Calculation window
X = array.new_int(0) //Linreg independant variable

//Populate calculation window
for i = 0 to fcast3*2+length3
    A.push(src[i])

    //Populate independant variable array
    if cmode == 'Linreg'
        X.push(n[i]) 

a = A.slice(0, length3-1) //Reference window

//Find window to produce forecast
for i = 0 to length3-1
    b = A.slice(fcast3 + i, fcast3 * 2 + i - 1)     //Evaluation window
    r = a.covariance(b) / (a.stdev() * b.stdev()) //Correlation

    //Maximization or minimization problem
    if fmode == 'Similarity'
        val := r >= nz(val, r) ? r : val
    else
        val := r <= nz(val, r) ? r : val

    k := val == r ? i : k
    
//Set ECHO
prev = src
current = src
for i = 0 to fcast3-1
    e = d[fcast3 + k + (fcast3-i-1)]

    //Get forecast point
    if cmode == 'Mean'
        current := array.avg(a) + e
    else if cmode == 'Linreg'
        a := A.slice(0, fcast3)
        x = X.slice(0, fcast3)

        alpha = a.covariance(x) / x.variance()
        beta = a.avg() - alpha * x.avg()
        current := alpha * (n + i + 1) + beta + e
    else
        current += e
    
    l = lines3.get(i)
    l.set_xy1(n+i, prev)
    l.set_xy2(n+i+1, current)
    
    prev := current
        //Set areas
//-----------------------------------------------------------------------------}
//Calculations
//-----------------------------------------------------------------------------{

if showArea  
    var eval_window = box.new(na,na,na,na,na, bgcolor = evalArea)
    var ref_window = box.new(na,na,na,na,na, bgcolor = refArea)
    var corr_window = box.new(na,na,na,na,na, bgcolor = corrArea)
    //Evaluation window
    eval_window.set_lefttop(n-math.max(length,length1,length2,length3)-math.max(fcast,fcast1,fcast2,fcast3)*2+1, top)
    eval_window.set_rightbottom(n-math.max(fcast,fcast1,fcast2,fcast3)+1, btm)
    
    //Reference window
    ref_window.set_lefttop(n-math.max(fcast,fcast1,fcast2,fcast3)+1, top)
    ref_window.set_rightbottom(n, btm)

    //Correlation window
    corr_window.set_lefttop(n-k-math.max(fcast,fcast1,fcast2,fcast3)*2+1, top)
    corr_window.set_rightbottom(n-k-math.max(fcast,fcast1,fcast2,fcast3), btm)

//-----------------------------------------------------------------------------}

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