Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TickingJob
- @queue = :normal
- def self.perform
- buying_amount = 0.0000
- selling_amount = 0.0000
- buying_threshold = 125
- selling_threshold = 125
- # Returns orders that have been updated/created during the last two minutes
- Order.where(["updated_at >= ?", Time.now - 2.minutes]).each do |order|
- if order.status["EXECUTED"] == "EXECUTED" # or order.status["FORCED"] == "FORCED"
- time_factor = (120 - (Time.now - order.updated_at))/120
- buying_threshold += order.amount.abs * time_factor if order.amount < 0
- selling_threshold += order.amount.abs * time_factor if order.amount > 0
- end
- end
- # Do we have a record in the tickers database table ?
- if Ticker.first
- buying = Ticker.last_tick("BTCUSD").buying
- selling = Ticker.last_tick("BTCUSD").selling
- end
- # Ughh, floats everywhere
- spread = Setting.where(:key => "spread").first.value.to_f
- # The MtGox.asks method could easily be overriden to return canned data
- @asks = MtGox.asks
- @asks.each do |ask|
- selling_amount += ask.amount
- if selling_amount >= selling_threshold
- selling = (ask.price * spread).to_f
- break
- end
- end
- @bids = MtGox.bids
- @bids.each do |bid|
- buying_amount += bid.amount
- if buying_amount >= buying_threshold
- buying = (bid.price / spread).to_f
- break
- end
- end
- # Prices adjustment
- selling = selling * (1 - selling_amount / 2000000)
- buying = buying * (1 + buying_amount / 2000000)
- if buying > selling
- midpoint = (buying + selling) / 2
- buying = midpoint / spread
- selling = midpoint * spread
- end
- # Add a record in the ticker table
- Ticker.create(:buying => buying, :selling => selling, :pair => "BTCUSD")
- $redis.set "ticker:btcusd:buying", buying
- $redis.set "ticker:btcusd:selling", selling
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement