| 1 | package Finance::GeniusTrader::Indicators::Test; | ||
| 2 | |||
| 3 | # Copyright 2008 Thomas Weigert | ||
| 4 | # Based on and for GeniusTrader (C) 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 5 | # This file is distributed under the terms of the General Public License | ||
| 6 | # version 2 or (at your option) any later version. | ||
| 7 | |||
| 8 | # initial version 29 feb 08 | ||
| 9 | # $Id: Test.pm,v 1.1 2008/03/01 04:08:16 ras Exp ras $ | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 13 | |||
| 14 | use Finance::GeniusTrader::Indicators; | ||
| 15 | use Finance::GeniusTrader::Prices; | ||
| 16 | |||
| 17 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 18 | @NAMES = ("Test[#1,#2,#3,#4]","CM[#1]"); | ||
| 19 | @DEFAULT_ARGS = (5, 3, 3, "EMA", "Generic::MinInPeriod", "{I:Prices CLOSE}" ); | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | Finance::GeniusTrader::Indicators::Test - Indicator to test embedding of indicators | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This indicator functions as a test rig to ensure that another indicator | ||
| 28 | is robust in the presence of complex embedding into other indicators. | ||
| 29 | |||
| 30 | It is not generic, unfortunately, but tests indicators only with respect | ||
| 31 | to their first argument. | ||
| 32 | |||
| 33 | Use it as follows: | ||
| 34 | |||
| 35 | ./display_indicator.pl --start=2000-06-16 --end=2000-06-29 \ | ||
| 36 | I:Test 13000 60 60 60 SMA Generic::MaxInPeriod | ||
| 37 | |||
| 38 | This will test the SMA; the second indicator Generic::MaxInPeriod is only | ||
| 39 | there to add complexity. It defaults to Generic::MinInPeriod. | ||
| 40 | |||
| 41 | This test will do the following: | ||
| 42 | |||
| 43 | 1. Apply Arg5 to Arg6, using Arg1 as parameter (by default: | ||
| 44 | {I:Generic:MinInPeriod 5 {I:Prices CLOSE}} | ||
| 45 | 2. Smooth the result by Arg4, using Arg2 as parameter (by default: | ||
| 46 | {I:EMA 3 ...} | ||
| 47 | 3. Smooth the result by Arg4, using Arg3 as parameter (by default: | ||
| 48 | {I:EMA 3 ...} | ||
| 49 | |||
| 50 | The first output is the result (3), the second output is the result (1). | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | |||
| 55 | sub initialize { | ||
| 56 | my $self = shift; | ||
| 57 | |||
| 58 | my $ind1 = $self->{'args'}->get_arg_names(4); | ||
| 59 | my $ind2 = $self->{'args'}->get_arg_names(5); | ||
| 60 | eval "use Finance::GeniusTrader::Indicators::$ind1;\n"; | ||
| 61 | eval "use Finance::GeniusTrader::Indicators::$ind2;\n"; | ||
| 62 | |||
| 63 | # We need to call MIN and MAX first | ||
| 64 | my $min = "\$self->{'min'} = Finance::GeniusTrader::Indicators::$ind2->new([ \$self->{'args'}->get_arg_names(1), \$self->{'args'}->get_arg_names(6) ]);"; | ||
| 65 | eval $min; | ||
| 66 | |||
| 67 | |||
| 68 | # Initialize smoothing of CM | ||
| 69 | my $smoothing = "\$self->{'smooth_cm'} = Finance::GeniusTrader::Indicators::$ind1->new([ \$self->{'args'}->get_arg_names(3), \"{I:$ind1 \" . \$self->{'args'}->get_arg_names(2) . \"{I:Generic:ByName \" . \$self->get_name(1) . \"}}\" ])"; | ||
| 70 | eval $smoothing; | ||
| 71 | |||
| 72 | |||
| 73 | # Smoothing functions are args 2 and 3 | ||
| 74 | my $nb_days = $self->{'args'}->get_arg_names(2)+$self->{'args'}->get_arg_names(3); | ||
| 75 | |||
| 76 | $self->add_indicator_dependency($self->{'min'}, $nb_days+1); | ||
| 77 | $self->add_arg_dependency(6, $nb_days + $self->{'args'}->get_arg_constant(1)); | ||
| 78 | |||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | =pod | ||
| 83 | |||
| 84 | =head2 Finance::GeniusTrader::Indicators::Test::calculate($calc, $day) | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub calculate { | ||
| 89 | my ($self, $calc, $i) = @_; | ||
| 90 | my $indic = $calc->indicators; | ||
| 91 | my $prices = $calc->prices; | ||
| 92 | my $min_name = $self->{'min'}->get_name; | ||
| 93 | my $name = $self->get_name(0); | ||
| 94 | my $cm_name = $self->get_name(1); | ||
| 95 | |||
| 96 | return if ($indic->is_available($cm_name, $i) && | ||
| 97 | $indic->is_available($name, $i)); | ||
| 98 | |||
| 99 | |||
| 100 | # Smoothing functions are args 2 | ||
| 101 | my $nb_days = $self->{'args'}->get_arg_values($calc, $i, 2)+$self->{'args'}->get_arg_values($calc, $i, 3); | ||
| 102 | |||
| 103 | return if (! $self->check_dependencies($calc, $i)); | ||
| 104 | |||
| 105 | # Calculate CM | ||
| 106 | for (my $n = 0; $n < $nb_days; $n++) { | ||
| 107 | |||
| 108 | # Return if CM is available | ||
| 109 | next if $indic->is_available($cm_name, $i - $n); | ||
| 110 | |||
| 111 | # Get MIN and MAX | ||
| 112 | my $lowest_low = $indic->get($min_name, $i - $n); | ||
| 113 | |||
| 114 | # Calculate CM | ||
| 115 | my $cm_value = $lowest_low; | ||
| 116 | |||
| 117 | # Return the results | ||
| 118 | $indic->set($cm_name, $i - $n, $cm_value); | ||
| 119 | |||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | $self->{'smooth_cm'}->calculate($calc, $i); | ||
| 124 | my $s2 = $indic->get($self->{'smooth_cm'}->get_name, $i); | ||
| 125 | $indic->set($name, $i, $s2); | ||
| 126 | |||
| 127 | |||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | 1; |