| 1 | package Finance::GeniusTrader::TradeFilters::Generic; | ||
| 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(@ISA @NAMES @DEFAULT_ARGS); | ||
| 11 | |||
| 12 | use Finance::GeniusTrader::Prices; | ||
| 13 | use Finance::GeniusTrader::TradeFilters; | ||
| 14 | use Finance::GeniusTrader::Eval; | ||
| 15 | use Finance::GeniusTrader::Tools qw(:generic); | ||
| 16 | |||
| 17 | @ISA = qw(Finance::GeniusTrader::TradeFilters); | ||
| 18 | @NAMES = ("Generic[#*]"); | ||
| 19 | @DEFAULT_ARGS = ("{S:Generic:False}", "{S:Generic:False}"); | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | TradeFilters::Generic - Accept or refuse trades based on specific signals | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This tradefilter takes two signals as parameter. The first decides if a buy | ||
| 28 | order is allowed, the second one decides if a sell order is allowed. If | ||
| 29 | you don't precise a parameter, the corresponding orders will be refused. | ||
| 30 | |||
| 31 | =head1 EXAMPLES | ||
| 32 | |||
| 33 | Allow buy orders only when SMA 20 is moving up and sell orders when | ||
| 34 | SMA 20 is decreasing : | ||
| 35 | |||
| 36 | TF:Generic {S:Generic:Increase {I:SMA 20}} {S:Generic:Decrease {I:SMA 20}} | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | |||
| 41 | sub initialize { | ||
| 42 | my ($self) = @_; | ||
| 43 | |||
| 44 | $self->add_arg_dependency(1, 1); | ||
| 45 | $self->add_arg_dependency(2, 1); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub precalculate_interval { | ||
| 49 | my ($self, $calc, $first, $last) = @_; | ||
| 50 | |||
| 51 | $self->{'args'}->prepare_interval($calc, $first, $last); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub accept_trade { | ||
| 55 | my ($self, $order, $i, $calc, $portfolio) = @_; | ||
| 56 | |||
| 57 | return if (! $self->check_dependencies($calc, $i)); | ||
| 58 | |||
| 59 | if ($order->is_buy_order()) { | ||
| 60 | # Buy order, allow only if first signal is true | ||
| 61 | if ($self->{'args'}->get_arg_values($calc, $i, 1)) { | ||
| 62 | return 1; | ||
| 63 | } else { | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | } else { | ||
| 67 | # Sell order, allow only if second signal is true | ||
| 68 | if ($self->{'args'}->get_arg_values($calc, $i, 2)) { | ||
| 69 | return 1; | ||
| 70 | } else { | ||
| 71 | return 0; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; |