pine 脚本的数组问题

问题描述 投票:0回答:1

enter image description here

我正在为 pine 脚本编写策略,但我不断收到错误代码,例如无法检索数据。

我试图找到一个新的字符串来检索数据,但我一无所知。

在第 90 行,我收到一个错误代码,上面写着“Error on bar o: In 'array.get()' function. Index -1 is out ofbounds, array size is Zero at #main(): 90” 在这一行中它应该检索数据来计算流动性中断,但我不断遇到该错误代码。

//@version=5
strategy("Market Structure Strategy with Liquidity, ChoCh, and Selectable MA", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// User Inputs
internalLength = input.int(10, title="Internal Length")
externalLength = input.int(50, title="External Length")
length_option = input.string("ALL", title="Length Option", options=["INTERNAL", "EXTERNAL", "ALL","NONE"])
swingSize_swing = input.int(5, title="Swing Size")
liquidityLengthOption = input.string("MID", title="Liquidity Length Option", options=["SHORT", "MID", "LONG"])
liquidityswingLength = input.int(30)
liquidityLength = switch liquidityLengthOption
    "SHORT" => 10
    "MID" => 28
    "LONG" => 50

// Selectable Moving Average Parameters
ma_type = input.string("SMA", title="Moving Average Type", options=["SMA", "EMA", "WMA", "VWMA"])
ma_length = input.int(20, title="Moving Average Length")

// Function to select the moving average based on user input
get_moving_average(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
        => na  // Default to avoid errors if no type matches

// Apply the selected moving average
selected_ma = get_moving_average(close, ma_length, ma_type)

// Plot the selected moving average for visualization
plot(selected_ma, title="Selected Moving Average", color=color.blue, linewidth=2)

// Liquidity Detection Parameters
showInternalMS = (length_option == "INTERNAL" or length_option == "ALL" ) and length_option != "NONE"
showExternalMS = (length_option == "EXTERNAL" or length_option == "ALL" or length_option != "NONE")

// Adjusted Left Bars Based on Timeframe
adjustedLeftBars(tf, baseLeftBars) =>
    tf_seconds = timeframe.in_seconds(tf)
    if tf_seconds <= 60 
        10
    else if tf_seconds <= 300 
        20
    else
        baseLeftBars

leftBars = adjustedLeftBars("D", liquidityLength)  // Daily timeframe adjustment
secondLeftBars = adjustedLeftBars("W", liquidityLength)  // Weekly timeframe adjustment

// Liquidity detection variables
var highLineArrayHTF = array.new_line()
var lowLineArrayHTF  = array.new_line()
var highBoxArrayHTF  = array.new_box()
var lowBoxArrayHTF   = array.new_box()

// Persistent variables for liquidity levels
var float lastBuySideLiquidityTop = na
var float lastSellSideLiquidityBottom = na

// Detect liquidity pivots
pivotHighHTF  = ta.pivothigh(high, leftBars, leftBars)
pivotLowHTF   = ta.pivotlow(low, leftBars, leftBars)

// Populate highBoxArrayHTF and lowBoxArrayHTF when pivots are detected
if (pivotHighHTF)
    highBox = box.new(bar_index - leftBars, high[pivotHighHTF], bar_index, high)  // Create box for pivot high
    array.push(highBoxArrayHTF, highBox)

if (pivotLowHTF)
    lowBox = box.new(bar_index - leftBars, low[pivotLowHTF], bar_index, low)  // Create box for pivot low
    array.push(lowBoxArrayHTF, lowBox)

// Track Highs and Lows for ChoCh
var float recentHigh = na
var float recentLow = na

// Identify highs and lows for ChoCh
if ta.highestbars(high, swingSize_swing) == 0
    recentHigh := high
if ta.lowestbars(low, swingSize_swing) == 0
    recentLow := low

// ChoCh Conditions
chochToDowntrend = close < recentLow and ta.highestbars(low, swingSize_swing) > 0
chochToUptrend = close > recentHigh and ta.lowestbars(high, swingSize_swing) > 0

// Liquidity Break Conditions with Array Check
liquidityBreakShort = barstate.isconfirmed and array.size(highBoxArrayHTF) > 0 and close > box.get_top(array.get(highBoxArrayHTF, array.size(highBoxArrayHTF) - 1))
liquidityBreakLong = barstate.isconfirmed and array.size(lowBoxArrayHTF) > 0 and close < box.get_bottom(array.get(lowBoxArrayHTF, array.size(lowBoxArrayHTF) - 1))

// Combined Entry Conditions (including Moving Average filter)
longEntryCondition = chochToUptrend and liquidityBreakLong and close > selected_ma
shortEntryCondition = chochToDowntrend and liquidityBreakShort and close < selected_ma

// Stop Loss Levels
longStopLoss = recentLow - swingSize_swing
shortStopLoss = recentHigh + swingSize_swing

// Execute Trades
if (longEntryCondition)
    strategy.entry("Long Entry", strategy.long, stop=longStopLoss)

if (shortEntryCondition)
    strategy.entry("Short Entry", strategy.short, stop=shortStopLoss)

// Configure trail stop level with input options (optional)
longTrailPerc = input.float(3, title="Trail Long Loss (%)",
     minval=0.0, step=0.1) * 0.01

shortTrailPerc = input.float(3, title="Trail Short Loss (%)",
     minval=0.0, step=0.1) * 0.01

// Alerts for Liquidity Touch (Optional)
if barstate.isconfirmed and array.size(highBoxArrayHTF) > 0
    lastBuySideLiquidityBottom = box.get_bottom(array.get(highBoxArrayHTF, array.size(highBoxArrayHTF) - 1))
    if high >= lastBuySideLiquidityBottom
        alert("Buy-Side Liquidity Touched", alert.freq_once_per_bar)

if barstate.isconfirmed and array.size(lowBoxArrayHTF) > 0
    lastSellSideLiquidityTop = box.get_top(array.get(lowBoxArrayHTF, array.size(lowBoxArrayHTF) - 1))
    if low <= lastSellSideLiquidityTop
        alert("Sell-Side Liquidity Touched", alert.freq_once_per_bar)
arrays pine-script pine-script-v5
1个回答
0
投票

您的代码将从第一个柱开始在每个柱上执行。

您试图访问数组的元素,而不检查数组中是否有任何内容。当你的数组为空并且你仍然尝试访问最后一个元素时,你会收到错误消息。

您应该始终首先使用

if
检查数组大小,然后尝试访问该 if 块中的数组元素。

len = array.size(highBoxArrayHTF)

if (len > 0)
    ...
© www.soinside.com 2019 - 2024. All rights reserved.