autotrade::Market::Simulated, autotrade-sim.pl: Support for strategy simulation on...
[autotrade.git] / autotrade / MarketTools.pm
blobcc410fd4aaab0b44f3641b19164559c572f294fd
1 package autotrade::MarketTools;
3 use Carp;
4 use Moose::Util::TypeConstraints;
6 require Exporter;
7 our @ISA = qw(Exporter);
8 our @EXPORT = qw($mult str_curr curr_str curr_conv doublefee_cost);
10 our $mult = 1000;
11 sub str_curr { sprintf '%d', $_[0] * $mult; }
12 sub curr_str { sprintf '%.3f', $_[0] / $mult; }
13 subtype "Curr", as "Str";
14 coerce "Curr", from "Str", via { str_curr($_) };
15 # coerce "Str", from "Curr", via { curr_str($_) };
17 # Convert from currency to currency by rate.
18 sub curr_conv {
19 my ($amount, $rate) = @_;
20 $amount * $rate / $mult;
23 # How much fees we need to pay for a buy-sell combo, assuming we will
24 # reimburse for the fees within the sell amount.
25 sub doublefee_cost {
26 my ($m, $buy, $sell) = @_;
27 # This solves the equation
28 # (buy + (sell + fees)) * tfee = fees
29 # returning $fees.
30 return ($buy + $sell) * $m->fee() / (1 - $m->fee());