in Search

FisherXForm divide by Zero error

Last post 06-19-2007, 22:37 by wabbit. 12 replies.
Sort Posts: Previous Next
  •  06-17-2007, 16:11 24387

    FisherXForm divide by Zero error

    I have been experiencing the dreaded 'divide by zero' for the FisherXForm(from EL code by  John Ehlers).

    My 'v1' for MS code worked fine for daily and weekly charts. However, when I put it on 1 minute
    bars, MS was not happy....lots of
    'divide by zero' errors and a lot if wasted time/money.
    (I was using MS v 9.0 Pro for RT data)

    Here is my MS v1 code;

    Code:

    ********************************************************************
    v1 FisherXForm   {John Ehlers}
    ********************************************************************
    Len:=Input("FishLen",1,16,10);
    FishULvl:= Input("FishULvl",1,6,3);
    FishLLvl:= Input("FishLLvl",-6,-1,-3);

    Pr:=(H + L)/2;

    MaxH:= HHV(pr,Len);
    MinL:= LLV(pr,Len);

    Val1:= .33 * 2 * ((Pr - MinL)/ (MaxH - MinL) - .5) + .67 * PREV;
    Value1:= If(Val1 > .99,.999,If(Val1 < -.99,  -.999,val1));
    Fish:= .5 * Log((1 + Value1)/(1 - Value1)) + .5 * PREV;

    Fish;
    Ref(Fish,-1);
    FishULvl;
    FishLLvl;
    0;
    **********************************************************************


    Here is my 'v2' that I mangled somewhere. It is not giving the
    'divide by zero' error consistently?? It usually works OK when
    I am loading the chart/template, but if I change to a very small length
    parameter(2,4,etc), it throws the error. Something should be obvious
    to me here, but I'm not seeing it for some reason.   Sad [:(]

    [Removed at the request of the gruvtune, by wabbit]

    The next bit of code is from.....ah ah....well it doesn't give a 'divide by zero' error.

    Inputs: Price((H + L)/2), Len(10), Cline(0);

    Vars: MaxH(0), MinL(0), Fish(0);

    MaxH = Highest(Price, Len);
    MinL = Lowest(Price, Len);

    If MaxH - MinL <> 0 then Value1 = .33 * 2 * ((Price - MinL)/ (MaxH - MinL) - .5) + .67 * Value1[1];
       
    If Value1 > .99 then Value1 = .999;
    If Value1 < -.99 then Value1 = -.999;   
       
    If Value1 <> 1 then
       
    Fish = .5 * Log((1 + Value1)/(1 - Value1)) + .5 * Fish[1];

    Plot1(Fish,"Fisher");

    Plot2(Fish[1],"Trigger")
    **********************************************************************************************
    The divide by zero error is prevented by  '
    If MaxH - MinL <> 0 then'

    I was attempting do the same thing in my 'v2' MS code and went into brainlock!
    I ended up with this, which is not quite correct/consistent or calibrated to the other....
    and still gives the 'divide by zero' error..whenever it wants to

    Val1:= .33 * 2 * ((Pr - MinL)/ (MaxH - MinL) - .5) + .67 * PREV;
    If(MaxH - MinL <> 0, Val1 = .33 * 2 * ((PRICE - MinL)/ (MaxH - MinL) - .5) + .67 * PREV,MaxH - MinL);

    Value1:= If(Val1 > .99,.999,If(Val1 < -.99,  -.999,val1));
    *********************************************************************************************
    Some data points;

    MS   ER2 U7  6/15/2007 1 min bar at 2:15 PM (USA)
            Fish = 2.17         length = 6
            Trigger = 1.79

    TS  
    ER2 U7  6/15/2007 1 min bar at 2:15 PM (USA)
            Fish = 2.07       
    length = 6
            Trigger = 1.68

    MS   ES U7  6/15/2007 1 min bar at 2:15 PM (USA)
            Fish = -1.74      
    length = 10
            Trigger = -1.27

    TS  
    ES U7  6/15/2007 1 min bar at 2:15 PM (USA)
            Fish = -1.70      
    length = 10
            Trigger = -1.30

    Those values for Fish and Trigger ought to be close enough for 'jazz'....
    If anyone has the time or inclination to look at this.....I'm sure some other
    folks could get some good use of this...

    Thanks,
    gruvtune

  •  06-17-2007, 20:15 24391 in reply to 24387

    Re: FisherXForm divide by Zero error

    hey gruvtune...... sometimes adding a small number to the divsor handles that..... as example if you change  (MaxH - MinL )  to (MaxH - MinL +.000001) in each place it occurs it might solve the errors......  the number must be large enough to be recognized but small enough to not have a  negative  impact on the calculations......

    interesting  indicator..... thanks....h

  •  06-17-2007, 20:45 24392 in reply to 24391

    gruv's fisher exploration

    made a quick exploration for gruv's fisher....  used his v2 version but named it fisher gruvtune.....  column 1 returns new up crosses and column 2 returns down crosses.....  obviously the exploration has vast room for improvement but looks promising....... thanks again gruv........h

     

     -------------------------------------------------------- 
    Exploration notes  
      
    Col A: buy 

    a:=   FmlVar("fisher gruvtune","FISH") ; 
    aa:=  Ref(a,-1); 
    Cross(a,aa); 


    Col B: sell 

    a:=   FmlVar("fisher gruvtune","FISH") ; 
    aa:=  Ref(a,-1); 
    Cross(aa,a); 


    Filter colA OR colB 
      
    Filter enabled Yes 
    Periodicity Daily 
    Records required 61 

    ----------------------------------------------------------------

  •  06-18-2007, 0:37 24394 in reply to 24392

    Re: gruv's fisher exploration

    gruvtune,

    Not only should you trap the DBZ error but also the Log overflow errors too!  The 'x' values are bounded by 1 and -1, and both of these extremes need to be accounted for.  If x=1 a DBZ error will occur, if x=-1 a Log Overflow error will occur

    I added your stuff to the exisitng stuff I had and came up with this:

    Code:

    {adaption of http://www.traders.com/Documentation/FEEDbk_docs/Archive/112002/TradersTips/TradersTips.html#meta2 and
    http://forum.equis.com/forums/thread/24387.aspx
    }

    len:=Input("FishLen",1,16,10);
    FishULvl:= Input("FishULvl",1,6,3);
    FishLLvl:= Input("FishLLvl",-6,-1,-3);

    pr:=(H+L)/2;
    maxh:=HHV(pr,len);
    minl:=LLV(pr,len);

    x:=(2/3)*(((pr-minl)/(Max(maxh-minl,0.00001))-.5)+PREV);
    ft:=0.5*(Log(If(x=-1,0.00001,1+x)/If(x=1,0.00001,1-x))+PREV);

    {plot/return}
    ft;
    Ref(ft,-1);
    FishULvl;
    FishLLvl;
    0;


    which should be error free!!!


    Hope this helps.

    wabbit Big Smile [:D]


    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  06-18-2007, 7:14 24395 in reply to 24394

    Re: gruv's fisher exploration

    hey wabbit..... still getting divsion by 0 errors with your code....  i created a template with both indicators and scrolled thru the nas 100,  to test.....  

     below is gruv's exact code except added the " .000001 " ....  h

     

    -----------------------------------------------------------------

    Len:=Input("FishLen",1,16,4);
    FishULvl:= Input("FishULvl",1,6,2);
    FishLLvl:= Input("FishLLvl",-6,-1,-2);

    Pr:=(H + L)/2 ;

    MaxH:= HHV(pr,Len);
    MinL:= LLV(pr,Len);

    Val1:= .33 * 2 * ((Pr - MinL)/ (MaxH - MinL +.000001) - .5) + .67 * PREV +.000001 ;
    If(MaxH - MinL <> 0, Val1 = .33 * 2 * ((PRICE - MinL)/ (MaxH - MinL +.000001) - .5) + .67 * PREV,MaxH - MinL);

    Value1:= If(Val1 > .99,.999,If(Val1 < -.99,  -.999,val1));


    Fish:= .5 * Log((1 + Value1)/(1 - Value1)) + .5 * PREV +.000001;

    Val1:= .33 * 2 * ((Pr - MinL)/ (MaxH - MinL +.000001) - .5) + .67 * PREV;
    Value1:= If(Val1 > .99,.999,If(Val1 < -.99,  -.999,val1));
    Fish:= .5 * Log((1 + Value1)/(1 - Value1)) + .5 * PREV +.000001;

    Fish;
    Ref(Fish,-1);
    FishULvl;
    FishLLvl;
    0;

    -----------------------------------------------------------------

  •  06-18-2007, 8:30 24396 in reply to 24395

    Re: gruv's fisher exploration

    h,

    fixed small typo.... please try again

    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  06-18-2007, 10:38 24397 in reply to 24396

    Re: gruv's fisher exploration

    hey wabbit..... that fixed it.....  after all these years i finally see what yall are refering to about avoiding the 'prev' function..... try putting 4 versions of that indicator on the same template and scroll thru a folder looking for errors.....   talk about slow...... h
  •  06-18-2007, 16:07 24401 in reply to 24397

    Re: fisherXForm code, exploration, etc

    Hayseed and wabbit,

    Guys, thanks so much for your insights and efforts!! I think wabbits new code did the trick. I just tried loading RT ES data(1 min bars, after the mkt close) and MS didn't get upset! So that is a big improvement. I hope other folks can get some use out of this new and improved code.

    By the time I made it here this afternoon, I had already tried Hayseed's recommendation of adding the 0.00001 to the code. I used that on what I referred to in my original post as 'v1'. It seemed to work fine, but the value's at each point I looked at were not the same as...well..that other thing...:)

    I have only had a few minutes to look at wabbits new code...all the primary crossover of zero pts, max peaks and troughs, all happen at the same time as the other thing! And as corny as this may sound, it looks the same!!! The values at each point are still not not the same, but I don't think that is an issue. (I'm definitly not complaining either) I think the addition of the 0.00001 has a lot to do with those deltas.

    Before I forget, I would suggest that what I called 'v2' in my original post, people should avoid at all costs!
    (Can I, or someone(?, moderator?) edit that out of the original post? I don't want that 'stuff' misleading
    anyone/casuing problems. Or put a disclaimer in there!

    Also, for those that may not know, the FisherXForm is a well known statistical funcion and John Ehlers put that and many other nice calculations out into the public domain, via his books('Rocket Science for Traders' and 'Cybernetic Analysis for Stocks and Futures' and various .ppt shows) Some of which were already in MS code, like the MAMA and FAMA, etc.

    I will have to modify my Fisher explorations now and when get those finished and checked out, I will post
    one or two, if anyone is interested. Hayseed was all over that one pretty quickly!

    Once again, Hayseed & wabbit, thanks for the suggestions/help.
    gruvtune

  •  06-19-2007, 19:44 24410 in reply to 24401

    Re: fisherXForm code, exploration, etc

    gruvtune,

    Although Hayseed's method to avoid DBZ errors may have worked, you might have noticed the effect that adding even a very small number into every computation may have.  This is the reason why I prefer to look for those instances when an error may be generated, and only then, insert a new value to prevent the error.

    You also noticed that my code is slightly different from what was posted in the TASC magazine as I use in the first variable a muliplier of (2/3) when Ehler used 0.33 * 2 and 0.67.  This will have a small difference, but it makes more (mathematical) sense to me.

    In terms of small numbers, try for yourself replacing 0.00001 with Power(10,-30) or similar (0.0000000000000000000000000000001) and see if this makes a difference.  Sometimes, making an "interpretation" about what is a small number and what is not can skew the results.  Note: not all the MS functions have the same numerical limits, there are different limiting values for Log(), division etc so you might cannot globally use the same zero-representative.  The same concepts apply to making large numbers in MS too.  Sometimes, it might be better to just program the function output instead of the function input, but this again leads to more error management code that is not that friendly.



    Hope this helps.

    wabbit Big Smile [:D]


    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  06-19-2007, 21:56 24414 in reply to 24410

    Re: Hope this helps,fisherXForm code, exploration, etc

    wabbit,

    Your last post makes perfect sense. I certainly was not aware of some of the finer points you mentioned, such as
    "
    not all the MS functions have the same numerical limits", etc. Will have to replace the 0.00001 with the
    Power(10,-30) as you suggest.

    I just plotted your improved Fisher on some daily bars, directly below what I called my v1 code.
    Exactly the same....all values that I looked at were the same on daily bars, so therefor I would expect
    the valuesfor weekly bars to be the same.

    What I think I will do is retain my v1 for daily/weekly bars, so I don't have to tweak any of my explorations
    for daily/weekly bars. I would use your Fish code for intraday bars! Problem solved!

    Thanks again for the good work and taking care of the edit I requested,
    gruvtune
  •  06-19-2007, 22:08 24415 in reply to 24414

    Re: Hope this helps,fisherXForm code, exploration, etc

    gruvtune,

    The smallest positive number I could find to approximate zero in the code above is 0.000000000000000000000000000000000000001 which does not produce any errors at all on my computer.  Is this the case on your machine?  Every machine?

    Code:

    {adaption of http://www.traders.com/Documentation/FEEDbk_docs/Archive/112002/TradersTips/TradersTips.html#meta2 and
    http://forum.equis.com/forums/thread/24387.aspx}

    len:=Input("FishLen",1,16,10);
    FishULvl:= Input("FishULvl",1,6,3);
    FishLLvl:= Input("FishLLvl",-6,-1,-3);

    {approximate zero}
    zero:=Power(10,-38);

    pr:=(H+L)/2;
    maxh:=HHV(pr,len);
    minl:=LLV(pr,len);

    x:=(2/3)*(((pr-minl)/(Max(maxh-minl,zero))-.5)+PREV);
    ft:=0.5*(Log(If(x=-1,zero,1+x)/If(x=1,zero,1-x))+PREV);

    {plot/return}
    ft;
    Ref(ft,-1);
    FishULvl;
    FishLLvl;
    0;



    Hope this helps.

    wabbit Big Smile [:D]

    [edited : forgot to thest the x=1 scenario!  limit is now 10E-38 on my machine]


    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  06-19-2007, 22:25 24416 in reply to 24410

    Re: fisherXForm code, exploration, etc

    hey wabbit..... math can be funny  , as you know...... adding a large number  can have  no effect  some times and other times adding a small number can have a large effect.....  this is most often seen when the number is added to the divisor......  what effect did you notice with the .000001  other than a small difference in calculation.... which really has no impact..... 

    i  ran an exploration of both codes where fisher crossed its ref-1 and also where fisher crossed 0.... both versions returned the exact same results....

    there is a rule in complex math that claims , ' a difference that makes no difference is not a difference'......h

     

    --------------------------------------------------

      
      
    Exploration notes  
      
    Col A: fisher gruvtune buy 

    a:=   FmlVar("fisher gruvtune","FISH") ; 
    aa:=  Ref(a,-1); 
    Cross(a,aa); 


    Col B: fisher gruvtune sell

    a:=   FmlVar("fisher gruvtune","FISH") ; 
    aa:=  Ref(a,-1); 
    Cross(aa,a); 


    Col C:  wabbit fisher buy

    a:=   FmlVar("wabbits fisher","FT")  ; 
    aa:=  Ref(a,-1); 
    Cross(a,aa); 


    Col D:  wabbit fisher sell

    a:=   FmlVar("wabbits fisher","FT")  ; 
    aa:=  Ref(a,-1); 
    Cross(aa,a); 


    Col E:  fisher gruvtune 0 cross up

    a:=   FmlVar("fisher gruvtune","FISH") ; 
    aa:=  0; 
    Cross(a,aa); 


    Col F: fisher gruvtune 0 cross down

    a:=   FmlVar("fisher gruvtune","FISH") ; 
    aa:=  0; 
    Cross(aa,a); 


    Col G:  wabbit fisher 0 cross up

    a:=   FmlVar("wabbits fisher","FT")  ; 
    aa:=  0; 
    Cross(a,aa); 


    Col H:  wabbit fisher 0 down cross

    a:=   FmlVar("wabbits fisher","FT")  ; 
    aa:=  0; 
    Cross(aa,a); 

     


    Filter colA OR colB OR colC OR colD OR colE OR colF OR 
     colG OR colH 
      
    Filter enabled Yes 
    Periodicity Daily 
    Records required 61 

     

    -------------------------------------------------

     

  •  06-19-2007, 22:37 24418 in reply to 24416

    Re: fisherXForm code, exploration, etc

    h,

    I didn't comparatively test the codes, so this is a guess-timate : The crossovers will still occur on the same bars, but it is the VALUE of the indicator that will be slightly different if you add a value to every computation.

    Adding values is OK if you are using a relational comparison (today versus yesterday, last week, last month etc, 10% increase over three bars etc) because the same value will be added to every value; but this may not be OK if your explorations requires indicator absolute values (RSI > 90, ROC(1,ind,$) > 3 etc) in which case the value required will have to be adjusted for the additional input.


    Its just something to be aware of...


    wabbit Big Smile [:D]



    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

View as RSS news feed in XML