trading with opensource tools, two years later
TRANSCRIPT
![Page 2: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/2.jpg)
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
![Page 3: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/3.jpg)
The OSDC Trading System
(in 2009 talk)
![Page 4: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/4.jpg)
Risk = controlled by you
Profit = probabilities
![Page 5: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/5.jpg)
quote
strategy
order(via broker)
Automated TradingComponents
![Page 6: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/6.jpg)
bayen2010 results
40.15% net, end of year
(Not with dice!)
![Page 7: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/7.jpg)
Architecture 2009
![Page 8: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/8.jpg)
quote
strategy
order(via broker)
memcached
jsonrpc
GeniusTrader
pos: +1, -1, 0
![Page 9: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/9.jpg)
Architecture 2011
![Page 10: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/10.jpg)
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
![Page 11: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/11.jpg)
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
![Page 12: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/12.jpg)
Quote
• Source
• Commercial Quote services
• Win32::DDE
• AnyMQ
• AnyEvent::Monitor
• Price AGgregation Manager: serve history and manages real time quotes
![Page 13: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/13.jpg)
Source
• was running on MicroXP in VirtualBox
• now on EC2 win32 instances
• Republish to rabbitmq with AnyMQ
![Page 14: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/14.jpg)
AnyMQ
• inspired by Tatsumaki::MessageQueue
• Use Moose traits
• Available on CPAN
• can talk to amqp servers like rabbitmq
![Page 15: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/15.jpg)
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)
![Page 16: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/16.jpg)
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)
![Page 17: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/17.jpg)
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
![Page 18: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/18.jpg)
Service monitoring
• Is it running?
• Can we fix it when it fails?
![Page 19: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/19.jpg)
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"; } });
![Page 20: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/20.jpg)
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 }
![Page 21: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/21.jpg)
Strategy
• the core that decides:
• when to buy/sell?
• how much to buy/sell?
• when to stop loss, take profit?
• Finance::GeniusTrader
• TradeSpring
![Page 22: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/22.jpg)
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
![Page 23: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/23.jpg)
TradeSpring!
![Page 24: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/24.jpg)
♨++
![Page 25: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/25.jpg)
(Second largest profit this year was on 3/15,went to Japan Red Cross Society)
![Page 26: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/26.jpg)
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?
![Page 27: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/27.jpg)
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
![Page 28: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/28.jpg)
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
![Page 29: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/29.jpg)
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
![Page 30: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/30.jpg)
EC2
• multiple az
• ap-southeast-1 latency: 60ms
• ap-northeast-1 latency: 40ms
• (to broker endpoint)
• multiple region
![Page 31: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/31.jpg)
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
![Page 32: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/32.jpg)
Ganglia
ec2 costs: ~US$100 / month
![Page 33: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/33.jpg)
Analysis
• R
• protovis
• ask interesting questions and check your records!
![Page 34: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/34.jpg)
Trade result breakdown by time and day of week
![Page 35: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/35.jpg)
?
?
2011 results YOD
10% of profit will go to TPF
![Page 36: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/36.jpg)
PROFIT!!
![Page 38: Trading with opensource tools, two years later](https://reader035.vdocuments.net/reader035/viewer/2022081400/55493583b4c905144d8b46f8/html5/thumbnails/38.jpg)
謝謝!