| 1 | package Finance::GeniusTrader::Brokers::InteractiveBrokers; | ||
| 2 | |||
| 3 | # Copyright 2003 Olaf Dietsche | ||
| 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(@NAMES @ISA); | ||
| 9 | |||
| 10 | use Finance::GeniusTrader::Brokers; | ||
| 11 | use Finance::GeniusTrader::Eval; | ||
| 12 | use Finance::GeniusTrader::Conf; | ||
| 13 | |||
| 14 | @NAMES = ("InteractiveBrokers[#1]", "IB[#1]"); | ||
| 15 | @ISA = qw(Finance::GeniusTrader::Brokers); | ||
| 16 | |||
| 17 | =head1 Finance::GeniusTrader::Brokers::InteractiveBrokers | ||
| 18 | |||
| 19 | =head2 Overview | ||
| 20 | |||
| 21 | This module will calculate all commissions and charges according to | ||
| 22 | InteractiveBrokers rules. | ||
| 23 | |||
| 24 | =head2 Calculation | ||
| 25 | |||
| 26 | Current calculation for InteractiveBrokers at: | ||
| 27 | L<http://www.interactivebrokers.com/index.html?html/retailAccount/fees.html~top.body> | ||
| 28 | |||
| 29 | Germany XETRA/IBIS: | ||
| 30 | 0,1% of stock value, minimum of 4 EUR, maximum of 29 EUR | ||
| 31 | |||
| 32 | Switzerland: | ||
| 33 | 0,1% of stock value, minimum of 10 CHF + 0.07% Stamp Tax | ||
| 34 | |||
| 35 | UK: | ||
| 36 | 0,1% of stock value, minimum of 5 GBP + 0.5% UK Stamp Tax on purchase | ||
| 37 | |||
| 38 | Ireland: | ||
| 39 | same as UK, but 1% Irish Stamp Tax | ||
| 40 | |||
| 41 | US: | ||
| 42 | USD 0.01 / share, up to 500 shares | ||
| 43 | USD 0.005 / share, for 501th share and up | ||
| 44 | minimum of 1 USD | ||
| 45 | |||
| 46 | Options and futures commissions are not considered. | ||
| 47 | |||
| 48 | No annual charge. | ||
| 49 | |||
| 50 | =head2 Parameters | ||
| 51 | |||
| 52 | The first parameter could be initialized to : | ||
| 53 | |||
| 54 | 'de' => Germany Xetra, | ||
| 55 | 'ch' => Switzerland, | ||
| 56 | 'ie' => Ireland, | ||
| 57 | 'uk' => United Kingdom, | ||
| 58 | 'us' => US Markets | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub new { | ||
| 63 | my $type = shift; | ||
| 64 | my $class = ref($type) || $type; | ||
| 65 | my $args = shift; | ||
| 66 | Finance::GeniusTrader::Conf::default('Brokers::InteractiveBrokers::Market', 'de'); | ||
| 67 | my $option = Finance::GeniusTrader::Conf::get('Brokers::InteractiveBrokers::Market'); | ||
| 68 | my $self = { 'args' => defined($args) ? $args : [$option] }; | ||
| 69 | |||
| 70 | return manage_object(\@NAMES, $self, $class, $self->{'args'}, ''); | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 $broker->calculate_order_commission($order) | ||
| 74 | |||
| 75 | Return the amount of money ask by the broker for the given order. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub calculate_order_commission_percentage { | ||
| 80 | my($self, $order, $percent, $min, $max) = @_; | ||
| 81 | my $commission = 0; | ||
| 82 | |||
| 83 | if ((defined($order->quantity) && $order->quantity) and | ||
| 84 | (defined($order->price) && $order->price)) { | ||
| 85 | my $investment = $order->quantity * $order->price; | ||
| 86 | $commission = sprintf("%.2f", $investment * $percent); | ||
| 87 | $commission = $min if ($min and $commission < $min); | ||
| 88 | $commission = $max if ($max and $commission > $max); | ||
| 89 | return $commission if ($commission != 0); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub calculate_order_commission_de { | ||
| 94 | my ($self, $order) = @_; | ||
| 95 | return $self->calculate_order_commission_percentage($order, 0.1 / 100, 4, 29); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub calculate_order_commission_ch { | ||
| 99 | my ($self, $order) = @_; | ||
| 100 | my $commission = $self->calculate_order_commission_percentage($order, 0.1 / 100, 10); | ||
| 101 | $commission += $commission * 0.07 / 100; | ||
| 102 | return $commission if ($commission != 0); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub calculate_order_commission_ie { | ||
| 106 | my ($self, $order) = @_; | ||
| 107 | my $commission = $self->calculate_order_commission_percentage($order, 0.1 / 100, 5); | ||
| 108 | $commission += $commission * 1.0 / 100 if ($order->is_buy_order()); | ||
| 109 | return $commission if ($commission != 0); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub calculate_order_commission_uk { | ||
| 113 | my ($self, $order) = @_; | ||
| 114 | my $commission = $self->calculate_order_commission_percentage($order, 0.1 / 100, 5); | ||
| 115 | $commission += $commission * 0.5 / 100 if ($order->is_buy_order()); | ||
| 116 | return $commission if ($commission != 0); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub calculate_order_commission_us { | ||
| 120 | my ($self, $order, $min) = @_; | ||
| 121 | my $quantity = $order->quantity; | ||
| 122 | my $commission = 0; | ||
| 123 | |||
| 124 | if ($quantity) { | ||
| 125 | if ($quantity > 500) { | ||
| 126 | $commission = sprintf("%.2f", ($quantity - 500) * 0.005); | ||
| 127 | $quantity = 500; | ||
| 128 | } | ||
| 129 | |||
| 130 | $commission += $quantity * 0.01; | ||
| 131 | $commission = 1 if ($min and $commission < 1); | ||
| 132 | return $commission if ($commission != 0); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | sub calculate_order_commission { | ||
| 137 | my ($self, $order) = @_; | ||
| 138 | my $market = $self->{args}->[0]; | ||
| 139 | return $self->calculate_order_commission_de($order) if ($market eq 'de'); | ||
| 140 | return $self->calculate_order_commission_ch($order) if ($market eq 'ch'); | ||
| 141 | return $self->calculate_order_commission_ie($order) if ($market eq 'ie'); | ||
| 142 | return $self->calculate_order_commission_uk($order) if ($market eq 'uk'); | ||
| 143 | return $self->calculate_order_commission_us($order) if ($market eq 'us'); | ||
| 144 | |||
| 145 | die "$market: unknown market"; | ||
| 146 | } | ||
| 147 | |||
| 148 | =head2 $broker->calculate_annual_account_charge($portfolio, $year) | ||
| 149 | |||
| 150 | Return the amount of money ask by the broker for the given year | ||
| 151 | according to the given portfolio, which is 0 EUR. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub calculate_annual_account_charge { | ||
| 156 | my ($self, $portfolio, $year) = @_; | ||
| 157 | return 0; | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; |