簡體   English   中英

松樹腳本中的變量 scope

[英]Variable scope in pine script

在這里,我使用一些變量通過定義profitstop的方式來設置我的退出條件

stopLR          = float(floor(stopLoss))    - (3* syminfo.mintick)
targetLR        = float(floor(target))      - (3* syminfo.mintick

if(longCondition and strategy.position_size == 0)
    //average_long    = close
    targetLTick     := (targetLR - close) / syminfo.mintick
    stopLTick       := (close - stopLR) / syminfo.mintick
    

strategy.exit("Long Buy",   profit = targetLTick,   comment = "Take Profit")  //line 460
strategy.exit("Long Buy",   loss = stopLTick,       comment = "Book Loss")    //line 461

添加到圖表操作失敗,原因:第 460 行:未聲明的標識符“targetLTick”;

第 461 行:未聲明的標識符“stopLTick”

這里有什么問題? 變量是全局 scope 並且拼寫沒有問題,為什么松樹給我錯誤?

根據您的代碼段, targetLTickstopLTick在本地 scope 中聲明。 嘗試使用na值在全局 scope 中聲明它們,然后在本地 scope 中重新分配,如下例所示:

stopLR          = float(floor(stopLoss))    - (3* syminfo.mintick)
targetLR        = float(floor(target))      - (3* syminfo.mintick

var float targetLTick = na     
var float stopLTick = na
       
if(longCondition and strategy.position_size == 0)
    //average_long    = close
    targetLTick     := (targetLR - close) / syminfo.mintick
    stopLTick       := (close - stopLR) / syminfo.mintick
    

strategy.exit("Long Buy",   profit = targetLTick,   comment = "Take Profit")  //line 460
strategy.exit("Long Buy",   loss = stopLTick,       comment = "Book Loss")    //line 461 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM