stockmonkey

 view release on metacpan or  search on metacpan

t/13_sto.t  view on Meta::CPAN


use Test;
use strict;
no warnings;
use Math::Business::SM::Stochastic;
use Math::Business::SMA;

my $number = 30*6;
plan tests => $number;

my $fsto = Math::Business::SM::Stochastic->method_fast(14,3);
my $ssto = Math::Business::SM::Stochastic->method_slow(14,3);
my $sful = Math::Business::SM::Stochastic->method_full(14,3,5);

my $K3  = Math::Business::SMA->new(3);
my $K33 = Math::Business::SMA->new(3);

my $K5  = Math::Business::SMA->new(5);
my $K55 = Math::Business::SMA->new(5);

while(<DATA>) {
    # data ripped from http://stockcharts.com/help/doku.php?id=chart_school:technical_indicators:stochastic_oscillato
    chomp;
    my ($line, $date, $high, $low, $highest, $lowest, $close, $theirK) = split m/,/;

    $fsto->insert( my $i = [$high,$low,$close] );
    $ssto->insert( $i );
    $sful->insert( $i );

    my ($D,$K) = $fsto->query;

    $K3->insert($K) if defined $K;
    my $k3 = $K3->query;

    $K33->insert($k3) if defined $k3;
    my $k33 = $K33->query;

    $K5->insert($K) if defined $K;
    my $k5 = $K5->query;

    $K55->insert($k5) if defined $k5;
    my $k55 = $K55->query;

    if( not defined $K and not $theirK ) {
        ok(1); 
        ok(2);

    } else {
        ok( 1 > abs($K - $theirK) );
        ok( 1 > abs($D - $k3) );
    }

    ($D,$K) = $ssto->query;
    if( not defined $K or not defined $D ) {
        ok(1);
        ok(2);

    } else {
        ok( 1 > abs($K - $k3) );
        ok( 1 > abs($D - $k33) );
    }

    ($D,$K) = $sful->query;
    if( not defined $K or not defined $D ) {
        ok(1);
        ok(2);

    } else {
        ok( 1 > abs($K - $k5) );
        ok( 1 > abs($D - $k55) );
    }
}

__DATA__
1,2010/02/23,127.009,125.3574,,,,
2,2010/02/24,127.6159,126.1633,,,,
3,2010/02/25,126.5911,124.9296,,,,
4,2010/02/26,127.3472,126.0937,,,,
5,2010/03/01,128.173,126.8199,,,,
6,2010/03/02,128.4317,126.4817,,,,
7,2010/03/03,127.3671,126.034,,,,
8,2010/03/04,126.422,124.8301,,,,
9,2010/03/05,126.8995,126.3921,,,,
10,2010/03/08,126.8498,125.7156,,,,



( run in 0.553 second using v1.01-cache-2.11-cpan-71847e10f99 )