Announcement

Collapse
No announcement yet.

Twiggs Money Flow

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

  • Twiggs Money Flow

    Hi all,
    I worte this EFS to implement Twiggs Money Flow concept but am having a problem with the calculation of the last bar on the chart. It is often way too lrg or small. I can't figure out what I'm doing wrong in the code.

    If anyone could take a look I would really appreciate it!

    Jonathan

  • #2
    screen shot

    Screenshot of the error.
    Attached Files

    Comment


    • #3
      Duh!
      Here is the efs.
      Attached Files

      Comment


      • #4
        Jonathan
        There are a couple of errors in your formula. The first one which is the cause of the indicator going heywire on the last bar (ie when computing in real time) is that you are compounding the values of Sum and SumVol on every tick instead of smoothing the corresponding prior values and then adding to them the current values of AD and Volume.
        The second error which can prevent the indicator to be calculated beyond a certain point is caused by a divide by 0 error which can occurr if the True Range ie TRH-TRL is equal to 0.
        The enclosed revision of the relevant section of your script should fix both these errors. Some comments are included in the script
        Alex

        PHP Code:
        var xInit false;
        var 
        xClose null;
        var 
        xHigh  null;
        var 
        xLow   null;
        var 
        xVol   null;
        var 
        Sum 0;//moved from line 13
        var Sum1 0;//added to store prior value of Sum
        var SumVol 0;//moved from line 14
        var SumVol1 0;//added to store prior value of SumVol
         
        function calcCMF(length,source){
         
            if(
        xInit == false){//reinstated this routine
                
        xClose close();
                
        xHigh  high();
                
        xLow   low();
                
        xVol   volume();
                
        xInit true;
            }
            
            if(
        getBarState()==BARSTATE_NEWBAR){//at every new bar
                
        Sum1 Sum;//assign to Sum1 the last computed value of Sum
                
        SumVol1 SumVol;//same logic as above
            
        }
            
            var 
        TRH Math.max(xHigh.getValue(0),xClose.getValue(-1));
            var 
        TRL Math.min(xLow.getValue(0),xClose.getValue(-1)); 
            var 
        TR  TRH-TRL;//compute True Range
            
        if(TR==0TR 999999;//assign a value if TrueRange = 0 to eliminate divide by 0 errors
            
        var AD = ((xClose.getValue(0)-TRL)-(TRH-xClose.getValue(0)))/TR*xVol.getValue(0);
         
            if(
        getCurrentBarCount()<= length) {
                
        Sum    Sum1+AD;//modified to sum AD to prior value of Sum not current
                
        SumVol SumVol1+xVol.getValue(0);//same logic as above
            
        } else {
                
        Sum Sum1*((length-1)/length)+AD;//prior value of Sum by coefficient plus current AD
                
        SumVol SumVol1*((length-1)/length)+xVol.getValue(0);//same logic as above
            
        }
         
         if(
        SumVol==0) return;
         return (
        Sum/SumVol);

        Comment


        • #5
          Alex,
          Thanks for the quick reply! I am still learning the language and didn't even realize there would be a problem with Tick data. Still have more to learn.
          Jonathan

          Comment


          • #6
            Jonathan
            The problem is not with tick data but with the fact that your formula computed those sums on every tick when run in real time.
            Alex

            Comment


            • #7
              Alex,
              I just reviewed the formula and can see that it now. Thanks for helping me see this. Formula works like a charm now on all chart time frames
              Take care,

              Jonathan

              Comment


              • #8
                Jonathan
                You are most welcome
                Alex

                Comment

                Working...
                X