繁体   English   中英

TradingView Pinescript,为什么三元运算符在这个 plot 示例中不起作用?

[英]TradingView Pinescript, why isn't the ternary operator working in this plot example?

我在 pinescript 中有一个非常简单的策略。

//@version=4
strategy("My Strategy", overlay=true)

longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)
    
...

我想 plot 一行值为 0 或 1,具体取决于 position 大小是否为 0。

...
x = strategy.position_size == 0 ? 0 : 1
plot(x)

然而,绘制 x 的结果是一条仅在值 1 处的线。在图表上它清楚地显示在某些点上没有开仓。 因此,变量position_size在某些时候应该为 0,但它似乎只有 1。为什么会这样?

您的策略从多头转向空头,并在第一次交易后不断处于 position。 请参阅随附的屏幕截图。 在此处输入图像描述

您应该在反向信号出现之前使用strategy.exitstrategy.close函数退出 position。

例子:

// Exit long after 2 candles from the entry
exitLong = nz(longCondition[2])
if exitLong
    strategy.close("My Long Entry Id", exitLong)

在此处输入图像描述

暂无
暂无

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

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