簡體   English   中英

如何在 Pine Script 策略中使用條件?

[英]How can I use conditionals in Pine Script strategies?

我正在嘗試學習 Pine Script,對於我的第一個項目,我想根據以下代碼構建一個簡單的 Fisher 指標策略(用戶 HPotter 在 TradingView 上公開提供):

Length = input(10, minval=1)
xHL2 = hl2
xMaxH = highest(xHL2, Length)
xMinL = lowest(xHL2,Length)
nValue1 = 0.33 * 2 * ((xHL2 - xMinL) / (xMaxH - xMinL) - 0.5) + 0.67 * nz(nValue1[1])
nValue2 = iff(nValue1 > .99,  .999,
            iff(nValue1 < -.99, -.999, nValue1))
nFish = 0.5 * log((1 + nValue2) / (1 - nValue2)) + 0.5 * nz(nFish[1])
plot(nFish, color=green, title="Fisher")
plot(nz(nFish[1]), color=red, title="Trigger")

據我了解轉換,您對一個值執行操作,plot 該值(nFish),你得到你的 Fisher Line。 從一根柱線上取值 nz(nFish[1]), plot ,然后你就得到了你的觸發線。 當您在費希爾線和觸發線之間獲得交叉時,您可能有興趣在附近交易 position。 我當時的想法是,在制定策略時要實施以下內容:

"If the Fisher Line is above the Trigger Line now AND IF the Fisher Line was below the Trigger Line on the last bar, enter a long position"

我試圖通過以下方式實現:

if (nFish > nz(nFish[1]))
    if (nz(nFish[1]) < nz(nFish[2]))
        strategy.entry("Long", strategy.long)

但是,此代碼無法生成任何 position 條目,並且對 if 語句中的值的任何修改也無法生成任何 output。 我怎樣才能修復我的條件來實現我想要測試的想法?

首先,我不建議使用那么舊的源代碼來學習。 您可以從任何版本開始,但我建議您不要使用任何版本,除非您在源代碼頂部附近看到//@version=4 我剛開始時犯了這個錯誤,改編了一個舊腳本並在此過程中學習了一個過時的版本。

所以在這里我修改了 V4 的腳本,注意更改。 一些語法,並用 var 關鍵字聲明變量來初始化某些變量。 在 V4 中,您不能在聲明中使用變量時聲明變量。 此外,我將iff替換為三元,您會看到它更頻繁地使用。

//@version=4
study(title="Fisher Transform Indicator by Ehlers - modified for V4", shorttitle="Fisher Transform Indicator by Ehlers")
length = input(title='Length', type=input.integer, defval=10, minval=1)

MaxHL2 = highest(hl2, length)
MinHL2 = lowest(hl2,  length)
var float nValue1 = na
var float nValue2 = na
var float nFish = na
nValue1 := 0.33 * 2 * ((hl2 - MinHL2) / (MaxHL2 - MinHL2) - 0.5) + 0.67 * nz(nValue1[1])
nValue2 := nValue1 > 0.99 ? 0.999 : nValue1 < -0.99 ?  -0.999 : nValue1
nFish := 0.5 * log((1 + nValue2) / (1 - nValue2)) + 0.5 * nz(nFish[1])
plot(nFish, color=color.green, title="Fisher")
plot(nFish[1], color=color.red, title="Trigger")

接下來,您描述的邏輯稱為crossover 這意味着第一個參數超過了第二個。 熟悉crosscrossunder 因此,為了簡單起見,將其調整為策略,我們可以創建一個交叉變量:

//@version=4
strategy(title="Fisher Transform Indicator by Ehlers - modified for V4", shorttitle="Fisher Transform Indicator by Ehlers")
length = input(title='Length', type=input.integer, defval=10, minval=1)

MaxHL2 = highest(hl2, length)
MinHL2 = lowest(hl2,  length)
var float nValue1 = na
var float nValue2 = na
var float nFish = na
nValue1 := 0.33 * 2 * ((hl2 - MinHL2) / (MaxHL2 - MinHL2) - 0.5) + 0.67 * nz(nValue1[1])
nValue2 := nValue1 > 0.99 ? 0.999 : nValue1 < -0.99 ?  -0.999 : nValue1
nFish := 0.5 * log((1 + nValue2) / (1 - nValue2)) + 0.5 * nz(nFish[1])

longCross = crossover(nFish,nFish[1])
shortCross = crossunder(nFish, nFish[1])
plotshapePosition = longCross ? nFish : na

strategy.entry('long', strategy.long, when=longCross)
strategy.close('long', when=shortCross)

plotshape(plotshapePosition, style=shape.circle, color=color.green, size=size.small, location=location.absolute)
plot(nFish, color=color.green, title="Fisher")
plot(nFish[1], color=color.red, title="Trigger")

請注意,我還做了一個crossunder ,只是為了退出該策略。

另外,我將變量 plot 設置為一個形狀,這對於不使用時的定位是必需的overlay=true Sp plotshapePosition 在發生crossover時返回 na 或 nFish 值。 location.absolute參數使用此級別來 position 形狀,而如果您在主圖表上繪圖,則不是絕對必要的。 在測試您的條件是否有效時, plotshape會很方便。

此外,如果您確實有需要手動構建的條件,而不是使用 function 之類的crossover ,您可以使用 boolean 邏輯來構建它。

longCondition = nFish > nFish[1] and nFish[1] < nFish[2]
strategy.entry("Long", strategy.long, when = longCondition)

暫無
暫無

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

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