views:

59

answers:

1

Script pulls data from mysql:

$DBI::result = $db->prepare(qq{
    SELECT close 
    FROM $table 
    WHERE day <= '$DATE' 
    ORDER BY day DESC 
    LIMIT $EMA
 });
$DBI::result->execute();

while($row = $DBI::result->fetchrow) {
    print "$row\n";
};

with the following example results:

1.560
1.560
1.550...

But I need to work out the EMA using Math::Business::EMA; and I'm not sure how to calculate this while maintaining the accuracy. EMA is weighted and My lack of Perl knowledge is not helping.

+2  A: 

First, some comments on the code:

  1. You do not seem to be using strict. You should.

  2. You seem to think it is OK to trample all over the DBI namespace. It is not.

  3. You should use placeholders instead of interpolating into the SQL string.

Now, for the actual task (untested code):

my $averager = Math::Business::EMA->new;
$averager->set_days(3);

my $sth = $db->prepare(sprintf q{
    SELECT close 
    FROM  %s
    WHERE day <= ? 
    ORDER BY day DESC 
    LIMIT ?
}, $table);

$sth->execute($DATE, $EMA); # what is $EMA?
while ( my $row = $sth->fetchrow_arrayref ) {
    $averager->insert( $row->[0] );
    my $avg = $averager->query;
    $avg = 'n/a' unless defined $avg;
    print "$avg\n";
}
Sinan Ünür
Thanks for the advice. time to learn about placeholders.
Dustin