| 1 | package Finance::GeniusTrader::TradeFilters::FollowTrend; | ||
| 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 | # Standards-Version: 1.0 | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use vars qw(@NAMES @ISA @DEFAULT_ARGS); | ||
| 11 | |||
| 12 | use Finance::GeniusTrader::TradeFilters; | ||
| 13 | use Finance::GeniusTrader::Indicators::SMA; | ||
| 14 | |||
| 15 | @NAMES = ("FollowTrend[#1]"); | ||
| 16 | @ISA = qw(Finance::GeniusTrader::TradeFilters); | ||
| 17 | @DEFAULT_ARGS = (50); | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | Finance::GeniusTrader::TradeFilters::FollowTrend - Allow only trades following the direction of an SMA | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This filter tries to limit the risks by refusing trades againts the | ||
| 26 | market (ie like buying in a bear market or selling in a bullish market). | ||
| 27 | |||
| 28 | The first parameter is the number of days used to calculate the SMA. | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | sub initialize { | ||
| 33 | my ($self) = @_; | ||
| 34 | |||
| 35 | $self->{'mm'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names(1) ]); | ||
| 36 | |||
| 37 | $self->add_indicator_dependency($self->{'mm'}, 2); | ||
| 38 | |||
| 39 | return; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub accept_trade { | ||
| 43 | my ($self, $order, $i, $calc, $portfolio) = @_; | ||
| 44 | my $mm_name = $self->{'mm'}->get_name; | ||
| 45 | |||
| 46 | # Refuse if we can't evaluate the risk | ||
| 47 | return 0 if (! $self->check_dependencies_interval($calc, $i - 1, $i)); | ||
| 48 | |||
| 49 | if ($calc->indicators->get($mm_name, $i-1) < | ||
| 50 | $calc->indicators->get($mm_name, $i)) | ||
| 51 | { | ||
| 52 | # Bull market | ||
| 53 | if ($order->is_buy_order()) | ||
| 54 | { | ||
| 55 | return 1; | ||
| 56 | } else { | ||
| 57 | return 0; | ||
| 58 | } | ||
| 59 | } else { | ||
| 60 | # Bear market | ||
| 61 | if ($order->is_sell_order()) | ||
| 62 | { | ||
| 63 | return 1; | ||
| 64 | } else { | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; |