求助一下,pine语言策略如何实现下单的循环,现在只能下一单就没有后续了

Author: CryptoLiu, Created: 2022-11-14 10:11:47, Updated:

//@version=4 strategy(title=“EMA crosses”, overlay=true)

// Inputs priceData = input(title=“Price data”, type=input.source, defval=hl2) ema1Length = input(title=“EMA 1”, type=input.integer, defval=12) ema2Length = input(title=“EMA 2”, type=input.integer, defval=24) ema3Length = input(title=“EMA 3”, type=input.integer, defval=36)

// Compute values ema1 = ta.ema(priceData, ema1Length) ema2 = ta.ema(priceData, ema2Length) ema3 = ta.ema(priceData, ema3Length)

enterLong = ema1 > ema2 and ema2 > ema3 enterShort = ema1 < ema2 and ema2 < ema3

// Plot values plot(series=ema1, color=color.orange, linewidth=2) plot(series=ema2, color=color.maroon, linewidth=2) plot(series=ema3, color=color.blue, linewidth=2)

// Submit orders if (enterLong) strategy.entry(id=“Enter Long”, long=strategy.long)

if (enterShort) strategy.entry(id=“Enter Short”, long=strategy.short)

目前这套代码在回测的时候总是在最开始的时候会下一单,然后其他的似乎就不触发了,哪位大佬可以指导一下怎么让策略循环执行起来呢,感恩。


More

小小梦 strategy.entry(id="Enter Long", long=strategy.long),这个函数调用写错了吧。 直接写:strategy.entry("Enter Long", strategy.long, 1) 这样就下单1手,张,一个币。

小小梦 需要设置金字塔加仓选项:strategy函数的pyramiding参数。

小小梦 666,加油~

CryptoLiu 还想咨询一下同方向持续触发信号的写法,目前的写法只能是一个方向开一次仓位,如果enterLong持续被触发,那么后面的触发没法下单。请教一下应该怎么写,能让我连续开多呢?

CryptoLiu 谢谢,可以了,又可以继续打怪通关了~

小小梦 ``` // Submit orders if enterLong and strategy.position_size <= 0 strategy.entry("Enter_Long", strategy.long, 1) // strategy.exit("exit_long", "Enter_Long", when = enterShort) if enterShort and strategy.position_size >= 0 strategy.entry("Enter_Short", strategy.short, 1) // strategy.exit("exit_short", "Enter_Short", when =enterLong) ``` 这里判断设置不合适,我修改了下正常了。如果你写成==0,那么开仓以后另一个就不会触发了,因为有持仓了就不等于0了。

CryptoLiu //@version=4 strategy(title="EMA_crosses", overlay=true) // Inputs priceData = input(title="Price_data", type=input.source, defval=hl2) ema1Length = input(title="EMA_1", type=input.integer, defval=12) ema2Length = input(title="EMA_2", type=input.integer, defval=24) ema3Length = input(title="EMA_3", type=input.integer, defval=36) // Compute values ema1 = ta.ema(priceData, ema1Length) ema2 = ta.ema(priceData, ema2Length) ema3 = ta.ema(priceData, ema3Length) enterLong = ema1 > ema2 and ema2 > ema3 enterShort = ema1 < ema2 and ema2 < ema3 // Plot values plot(series=ema1, color=color.orange, linewidth=2) plot(series=ema2, color=color.maroon, linewidth=2) plot(series=ema3, color=color.blue, linewidth=2) // Submit orders if enterLong and strategy.position_size == 0 strategy.entry("Enter_Long", strategy.long, 1) strategy.exit("exit_long", "Enter_Long", when = enterShort) if enterShort and strategy.position_size == 0 strategy.entry("Enter_Short", strategy.short, 1) strategy.exit("exit_short", "Enter_Short", when =enterLong) // Colour background backgroundColour = (strategy.position_size > 0) ? color.green : color.red bgcolor(color=backgroundColour, transp=85) 调整了一下,但是还是只有策略开始的时候触发了一次,后面就没有触发了。辛苦小小梦再帮忙看看是哪里出错了。 /upload/asset/573cd6ad4478a20fdb02.png