首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在尝试编写一个仅在ThinkScript中满足条件时才调整为新值的拖尾值

我正在尝试编写一个仅在ThinkScript中满足条件时才调整为新值的拖尾值
EN

Stack Overflow用户
提问于 2021-03-26 02:22:19
回答 1查看 59关注 0票数 0

类似于PreDefined的研究"PriceChannel“,它只在出现新的低点或新高时改变它的值,我希望它只在满足某个条件时改变它的值,然后保持该值直到再次满足它。

这是我到目前为止的代码,现在它检查最后一个条形的"b“值,如果它> 0,那么它绘制"b",如果不是,它从第二个最近的条形开始,然后第三个,依此类推,直到它找到一个> 0的"b”值。

代码可以工作,但我必须添加一个新的"else if“语句为每第n条在过去,300条就足够了,然而这将意味着我将不得不键入相同的行300次,每次只是改变数字,我想避免这样做,另外,如果它检查n=n+1的次数,它会更干净。

对我应该做什么有什么建议吗?

代码语言:javascript
复制
plot b = if SMA30 crosses below 0 or
SZO crosses below 7 and SMA30 < SMA30[1]
then open
else 0;

plot g = if b>0
then b
else if b[1]>0
then b[1]
else if b[2]>0
then b[2]
else if b[3]>0
then b[3]
else 0;
EN

回答 1

Stack Overflow用户

发布于 2021-03-30 01:37:27

您可以使用递归变量。有两种方法可以完成此操作:

  • 简单递归变量:

代码语言:javascript
复制
def gVal = if b > 0 then b else gVal[1];
plot g = gVal;

  • CompoundValue递归变量:

代码语言:javascript
复制
def gVal = 
    CompoundValue(
      1, 
      if GetValue(b, 0) > 0 then GetValue(b, 0) else GetValue(gVal, 1),
      GetValue(b, 0)
    );
plot g = gVal;

通常,递归变量会工作得很好。如果你的代码中有不同的“长度”或“偏移量”,那么CompoundValue是必要的(查看我的answer here了解它是如何工作的)。

我用来测试的代码:

  • 常规递归变量

代码语言:javascript
复制
#hint: SO q: https://stackoverflow.com/q/66805478/1107226

def price_to_beat = 2.06;

declare lower;

# b could also be a plot; I had a separate plot, so I `def`d it here
def b =
    if open > price_to_beat
    then open
    else 0;

def gVal = if b > 0 then b else gVal[1];
plot g = gVal;

AddChartBubble(yes, gVal, "gVal:" + gVal, Color.YELLOW, no);

AddLabel(yes, "RecursiveVariable", Color.CYAN);

  • CompoundValue

代码语言:javascript
复制
#hint: SO q: https://stackoverflow.com/q/66805478/1107226

def price_to_beat = 2.06;

declare lower;

# b could also be a plot; I had a separate plot, so I `def`d it here
def b = if open > price_to_beat
        then open
        else 0;

def gVal = 
    CompoundValue(
      1, 
      if GetValue(b, 0) > 0 then GetValue(b, 0) else GetValue(gVal, 1),
      GetValue(b, 0)
    );
plot g = gVal;
g.SetDefaultColor(Color.CYAN);

AddChartBubble(yes, g, "b: " + b + ", g: " + g, Color.YELLOW, yes);

AddLabel(yes, "CompoundValue", Color.CYAN);

6条形图上的测试结果图像:

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

https://stackoverflow.com/questions/66805478

复制
相关文章

相似问题

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