Trading with opensource tools, two years later
-
Upload
clkao -
Category
Technology
-
view
3.602 -
download
1
Transcript of Trading with opensource tools, two years later
Trading System
Trading system, also known as algorithmic trading, is the use of computer programs for entering trading orders with the computer algorithm deciding on certain aspects of the order such as the timing, price, quantity of order
Even with dice
The OSDC Trading System
(in 2009 talk)
Risk = controlled by you
Profit = probabilities
quote
strategy
order(via broker)
Automated TradingComponents
bayen2010 results
40.15% net, end of year
(Not with dice!)
Architecture 2009
quote
strategy
order(via broker)
memcached
jsonrpc
GeniusTrader
pos: +1, -1, 0
Architecture 2011
Runs on Amazon EC2(ap-southeast-1)
quote
strategy2
order(via broker)
rabbitmqanymq
jsonrpc
TradeSpringstrategy1 order ensuring
position sizing
quote monitoring
system monitoring emergency fallback
moved to AnyMQmoved to EC2
avg: -2.5 in 2010
avg: -0.5 in 2011
95.71% gross-21.85% cost-33.71% slippage
40.15% net, end of year
Quote
• Source
• Commercial Quote services
• Win32::DDE
• AnyMQ
• AnyEvent::Monitor
• Price AGgregation Manager: serve history and manages real time quotes
Source
• was running on MicroXP in VirtualBox
• now on EC2 win32 instances
• Republish to rabbitmq with AnyMQ
AnyMQ
• inspired by Tatsumaki::MessageQueue
• Use Moose traits
• Available on CPAN
• can talk to amqp servers like rabbitmq
AnyMQ
my $bus = AnyMQ->new;my $topic = $bus->topic("Foo");
my $sub = $bus->new_listener($topic);$sub->poll(sub { my $msg = shift; })
$topic->publish($msg)
AnyMQ with AMQP
my $bus = AnyMQ->new_with_traits ( traits => [ ‘AMQP’], # host => ..., port => ..);my $topic = $bus->topic("Foo");
my $sub = $bus->new_listener($topic);$sub->poll(sub { my $msg = shift; })
$topic->publish($msg)
AnyMQ with AMQPmy $bus = AnyMQ->new_with_traits ( traits => [ ‘AMQP’], # host => ..., port => ..);my $topic = $bus->topic("Foo");
my $sub = $bus->new_listener($topic);$sub->poll(sub { my $msg = shift; })
$topic->publish($msg)
my $bus = AnyMQ->new_with_traits ( traits => [ ‘AMQP’], # host => ..., port => ..);my $topic = $bus->topic("Foo");
my $sub = $bus->new_listener($topic);$sub->poll(sub { my $msg = shift; })
$topic->publish($msg)
my $bus = AnyMQ->new_with_traits ( traits => [ ‘AMQP’], # host => ..., port => ..);my $topic = $bus->topic("Foo");
my $sub = $bus->new_listener($topic);$sub->poll(sub { my $msg = shift; })
$topic->publish($msg)
my $bus = AnyMQ->new_with_traits ( traits => [ ‘AMQP’], # host => ..., port => ..);my $topic = $bus->topic("Foo");
my $sub = $bus->new_listener($topic);$sub->poll(sub { my $msg = shift; })
$topic->publish($msg)AMQP Server
Service monitoring
• Is it running?
• Can we fix it when it fails?
AnyEvent::Monitor my $foo = AnyEvent::Monitor->new( name => 'foo', on_softfail => sub { warn "==> service fail: $_[1]"; }, on_hardfail => sub { my ($resume_check); warn "==> service fail, should attempt to do something to fix it: $_[1]"; $resume_check->(60); # resume checking after 60 secs }, on_resume => sub { my ($prev, $outage) = @_; if ($prev) { warn "service resumed from: $prev, total outage: $outage secs"; } });
PAGM
• a daemon listening on rabbitmq topic
• tells client where to look for aggregated price ticks
• answers historical price request:
{ type: ‘pagm.history’, reply: ‘client-name’,
code: ‘XTAF.TX’, timeframe: ‘5min’, count: 180 }
Strategy
• the core that decides:
• when to buy/sell?
• how much to buy/sell?
• when to stop loss, take profit?
• Finance::GeniusTrader
• TradeSpring
Finance::GeniusTrader
• DSL for trade systemsSY:Generic {S:G:Above {I:Random 6} 2} {S:G:Below {I:Random 6} 3} \ |TF:Generic {S:G:CrossOverUp {I:Hour} 850} \ {S:G:CrossOverUp {I:Hour} 850} \ |TF:MaxOpenTrades 1 \ |CS:DayTrade
• too verbose
• designed for backtesting
• didn’t support live trading by default
TradeSpring!
♨++
(Second largest profit this year was on 3/15,went to Japan Red Cross Society)
TradeSpring
• New strategy runner engine
• backtest code = live code
• supports complex order types
• attached, OCA, trail stop, etc
• Moose based
• More abstracted
• release this year?
TradeSpringmethod detect_direction { for (1,-1) { local $self->{direction} = $_; if ($self->bt($self->better, $self->better($self->i-1)) && $self->bt($self->better($self->i-1), $self->better($self->i-2)) && $self->bt($self->close, $self->close( $self->i-1)) && $self->bt($self->close($self->i-1), $self->close( $self->i-2))) {
return $self->{direction}; } }}
dir = -1
better
dir = 1
TradeSpring examplesmethod detect_direction { for (1,-1) { local $self->{direction} = $_; if ($->bt($->better, $->better($->i-1)) && $->bt($->better($->i-1), $->better($->i-2)) && $->bt($->close, $->close( $->i-1)) && $->bt($->close( $->i-1), $->close( $->i-2))) {
return $self->{direction}; } }}
invoker.pm on CPAN
TradeSpring Order
$self->new_bracket_order( { dir => $direction, type => 'lmt', price => $self->better($self->i-1), qty => $self->position_qty }, { price => $self->worse($self->i-1) }, { type => 'mkt', price => 0, timed => $self->order_timed(13, 43, 00) });
stop
exit
entry
EC2
• multiple az
• ap-southeast-1 latency: 60ms
• ap-northeast-1 latency: 40ms
• (to broker endpoint)
• multiple region
Net::Amazon::EC2
• cloud-init, user-data
tn1: persistent: 1 ami_name: tn-dev6 type: m1.small security_group: default key_name: clec availability_zone: ap-southeast-1a
Ganglia
ec2 costs: ~US$100 / month
Analysis
• R
• protovis
• ask interesting questions and check your records!
Trade result breakdown by time and day of week
?
?
2011 results YOD
10% of profit will go to TPF
PROFIT!!
謝謝!