0

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

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

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

plot scan = 
counter >= 5;
4

2 回答 2

1

以下是一些替代方案:

  • 您是否希望过去 4 天或 5 天连续收盘走高?如果是这样,此选项将检查:
# 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;


  • 如果收盘价不需要连续:
# 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 个,但不一定是连续的:
# 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

于 2021-02-19T20:45:47.370 回答
1

简单的方法:

#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);

这将在至少五个周期中的最后四个收盘价高于开盘价的任何条形或点下方放置一个青色箭头。我知道它看起来很笨重,但如果你使用 fold 语句,它会慢得多,并且会延迟图表的呈现。

于 2019-10-26T19:38:46.587 回答