我正在尝试编写一个尾随值,该值仅在 ThinkScript 中满足条件时调整为新值

如何解决我正在尝试编写一个尾随值,该值仅在 ThinkScript 中满足条件时调整为新值

类似于仅在出现新低或新高时才更改其值的预定义研究“PriceChannel”,我希望它仅在满足条件时更改其值,然后保持该值直到再次满足.

这是我到目前为止的代码,现在它检查最后一根柱线的“b”值,如果它 > 0,则绘制“b”,如果不是,则从最近的第二根柱线开始再次尝试,然后是第三个,依此类推,直到找到大于 0 的“b”值。

代码有效,但我必须为过去的每个第 n 个小节添加一个新的“else if”语句,300 个小节就足够了,但这意味着我必须输入同一行 300 次,然后每次只更改数字,我想避免这样做,另外,如果它检查 n=n+1 次,它会更干净。

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

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;

解决方法

您可以使用递归变量。有两种方法可以做到这一点:

  • 简单的递归变量:
def gVal = if b > 0 then b else gVal[1];
plot g = gVal;
  • CompoundValue 递归变量:
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 以了解其工作原理)。


我用于测试的代码:

  • 正则递归变量
#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);

  • 复合价值
#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,0)
    );
plot g = gVal;
g.SetDefaultColor(Color.CYAN);

AddChartBubble(yes,g,"b: " + b + ",g: " + g,yes);

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

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

6-Bar Chart showing results of test code for comparison

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?