| 1 | package Math::Business::ATR; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | 1; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 0 | ▶ | sub tag { (shift)->{tag} } | ||||||||||||||||||||||||||
|
Subroutine
tag: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub recommended { | ||||||||||||||||||||||||||||
| 12 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
recommended: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | $class->new(14); | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub new { | ||||||||||||||||||||||||||||
| 18 | 5 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 5 | my $this = bless { | |||||||||||||||||||||||||||
| 20 | }, $class; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 5 | my $days = shift; | |||||||||||||||||||||||||||
| 23 | 5 | ▶ | if( defined $days ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 1 | $this->set_days( $days ); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | return $this; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub set_days { | ||||||||||||||||||||||||||||
| 31 | 5 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_days: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 5 | my $arg = int(shift); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | ▶ | croak "days must be a positive non-zero integer" if $arg <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 5 | $this->{tag} = "ATR($arg)"; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # NOTE: wilder uses 13/14 * last + 1/14 * current for his exponential average ... | ||||||||||||||||||||||||||||
| 39 | # probably wouldn't have been my first choice, but that's how ATR is defined. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 5 | $this->{days} = $arg; | |||||||||||||||||||||||||||
| 42 | 5 | $this->{R1} = ($arg-1)/$arg; | |||||||||||||||||||||||||||
| 43 | 5 | $this->{R} = 1/$arg; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub insert { | ||||||||||||||||||||||||||||
| 47 | 1124 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
insert: called CC=12 SCAR=25.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1124 | my $y_close = $this->{y_close}; | |||||||||||||||||||||||||||
| 50 | 1124 | ▶ | while( defined( my $point = shift ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 1124 | ▶ | croak "insert takes three tuple [high, low, close]" unless ref $point eq "ARRAY" and @$point == 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $point eq "ARRAY" and @$point == 3
MC/DC0%0/2
ref $point eq "ARRAY" and @$point == 3
ref $point eq "ARRAY"
@$point == 3
Truth table33%1/3
A and B
A= ref $point eq "ARRAY"B= @$point == 3
| |||||||||||||||||||||||||||||
| 52 | 1124 | my ($t_high, $t_low, $t_close) = @$point; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1124 | ▶ | if( defined $y_close ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 1119 | my $A = abs( $t_high - $t_low ); | |||||||||||||||||||||||||||
| 56 | 1119 | my $B = abs( $t_high - $y_close ); | |||||||||||||||||||||||||||
| 57 | 1119 | my $C = abs( $t_low - $y_close ); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1119 | my $true_range = $A; | |||||||||||||||||||||||||||
| 60 | 1119 | ▶ | $true_range = $B if $B > $true_range; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 1119 | ▶ | $true_range = $C if $C > $true_range; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1119 | ▶ | if( defined(my $atr = $this->{ATR}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1065 | $this->{ATR} = $this->{R1} * $atr + $this->{R} * $true_range; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||||||||||
| 67 | 54 | my $p; | |||||||||||||||||||||||||||
| 68 | 54 | my $N = $this->{days}; | |||||||||||||||||||||||||||
| 69 | 54 | ▶ | if( ref($p = $this->{_p}) and (@$p >= $N-1) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref($p = $this->{"_p"}) and @$p >= $N - 1
MC/DC50%1/2
ref($p = $this->{"_p"}) and @$p >= $N - 1
ref($p = $this->{"_p"})
@$p >= $N - 1
Truth table66%2/3
A and B
A= ref($p = $this->{"_p"})B= @$p >= $N - 1
| |||||||||||||||||||||||||||||
| 70 | 4 | my $sum = 0; | |||||||||||||||||||||||||||
| 71 | 4 | $sum += $_ for @$p; | |||||||||||||||||||||||||||
| 72 | 4 | $sum += $true_range; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 4 | $this->{ATR} = $sum / $N; | |||||||||||||||||||||||||||
| 75 | 4 | delete $this->{_p}; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | } else { | ||||||||||||||||||||||||||||
| 78 | 50 | push @{$this->{_p}}, $true_range; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||
| 83 | 5 | my $true_range = $t_high - $t_low; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # NOTE: _p shouldn't exist because this initializer is only used for the very first entry | ||||||||||||||||||||||||||||
| 86 | 5 | ▶ | die "something is clearly wrong, see note below above line" if exists $this->{_p}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # NOTE: this initializer sucks because the calculation is done | ||||||||||||||||||||||||||||
| 89 | # differently than it would be if you had data from the day before. | ||||||||||||||||||||||||||||
| 90 | # IMO, we should just return undef for an extra day, but this | ||||||||||||||||||||||||||||
| 91 | # appears to be by definition, so we do it: | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 5 | $this->{_p} = [$true_range]; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1124 | $y_close = $t_close; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1124 | $this->{y_close} = $y_close; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub query { | ||||||||||||||||||||||||||||
| 103 | 1085 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1085 | return $this->{ATR}; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||