我已将 pine 脚本 V5 策略转换为 V6。 例如,当 sma_1 长度在设置中从 11 更改为 36 时,脚本不会计算,它会在前 2 笔交易后停止。
我已经更改了脚本,为 IF 条件之外的每个参数声明一个单独的变量,但问题仍然存在。
谢谢您的帮助。
V5中的策略:
//@version=5
strategy("V5_long", precision = 2, overlay=true, default_qty_type=strategy.cash, default_qty_value=50, currency=currency.USD, initial_capital=50, commission_type =strategy.commission.percent, commission_value =0.2)
// Input RSI
rsi_1_length = input.int(defval=27, title="RSI_1 Length", minval=1, maxval=100, step=1, group ='RSI')
rsi_2_length = input.int(defval=77, title="RSI_2 Length", minval=1, maxval=100, step=1, group ='RSI')
cross_bull= input.int(51, minval=1, maxval=99, step=1, group ='RSI')
cross_bear= input.int(50, minval=1, maxval=99, step=1, group ='RSI')
//input MA
sma_1_length = input.int(defval=10, title="SMA_1 Length", minval=1, maxval=200, step=1, group ='Moving Averages')
ema_1_length = input.int(defval=16, title="EMA_1 Length", minval=1, maxval=200, step=1, group ='Moving Averages')
// calculate MA
sma_1 = ta.sma(close, sma_1_length)
ema_1 = ta.ema(close, ema_1_length)
// Calculate RSI
rsi_1 = ta.rsi(close, rsi_1_length)
rsi_2 = ta.rsi(close, rsi_2_length)
// Detect Cross Overs
bullish_cross = ta.crossover(rsi_1, cross_bull) and ema_1 > sma_1
bearish_cross = ta.crossunder(rsi_2, cross_bear)
// Entry and Exit Conditions
if bullish_cross and strategy.position_size == 0 and barstate.isconfirmed
strategy.entry(id = "buy", direction=strategy.long, comment="B")
if bearish_cross and strategy.position_size > 0 and barstate.isconfirmed
strategy.close(id = "buy", comment="C")
在版本 6 中,我更改了“检测交叉”和“进入和退出条件”,但无法解决问题:
// Detect Cross Overs
bullish_cross_1 = ta.crossover(rsi_1, cross_bull)
bullish_cross_2 = ema_1 > sma_1
bearish_cross = ta.crossunder(rsi_2, cross_bear)
// Entry and Exit Conditions
if bullish_cross_1 and bullish_cross_2 and barstate.isconfirmed and strategy.position_size == 0
strategy.entry(id = 'buy', direction = strategy.long, comment = 'B')
if bearish_cross and strategy.position_size > 0 and barstate.isconfirmed
strategy.close(id = 'buy', comment = 'C')
还在版本 6 中尝试了以下操作:
// Detect Cross Overs
bullish_cross_1 = ta.crossover(rsi_1, cross_bull)
bullish_cross_2 = ema_1 > sma_1
bearish_cross = ta.crossunder(rsi_2, cross_bear)
entry_long = bullish_cross_1 and bullish_cross_2
// Entry and Exit Conditions
if entry_long and barstate.isconfirmed and strategy.position_size == 0
strategy.entry(id = 'buy', direction = strategy.long, comment = 'B')
if bearish_cross and strategy.position_size > 0 and barstate.isconfirmed
strategy.close(id = 'buy', comment = 'C')
这与惰性评估无关。这是关于默认保证金百分比。
在
v6
中,margin_long
和margin_short
参数的默认值为100。这意味着,只有在您有足够的资金的情况下才能进行交易。
可以在
0
代码中将这些参数更改为 v6
,或者如果您想获得相同的行为,也可以在 100
代码中将它们更改为 v5
。