Announcement

Collapse
No announcement yet.

Hull ma strategy for daily chart

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hull ma strategy for daily chart

    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???


    Click image for larger version

Name:	Chart20150325145114.png
Views:	1
Size:	58.6 KB
ID:	246993


    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);
        }
    }

  • #2
    need a back testing code for the bollinger bands tool kit in esignal software

    Originally posted by bdfranca View Post
    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???


    [ATTACH=CONFIG]16751[/ATTACH]


    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);
        }
    }
    hi,

    I have seen your code development for the strategy.. plz can u prepare the coding for the BBTK indicators and also the Back Testing Indicators.

    Comment


    • #3
      Hello, I'm a begginer on coding. Really need some help too but I really don't know how to get that here.
      Can't guarantee to you that the code I can write would be right. I could get you wrong results.

      Comment


      • #4
        I really would like to know what do I have to do to get any help here.
        Not the first time that I get no response.......

        Comment


        • #5
          bdfranca
          If you actually look at the back test report you will see that none of the trades are opened and closed on the same day.
          Because an efs will iterate once only per bar on historical bars and you want to enter and exit a trade on the same bar you need to reverse your trading logic ie you need to first execute the entries and then the exits (else the trade gets carried over to the following day or later if the exit condition is not met on that or subsequent bars).
          While at it you may want to simplify things and use Strategy.MARKET (which equates to the Open) to enter the trades and Strategy.CLOSE (which equates to the Close) to exit the trades on the Close and avoid using Strategy.LIMIT in those two conditions
          Once you do the above the script will function as you expect.
          Alex


          Originally posted by bdfranca View Post
          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???


          [ATTACH=CONFIG]16751[/ATTACH]


          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


          • #6
            ACM,
            many thanks! Helped a lot!
            I'll work on that during the weekend.

            Comment


            • #7
              bdfranca
              You are welcome
              Alex


              Originally posted by bdfranca View Post
              ACM,
              many thanks! Helped a lot!
              I'll work on that during the weekend.

              Comment

              Working...
              X