Recursion Thinkscript:递归计数器

Recursion Thinkscript:递归计数器,recursion,counter,thinkscript,Recursion,Counter,Thinkscript,我想在thinkorswim中创建一个扫描,它返回的股票在过去5天中开盘4天或更长时间后收盘价高于开盘价 这是我目前拥有的代码,但我不知道它是否正确,或者如何将其限制在最近5天内: def count = if (close > open) then 1 else 0; rec counter = counter[1] + count; plot scan = counter >= 5; 简易方法: #comment: declare five Booleans for eac

我想在thinkorswim中创建一个扫描,它返回的股票在过去5天中开盘4天或更长时间后收盘价高于开盘价

这是我目前拥有的代码,但我不知道它是否正确,或者如何将其限制在最近5天内:

def count = if (close > open) then 1 else 0;
rec counter = counter[1] + count;

plot scan = 
counter >= 5;
简易方法:

#comment: declare five Booleans for each of five days
def candle1 = close[5] > open[5];
def candle2 = close[4] > open[4];
def candle3 = close[3] > open[3];
def candle4 = close[2] > open[2];
def candle5 = close[1] > open[1];
#
#comment: add up all the green candles(value == 1) and red candles(value == 0)
def sumofgreencandles = candle1 + candle2 + candle3+ candle4 + candle5;
#
#comment: return one if 4 or five candles are green, zero if 3 or fewer are green-
plot fouroffivegreen= if sumofgreencandles > 3 then low else double.nan;
#
#comment: add an arrow to graph where condition exists:
fouroffivegreen.setpaintingstrategy(paintingstrategy.arrow_UP);
fouroffivegreen.setdefaultcolor(color.cyan);
这将在任何条或点下放置一个青色箭头,其中至少有五个周期中的最后四个周期关闭高于打开。我知道它看起来很笨重,但如果使用折叠语句,速度会慢得多,并且会延迟图表的渲染。

简单方法:

#comment: declare five Booleans for each of five days
def candle1 = close[5] > open[5];
def candle2 = close[4] > open[4];
def candle3 = close[3] > open[3];
def candle4 = close[2] > open[2];
def candle5 = close[1] > open[1];
#
#comment: add up all the green candles(value == 1) and red candles(value == 0)
def sumofgreencandles = candle1 + candle2 + candle3+ candle4 + candle5;
#
#comment: return one if 4 or five candles are green, zero if 3 or fewer are green-
plot fouroffivegreen= if sumofgreencandles > 3 then low else double.nan;
#
#comment: add an arrow to graph where condition exists:
fouroffivegreen.setpaintingstrategy(paintingstrategy.arrow_UP);
fouroffivegreen.setdefaultcolor(color.cyan);

这将在任何条或点下放置一个青色箭头,其中至少有五个周期中的最后四个周期关闭高于打开。我知道它看起来很笨重,但如果使用折叠语句,速度会慢得多,并且会延迟图表的呈现。

以下是一些备选方案:

  • 您是否希望过去4天或5天连续收盘走高?如果是,此选项将检查:


  • 如果收盘上涨的天数不需要连续:


  • 使用
    fold
    ,ThinkScript相当于
    for
    循环。这一项在最后5项中找到4项,但不一定是连续的:

*编辑以澄清
true
值等于1,而
false
值等于0

以下是一些备选方案:

  • 您是否希望过去4天或5天连续收盘走高?如果是,此选项将检查:


  • 如果收盘上涨的天数不需要连续:


  • 使用
    fold
    ,ThinkScript相当于
    for
    循环。这一项在最后5项中找到4项,但不一定是连续的:
*编辑以澄清
true
值等于1,而
false
值等于0

# set aggregation because you want the last 5 days, not the last 5 bars
def closeVal = close(period=AggregationPeriod.DAY);
def openVal = open(period=AggregationPeriod.DAY);

# define the condition you want met (it'll check for this on every bar)
def countCondition = closeVal > openVal;

# sum the last 5 bars (days in this case, due to aggregation setting)
# note: each `true` condition will equal 1; `false` is 0
def sumLast5 = countCondition + countCondition[1] + countCondition[2] + countCondition[3] + countCondition[4];

# plot the final desired match
plot scan = sumLast5 > 3;
# set the length - using `input` means you can edit in the scan settings
input length = 5;

# set aggregation because you want the last 5 days, not the last 5 bars
def closeVal = close(period=AggregationPeriod.DAY);
def openVal = open(period=AggregationPeriod.DAY);

# declare a `counter` variable, but we'll set its value in the `if` statement
def counter;

# ensure all data is available by checking to see if the 5th bar back is a number 
if !isNaN(closeVal[length - 1]){
    # we have a number, so let's fold...
    # `with count = 0` initializes a variable called `count` to 0
    # for i = 0 to length... (i will go 0 thru 4; it ends when it sees 5)
    # if closeVal[i] (the closeVal i bars back) is less than openVal[i]
    # then `count = count + 1` else `count` just remains the same as it last was
    # when the loop is done, put the `count` value into the `counter` variable
    counter = fold i = 0 to length with count = 0 do if getValue(closeVal, i) > getValue(openVal, i) then count + 1 else count;
}
else {
    # if the 5th bar back wasn't a number, we can't calculate
    #   so, simply set the counter to indicate it's Not a Number
    counter = Double.NaN;
}

plot scan = counter > 3;