返回

pinescript v5-“;最大“返回错误”;但仅在几个小节之后

发布时间:2022-05-02 11:35:52 250

我编写了一个指标,将市场结构绘制为图表上的线条。

它的工作原理与几支蜡烛的工作原理相同,但最终出现了一个错误:“quot;Pine无法确定序列的参考长度。尝试在学习或策略功能中使用最大“条”;。

我已经尝试在代码中使用的几乎每个变量中使用max_bars_back(),甚至在indicator()函数中用作参数,但错误只是被延迟了,最终导致指示器停止运行。

以下是代码,经过充分注释后更易于阅读:

//@version=5

//==========DESCRIPTION=============//
//the code creates low and high structures according to this logic:
//At the beginning the high and low structures are just pivots, 
//the structures upadate only when one of them is broken:
//if the low is broken then the new low will be simply another pivot low
//and the new high will be the highest point between the starting point and the break of the low structure.
//if the high is broken then the new high will be another pivot high
//and the new low will be the lowest point between the strating point and the break of the high structure.






indicator('Auto Structures',overlay=true, max_bars_back=500)


//===================================================================================================================================\\
//===================================================================: INPUT :=======================================================\\

//--Pivot{---------------------------------------------------------------------\\
var G_piv     = '|---------------------Pivot----------------------|'
var left      =   input.int    (10, 'left'     , minval=1,  group=G_piv ,    inline='len')  //left and right are the pivots lenghts
var right     =   input.int    (10, 'right'    , minval=1,  group=G_piv ,    inline='len')    
var LTF_colHi =   input.color  (color.blue  , 'LTF'   ,     group=G_piv ,    inline='LTF')  //LTF = Lower Time Frame
var LTF_colLo =   input.color  (color.orange, ''      ,     group=G_piv ,    inline='LTF')
var HTF_colHi =   input.color  (color.green , 'HTF'   ,     group=G_piv ,    inline='HTF')  //HTF = Higher Time Frame
var HTF_colLo =   input.color  (color.red   , ''      ,     group=G_piv ,    inline='HTF')
var bars      =   input.int    (3,'How many candles to confirm a break'  ,  minval=1, maxval=100)
var start     =   input.time   (timestamp('01 Jan 2022 00:00 +0100'), 'Start', group=G_piv, confirm=true) //The code begins reading the structures only after the "start" point
//}


//===================================================================================================================================\\
//=================================================================: VARIABLES :=====================================================\\

var reset=false     // it will be used to restart the Lower Time Frame structures in the "CREATING FIRST STRUCTUES" section

//--Timeframe multiplier variables{-----------------------------------------------------------------\\
var pair = syminfo.tickerid
var bool is_m15 = timeframe.period == '15'
var bool is_H1  = timeframe.period == '60'
var bool is_H4  = timeframe.period == '240'
var bool is_D   = timeframe.period == 'D'

var LTF = is_m15 ? '15' : is_H1 ? '60'  : is_H4 ? '240' : na
var HTF = is_m15 ? '60' : is_H1 ? '240' : is_H4 ? 'D'   : na
//}


//===================================================================================================================================\\
//=================================================================: FUNCTIONS :======================================================\\

//--Timeframe Multiplier{---------------------------------------------------\\
f_mult(_tf) =>
    var int mult = switch
        _tf == 'D'   => is_D   ? 1 : is_H4  ? 6 : is_H1  ? 24 : is_m15 ? 96 : na
        _tf == '240' => is_H4  ? 1 : is_H1  ? 4 : is_m15 ? 16 : na
        _tf == '60'  => is_H1  ? 1 : is_m15 ? 4 : na
        _tf == '15'  => is_m15 ? 1 : na
    mult
//}



//--Pushing pivot values and index into the structures{------------------------------------------------\\
f_array_push(_val_arr, _dex_arr, _val, _dex) =>
    array.push (_val_arr, _val)
    array.push (_dex_arr, _dex)
//}



