GeniusTrader/Indicators/FISH.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 75 CC 5 SCAR 26.1
CC
5
Cov
0%
calculate
30.0
initialize
2.0
BEGIN
2.0
CRAP 13.7
1 package Finance::GeniusTrader::Indicators::FISH;
2  
3 # Copyright 2008 Karsten Wippler
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 # $Id: FISH.pm,v 1.4 2008/03/14 17:06:09 ras Exp ras $
9  
10 # Standards-Version: 1.0
11  
12 use strict;
13 use vars qw(@ISA @NAMES @DEFAULT_ARGS);
14  
15 use Finance::GeniusTrader::ArgsTree;
16 use Finance::GeniusTrader::Indicators;
17 use Finance::GeniusTrader::Indicators::EMA;
18 use Finance::GeniusTrader::Prices;
19  
20 @ISA = qw(Finance::GeniusTrader::Indicators);
21 @NAMES = ("FISH[#1]","RAW[#1]");
22 @DEFAULT_ARGS = (10, "{I:MEAN}" );
23  
24 =head1 Finance::GeniusTrader::Indicators::FISH
25  
26 =head2 Overview
27 Infos cited from chart manual at
28 http://www.geocities.com/
29 user42_kevin/chart/index.html
30  
31 The fisher transform indicator by John Ehlers is a range oscillator
32 showing where today's price is within the past N-days highest and lowest,
33 with some smoothing is used plus what's known in mathematics as a fisher transform.
34 This is similar to Stochastics and Williams %R but the transformation stretches
35 values near the high and low, helping to highlight extremes.
36 =head2 Calculation
37  
38 The calculation is as follows. The prices used are the midpoint between the day's
39 high and low (as in most of Ehlers' indicators). Today's price is located within
40 the highest and lowest of those prices from the past N days, scaled to -1 for the
41 low and 1 for the high.
42  
43 price = (high + low) / 2
44
45 price - Ndaylow
46 raw = 2 * ------------------ - 1
47 Ndayhigh - Ndaylow
48  
49 This raw position is smoothed by a 5-day EMA and a log form which is the
50 mathematical fisher transform, before a final further 3-day EMA smoothing.
51  
52 smoothed = EMA[5] of raw
53
54 1 + smoothed
55 fisher = EMA[3] of 0.5 * log ------------
56 1 - smoothed
57  
58 =head2 Parameters
59  
60 The standard Fisher-Transform works with a 10-days parameter : n = 10
61  
62 =head2 Links
63 http://mesasoftware.com/technicalpapers.htm
64 http://www.geocities.com/user42_kevin/chart/index.html
65  
66 =head2 Creation
67  
68 Finance::GeniusTrader::Indicators::FISH->new()
69 Finance::GeniusTrader::Indicators::FISH->new([20])
70  
71 If you need a 30 days Fisher Transform of the opening prices you can write
72 one of those lines :
73  
74 Finance::GeniusTrader::Indicators::FISH->new([30, "{I:Prices OPEN}"])
75  
76 A 10 days Fisher Transform of the RSI could be created with :
77  
78 Finance::GeniusTrader::Indicators::FISH->new([10, "{I:RSI}"])
79  
80  
81 =cut
82 sub initialize {
83 my ($self) = @_;
84
85 $self->{'ema1'} = Finance::GeniusTrader::Indicators::EMA->new([5,"{I:Generic::Eval ({I:STO/1 ".
86 $self->{'args'}->get_arg_names(1) . " 1 1 1" .
87 $self->{'args'}->get_arg_names(2) ."})/50 - 1}" ]);
88  
89 $self->{'ema2'} = Finance::GeniusTrader::Indicators::EMA->new([3,"{I:Generic:ByName " . $self->get_name(1) . "}" ]);
90  
91  
92 # Smoothing functions are args 2 and 3
93 my $nb_days = $self->{'args'}->get_arg_names(1) + 8;
94  
95 $self->add_indicator_dependency($self->{'ema1'}, 5);
96 $self->add_arg_dependency(2, $nb_days);
97  
98
99 }
100  
101 =pod
102  
103 =head2 Finance::GeniusTrader::Indicators::SMI::calculate($calc, $day)
104  
105 =cut
106  
107 sub calculate {
108 my ($self, $calc, $i) = @_;
109 my $indic = $calc->indicators;
110 my $prices = $calc->prices;
111 my $ema1_name = $self->{'ema1'}->get_name;
112 my $name = $self->get_name(0);
113 my $raw1_name = $self->get_name(1);
114
115 return if ($indic->is_available($raw1_name, $i) &&
116 $indic->is_available($name, $i));
117 return if (! $self->check_dependencies($calc, $i));
118  
119 for (my $n = 0; $n < 3; $n++) {
120  
121 # Return if RAW1 is available
122 next if $indic->is_available($raw1_name, $i - $n);
123
124 # Calculate CM
125 my $sraw = $indic->get($ema1_name, $i - $n);
126 my $lraw_value = (abs($sraw) > 0.999) ?
127 $sraw/abs($sraw)*0.999 : $sraw;
128 my $raw1_value = log((1+$lraw_value)/(1-$lraw_value));
129  
130 # Return the results
131 $indic->set($raw1_name, $i - $n, $raw1_value);
132 }
133 # Return the results
134 $self->{'ema2'}->calculate($calc, $i);
135 my $fish=$indic->get($self->{'ema2'}->get_name, $i);
136 $indic->set($name, $i, $fish);
137  
138 }
139 1;