PineScript 策略脚本故障排除

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

我是 PineScript 的新手,我正在尝试回测我根据其他一些指标脚本创建的策略。我相信我已经正确地编写了所有内容,但是当我去编译时,我收到错误“Incorrect 'for' statement.期待‘到’。”对于下面我的主要功能中的第一个 for 循环。有人介意帮我解决这个问题吗?我查看了堆栈溢出,似乎每当 for 块中出现任何类型的语法错误时都会显示此错误。我只是不确定错误是什么。我试过要求 Codex 和 ChatGPT 找出给出错误声明的错误,但它一直给我虚假的答案,比如“函数声明只对一行使用 =>,多行函数需要 {}”。 这部分代码应该搜索之前代码生成的附近订单块,然后将与这些订单块关联的价格水平保存并设置为任何单笔交易的 sl 和 tp 水平。我用于生成订单块的代码直接取自 LuxAlgo 的 SMC 指标:https://www.tradingview.com/script/CnB3fSph-Smart-Money-Concepts-LuxAlgo/ 特别是第 630-640 和 754-810 行。 这是我的代码:

ob_price_lvls = array.new_float(2)

get_ob_price_level() =>

    // get entry price level
    entry_price_level = strategy.position_avg_price 

    // get the order block closest to entry price against our direction
    for j = 0 to (bar_index - array.get(iob_left, 0)) //something inside this for block is wrong, possibly namespace
                                                      // or outdated method usage
      if array.get(iob_type, j) == -strategy.position_size // if the order block is against our position
        if strategy.position_size > 0 and array.get(iob_top, j) < entry_price_level // if long and order block top is below entry level
          array.set(ob_price_lvls, 0, array.get(iob_top, j))
        if strategy.position_size < 0 and array.get(iob_btm, j) > entry_price_level // if short and order block bottom is above entry level
          array.set(ob_price_lvls, sl_ob_price_level, array.get(iob_btm, j))

    // get the order block in our direction closest to double the difference between our entry price and sl_ob_price_level
    var float ob_dist = math.abs(array.get(ob_price_lvls, sl_ob_price_level) - entry_price_level) * 2
    for j = 0 to (bar_index - array.get(iob_left, 0))
      if array.get(iob_type, j) == strategy.position_size // if the order block is in our direction
        if strategy.position_size > 0 and array.get(iob_btm, j) > entry_price_level and array.get(iob_btm, j) - entry_price_level >= ob_dist // if long and order block bottom is above ob_price_level
          array.set(ob_price_lvls, 1, array.get(iob_btm, j))
        if strategy.position_size < 0 and array.get(iob_top, j) < entry_price_level and entry_price_level - array.get(iob_top, j) >= ob_dist // if short and order block top is below ob_price_level
          array.set(ob_price_lvls, tp_ob_price_level, array.get(iob_top, j))

// Alerts, Entry & Exit

bool longOrShort = na

alertOn = input(true, 'Alerts')
if longValOne and longValTwo and alertOn
  alert('New Long Signal - MACD/RSI', alert.freq_once_per_bar)
  strategy.entry("Long", strategy.long, 1)
  longOrShort := true
  get_ob_price_level()
if shortValOne and shortValTwo and alertOn
  alert('New Short Signal - MACD/RSI', alert.freq_once_per_bar)
  strategy.entry("Short", strategy.short, 1)
  longOrShort := false
  get_ob_price_level()

我尝试在主函数中使用缩进,我尝试向 Codex 和 ChatGPT 寻求建议,但没有任何效果。

pine-script pine-script-v5 algorithmic-trading
2个回答
0
投票

我相信你需要多尝试

我试着玩缩进

正如我在这段代码中看到的,

if
和其中的所有内容只有两个空格,而不是四个。 所有局部作用域必须以 4 个空格的倍数分隔。你在主代码和函数中都有这个问题
get_ob_price_level()

以后请附上重现问题并按照您描述执行的代码示例。 您的代码段没有定义很多变量、脚本版本和类型,这就是它生成完全不同的错误的原因。


0
投票

//@version=5
strategy("My Script", overlay=true)

var entry_price_level = strategy.position_avg_price
ob_price_lvls = array.new_float(2)

//Order Blocks Arrays
//-----------------------------------------------------------------------------{
var iob_top = array.new_float(0)
var iob_btm = array.new_float(0)
var iob_left = array.new_int(0)
var iob_type = array.new_int(0)

var ob_top = array.new_float(0)
var ob_btm = array.new_float(0)
var ob_left = array.new_int(0)
var ob_type = array.new_int(0)

var int sl_ob_price_level = na
var float ob_dist = na
// Define OB distance variable
// Define TP OB price level variable
var tp_ob_price_level = 1

// Get order block price level
get_ob_price_level(entry_price_level) =>
    // get the order block closest to entry price against our direction
    for j = 0 to (bar_index - array.get(iob_left, 0))
        if array.get(iob_type, j) == -strategy.position_size // if the order block is against our position
            if strategy.position_size > 0 and array.get(iob_top, j) < entry_price_level // if long and order block top is below entry level
                array.set(ob_price_lvls, 0, array.get(iob_top, j))
            if strategy.position_size < 0 and array.get(iob_btm, j) > entry_price_level // if short and order block bottom is above entry level
                array.set(ob_price_lvls, 0, array.get(iob_btm, j))

ob_dist := math.abs(array.get(ob_price_lvls, sl_ob_price_level) - entry_price_level) * 2
for j = 0 to (bar_index - array.get(iob_left, 0))
    if array.get(iob_type, j) == strategy.position_size // if the order block is in our direction
        if strategy.position_size > 0 and array.get(iob_btm, j) > entry_price_level and array.get(iob_btm, j) - entry_price_level >= ob_dist // if long and order block bottom is above ob_price_level
            array.set(ob_price_lvls, 1, array.get(iob_btm, j))
        if strategy.position_size < 0 and array.get(iob_top, j) < entry_price_level and entry_price_level - array.get(iob_top, j) >= ob_dist // if short and order block top is below ob_price_level
            array.set(ob_price_lvls, tp_ob_price_level, array.get(iob_top, j))
longValOne = 
longValTwo =
shortValOne =
shortValTwo =
// Alerts, Entry & Exit
alertOn = input(true, 'Alerts')
if longValOne and longValTwo and alertOn
    alert('New Long Signal - MACD/RSI', alert.freq_once_per_bar)
    strategy.entry("Long", strategy.long, 1)
    get_ob_price_level(entry_price_level)
if shortValOne and shortValTwo and alertOn
    alert('New Short Signal - MACD/RSI', alert.freq_once_per_bar)
    strategy.entry("Short", strategy.short, 1)
    get_ob_price_level(entry_price_level)

© www.soinside.com 2019 - 2024. All rights reserved.