我是pine脚本的新手,我试图弄清楚如何进行符合以下条件的交易录入:
IF (3 consecutive candles close above ema 9) THEN (enter the trade)
。
到目前为止,当EMA线超过价格时,我所拥有的只是一个进入信号。
//@version=4
strategy("My Script", overlay = true)
ma = ema(close, 9)
entry() => crossover(close, ma)
exit() => crossover(close, ma)
strategy.entry(id = "long_ma", long = true, when = entry())
strategy.close(id = "long_ma", when = exit())
如何使用最后3根蜡烛输入?
答案 0 :(得分:2)
此代码定义了一个aboveMa
基本条件,当close
为> ma时为真。从该基本条件开始,我们使用history-referencing operator来指代某些条件,然后再组合这些条件以构建复合条件。
构建了两个版本的复合条件:
close
(aboveMa
定义中的threeAboveMa2
条件)而重新粉刷。对于每种情况,您都有两个条件:
标记有助于查看每个条件的触发位置。非重绘标记会比重绘标记晚触发一个小节,但是由于它正在查看的所有小节都是封闭的小节,因此它会更可靠。
//@version=4
strategy("My Script", overlay = true)
ma = ema(close, 9)
// Base condition
aboveMa = close > ma
// No repaint
threeAboveMa = aboveMa[1] and aboveMa[2] and aboveMa[3]
first3AboveMa = not aboveMa[4] and threeAboveMa
// Repaint
threeAboveMa2 = aboveMa and aboveMa[1] and aboveMa[2]
first3AboveMa2 = not aboveMa[3] and threeAboveMa2
// Debugging
plot(ma)
plotchar(aboveMa, "aboveMa", ".", location.top)
plotchar(threeAboveMa, "threeAboveMa", "•", location.top)
plotchar(first3AboveMa, "first3AboveMa", "▲", location.top)
plotchar(first3AboveMa2, "first3AboveMa", "▼", location.top)