//--Break Confirmation{-------------------------------------------------------\\
f_break(_num, _valH, _valL) =>
    breakH  = false     //is the high broken?
    breakL  = false     //is the low broken?
    
    bars_up   = 0       //how many bars are above the high
    bars_do   = 0       //how many bars are below the low
    
    for i     = 0 to _num - 1
        if  close[i]  > _valH
            bars_up  += 1
            
        if  close[i]  < _valL
            bars_do  += 1
            
        if  bars_up  >= _num
            breakH   := true
            
        if  bars_do  >= _num
            breakL   := true
            
    [breakH, breakL]
//}



//--Lowest and Highest price reached between a structure generation and its breaking point{----------------------------------------------------\\
f_hi_lo(_arrH, _arrL) =>
    int sizeH = array.size(_arrH)  
    int sizeL = array.size(_arrL)
    
    int lenH  = (sizeH > 0 and time > start) ? (bar_index - array.get(_arrH, sizeH - 1)) : 1    //length of the high structure
    int lenL  = (sizeL > 0 and time > start) ? (bar_index - array.get(_arrL, sizeL - 1)) : 1    //length of the low structure
    
    float lowest    = ta.lowest (lenH)
    float highest   = ta.highest(lenL)
    
    int lowest_dex  = bar_index + ta.lowestbars (lenH)  //index of the lowest price
    int highest_dex = bar_index + ta.highestbars(lenL)  //index of the highest price
    
    [lowest, highest, lowest_dex, highest_dex]
//}



//--Getting the last values and index in the array containing high and low structures{------------------------------------------------------------\\
f_last_val(_val_arrH, _dex_arrH, _val_arrL, _dex_arrL) =>
    var float last_max   = 0.0
    var float last_min   = 0.0
    var int dex_last_max = 0
    var int dex_last_min = 0
    
    if array.size(_val_arrH) > 0 and time > start 
        last_max     := array.get(_val_arrH, array.size(_val_arrH) - 1)
        dex_last_max := array.get(_dex_arrH, array.size(_dex_arrH) - 1)
        
    if array.size(_val_arrL) > 0 and time > start
        last_min     := array.get(_val_arrL, array.size(_val_arrL) - 1)
        dex_last_min := array.get(_dex_arrL, array.size(_dex_arrL) - 1)
        
    [last_max, dex_last_max, last_min, dex_last_min]
//}



//--Logic creating the higher structure{------------------------------------------------------\\

f_structureH(_val_arrH, _val_arrL, _dex_arrH, _dex_arrL, _break, _tf, _lowest, _piv, _indexhi, _indexlo) =>
    var int  break_dex= na
    var bool brokenup = false           //variable used to know if the most recent higher structures has been broken
    var int  c        = 0               //counter used to let the break logic run only once
    
    if array.size(_val_arrH) > 0 and time > start
        if  _break and c == 0 and barstate.isconfirmed   // the _break argument is true only if in the last confirmed bar the break confirmation cryteria is met
            brokenup        := true                      // if the break confirmation cryteria has been met then brokenup will reamin true
            array.push(_val_arrL, _lowest )              // pushing in to the low structure the value and index of [...]
            array.push(_dex_arrL, _indexlo)              // [...] the lowest price reached since the creation of the most recent high structure
            break_dex := bar_index                       // index of the candle that caused the break
            c += 1                                       
            
        if  _indexhi > (break_dex - (bars * f_mult(_tf))) and brokenup == true  //if the most recent high has been broken and a valid pivot is found afterwards [...]
            array.push (_val_arrH, _piv    )                                    //[...] then push a new high pivot in to the high structure to update it
            array.push (_dex_arrH, _indexhi)
            brokenup := false                                                   
            c       := 0                                                        //once the high structure is updated, reset the counter and the breakup values
            
        if  array.size (_val_arrH) > 3                                          //deleting old structures
            array.shift(_val_arrH)
            array.shift(_dex_arrH)
//}



