首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Thinkscript:递归计数器

Thinkscript:递归计数器
EN

Stack Overflow用户
提问于 2019-09-15 05:54:24
回答 2查看 900关注 0票数 0

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

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

代码语言:javascript
复制
def count = if (close > open) then 1 else 0;
rec counter = counter[1] + count;

plot scan = 
counter >= 5;
EN

回答 2

Stack Overflow用户

发布于 2019-10-27 03:38:47

简单的方法:

代码语言:javascript
复制
#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);

这将在任何条或点下放置一个青色箭头,其中至少有五个阶段中的最后四个阶段收盘高于开盘。我知道它看起来很笨拙,但是如果你使用一个折叠语句,它会慢得多,并且会延迟你的图表的渲染。

票数 1
EN

Stack Overflow用户

发布于 2021-02-20 04:45:47

以下是一些替代方案:

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

代码语言:javascript
复制
# set aggregation because you want the last 5 days,
#   not the last 5 bars regardless of time set
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 4 bars (days in this case, due to aggregation setting)
# note: the indexes are pointing to the current bar, 1 bar prior, 2 bars prior, and 3 bars prior
# also, each `true` condition will equal 1; `false` is 0
def sumLast4 = countCondition + countCondition[1] + countCondition[2] + countCondition[3];

# ensure last 4 all closed higher than they opened
def last4Condition = (sumLast4 == 4);

# if the last 4 days met the condition, then add in the 5th day prior;
# else, just use the sum of those last 4
def total = if last4Condition then sumLast4 + countCondition[5] else sumLast4;

# plot the final desired condition:
#   at least the last 4 days closed higher than their open
plot scan = total >= 4;

如果收高的日子不需要是连续的,

代码语言:javascript
复制
# 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;

使用fold

  • ,ThinkScript的等价物是for循环。这个可以找到最后5个中的4个,但不一定是consecutive:

代码语言:javascript
复制
# 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;

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57939548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档