autotrade::Trader: Support expiring old orders in default beat(); LowHigh: Expire...
[autotrade.git] / autotrade / Trader.pm
blobba2628798fc06ce8a76b9a8f441f02d547028a3f
1 package autotrade::Trader;
3 use Moose;
5 has 'm' => ( isa => 'autotrade::Market', is => 'ro', required => 1 );
7 has 'buy_lifetime' => ( isa => 'Int', is => 'rw', default => 9999999 );
8 has 'sell_lifetime' => ( isa => 'Int', is => 'rw', default => 9999999 );
10 # Single frame; analyze market data and make buys/sells.
11 # Takes parameters: autotrade::MarketData
12 # Returns 1 if we should try to make more buys/sells right away.
13 sub beat {
14 my $self = shift;
15 my ($md) = @_;
17 # The default implementation will just prune old orders.
18 # In theory - default lifetimes actually prevent that.
19 my $now = time;
20 my @o = grep {
21 defined $_->time()
22 and $_->time() < $now - ($_->otype() eq 'buy' ? $self->buy_lifetime() : $self->sell_lifetime())
23 } @{$md->orders()};
24 foreach my $o (@o) {
25 printf "\tCancelling too old order %d (%d): %s %s BTC @ %s USD\n",
26 $o->oid(), $o->time(), $o->otype(), $o->amount(), $o->rate();
27 $self->m()->cancel($o);
30 return 0;