当我试图创建一个计数器并在if- tell语句中增加它时,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);发布于 2019-10-26 21:11:45
一旦您在Thinkscript中定义了一个变量并给它赋值,它只对一个条形有效,它作为一个常量运行,因此不能重新分配。我非常肯定,您甚至不能将Def命令放入条件中,就像在大多数代码中一样。为了创建一个“动态”分数,您需要在实例化的同一行中分配动态值。你不需要
def score = 0;因为当您定义变量时,它无论如何都会有一个零值。
你也不需要额外的变量作为“战壕”占位符,因为真的
secondLastTrendisUp 就像说
lastTrendisUp[1]因为它已经在最后一个栏中计算了。
您可以使用折叠语句完成计数器,而不需要额外的变量,如下所示:
def score= fold index=0 to 4
with p=0
do p + ((bearcross[index] or bullcross[index]) and lastTrendisUp[index]);这将在每次条件为真时向分数中添加一个,并将总计分配给得分变量。我想这就是你想要完成的,我不知道,因为你以后从来不显示你在用分数变量做什么……如果您只想找出斗牛或熊架条件以及lasttrendisup条件在最后五条中的任一条中是否为true,则在with上添加‘then p=0’,当遇到第一个真实例时,它将返回一个值来得分。
发布于 2020-06-27 00:24:33
计数器将变量增加1,在每条上:
score = score[1] + 1;1的意思是,从一个条形上得到这个变量的值。
https://stackoverflow.com/questions/58294233
复制相似问题