pine-script

Issues with lazy evaluation of buying and selling conditions - V6


I have converted a pine script V5 strategy into a V6. For example, when sma_1 length is changed in the settings from 11 to 36 the script doesn´t calculate, it stops after the first 2 trades.

I have changed the script, by declaring a separate variable to each argument outside the IF condition, but the issue persists.

Thank you for your help.

The strategy in 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")

In Version 6 I have changed the "Detect Cross Overs" and "Entry and Exit conditions" but was not able to resolve the issue:

// 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')

Also tried in Version 6 the following:

// 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')

Solution

  • This is not about the lazy evaluation. It is about the Default margin percentage.

    In v6, the default value of the margin_long and margin_short parameters is 100. This means, you can only enter a trade if you have enough capital.

    Either change those parameters to 0 in your v6 code, or change them to 100 in your v5 code if you want to have the same behavior.