簡體   English   中英

松腳本變量 scope

[英]Pine Script variable scope

我正在使用內欄編寫一個簡單的策略。 我遇到的問題是,如果進入蠟燭后任何柱的高點高於內柱高點,我想取消交易。

下面的代碼片段應該希望能更好地解釋它

var short_Stop_Loss_Level = 0
if (high[0] > short_Stop_Loss_Level)
    label.new(bar_index, high, style=label.style_none, text="C=" + tostring(short_Stop_Loss_Level), yloc=yloc.abovebar)
    strategy.cancel_all()


Short_Condition = t and Inside_Bar
if Short_Condition
    short_Stop_Loss_Level = high[0] + 0.03

    label.new(bar_index, low, style=label.style_none,
          text="s=" + tostring(short_Stop_Loss_Level), yloc=yloc.belowbar)

    strategy.cancel_all()
    // strategy.close_all()
    strategy.entry("Enter", strategy.short, stop=Short_Stop_Buy_Level, qty=100)

在此處輸入圖像描述

從圖片中您可以看到停止值沒有在 if scope 之外維護,即使我已經聲明了一個全局停止變量。 我對此很陌生,也許我犯了一個我無法發現的簡單錯誤

pine-script中, 變量聲明是使用=運算符完成的。 就像您喜歡var short_Stop_Loss_Level = 0一樣。

但是, 變量賦值是使用:=運算符完成的。 因此,每當您想為已定義的變量賦予新值時,都應使用:=運算符。

if Short_Condition
    short_Stop_Loss_Level := high[0] + 0.03

您應該仔細檢查您的代碼,並確保在需要時使用:=運算符。

暫無
暫無

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

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