当我尝试创建一个计数器并在 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);