I do not know why trading view is not able to backtest this code

122 Views Asked by At

I have created this code on pine and I though it would work but is does not. I have no error according to them but when I add the code to the graph I am not able to see where it buys and sells. Furthermore when I try strategy test it is not allowing me to backtest it. It does not show any data.

//@version=4
strategy("Bushiri project",default_qty_type=strategy.percent_of_equity, default_qty_value=2, pyramiding=5, initial_capital=1000, overlay=true)
// MTF analysis
len = input(8, minval=1, title="Length")
src = input(close, title="Source")
out = sma(src, len)
res = input(title="Resolution", type=input.resolution, defval="1D")
s1 = security(syminfo.tickerid, res, out, gaps=true)
plot(s1, color=color.blue)
len2 = input(21, minval=1, title="Length2")
src2 = input(close, title="Source2")
out2 = sma(src, len2)
res2 = input(title="Resolution2", type=input.resolution, defval="1D")
s2 = security(syminfo.tickerid, res2, out2, gaps=true)
plot(s2, color=color.yellow)

//Ema inputs 
fastemaLength= input(50, title="EMA Length", minval=1, maxval=200)
slowemaLength= input(200, title="EMA Length", minval=1, maxval=200)

//values 
fastemaVal=ema(close, fastemaLength)
slowemaVal=ema(close, slowemaLength)

//plot values 
plot(fastemaVal, title="EMA", color=color.red,  transp=2)
plot(slowemaVal, title="EMA", color=color.green,  transp=2)

// Entry requirement
dcross= s1>s2
ecross=crossover(fastemaVal, slowemaVal)
if(ecross and dcross) 
   strategy.entry(id="enterbuy", long=true, stop=20, comment="BUY")

//exit requirement
dcross1=s1>s2
ecross1=crossunder(fastemaVal, slowemaVal)
if(ecross1 and dcross1)
   strategy.close(id="enterbuy", comment="EXIT")
1

There are 1 best solutions below

1
On

You might have overlooked the fact that stop= 20 at 'strategy.entry(id="enterbuy", long=true, stop=20, comment="BUY")' will place an order at a price of 20. If you are trying this on the eurusd you will never place a trade because the price has never reaches 20 price. Another thing is that, as Bjorn Mistiaen said, this script rarely places trades. The condition to strategy.entry rarely takes place(specifically the dcross and the dcross1) and both ecross1 and dcross1 have to be true to also close the trade. You might have place a trade but it does not close. Look at your list of trades. Also don't forget to add the script to the chart by clicking "add to chart" at the pine editor