繁体   English   中英

如何在 PineScript 中声明条件的考虑期?

[英]How to declare a consideration period for conditions in PineScript?

如何在 PineScript 中声明条件的考虑期?

我希望 PineScript 检查两个条件是否在同一时间段内得到满足。

例子:

指数值 = 0

如果在同一周内,RSI > 20 且 MACD > 25,则 IndexValue 应增加 1 (+1)。 这意味着这两个指标不必在同一天为真。 即使两者在同一周的不同日子产生信号,条件也被视为满足,IndexValue 应增加 1。

因此,程序应该在 7 天的周期内简单地检查这两个条件,如果在同一周内两者都为真,则将索引值加一。

提前谢谢了

//
// use request.security to access the values from the same week
// 
RSIval = request.security(syminfo.tickerid, "W", ta.rsi(..) )
MACDval = request.security(syminfo.tickerid, "W", ta.macd(..) )
if RSIval > 20 and MACDval > 25
...

在计算 rsi 和 macd 之后,您可以创建一个变量,只要在一周内满足这两个条件,该变量就会设置为 true。 在周末,我们将检查变量和增量索引,并为下周重置变量。 下面的例子

//@version=5
indicator("My script")

rsi=ta.rsi(close,14)
[macd,signal,histogram]=ta.macd(close,12,26,9)
var IndexValue =0 
var conditiontrueinweek=false
if rsi>20 and macd>1
    conditiontrueinweek:=true
bi=request.security(syminfo.tickerid,"W",bar_index)
if bi>bi[1] //check if new week has started
    if conditiontrueinweek
        IndexValue:=IndexValue+1
    conditiontrueinweek:=false
plot(IndexValue)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM