簡體   English   中英

在 pine 腳本中通過具有多個“if”條件的 for 循環時出錯

[英]Error when going through a for loop with multiple "if" conditions in pine script

我告訴你,我有一個以前的版本可以使用多個條件來執行一個條目,我給你舉個例子:

lo0 := deltaCloseMa0>=lo0CloseMa0Thresold and rsi>=lo0RsiThreshold
lo1 := deltaCloseMa0>=lo1CloseMa0Thresold and rsi>=lo1RsiThreshold and deltaCloseOpen>=lo1CloseOpenThreshold 
lo2 := ....
lo3 := ....
lo4 := ....
....
lo25 := ....

問題來自於代碼已經大幅增長,現在不可能繼續將條件添加到無窮大,因為它不可擴展。

我想到了一個可能的解決方案,它處理一個一個地評估條件,然后如果滿足所有啟用的條件,則執行輸入。

我放了我寫的代碼片段,但我無法讓它編譯腳本。

var entryOrderConditions = false, constructorMa0Cond = false, constructorRsiCond = false
var orderBuilder = array.new_bool(0)
if orderType    // If orderType is enable, then evaluate the conditions for long .. if disable, for short
    // LONG
    if barstate.isconfirmed and strategy.position_size == 0
        // ## Setup constructor
        if enableCloseToMa0
            if deltaCloseMa0 >= ma0CloseThreshold
                array.push(orderBuilder, true)
            else
                array.push(orderBuilder, false)
        if enableRsi
            if rsi >= rsiThreshold
                array.push(orderBuilder, true)
            else
                array.push(orderBuilder, false)
        
        for signal in orderBuilder
            if signal
                entryOrderConditions := true
            else
                entryOrderConditions := false
                break

問題是相同的條件並不總是啟用,容差閾值也不總是相同的,所以它們在上面用輸入動態化(bool,int,float ...)

所以我必須尋找一個靈活的系統,它允許我評估盡可能多的條件,並且具有足夠的可擴展性,不會在代碼中定義 100 種輸入類型。

非常感謝您的幫助。

骯臟的解決方案:

我試過各種類型的 arrays,用各種方式遍歷它們,用不同的檢查,我什至尋找一個“開關”來過濾......但沒有任何效果。

最后我找到了一個可行的解決方案,它不是世界上最優雅和最干凈的代碼,但它是功能性的,我認為我無法改進它。

如果有人讀到這篇文章並認為有更好的方法,我願意接受建議。

自己創作的一點哲學,

有時,最好的解決方案並不是最令人愉快的

即便如此,它仍然比開始時更具可擴展性。

float constructorMa0Cond = na
float constructorRsiCond = na
if orderType
    // LONG
    if barstate.isconfirmed and strategy.position_size == 0
        // ## Setup constructor
        if enableCloseToMa0
            if deltaCloseMa0 >= ma0CloseThreshold
                constructorMa0Cond := 1
            else
                constructorMa0Cond := 0
        if enableRsi
            if rsi >= rsiThreshold
                constructorRsiCond := 1
            else
                constructorRsiCond := 0

        for i = 0 to 1
            if i == 0
                if not na(constructorMa0Cond)
                    if constructorMa0Cond
                        entryOrderConditions := true
                    else
                        entryOrderConditions := false
                        break
            else if i == 1
                if not na(constructorRsiCond)
                    if constructorRsiCond
                        entryOrderConditions := true
                    else
                        entryOrderConditions := false
                        break

暫無
暫無

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

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