Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
airhorns committed Nov 20, 2011
1 parent d4ad5b1 commit 8221d9a
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -17,6 +17,10 @@ group :server do
gem 'redis-scripted', :require => "redis/scripted"
end

group :sms do
gem 'twilio-ruby'
end

group :test do
gem 'turn'
gem 'minitest', '~> 2.7.0'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Expand Up @@ -4,6 +4,7 @@ GEM
ansi (1.4.0)
async-rack (0.5.1)
rack (~> 1.1)
builder (3.0.0)
em-synchrony (1.0.0)
eventmachine (>= 1.0.0.beta.1)
eventmachine (1.0.0.beta.4)
Expand All @@ -24,6 +25,8 @@ GEM
rack (>= 1.0.0)
url_mount (~> 0.2.1)
json (1.6.1)
jwt (0.1.4)
json (>= 1.2.4)
log4r (1.1.9)
minitest (2.7.0)
multi_json (1.0.3)
Expand All @@ -45,6 +48,10 @@ GEM
tilt (1.3.3)
turn (0.8.3)
ansi
twilio-ruby (3.4.2)
builder (>= 2.1.2)
jwt (>= 0.1.2)
multi_json (>= 1.0.3)
url_mount (0.2.1)
rack
will_paginate (3.0.2)
Expand All @@ -63,4 +70,5 @@ DEPENDENCIES
redis-scripted
sinatra
turn
twilio-ruby
will_paginate
3 changes: 3 additions & 0 deletions exchange_server.rb
Expand Up @@ -97,7 +97,10 @@ def validate(params)
@twilio = params['Twilio']
if @twilio != 'Y' and @twilio != 'N'
return 'T'
else
@twilio = @twilio == 'Y'
end

@price = params['Price'].to_f
if @price > 100000 or @price < 1
return "X"
Expand Down
2 changes: 1 addition & 1 deletion redis_scripts/fill_order.lua
@@ -1,6 +1,6 @@
local MAX_ID = 100000000
local stock, order_type = KEYS[1], KEYS[2]
local from, shares, price, twilio, broker, created = ARGV[1], tonumber(ARGV[2]), tonumber(ARGV[3]), tonumber(ARGV[4]), ARGV[5], ARGV[6]
local from, shares, price, twilio, broker, created = ARGV[1], tonumber(ARGV[2]), tonumber(ARGV[3]), ARGV[4], ARGV[5], ARGV[6]

function score(price, id, order_type)
if order_type == 'buy' then
Expand Down
Binary file modified sender_python/.Sender.py.swp
Binary file not shown.
50 changes: 50 additions & 0 deletions sms_server.rb
@@ -0,0 +1,50 @@
require "rubygems"
require "bundler/setup"
require 'net/http'
require 'uri'

Bundler.require :default, :sms

$redis = Redis::Scripted.connect(scripts_path: "./redis_scripts")
subscriber = Redis.new

$: << "."
require 'trade_manager'

trap(:INT) { puts; exit }

# put your own credentials here
account_sid = "AC888a285988894223a40b8d0df20d6d58"
auth_token = "6ea425d908a216d20d505eb013d55985"
number = "+15148005440"

# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new account_sid, auth_token

def notify_via_sms(order, trade)
puts "Notifying #{order['from']}", @client.account.sms.messages.create(
:from => number,
:to => order['from'],
:body => sms_body(order, trade)
)
end

def sms_body(order, trade)
"Your order #{order['id']} has been executed for #{trade['shares']} shares. Your match # is #{trade['id']} and the trade executed at #{trade['price'].to_f / 100} per share."
end

subscriber.subscribe('trades') do |on|
on.message do |channel, message|
trade = TradeManager.get(message)
buy_order = TradeManager.get_root(trade['buy_order'])
sell_order = TradeManager.get_root(trade['sell_order'])

puts "*******"
puts buy_order
puts sell_order

[buy_order, sell_order].each do |order|
notify_via_sms(order, trade) if order['twilio'] == 'true'
end
end
end
2 changes: 1 addition & 1 deletion snapshot.rb
Expand Up @@ -27,7 +27,7 @@ def all_rows
end

ITEM_TO_ROW = {'created' => 'timestamp', 'shares' => 'amount', 'stock' => 'symbol', 'price' => 'price'}
TRADE_TO_ROW = ITEM_TO_ROW.merge({'buy_order_id' => 'buyOrderRef', 'sell_order_id' => 'sellOrderRef', 'id' => 'matchNumber'})
TRADE_TO_ROW = ITEM_TO_ROW.merge({'buy_order' => 'buyOrderRef', 'sell_order' => 'sellOrderRef', 'id' => 'matchNumber'})
ORDER_TO_ROW = ITEM_TO_ROW.merge({'from' => 'phone', 'parent' => 'parentOrderRef', 'twilio' => false, 'broker' => false, 'id' => 'orderRef'})

def trade_to_row(raw)
Expand Down
14 changes: 6 additions & 8 deletions test/test.rb
Expand Up @@ -3,11 +3,9 @@
@manager = StockManager.new("apple")
@manager.reset!

100.times do
@manager.buy("1234", 50, 91, false, "a")
@manager.buy("1234", 150, 91, false, "a")
@manager.buy("1234", 100, 92, false, "a")
@manager.sell("1234", 200, 90, false, "a")
@manager.sell("1234", 200, 90, false, "a")
@manager.buy("1234", 100, 92, false, "a")
end
@manager.buy("1234", 50, 91, true, "a")
@manager.buy("1234", 150, 91, true, "a")
@manager.buy("1234", 100, 92, true, "a")
@manager.sell("1234", 200, 90, true, "a")
@manager.sell("1234", 200, 90, true, "a")
@manager.buy("1234", 100, 92, true, "a")
12 changes: 12 additions & 0 deletions trade_manager.rb
@@ -1,3 +1,7 @@
$redis = Redis::Scripted.connect(scripts_path: "./redis_scripts")

require 'stock_manager'
require 'trade_manager'
require File.expand_path('./stock_manager', File.dirname(__FILE__))

class TradeManager
Expand All @@ -12,4 +16,12 @@ def self.get(id)
['shares', 'price'].each { |k| hash[k] = hash[k].to_i }
end
end

def self.get_root(id)
order = self.get(id)
until order['parent_id'].nil? || order['parent_id'].empty?
order = self.get(order['parent_id'])
end
order
end
end

0 comments on commit 8221d9a

Please sign in to comment.