【问题标题】:Why does thinkscript throw these issues when I try to create a counter?当我尝试创建计数器时,为什么 thinkscript 会抛出这些问题?
【发布时间】:2019-10-08 21:19:59
【问题描述】:

当我尝试创建一个计数器并在 if-else 语句中递增它时,thinkscript 编译器会抛出令人困惑的错误,告诉我这是不允许的,但我已经在几个示例中看到了这一点。他们甚至有一个保留字:rec,以便允许递增计数器。

score = score + 1; 产生:# 已分配:得分...

rec score = score + 1; 产生:# 标识符已使用:得分在 ... # 不允许在 IF/THEN/ELSE 语句中

#
# TD Ameritrade IP Company, Inc. (c) 2017-2019
#

input price = close;
input length = 9;
input displace = 0;

def score = 0;


def smavrgg = Average(price[-displace], length);
def expMvAvrg = ExpAverage(price[-displace], length);

plot SMA = smavrgg;
SMA.SetDefaultColor(GetColor(1));

plot AvgExp = expMvAvrg;
AvgExp.SetDefaultColor(GetColor(1));


# 1 if uptrend, 0 if downtrend
def lastTrendisUp = (close[0] - close[1]) > 0 ;
def secondLastTrendisUP = (close[1] - close[2]) > 0;
def thirdLastTrendisUP = (close[2] - close[3]) > 0;
def fourthLastTrendisUP = (close[3] - close[4]) > 0;


input lookback = 5;

# defines intBool (array) that indicates whether one or the other crossed. 

def bull_cross = SMA crosses above AvgExp;
def bear_cross = AvgExp crosses below SMA;

# returns the highest value in the data array for the lookback.  
# so [0, 1, 0, 0] means a cross happened within the last units. and 1 will be returned.  



if (bull_cross[0] or bear_cross[0]) then {
    if lastTrendisUp {
        # Already assigned: Score at...
          score = score + 1;


        # identifier already used: score at ...
        # not allowed inside an IF/THEN/ELSE statement
          rec score = score + 1;

    } else {

    }
} else if (bull_cross[1] or bear_cross[1])  {
    if secondLastTrendisUP {

    } else {

    }
} else if (bull_cross[2] or bear_cross[2]) {
    if thirdLastTrendisUP {

    } else {

    }
} else if (bull_cross[3] or bear_cross[3])  {
    if fourthLastTrendisUP {

    } else {

    }
} else if (bull_cross[4] or bear_cross[4])  {

} else {

}

# If most recent cross happened in the last 4
# and most recent cross occured on a green candle.

def bull_lookback = Highest(bull_cross, lookback);
def bear_lookback = Highest(bear_cross, lookback);






# def think = if bull_lookback or bear_lookback  

plot signal = if bull_lookback then 2 else if bear_lookback then 1 else 0;
signal.AssignValueColor(if signal == 2 then Color.DARK_GREEN else if signal == 1 then Color.DARK_RED else Color.DARK_ORANGE);
AssignBackgroundColor(if signal == 2 then Color.DARK_GREEN else if signal == 1 then Color.DARK_RED else Color.DARK_ORANGE);

【问题讨论】:

  • 如果你投反对票,至少有礼貌地解释原因。

标签: thinkscript


【解决方案1】:

在 Thinkscript 中定义变量并赋值后,它只对一根柱有效,它表现为常量,因此不能重新赋值。我很确定您甚至不能将 Def 命令放入条件中,就像在大多数代码中一样。为了创建“动态”SCORE,您需要在实例化的同一行中分配动态值。你不需要

def score = 0;

因为当你定义变量时,它无论如何都会有一个零值。

你也不要为“trendisup”占位符添加额外的变量,因为真的

secondLastTrendisUp 

和说的一样

lastTrendisUp[1]

因为它已经在最后一个小节中计算过了。

您可以使用 FOLD 语句在没有额外变量的情况下完成计数器,如下所示:

def score= fold index=0 to 4 
with p=0 
do p + ((bearcross[index] or bullcross[index]) and lastTrendisUp[index]);

这将在每次条件为真时将分数加一,并将总数分配给 SCORE 变量。我认为这是你想要完成的,我不能说,因为你以后永远不会展示你对 score 变量所做的事情......如果你只是想找出 bullcross 还是 Bearcross 条件和 lasttrendisup 条件在最后五个条中的任何一个中评估为真,然后在 with 上方添加 'while p=0',它将返回一个到 SCORE 为一旦遇到第一个真正的实例。

【讨论】:

    【解决方案2】:

    计数器在每个柱上增加一个变量:

    score = score[1] + 1;
    

    [1] 的意思是,从 1 小节前的那个变量中获取值。

    【讨论】:

      【解决方案3】:

      答案是thinkscript中的变量是不能改变的。

      【讨论】:

        猜你喜欢
        • 2012-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-28
        • 2022-09-23
        • 1970-01-01
        • 2013-01-01
        • 1970-01-01
        相关资源
        最近更新 更多