As an exercise to keep my mind nimble, here.s a write-up on how to use the power of computers to take over the world by out-foxing those slow moving meatbags who do stock trading and compete with skynet on making the most possible profit.
The pieces of this puzzle are:
- A messaging backbone (we.ll use AMQP with the RabbitMQ broker)
- A complex event processing engine (Esper)
- A way to express our greed (EPL statements)
- A software that ties this all together called new-hope (partially written by yours truly)
- A feed of stock prices
- An app to view the actions we must take.
Let's get everything installed.
On centos with the EPEL repo available:
|
|
Need ruby installed with rvm (jruby). Figure it out!
Install new-hope
|
|
Next some gems (or run the install gems shell script in the new-hope repo)
|
|
The new hope configs:
config/amqp.cfg
amqp_settings :host => "127.0.0.1",
:port => "5672",
:user => "skynet",
:pass => "skynet",
:vhost => "/stockexchange",
:exchange_input => "esper-in",
:exchange_output => "esper-out"
config/hope.epl
|
|
config/hope.xml:
|
|
A cheapy little stock price fetcher (generator.php):
|
|
A cheapy little event viewer (viewer.php):
|
|
I forget if you have to manually create the exchanges in rabbitmq. If so, that.s pretty straight forward in the rabbitmq management web console.
Running new-hope:
|
|
And the generator
|
|
And the viewer
|
|
Should produce something like this every 10 minutes or so:
stdClass Object
(
[new_events] => Array
(
[0] => stdClass Object
(
[event_type] => anonymous_75420a3c-b91a-435d-bcc9-ba4a79b23519_result_
[event_properties] => Array
(
[0] => symbol
[1] => LowerBand
[2] => MiddleBand
[3] => UpperBand
[4] => price
[5] => Bandwidth
)
[event] => stdClass Object
(
[price] => 281.07
[symbol] => AMZN
[MiddleBand] => 281.07
[LowerBand] => 281.06999184383
[UpperBand] => 281.07000815617
[Bandwidth] => 5.803654484539E-8
)
)
)
[old_events] => Array
(
)
[name] => 02-bollinger
)
Phew! So every 10 minutes, our little viewer will output the Bollinger band data for our selected stocks (GOOG,MSFT,RHT,AMZN,COST,KO). A saavy
investor watching this might want to buy stocks when the current price is close to the lower bands, and sell when the stocks are close to the
upper bands, as those indicate that the prices are close to the 5 day variances.
See here for details on the web service to pull the stock data.