//--Logic creating the lower structure{-------------------------------------------------------\\
f_structureL(_val_arrH, _val_arrL, _dex_arrH, _dex_arrL, _break, _tf, _highest, _piv, _indexhi, _indexlo) =>
    var break_dex       = int(na)
    var bool brokendown = false
    var int  c         = 0                                                      //the low structure works the same as the high one
    
    if array.size(_val_arrL)  > 0 and time > start
        if  _break and c    == 0 and barstate.isconfirmed
            brokendown       := true
            array.push(_val_arrH, _highest)
            array.push(_dex_arrH, _indexhi)
            break_dex := bar_index
            c += 1
            
        if  _indexlo > (break_dex - (bars * f_mult(_tf))) and brokendown == true
            array.push (_val_arrL, _piv    )
            array.push (_dex_arrL, _indexlo)
            brokendown := false
            c         := 0
            
        if  array.size (_val_arrL) > 3
            array.shift(_val_arrL)
            array.shift(_dex_arrL)
//}



//--Drawing Structures as Lines{-----------------------------------------------------\\
f_draw_line(dexH, valH, dexL, valL, _colH, _colL, _wdt) =>
    lineH = line(na)
    lineL = line(na)
    
    if time > start and dexH > 0
        lineH := line.new(dexH, valH, bar_index, valH, color=_colH, width=_wdt)
        
    if time > start and dexL > 0
        lineL := line.new(dexL, valL, bar_index, valL, color=_colL, width=_wdt)
        
    if valH == valH[1]                                  //if the new Line is just the continuation of the old one, then delete the old one[...]
        line.delete(lineH[1])                           //[...]otherwise keep the line saved in memory
        
    if valL == valL[1]
        line.delete(lineL[1])
//}



//===================================================================================================================================\\
//==================================================: DECLARING STRUCTURES' VARIABLES AND ARRAYS :===================================\\


//--Dichiarazione: array e pivot{----------------------------------------------\\
var float[] LTF_highs  = array.new_float(0)  ,  var float[] HTF_highs  = array.new_float(0)
var float[] LTF_lows   = array.new_float(0)  ,  var float[] HTF_lows   = array.new_float(0)
var   int[] LTF_Hindex = array.new_int  (0)  ,  var   int[] HTF_Hindex = array.new_int  (0)                  //Hindex= index of the high structure             
var   int[] LTF_Lindex = array.new_int  (0)  ,  var   int[] HTF_Lindex = array.new_int  (0)                  //Lindex= index of the low structure    

var float   LTF_pivHi       = 0.0            ,  var float   HTF_pivHi       = 0.0                           //pivHi= series containing all the pivot high prices
var float   LTF_pivLo       = 0.0            ,  var float   HTF_pivLo       = 0.0                           //pivLo= series containing all the pivot low prices
var   int   LTF_indexpivLo  = 0              ,  var   int   HTF_indexpivLo  = 0
var   int   LTF_indexpivHi  = 0              ,  var   int   HTF_indexpivHi  = 0

LTF_piv_max   = request.security(pair, LTF, ta.pivothigh(high, left, right))                                
LTF_piv_min   = request.security(pair, LTF, ta.pivotlow (low , left, right))    
HTF_piv_max   = request.security(pair, HTF, ta.pivothigh(high, left, right))
HTF_piv_min   = request.security(pair, HTF, ta.pivotlow (low , left, right))
//}


//===================================================================================================================================\\
//=============================================================: INITIALISING STRUCTURES :=========================================\\

//--Pivot values and index{-------------------------------------------------------------\\

//--Lower Time Frame high structure
if not na(LTF_piv_max)                                                          //if there is a valid high pivot then save its price and index
    LTF_indexpivHi := bar_index[right * f_mult(LTF)]
    LTF_pivHi      := LTF_piv_max
    
    if  (array.size (LTF_highs) == 0 and time > start) or reset                 //if the high structure is empty or reset condition is met[...]
        f_array_push(LTF_highs, LTF_Hindex, LTF_pivHi, LTF_indexpivHi)          //[...], then the first structure is the high pivot itself
        reset:=false

//--Lower Time Frame low structure
if not na(LTF_piv_min)                                                          //the same process is applied on every structure for both Lower and Higher time frames
    LTF_indexpivLo := bar_index[right * f_mult(LTF)]
    LTF_pivLo      := LTF_piv_min  
    
    if  (array.size (LTF_lows) == 0 and time > start) or reset
        f_array_push(LTF_lows, LTF_Lindex, LTF_pivLo, LTF_indexpivLo)
        reset:=false

