如何使用 Math::Business::EMA 和 DBI 来计算 Perl 中的指数移动平均线?

发布于 2024-08-28 03:50:13 字数 509 浏览 1 评论 0原文

脚本从 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";
};

具有以下示例结果:

1.560
1.560
1.550...

但我需要使用 数学::商业::EMA;我不知道如何在保持准确性的同时进行计算。 EMA 是加权的,我缺乏 Perl 知识并没有帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2024-09-04 03:50:13

首先,对代码的一些评论:

  1. 您似乎没有使用strict。你应该。

  2. 您似乎认为践踏整个 DBI 命名空间是可以的。事实并非如此。

  3. 您应该使用占位符,而不是插入到 SQL 字符串中。

现在,对于实际任务(未经测试的代码):

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

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";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文