如何在pinescript(Tradingview)中创建全局变量
How to make global variable in pinescript(Tradingview)
我正在努力在 tradingview 中创建一个趋势指标来跟踪趋势的走向。具体来说,我想要一个在几天内保持不变的变量,但是当满足特定条件时它会发生变化。看起来应该很简单,但每次我尝试时,我都会陷入一个永无止境的循环,而且我似乎无法绕过它。变量 "Trend"
///Condition
pos = close > open
neg = close < open
pos_cond = pos and pos[1]
neg_cond = neg and neg[1]
///Variables to keep track of trend
Trend = iff(***pos_cond or neg_cond not met***, Trend[1], Trend + real_trend)
trend_change_neg = iff(pos_cond, 1, 0)
trend_change_pos = iff(neg_cond, -1, 0)
real_trend = trend_change_neg + trend_change_pos
Trend = iff(Trend > 2, 2, iff(Trend < -2, -2, Trend))
/////////plots
plotshape(Trend > 0, color = color.green, location = location.top, style = shape.square, title="TrendLong")
plotshape( Trend == 0, color = color.yellow, location = location.top, style = shape.square, title = "TrendNeutral")
plotshape( Trend < 0, color = color.red, location = location.top, style = shape.square, title = "TrendShort")
所以基本上我想做的是保持 运行 趋势总计,每次有 2 根连续的蜡烛反对趋势时,它会切换到中性,但随着趋势继续朝 1 个方向移动它可以恢复到+-2(这是我们离中立的距离永远不会超过2 "pullbacks"。我已经为此绞尽脑汁好几天了,但是如果有人有任何想法,我们将不胜感激.
您需要使用变量。
示例:
var a = 0
a:=close>open?1:0
我正在努力在 tradingview 中创建一个趋势指标来跟踪趋势的走向。具体来说,我想要一个在几天内保持不变的变量,但是当满足特定条件时它会发生变化。看起来应该很简单,但每次我尝试时,我都会陷入一个永无止境的循环,而且我似乎无法绕过它。变量 "Trend"
///Condition
pos = close > open
neg = close < open
pos_cond = pos and pos[1]
neg_cond = neg and neg[1]
///Variables to keep track of trend
Trend = iff(***pos_cond or neg_cond not met***, Trend[1], Trend + real_trend)
trend_change_neg = iff(pos_cond, 1, 0)
trend_change_pos = iff(neg_cond, -1, 0)
real_trend = trend_change_neg + trend_change_pos
Trend = iff(Trend > 2, 2, iff(Trend < -2, -2, Trend))
/////////plots
plotshape(Trend > 0, color = color.green, location = location.top, style = shape.square, title="TrendLong")
plotshape( Trend == 0, color = color.yellow, location = location.top, style = shape.square, title = "TrendNeutral")
plotshape( Trend < 0, color = color.red, location = location.top, style = shape.square, title = "TrendShort")
所以基本上我想做的是保持 运行 趋势总计,每次有 2 根连续的蜡烛反对趋势时,它会切换到中性,但随着趋势继续朝 1 个方向移动它可以恢复到+-2(这是我们离中立的距离永远不会超过2 "pullbacks"。我已经为此绞尽脑汁好几天了,但是如果有人有任何想法,我们将不胜感激.
您需要使用变量。 示例:
var a = 0
a:=close>open?1:0