App-financeta

 view release on metacpan or  search on metacpan

lib/App/financeta/indicators.pm  view on Meta::CPAN

            [ 'InFastPeriod', 'Fast MA Period Window (2 - 100000)', PDL::long, 12],
            [ 'InSlowPeriod', 'Slow MA Period Window (2 - 100000)', PDL::long, 26],
            [ 'InSignalPeriod', 'Signal Line Smoothing (1 - 100000)', PDL::long, 9],
        ],
        code => sub {
            my ($obj, $inpdl, @args) = @_;
            $log->debug("Executing ta_macd with parameters ", dumper(\@args));
            my $fast = $args[0];
            my $slow = $args[1];
            my $signal = $args[2];
            my ($omacd, $omacdsig, $omacdhist) = PDL::ta_macd($inpdl, @args);
            return [
                ["MACD($fast/$slow/$signal)", $omacd, undef, "macd_$fast\_$slow\_$signal"],
                ["MACD Signal($fast/$slow/$signal)", $omacdsig, undef, "macdsig_$fast\_$slow\_$signal"],
                ["MACD Histogram($fast/$slow/$signal)", $omacdhist, { with => 'impulses' }, "macdhist_$fast\_$slow\_$signal"],
            ];
        },
        gnuplot => \&_plot_gnuplot_additional,
        highcharts => \&_plot_highcharts_additional,
    },
    macdext => {
        name => 'MACD with different Mov. Avg',
        params => [
            # key, pretty name, type, default value

lib/App/financeta/indicators.pm  view on Meta::CPAN

                    'Triple Exponential (T3)', #T3
                ],
            ],
        ],
        code => sub {
            my ($obj, $inpdl, @args) = @_;
            $log->debug("Executing ta_macdext with parameters ", dumper(\@args));
            my $fast = $args[0];
            my $slow = $args[2];
            my $signal = $args[4];
            my ($omacd, $omacdsig, $omacdhist) = PDL::ta_macdext($inpdl, @args);
            return [
                ["MACDEXT($fast/$slow/$signal)", $omacd, undef, "macdext_$fast\_$slow\_$signal"],
                ["MACDEXT Signal($fast/$slow/$signal)", $omacdsig, undef, "macdextsig_$fast\_$slow\_$signal"],
                ["MACDEXT Histogram($fast/$slow/$signal)", $omacdhist, { with => 'impulses' }, "macdexthist_$fast\_$slow\_$signal"],
            ];
        },
        gnuplot => \&_plot_gnuplot_additional,
        highcharts => \&_plot_highcharts_additional,
    },
    macdfix => {
        name => 'MACD Fixed to 12/26',
        params => [
            # key, pretty name, type, default value
            [ 'InSignalPeriod', 'Signal Line Smoothing (1 - 100000)', PDL::long, 9],
        ],
        code => sub {
            my ($obj, $inpdl, @args) = @_;
            $log->debug("Executing ta_macdfix with parameters ", dumper(\@args));
            my $signal = $args[0];
            my ($omacd, $omacdsig, $omacdhist) = PDL::ta_macdfix($inpdl, @args);
            return [
                ["MACD(12/26/$signal)", $omacd, undef, "macd_12_26_$signal"],
                ["MACD Signal(12/26/$signal)", $omacdsig, undef, "macdsig_12_26_$signal"],
                ["MACD Histogram(12/26/$signal)", $omacdhist, { with => 'impulses' }, "macdhist_12_26_$signal"],
            ];
        },
        gnuplot => \&_plot_gnuplot_additional,
        highcharts => \&_plot_highcharts_additional,
    },
    mfi => {
        name => 'Money Flow Index (MFI)',
        params => [
            # key, pretty name, type, default value

t/01_lang.t  view on Meta::CPAN

### START OF AUTOGENERATED CODE - DO NOT EDIT
#### The list of variables that you can use is below:
#### $open
#### $high
#### $low
#### $close
#### $bbands_upper_5
#### $bbands_middle_5
#### $bbands_lower_5
#### $macd_12_26_9
#### $macdsig_12_26_9
#### $macdhist_12_26_9
#### END OF AUTOGENERATED CODE
buy at $open WHEN $macdhist_12_26_9
  becomes positive AND $macd_12_26_9 crosses $macdsig_12_26_9 from BELOW;
sell at
  $high WHEN $macdhist_12_26_9 becomes negative AND $macd_12_26_9 crosses $macdsig_12_26_9
  from above;
TEST4
my $output4 = $lang->compile($test4, [qw/
open
high
low
close
bbands_upper_5
bbands_middle_5
bands_lower_5
macd_12_26_9
macdsig_12_26_9
macdhist_12_26_9
/]);
isnt($output4, undef, 'compiler can parse rules');
note($output4);
coderef_check($lang, $output4);

my $test5 = << 'TEST5';
### START OF AUTOGENERATED CODE - DO NOT EDIT
#### The list of variables that you can use is below:
#### $open
#### $high
#### $low
#### $close
#### $bbands_upper_5
#### $bbands_middle_5
#### $bbands_lower_5
#### $rsi_14
#### $macd_12_26_9
#### $macdsig_12_26_9
#### $macdhist_12_26_9
#### END OF AUTOGENERATED CODE
buy at $open WHEN $macdhist_12_26_9
  becomes positive AND $macd_12_26_9 crosses $macdsig_12_26_9 from BELOW and
  $rsi_14 > 30;
sell at
  $high WHEN $macdhist_12_26_9 becomes negative AND $macd_12_26_9 crosses $macdsig_12_26_9
  from above;
TEST5
my $output5 = $lang->compile($test5, [qw/
open
high
low
close
bbands_upper_5
bbands_middle_5
bands_lower_5
rsi_14
macd_12_26_9
macdsig_12_26_9
macdhist_12_26_9
/]);
isnt($output5, undef, 'compiler can parse rules');
note($output5);
coderef_check($lang, $output5);

my $test6 = << 'TEST6';
### START OF AUTOGENERATED CODE - DO NOT EDIT
#### The list of variables that you can use is below:
#### $open

t/02_exec.t  view on Meta::CPAN

my $lang = new_ok( 'App::financeta::language' => [ debug => 0 ] );

my $test = <<'TEST';
### START OF AUTOGENERATED CODE - DO NOT EDIT
#### The list of variables that you can use is below:
#### $open
#### $high
#### $low
#### $close
#### $macd_12_26_9
#### $macdsig_12_26_9
#### $macdhist_12_26_9
#### END OF AUTOGENERATED CODE
no short trades;
buy at $open WHEN $macdhist_12_26_9
  becomes positive AND $macd_12_26_9 crosses $macdsig_12_26_9 FROM BELOW;
sell at
  $high WHEN $macdhist_12_26_9 becomes negative AND $macd_12_26_9 crosses $macdsig_12_26_9
  from above;
TEST
my $output = $lang->compile(
    $test,
    [
        qw/
          open
          high
          low
          close
          macd_12_26_9
          macdsig_12_26_9
          macdhist_12_26_9
          /
    ]
);
isnt( $output, undef, 'compiler can parse rules' );
note($output);
my $coderef = $lang->generate_coderef($output);
is( ref $coderef, 'CODE', 'can eval output into a code-ref' )
  or diag("\$coderef is $coderef");

t/02_exec.t  view on Meta::CPAN

my $macdfix_output = $indicators->execute_ohlcv(
    $ohlc,
    {
        func   => 'MACD Fixed to 12/26',
        group  => 'Momentum Indicators',
        params => { InSignalPeriod => 9, },
    },
);
is( ref $macdfix_output, 'ARRAY', 'macd output is valid' );
my $macd_12_26_9     = $macdfix_output->[0]->[1];
my $macdsig_12_26_9  = $macdfix_output->[1]->[1];
my $macdhist_12_26_9 = $macdfix_output->[2]->[1];
note( $macd_12_26_9->dims );
note( $macdsig_12_26_9->dims );
note( $macdhist_12_26_9->dims );

my $coderef_test = sub {
    my $open             = shift;
    my $high             = shift;
    my $low              = shift;
    my $close            = shift;
    my $macd_12_26_9     = shift;
    my $macdsig_12_26_9  = shift;
    my $macdhist_12_26_9 = shift;
    my $buys             = zeroes( $close->dims );
    my $sells            = zeroes( $close->dims );
    my $lookback         = 1;
    my $idx_0            = xvals( $macdhist_12_26_9->dims ) - $lookback;
    $idx_0 = $idx_0->setbadif( $idx_0 < 0 )->setbadtoval(0);
    my $idx_1 = xvals( $macd_12_26_9->dims ) - $lookback;
    $idx_1 = $idx_1->setbadif( $idx_1 < 0 )->setbadtoval(0);
    my $idx_2 =
    which( ( $macdhist_12_26_9 >= 0.000001 ) &
        ( $macdhist_12_26_9->index($idx_0) < 0.000001 ) &
        ( $macd_12_26_9->index($idx_1) <
            $macdsig_12_26_9->index($idx_1) ) &
        ( $macd_12_26_9 > $macdsig_12_26_9 ) );
    $buys->index($idx_2) .= $open->index($idx_2);
    my $idx_3 = xvals( $macdhist_12_26_9->dims ) - $lookback;
    $idx_3 = $idx_3->setbadif( $idx_3 < 0 )->setbadtoval(0);
    my $idx_4 = xvals( $macd_12_26_9->dims ) - $lookback;
    $idx_4 = $idx_4->setbadif( $idx_4 < 0 )->setbadtoval(0);
    my $idx_5 =
    which( ( $macdhist_12_26_9 <= -0.000001 ) &
        ( $macdhist_12_26_9->index($idx_3) > -0.000001 ) &
        ( $macd_12_26_9->index($idx_4) >
            $macdsig_12_26_9->index($idx_4) ) &
        ( $macd_12_26_9 < $macdsig_12_26_9 ) );
    $sells->index($idx_5) .= $high->index($idx_5);
    return { buys => $buys, sells => $sells, long => 1, short => 0 };
};

my $result = &$coderef(
    $ohlc ( , (1) ),
    $ohlc ( , (2) ),
    $ohlc ( , (3) ),
    $ohlc ( , (4) ),
    $macd_12_26_9,
    $macdsig_12_26_9,
    $macdhist_12_26_9
);
is( ref $result,          'HASH', 'results is a hashref' );
is( ref $result->{buys},  'PDL',  'buys is a PDL' );
is( ref $result->{sells}, 'PDL',  'sells is a PDL' );
is($result->{short}, 0, 'no short trades');
is($result->{long}, 1, 'allow long trades');
note( $result->{buys} );
note( $result->{sells} );



( run in 0.496 second using v1.01-cache-2.11-cpan-5511b514fd6 )