Hello.
I designed a simple code but it's giving me a hard time.
The setup is based on a HULL MA.
Green line - MA HULL
Red line - MA HULL with -1 offset
Cyan flatline - entry price
Yellow flatline - stop price
The rules are:
Buy - green line > red line
buy at open
stop = open - fixed % (var st)
taget = close price
Sell - green line < red line
sell at open
stop = open + fixed % (var st)
taget = close price
Everyday It should place a trade and close it at stop price if hitted or at close price.
And that's where the problem is. Some signals are being filtered but I don't know why.
I tried everything but can't get why thats happening.
If look at the picture below you'll see that the yellow flatline (stop price) sometimes simply doesn't change and when that happens you see no cyan flatline (entry price).
Why some signals are being filtered???
I designed a simple code but it's giving me a hard time.
The setup is based on a HULL MA.
Green line - MA HULL
Red line - MA HULL with -1 offset
Cyan flatline - entry price
Yellow flatline - stop price
The rules are:
Buy - green line > red line
buy at open
stop = open - fixed % (var st)
taget = close price
Sell - green line < red line
sell at open
stop = open + fixed % (var st)
taget = close price
Everyday It should place a trade and close it at stop price if hitted or at close price.
And that's where the problem is. Some signals are being filtered but I don't know why.
I tried everything but can't get why thats happening.
If look at the picture below you'll see that the yellow flatline (stop price) sometimes simply doesn't change and when that happens you see no cyan flatline (entry price).
Why some signals are being filtered???
Code:
//PARAMS MA HULL var preco = "hl2()"; var periodo = 16; var coeficiente1 = 0.5; var coeficiente2 = 0.5; var expoente = 0.6; var offsetx = 0; var movmin = 5; //STOP PCT var st = 0.002; /********************************* BACKTEST *********************************/ var xLotSize = 1; function preMain() { setPriceStudy(true); setStudyTitle("MOV CONT"); setCursorLabelName("MOV CONT", 0); setDefaultBarThickness(1, 0); setPlotType(PLOTTYPE_FLATLINES, 0); setDefaultBarFgColor(Color.yellow,0); setDefaultBarThickness(1, 1); setPlotType(PLOTTYPE_LINE, 1); setDefaultBarFgColor(Color.lime,1); setDefaultBarThickness(1, 2); setPlotType(PLOTTYPE_LINE, 2); setDefaultBarFgColor(Color.red,2); setDefaultBarThickness(1, 3); setPlotType(PLOTTYPE_FLATLINES, 3); setDefaultBarFgColor(Color.cyan,3); setColorPriceBars(true); } // Global Variables var sig = null; var hullline = null; var hulllineX = null; var bInit = false; var oscx = null; var stox = null; var nTarget = null; var nStop = null; function main() { if (getCurrentBarIndex() == 0) return; if(bInit == false) { sig = efsInternal("signalhullincl"); hullline = getSeries(sig,1); hulllineX = getSeries(sig,2); bInit = true; } var nSig0 = sig.getValue(0); var nSig_1 = sig.getValue(-1); var hullline0 = hullline.getValue(0); var hullline1 = hulllineX.getValue(0); var bExitFlag = false; if(nSig0 == null) { return; } if (Strategy.isInTrade() == false) { nTarget = null; nStop = null; } // Exit Strategy if (Strategy.isLong() == true) { // Long Profit Target Exit if (low(0) <= nStop) { Strategy.doSell("Long Exit", Strategy.LIMIT, Strategy.THISBAR, xLotSize, nStop); } else { Strategy.doSell("Long Exit", Strategy.LIMIT, Strategy.THISBAR, xLotSize, close(0)); } } if (Strategy.isShort() == true) { // Short Profit Target Exit if ( high(0) >= nStop ) { Strategy.doCover("Short Exit", Strategy.LIMIT, Strategy.THISBAR, xLotSize, nStop); } else { Strategy.doCover("Short Exit", Strategy.LIMIT, Strategy.THISBAR, xLotSize, close(0)); } } // Entry Strategy if (Strategy.isLong() == false && nSig_1 == 1 ) { // Long Trade Signal var nEntry = open(0); Strategy.doLong("Long Signal", Strategy.LIMIT, Strategy.THISBAR, xLotSize, nEntry ); nStop = open(0)*(1-st); } else if (Strategy.isShort() == false && nSig_1 == -1 ) { // Short Trade Signal var nEntry = open(0); Strategy.doShort("Short Signal", Strategy.LIMIT, Strategy.THISBAR, xLotSize, nEntry ); nStop = open(0)*(1+st); } if(Strategy.isLong()) { setBarBgColor(Color.darkgreen, 0); } else if(Strategy.isShort()) { setBarBgColor(Color.maroon, 0); } return new Array(nStop, hullline0, hullline1, nEntry); } //SIGNAL FORMULAS var bInitx = false; var indd = null; var xMA = null; var calculo = null; var ponderada1 = null; var ponderada2 = null; var xMAF = null; function signalhullincl() { if(bInitx == false){ var periodo3 = Math.pow(periodo,expoente); var periodo1 = validar(periodo,coeficiente1); var periodo2 = validar(periodo,coeficiente2); if(periodo3<2){ periodo3=2; } periodo3 = Math.round(periodo3); ponderada1 = wma(periodo1,eval(preco)); ponderada2 = wma(periodo2,eval(preco)); calculo = efsInternal('calculado', ponderada1, ponderada2); xMA = wma(periodo3,calculo); bInitx = true; } xMAF = getSeries(xMA); if(xMA.getValue(-offsetx) > xMA.getValue(-1-offsetx) ) { indd = 1 } else if(xMA.getValue(-offsetx) < xMA.getValue(-1-offsetx) ) { indd = -1 } /*else { indd = 0 }*/ return new Array(indd,xMA.getValue(-offsetx),xMA.getValue(-1-offsetx)) ; } function calculado(ponderada1, ponderada2){ var valorponderada1 = getSeries(ponderada1); var valorponderada2 = getSeries(ponderada2); return 2*valorponderada1-valorponderada2; } function validar(periodo, coeficiente){ var valor = periodo*coeficiente; if(valor<2){ return 2 } else { return Math.round(valor); } }
Comment