//--Higher Time Frame high structure
if not na(HTF_piv_max)                                                          //higher time frame's structure does not have a reset condition
    HTF_indexpivHi := bar_index[right * f_mult(HTF)]
    HTF_pivHi      := HTF_piv_max
    
    if  (array.size (HTF_highs) == 0 and time > start)
        f_array_push(HTF_highs, HTF_Hindex, HTF_pivHi, HTF_indexpivHi)


//--Higher Time Frame low structure
if not na(HTF_piv_min)                                
    HTF_indexpivLo := bar_index[right * f_mult(HTF)]
    HTF_pivLo      := HTF_piv_min  
    
    if  (array.size (HTF_lows) == 0 and time > start)
        f_array_push(HTF_lows, HTF_Lindex, HTF_pivLo, HTF_indexpivLo)
//}


//===================================================================================================================================\\
//============================================================: OBTAINING ACTUAL VALUES FROM FUNCTIONS :===================================================\\

//--Lower and Higher Time Frames index and prices of highest and lowest points{----------------------------------------------\\
[LTF_lowest, LTF_highest, LTF_lowestindex, LTF_highestindex] = f_hi_lo(LTF_Hindex, LTF_Lindex)
[HTF_lowest, HTF_highest, HTF_lowestindex, HTF_highestindex] = f_hi_lo(HTF_Hindex, HTF_Lindex)
//}


//--Getting last values and candle index from high and low structure{------------------------------------------------\\
[LTF_last_max, LTF_indexlast_max, LTF_last_min, LTF_indexlast_min] = f_last_val(LTF_highs, LTF_Hindex, LTF_lows, LTF_Lindex)
[HTF_last_max, HTF_indexlast_max, HTF_last_min, HTF_indexlast_min] = f_last_val(HTF_highs, HTF_Hindex, HTF_lows, HTF_Lindex)
//}


//--Reset condition{-----------------------------------------------------------\\
if (HTF_last_max!=HTF_last_max[1]) or (HTF_last_min!=HTF_last_min[1])
    reset:=true                                                       //whenever there is a change in higher time frame structures, them reset the lower time frame ones

    
//}

//===================================================================================================================================\\
//=====================================================: GETTING THE ACTUAL STRUCTURES :=========================================\\

//--Has the most recent structure been broken?{---------------------------------------------------\\
[LTF_breakH, LTF_breakL] = f_break(bars * f_mult(LTF), LTF_last_max, LTF_last_min)
[HTF_breakH, HTF_breakL] = f_break(bars * f_mult(HTF), HTF_last_max, HTF_last_min)
//}

//--Updating structures{---------------------------------------------\\
f_structureH(LTF_highs, LTF_lows, LTF_Hindex, LTF_Lindex, LTF_breakH, LTF, LTF_lowest , LTF_pivHi, LTF_indexpivHi,   LTF_lowestindex)
f_structureH(HTF_highs, HTF_lows, HTF_Hindex, HTF_Lindex, HTF_breakH, HTF, HTF_lowest , HTF_pivHi, HTF_indexpivHi,   HTF_lowestindex)
f_structureL(LTF_highs, LTF_lows, LTF_Hindex, LTF_Lindex, LTF_breakL, LTF, LTF_highest, LTF_pivLo, LTF_highestindex, LTF_indexpivLo )
f_structureL(HTF_highs, HTF_lows, HTF_Hindex, HTF_Lindex, HTF_breakL, HTF, HTF_highest, HTF_pivLo, HTF_highestindex, HTF_indexpivLo )
//}


//===================================================================================================================================\\
//===================================================================: PLOT :========================================================\\



//--Plotting structures{-----------------------------------------------------------------\\
f_draw_line(LTF_indexlast_max, LTF_last_max, LTF_indexlast_min, LTF_last_min, LTF_colHi, LTF_colLo, 1)
f_draw_line(HTF_indexlast_max, HTF_last_max, HTF_indexlast_min, HTF_last_min, HTF_colHi, HTF_colLo, 3)
//}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像
相关帖子