Search code examples
pine-scriptmoving-averagecrossover

Backtest not showing any results for a SMA crossover script in tradingview


Trying below script in Pine Script version 5. No errors in scripts. But, it doesn't show any results in backtest. Can anyone please help?

code bold italic

quote ****//@version=5

strategy(shorttitle='Long on MA Crossover',title='Long Strategy with MA Crossover', overlay=true, initial_capital = 20000, currency = currency.USD, process_orders_on_close=true, default_qty_type = strategy.cash, default_qty_value = 20000, commission_type=strategy.commission.percent, commission_value=0.075)


//Backtest dates
fromMonth = input.int(defval = 1,    title = "From Month",  minval = 1, maxval = 12)
fromDay   = input.int(defval = 1,    title = "From Day",    minval = 1, maxval = 31)
fromYear  = input.int(defval = 2019, title = "From Year",   minval = 1970)
thruMonth = input.int(defval = 1,    title = "Thru Month",  minval = 1, maxval = 12)
thruDay   = input.int(defval = 1,    title = "Thru Day",    minval = 1, maxval = 31)
thruYear  = input.int(defval = 2112, title = "Thru Year",   minval = 1970)

showDate  = input.bool(defval = true, title = "Show Date Range")

start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // backtest start window
finish    = timestamp(thruYear, thruMonth, thruDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false       // create function "within window of time"

len1 = input.int(9, minval=1, title="MA1")
len2 = input.int(50, minval=1, title="MA2")
len3 = input.int(100, minval=1, title="MA3")
len4 = input.int(200, minval=1, title="MA4")

ma1 = ta.sma(close, len1)
ma2 = ta.sma(close, len2)
ma3 = ta.sma(close, len3)
ma4 = ta.sma(close, len4)

Take_profit= ((input (3))/100)

longTakeProfit = strategy.position_avg_price * (1 + Take_profit)

closeLong = close > longTakeProfit or ta.crossover(ma2,ma1)

//Entry 
strategy.entry("buy", strategy.long, limit=close[1], when = ta.crossover(ma1,ma2) and ma2 > ma3 and ma3 > ma4 and window())

//Exit
strategy.close("sell", when = closeLong and window())****

Solution

  • the error is in the last line , strategy.close("sell", when = closeLong and window()) when you use the strategy.close() function then you have to provide the same trade id as in the strategy.entry() function , you have placed a unknown id "sell" which is never used to enter any trade with this ID. The following is the updated code and working code.

    //@version=5
    strategy(shorttitle='Long on MA Crossover',title='Long Strategy with MA Crossover', overlay=true, initial_capital = 20000, currency = currency.USD, process_orders_on_close=true, default_qty_type = strategy.cash, default_qty_value = 20000, commission_type=strategy.commission.percent, commission_value=0.075)
    
    
    //Backtest dates
    fromMonth = input.int(defval = 1,    title = "From Month",  minval = 1, maxval = 12)
    fromDay   = input.int(defval = 1,    title = "From Day",    minval = 1, maxval = 31)
    fromYear  = input.int(defval = 2019, title = "From Year",   minval = 1970)
    thruMonth = input.int(defval = 1,    title = "Thru Month",  minval = 1, maxval = 12)
    thruDay   = input.int(defval = 1,    title = "Thru Day",    minval = 1, maxval = 31)
    thruYear  = input.int(defval = 2112, title = "Thru Year",   minval = 1970)
    
    showDate  = input.bool(defval = true, title = "Show Date Range")
    
    start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // backtest start window
    finish    = timestamp(thruYear, thruMonth, thruDay, 23, 59)        // backtest finish window
    window()  => time >= start and time <= finish ? true : false       // create function "within window of time"
    
    len1 = input.int(9, minval=1, title="MA1")
    len2 = input.int(50, minval=1, title="MA2")
    len3 = input.int(100, minval=1, title="MA3")
    len4 = input.int(200, minval=1, title="MA4")
    
    ma1 = ta.sma(close, len1)
    ma2 = ta.sma(close, len2)
    ma3 = ta.sma(close, len3)
    ma4 = ta.sma(close, len4)
    
    Take_profit= ((input (3))/100)
    
    longTakeProfit = strategy.position_avg_price * (1 + Take_profit)
    
    closeLong = close > longTakeProfit or ta.crossover(ma2,ma1)
    
    //Entry 
    strategy.entry("buy", strategy.long, limit=close[1], when = ta.crossover(ma1,ma2) and ma2 > ma3 and ma3 > ma4 and window())
    
    //Exit
    strategy.close("buy", when = closeLong and window())