[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Simple AFL Database Logic Fails



PureBytes Links

Trading Reference Links

Thanks Tooz!

Never occurred to me that the values could have more decimal places
than my actual data, which only goes to four. But that was in fact the
issue.


--- In amibroker@xxxxxxxxxxxxxxx, "tuzo_wilson" <j.tuzo.wilson@xxx> wrote:
>
> --- In amibroker@xxxxxxxxxxxxxxx, "ozzyapeman" <zoopfree@> wrote:
> >
> > Tuzo,
> > 
> > Thanks for the cleaner code and division by 0 catch (and for
staying up
> > late!)
> 
> I was up anyway.  :)
>  
> > I assume that this part: "if ( PairFrequency != 0 )..." should be part
> > of the barcount loop. But either way, it does not work.
> 
> In your first example the avg calculation was in the loop so the avg
> was only calculated if there was a buy signal so you would never have
> a division by zero error.  But, you don't need to calculate the
> average every iteration -- just at the end.
> 
> > There is still a mysterious bug. Now all values of DistanceMin are 0,
> > even though by definition they have to be greater than 0.
> 
> I think the logic is OK.  Your issue is formatting related.  In the
line:
> 
> StrFormat( "%.0f,%.0f,%.0f,%.4f,%.4f\n", PairFrequency, Fast, Slow,
> DistanceMin, DistanceAvg )
> 
> You are only printing out the first 4 decimal places (rounded).  So if
> your min is small (e.g. 0.00002) then it will show as zero.  Remove
> the formatting and change it to:
> 
> StrFormat( "%.0f,%.0f,%.0f,%f,%f\n", PairFrequency, Fast, Slow,
> DistanceMin, DistanceAvg )
> 
> and you should see the correct values.  Here is what I get:
> 
> 798,5,15,0.000020,0.056575
> 
> Good luck...remember that _TRACE is your friend!
> 
> 
> Tuzo
> 
> 
> > The code logic seems so straightforward, it's hard to figure out why
> > this isn't working just as is.
> > 
> > Anyone?
> > 
> > 
> > 
> > --- In amibroker@xxxxxxxxxxxxxxx, "tuzo_wilson" <j.tuzo.wilson@>
> > wrote:
> > >
> > > --- In amibroker@xxxxxxxxxxxxxxx, "ozzyapeman" zoopfree@ wrote:
> > >
> > > Ozzy,
> > >
> > > You've got a lot going on there.  :)
> > >
> > > It's 1:30am here so I don't have time to go into it all but this
> > should
> > > hopefully do what you want.  Take a look and see if it helps.
> > >
> > >
> > > //
> > >
> >
-----------------------------------------------------------------------
> > >
> > > //                   SIMPLE TRADING SYSTEM
> > >
> > > //
> > >
> >
-----------------------------------------------------------------------
> > >
> > >
> > >
> > > BuyPrice = SellPrice = ShortPrice = CoverPrice = Close;
> > >
> > >
> > >
> > >
> > >
> > > Fast     = Optimize( "fast", 5, 5, 10, 1 );
> > >
> > > Slow     = Optimize( "Slow", 15, 10, 15, 1 );
> > >
> > >
> > >
> > > FastMA   = MA( C, Fast );
> > >
> > > SlowMA   = MA( C, Slow );
> > >
> > >
> > >
> > > Buy      = Cross( Close, FastMA );
> > >
> > > Sell     = Cross( SlowMA, Close );
> > >
> > >
> > >
> > > Short    = Sell;
> > >
> > > Cover    = Buy;
> > >
> > >
> > >
> > >
> > >
> > > //
> > >
> >
-----------------------------------------------------------------------
> > >
> > > //   DUMP STATS TO FILE: Distance between the Close and FastMA @ Buy
> > >
> > > //
> > >
> >
-----------------------------------------------------------------------
> > >
> > >
> > >
> > >
> > >
> > > // For each Fast/Slow Pair buy, dump stats to a file.
> > >
> > > // Find the Minimum and Average Distances for each Pair.
> > >
> > >
> > >
> > >
> > >
> > > Distance      = Close - FastMA;
> > >
> > >
> > >
> > > PairFrequency = 0;
> > >
> > >
> > >
> > > DistanceMin = DistanceSum = DistanceAvg = 0;
> > >
> > >
> > >
> > > for ( i = 0; i < BarCount - 1; i++ )
> > >
> > > {
> > >
> > >      if ( Buy[i] )
> > >
> > >      {
> > >
> > >          // If current distance is less than the current min or this
> > is
> > > the first time
> > >
> > >          // then set DistanceMin
> > >
> > >          if ( Distance[i] < DistanceMin || PairFrequency == 0 )
> > >
> > >          {
> > >
> > >              DistanceMin = Distance[i];
> > >
> > >          }
> > >
> > >
> > >
> > >          PairFrequency++;
> > >
> > >
> > >          DistanceSum += Distance[i];
> > >
> > >      }
> > >
> > > }
> > >
> > >
> > >
> > > // Avoid division by zero error otherwise may show 1.#INF
> > >
> > > if ( PairFrequency != 0 )
> > >
> > > {
> > >
> > >      DistanceAvg = DistanceSum / PairFrequency;
> > >
> > > }
> > >
> > >
> > >
> > > fh = fopen( "C:\\DistanceStats.csv", "a" );  // a = appending, write
> > our
> > > stats to file
> > >
> > >
> > >
> > > if ( fh )
> > >
> > > {
> > >
> > >      fputs( StrFormat( "%.0f,%.0f,%.0f,%.4f,%.4f\n", PairFrequency,
> > Fast,
> > > Slow, DistanceMin, DistanceAvg ), fh );
> > >
> > >      fclose( fh );
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > > Tuzo
> > >
> > >
> > >
> > > > Hello, hoping someone can help spot the bug in this rather simple
> > > code.
> > > > I can't seem to find the flaw.
> > > >
> > > > All I am trying to do is optimize over a pair of variables,
and dump
> > a
> > > > database to a file that contains some stats for each winning pair.
> > > > Namely, I want to calculate the minimum distance between the Close
> > and
> > > > FastMA, as well as the average distance - over all the Buys
for each
> > > > pair of optimization variables.
> > > >
> > > > While everything appears to work fine, the problem is that
sometimes
> > > the
> > > > minimum is larger than the average! And of course, that cannot
be. 
> > If
> > > > you run an optimization and open up the resulting CSV file,
you will
> > > see
> > > > what I mean.
> > > >
> > > > I've been staring at this loop for hours, and just can't see
how it
> > > can
> > > > be wrong. But of course it must be. It's probably something
obvious
> > > that
> > > > my brain is overlooking. Any input much appreciated!
> > > >
> > > >
> > > > //
> > > >
> > >
> >
-----------------------------------------------------------------------
> > > > //                   SIMPLE TRADING SYSTEM
> > > > //
> > > >
> > >
> >
-----------------------------------------------------------------------
> > > >
> > > > BuyPrice = SellPrice = ShortPrice = CoverPrice = Close;
> > > >
> > > >
> > > > Fast     = Optimize( "fast", 5, 5, 10, 1 );
> > > > Slow     = Optimize( "Slow", 15, 10, 15, 1 );
> > > >
> > > > FastMA   = MA(C, Fast);
> > > > SlowMA   = MA(C, Slow);
> > > >
> > > > Buy      = Cross( Close, FastMA);
> > > > Sell     = Cross( SlowMA, Close);
> > > >
> > > > Short    = Sell;
> > > > Cover    = Buy;
> > > >
> > > >
> > > > //
> > > >
> > >
> >
-----------------------------------------------------------------------
> > > > //   DUMP STATS TO FILE: Distance between the Close and FastMA
@ Buy
> > > > //
> > > >
> > >
> >
-----------------------------------------------------------------------
> > > >
> > > >
> > > > // For each Fast/Slow Pair buy, dump stats to a file.
> > > > // Find the Minimum and Average Distances for each Pair.
> > > >
> > > >
> > > > Distance      = Close - FastMA;
> > > >
> > > > PairFrequency = 0;
> > > >
> > > > DistanceMin   = DistanceSum = DistanceAvg = DistanceOld = 0;
> > > >
> > > > for (i = 0; i < BarCount - 1; i++)
> > > > {
> > > > if(Buy[i])
> > > > {
> > > >     PairFrequency++;
> > > >
> > > >     if(Distance[i] < DistanceOld)  DistanceMin = Distance[i];
> > > >
> > > >     DistanceSum = DistanceSum + Distance[i];
> > > >     DistanceAvg = DistanceSum/PairFrequency;
> > > >
> > > >     DistanceOld = Distance[i];
> > > >    }
> > > > }
> > > >
> > > > fh = fopen( "C:\\DistanceStats.csv", "a" );  // a = appending,
write
> > > our
> > > > stats to file
> > > >
> > > >     if ( fh )
> > > >     {
> > > >      fputs( StrFormat( "%.0f,%.0f,%.0f,%.4f,%.4f\n",
PairFrequency,
> > > Fast,
> > > > Slow, DistanceMin, DistanceAvg), fh );
> > > >      fclose( fh );
> > > >      }
> > > >
> > >
> >
>



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

**** IMPORTANT ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

*********************
TO GET TECHNICAL SUPPORT from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com
*********************

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html

*********************************
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/