| 1 | package Finance::GeniusTrader::Systems::TTS; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 4 | # This file is distributed under the terms of the General Public License | ||
| 5 | # version 2 or (at your option) any later version. | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use vars qw(@ISA @NAMES); | ||
| 9 | |||
| 10 | use Finance::GeniusTrader::Prices; | ||
| 11 | use Finance::GeniusTrader::Systems; | ||
| 12 | use Finance::GeniusTrader::Indicators::Generic::MinInPeriod; | ||
| 13 | use Finance::GeniusTrader::Indicators::Generic::MaxInPeriod; | ||
| 14 | |||
| 15 | @ISA = qw(Finance::GeniusTrader::Systems); | ||
| 16 | @NAMES = ("TTS[#1, #2]"); | ||
| 17 | |||
| 18 | =head1 Turtle Trading System (TTS) | ||
| 19 | |||
| 20 | =head2 Overview | ||
| 21 | |||
| 22 | The Turtle Trading System is a very simple and very easy to understand. | ||
| 23 | |||
| 24 | It's an Asymetric Channel Breakout : | ||
| 25 | |||
| 26 | * Enter long above the highest high of the previous X days and exit with a | ||
| 27 | stop based on the lowest low of the Y previous days with Y < X | ||
| 28 | |||
| 29 | * Enter short below the lowest low of the previous W days and exit with a | ||
| 30 | stop based on the highest high of the Z previous days with Z < W | ||
| 31 | |||
| 32 | |||
| 33 | Parameters | ||
| 34 | |||
| 35 | Y = length of channel for longs | ||
| 36 | W = length of channel for shorts | ||
| 37 | =cut | ||
| 38 | |||
| 39 | sub new { | ||
| 40 | my $type = shift; | ||
| 41 | my $class = ref($type) || $type; | ||
| 42 | my $args = shift; | ||
| 43 | |||
| 44 | my $self = { "args" => defined($args) ? $args : [ 55, 20 ] }; | ||
| 45 | |||
| 46 | return manage_object(\@NAMES, $self, $class, $self->{'args'}, ""); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub initialize { | ||
| 50 | my ($self) = @_; | ||
| 51 | |||
| 52 | $self->{'max'} = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([ $self->{'args'}[0], "{I:Prices HIGH}"]); | ||
| 53 | $self->{'min'} = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([ $self->{'args'}[1], "{I:Prices LOW}"]); | ||
| 54 | |||
| 55 | $self->add_indicator_dependency($self->{'min'}, 2); | ||
| 56 | $self->add_indicator_dependency($self->{'max'}, 2); | ||
| 57 | $self->add_prices_dependency($self->{'args'}[0] + 1); | ||
| 58 | $self->add_prices_dependency($self->{'args'}[1] + 1); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub precalculate_interval { | ||
| 62 | my ($self, $calc, $first, $last) = @_; | ||
| 63 | $self->{'max'}->calculate_interval($calc, $first, $last); | ||
| 64 | $self->{'min'}->calculate_interval($calc, $first, $last); | ||
| 65 | |||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub long_signal { | ||
| 70 | my ($self, $calc, $i) = @_; | ||
| 71 | |||
| 72 | return 0 if (! $self->check_dependencies($calc, $i)); | ||
| 73 | |||
| 74 | if ( ( $calc->prices->at($i)->[$CLOSE] > | ||
| 75 | $calc->indicators->get($self->{'max'}->get_name, $i - 1) ) | ||
| 76 | ) | ||
| 77 | { | ||
| 78 | return 1; | ||
| 79 | } | ||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub short_signal { | ||
| 84 | my ($self, $calc, $i) = @_; | ||
| 85 | |||
| 86 | return 0 if (! $self->check_dependencies($calc, $i)); | ||
| 87 | |||
| 88 | if ( ( $calc->prices->at($i)->[$CLOSE] < | ||
| 89 | $calc->indicators->get($self->{'min'}->get_name, $i - 1) ) | ||
| 90 | ) | ||
| 91 | { | ||
| 92 | return 1; | ||
| 93 | } | ||
| 94 | return 0; | ||
| 95 | } | ||
| 96 | |||
| 97 | 1; |