| 1 | package Finance::GeniusTrader::CloseStrategy::Stop::SAR; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 4 | # Modified 2004 by Oliver Bossert | ||
| 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 | # Standards-Version: 1.0 | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 12 | |||
| 13 | use Finance::GeniusTrader::CloseStrategy; | ||
| 14 | use Finance::GeniusTrader::Indicators::SAR; | ||
| 15 | use Finance::GeniusTrader::Prices; | ||
| 16 | |||
| 17 | @ISA = qw(Finance::GeniusTrader::CloseStrategy); | ||
| 18 | @NAMES = ("SAR[#1,#2,#3]"); | ||
| 19 | @DEFAULT_ARGS = (0.02, 0.02, 0.20); | ||
| 20 | |||
| 21 | =head1 Finance::GeniusTrader::CloseStrategy::Stop::SAR | ||
| 22 | |||
| 23 | =head2 Overview | ||
| 24 | |||
| 25 | This strategy end up a position once prices have crossed the trailing stop | ||
| 26 | determined by the Parabolic SAR (Stop And Reversal). | ||
| 27 | |||
| 28 | =head2 Note | ||
| 29 | |||
| 30 | Keep in mind that some source say "the SAR value is today's, not tomorrow's stop level" and other don't ! :) | ||
| 31 | |||
| 32 | Using the Parabolic SAR can be very helpful as long as the security is | ||
| 33 | not prone to short term price trend reversals. If price is erratic, | ||
| 34 | reversing quickly in the short trend, the Parabolic SAR will likely | ||
| 35 | produce poor results. | ||
| 36 | |||
| 37 | =head2 Links | ||
| 38 | |||
| 39 | http://www.stockcharts.com/education/Resources/Glossary/parabolicSAR.html | ||
| 40 | http://www.equis.com/free/taaz/parabolicsar.html | ||
| 41 | http://www.linnsoft.com/tour/techind/sar.htm | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | sub initialize { | ||
| 46 | my ($self) = @_; | ||
| 47 | |||
| 48 | $self->{'sar'} = Finance::GeniusTrader::Indicators::SAR->new([ $self->{'args'}->get_arg_names(1), | ||
| 49 | $self->{'args'}->get_arg_names(2), | ||
| 50 | $self->{'args'}->get_arg_names(3) ]); | ||
| 51 | |||
| 52 | $self->add_indicator_dependency($self->{'sar'}, 1); | ||
| 53 | $self->add_prices_dependency(1); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub get_indicative_long_stop { | ||
| 57 | my ($self, $calc, $i, $order, $pf_manager, $sys_manager) = @_; | ||
| 58 | my $stop = 0; | ||
| 59 | |||
| 60 | return 0 if (! $self->check_dependencies($calc, $i)); | ||
| 61 | |||
| 62 | my $sar = $calc->indicators->get($self->{'sar'}->get_name, $i); | ||
| 63 | |||
| 64 | # Place the trailing stop only then SAR and Prices are moving together | ||
| 65 | if ($sar < $calc->prices->at($i)->[$LOW]) { | ||
| 66 | $stop = $sar; | ||
| 67 | } | ||
| 68 | return $stop; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub get_indicative_short_stop { | ||
| 72 | my ($self, $calc, $i, $order, $pf_manager, $sys_manager) = @_; | ||
| 73 | my $stop = 0; | ||
| 74 | |||
| 75 | return 0 if (! $self->check_dependencies($calc, $i)); | ||
| 76 | |||
| 77 | my $sar = $calc->indicators->get($self->{'sar'}->get_name, $i); | ||
| 78 | |||
| 79 | # Place the trailing stop only then SAR and Prices are moving together | ||
| 80 | if ($sar > $calc->prices->at($i)->[$HIGH]) { | ||
| 81 | $stop = $sar; | ||
| 82 | } | ||
| 83 | return $stop; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub long_position_opened { | ||
| 87 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 88 | |||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub short_position_opened { | ||
| 93 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 94 | |||
| 95 | return; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub manage_long_position { | ||
| 99 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 100 | |||
| 101 | return if (! $self->check_dependencies($calc, $i)); | ||
| 102 | |||
| 103 | my $sar = $calc->indicators->get($self->{'sar'}->get_name, $i); | ||
| 104 | |||
| 105 | # Place the trailing stop only then SAR and Prices are moving together | ||
| 106 | # Otherwise, exit immediately. | ||
| 107 | if ($sar < $calc->prices->at($i)->[$LOW]) { | ||
| 108 | $position->{'stop'} = $sar; | ||
| 109 | } else { | ||
| 110 | my $order = $pf_manager->sell_market_price($calc, $sys_manager->get_name); | ||
| 111 | $pf_manager->submit_order_in_position($position, $order, $i, $calc); | ||
| 112 | } | ||
| 113 | |||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub manage_short_position { | ||
| 118 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 119 | |||
| 120 | return if (! $self->check_dependencies($calc, $i)); | ||
| 121 | |||
| 122 | my $sar = $calc->indicators->get($self->{'sar'}->get_name, $i); | ||
| 123 | |||
| 124 | # Place the trailing stop only then SAR and Prices are moving together | ||
| 125 | # Otherwise, exit immediately. | ||
| 126 | if ($sar > $calc->prices->at($i)->[$HIGH]) { | ||
| 127 | $position->{'stop'} = $sar; | ||
| 128 | } else { | ||
| 129 | my $order = $pf_manager->buy_market_price($calc, $sys_manager->get_name); | ||
| 130 | $pf_manager->submit_order_in_position($position, $order, $i, $calc); | ||
| 131 | } | ||
| 132 | |||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; |