Advertisement
Guest User

Untitled

a guest
Jul 14th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.86 KB | None | 0 0
  1. class TickingJob
  2.  
  3.   @queue = :normal
  4.  
  5.   def self.perform
  6.    
  7.   buying_amount = 0.0000
  8.   selling_amount = 0.0000
  9.   buying_threshold = 125
  10.   selling_threshold = 125
  11.  
  12.   # Returns orders that have been updated/created during the last two minutes
  13.   Order.where(["updated_at >= ?", Time.now - 2.minutes]).each do |order|
  14.     if order.status["EXECUTED"] == "EXECUTED" # or order.status["FORCED"] == "FORCED"
  15.       time_factor = (120 - (Time.now - order.updated_at))/120
  16.       buying_threshold += order.amount.abs * time_factor if order.amount < 0
  17.       selling_threshold += order.amount.abs * time_factor if order.amount > 0
  18.     end
  19.   end
  20.  
  21.   # Do we have a record in the tickers database table ?
  22.   if Ticker.first
  23.     buying = Ticker.last_tick("BTCUSD").buying
  24.     selling = Ticker.last_tick("BTCUSD").selling
  25.   end
  26.  
  27.   # Ughh, floats everywhere
  28.   spread = Setting.where(:key => "spread").first.value.to_f
  29.  
  30.   # The MtGox.asks method could easily be overriden to return canned data
  31.   @asks = MtGox.asks
  32.   @asks.each  do |ask|
  33.     selling_amount += ask.amount
  34.     if selling_amount >= selling_threshold
  35.       selling = (ask.price * spread).to_f
  36.       break
  37.     end
  38.   end
  39.  
  40.   @bids = MtGox.bids
  41.   @bids.each  do |bid|
  42.     buying_amount += bid.amount
  43.     if buying_amount >= buying_threshold
  44.       buying = (bid.price / spread).to_f
  45.       break
  46.     end
  47.   end
  48.  
  49.   # Prices adjustment
  50.   selling = selling * (1 - selling_amount / 2000000)
  51.   buying = buying * (1 +  buying_amount / 2000000)
  52.  
  53.   if buying > selling
  54.     midpoint = (buying + selling) / 2
  55.     buying = midpoint / spread
  56.     selling = midpoint * spread
  57.   end
  58.  
  59.  
  60.   # Add a record in the ticker table
  61.   Ticker.create(:buying => buying, :selling => selling, :pair => "BTCUSD")
  62.   $redis.set "ticker:btcusd:buying", buying
  63.   $redis.set "ticker:btcusd:selling", selling
  64.    
  65.   end
  66. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement