The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Finance::Shares::Averages - Moving average lines and tests

SYNOPSIS

    use Finance::Shares::Sample;
    use Finance::Shares::Averages;

    my $s = new Finance::Shares::Sample(...);

    my $id1 = $s->simple_average(period => 4);
    my $id2 = $s->weighted_average(period => 4);
    my $id3 = $s->exponential_average(
        strict => 0,
        shown => 0,
        graph => 'prices',
        period => 4
    );
    

DESCRIPTION

This package provides additional methods for Finance::Shares::Sample objects. The functions provide moving average lines. Once a line has been constructed it may be referred to by a text identifier returned by the function.

They all take the same parameters in hash key/value format.

FUNCTIONS

simple_average

Produce a series of values representing a simple moving average over the entire sample period. All of the following keys are optional.

strict

If 1, return undef if the average period is incomplete. If 0, return the best value so far. (Default: 0)

shown

A flag controlling whether the function is graphed. 0 to not show it, 1 to add the line to the named graph. (Default: 1)

graph

A string indicating the graph for display: one of prices, volumes, cycles or signals. (Default: 'prices')

line

A string indicating the data/function to be averaged - normally the closing prices. (Default: 'close')

period

The number of readings used in making up the moving average. The actual time spanned depends on how the sample was configured. (Default: 5)

style

A hash ref holding settings suitable for the PostScript::Graph::Style object used when drawing the line. By default lines and points are plotted, with each line in a slightly different style. (Default: undef)

id

If given this becomes the internal identifier for the line. Where possible, allow the line to generate its own identifier, using the value returned by the method.

An arithmetical mean of the previous period values is calculated.

Nothing is done if there are no suitable data in the sample. Returns the hash key identifying the line to Finance::Shares::Sample.

weighted_average

Produce a series of values representing a weighted moving average over the entire sample period. See simple_average for parameters.

This is like a simple moving average except that the most recent values carry more weight.

Nothing is done if there are no suitable data in the sample. Returns the hash key identifying the line to Finance::Shares::Sample.

exponential_average

Produce a series of values representing an exponential moving average over the entire sample period. See simple_average for parameters.

Unlike a weighted average, all of the previous values are taken into accound. period affects how quickly the weighting falls off.

Nothing is done if there are no suitable data in the sample. Returns the hash key identifying the line to Finance::Shares::Sample.

BUGS

Please report those you find to the author.

AUTHOR

Chris Willmot, chris@willmot.org.uk

SEE ALSO

Finances::Shares::Sample, Finance::Shares::Chart and Finances::Shares::Model.

There is also an introduction, Finance::Shares::Overview and a tutorial beginning with Finance::Shares::Lesson1.