Trading with opensource tools, two years later

4,322 views

Published on

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,322
On SlideShare
0
From Embeds
0
Number of Embeds
528
Actions
Shares
0
Downloads
92
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Trading with opensource tools, two years later

  1. 1. Tradingwith open source tools two years later Chia-liang Kao clkao@clkao.org OSDC.TW 2011 Taipei
  2. 2. Trading SystemTrading system, also known as algorithmic trading, is the use ofcomputer programs for entering trading orders with the computeralgorithm deciding on certain aspects of the order such as thetiming, price, quantity of order Even with dice
  3. 3. The OSDC Trading System (in 2009 talk)
  4. 4. Risk = controlled by youProfit = probabilities
  5. 5. Automated Trading Components quote strategy order (via broker)
  6. 6. bayen2010 results 40.15% net, end of year (Not with dice!)
  7. 7. Architecture 2009
  8. 8. quotememcached strategy GeniusTrader jsonrpc order pos: +1, -1, 0 (via broker)
  9. 9. Architecture 2011
  10. 10. quote monitoring quote rabbitmq Runs on Amazon EC2 position sizing anymq strategy1 (ap-southeast-1) strategy2 TradeSpring order ensuring jsonrpc order (via broker)system monitoring emergency fallback
  11. 11. 40.15% net, end of year 95.71% gross -21.85% cost -33.71% slippage avg: -2.5 in 2010 avg: -0.5 in 2011 moved to EC2moved to AnyMQ
  12. 12. Quote• Source • Commercial Quote services • Win32::DDE• AnyMQ• AnyEvent::Monitor• Price AGgregation Manager: serve history and manages real time quotes
  13. 13. Source• was running on MicroXP in VirtualBox• now on EC2 win32 instances• Republish to rabbitmq with AnyMQ
  14. 14. AnyMQ• inspired by Tatsumaki::MessageQueue• Use Moose traits• Available on CPAN• can talk to amqp servers like rabbitmq
  15. 15. AnyMQmy $bus = AnyMQ->new;my $topic = $bus->topic("Foo");my $sub = $bus->new_listener($topic);$sub->poll(sub { my $msg = shift; })$topic->publish($msg)
  16. 16. 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)
  17. 17. AnyMQ with AMQPmy $bus = AnyMQ->new_with_traits my $bus = AnyMQ->new_with_traits ( traits => [ ‘AMQP’], ( traits => [ ‘AMQP’], # host => ..., port => ..); # host => ..., port => ..);my $topic = $bus->topic("Foo"); my $topic = $bus->topic("Foo");my $sub = $bus->new_listener($topic); my $sub = $bus->new_listener($topic);$sub->poll(sub { my $msg = shift; }) $sub->poll(sub { my $msg = shift; })$topic->publish($msg) $topic->publish($msg) AMQP Server my $bus = AnyMQ->new_with_traits my $bus = AnyMQ->new_with_traits ( traits => [ ‘AMQP’], ( traits => [ ‘AMQP’], # host => ..., port => ..); # host => ..., port => ..); my $topic = $bus->topic("Foo"); my $topic = $bus->topic("Foo"); my $sub = $bus->new_listener($topic); my $sub = $bus->new_listener($topic); $sub->poll(sub { my $msg = shift; }) $sub->poll(sub { my $msg = shift; }) $topic->publish($msg) $topic->publish($msg)
  18. 18. Service monitoring• Is it running?• Can we fix it when it fails?
  19. 19. AnyEvent::Monitormy $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"; } });
  20. 20. 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 }
  21. 21. Strategy• the core that decides: • when to buy/sell? • how much to buy/sell? • when to stop loss, take profit?• Finance::GeniusTrader• TradeSpring
  22. 22. 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
  23. 23. TradeSpring!
  24. 24. ++
  25. 25. (Second largest profit this year was on 3/15, went to Japan Red Cross Society)
  26. 26. 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?
  27. 27. 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
  28. 28. 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
  29. 29. TradeSpring Order$self->new_bracket_order( { dir => $direction, type => lmt, price => $self->better($self->i-1), entry qty => $self->position_qty }, { price => $self->worse($self->i-1) }, stop { type => mkt, price => 0, timed => $self->order_timed(13, 43, 00) }); exit
  30. 30. EC2• multiple az • ap-southeast-1 latency: 60ms • ap-northeast-1 latency: 40ms • (to broker endpoint)• multiple region
  31. 31. Net::Amazon::EC2• cloud-init, user-datatn1: persistent: 1 ami_name: tn-dev6 type: m1.small security_group: default key_name: clec availability_zone: ap-southeast-1a
  32. 32. Gangliaec2 costs: ~US$100 / month
  33. 33. Analysis•R• protovis• ask interesting questions and check your records!
  34. 34. Trade result breakdown by time and day of week
  35. 35. 2011 results YOD ? ?10% of profit will go to TPF
  36. 36. PROFIT!!
  37. 37. Questions? clkao@clkao.org

×