class MarketDataFaker::StockExchange

Constants

EURONEXT
FRANKFURT
LSE
NASDAQ
NYSE
XETRA

Public Class Methods

exchange(symbol, fake_it=true) click to toggle source
# File lib/market_data_faker/stock_exchange.rb, line 41
def self.exchange(symbol, fake_it=true)
  collection = []
  stock_exchanges = StockExchange.constants.map(&:downcase)
  quote           = MarketDataFaker::Quote.get(symbol, fake_it)
  stock_exchanges.each do |ex|
    random             = 1 + (Random.new.rand(0.0035) * ([-1,1].sample))
    is_open            = is_open(ex.to_s)
    exchange           = ex.to_s
    avg_volume         = quote.avg_total_volume   * random
    latest_price       = quote.latest_price       * random
    construct_realtime = fake_realtime(latest_price)
    ask_size           = (quote.iex_ask_size == 0  || quote.iex_ask_size.nil? ) ? construct_realtime[:ask_size]  : quote.iex_ask_size * random
    ask_price          = (quote.iex_ask_price == 0 || quote.iex_ask_price.nil?) ? construct_realtime[:ask_price] : quote.iex_ask_price * random
    bid_size           = (quote.iex_bid_size == 0  || quote.iex_bid_size.nil?)  ? construct_realtime[:bid_size]  : quote.iex_bid_size * random
    bid_prize          = (quote.iex_bid_price == 0 || quote.iex_bid_price.nil?) ? construct_realtime[:bid_price] : quote.iex_bid_price * random
    last_update        = quote.iex_last_updated_t
    
    collection << {
      symbol: symbol,
      exchange: exchange,
      avg_volume: avg_volume,
      latest_price: latest_price,
      ask_size: ask_size,
      ask_price: ask_price,
      bid_size: bid_size,
      bid_prize: bid_prize,
      last_update: last_update,
      is_open: is_open
    }
  end
  return collection
end
fake_realtime(latest_price) click to toggle source
# File lib/market_data_faker/stock_exchange.rb, line 27
def self.fake_realtime(latest_price)
  ask_size   = rand(50..250)
  bid_size   = ask_size
  ask_price  = latest_price * (1 - Random.new.rand(0.0005))
  bid_price  = latest_price * (1 - Random.new.rand(0.02))
  collection = {
    ask_size: ask_size,
    ask_price: ask_price,
    bid_size: bid_size,
    bid_price: bid_price
  }
  return collection
end
is_open(stock_exchange) click to toggle source
# File lib/market_data_faker/stock_exchange.rb, line 11
def self.is_open(stock_exchange)
  opening_hours = StockExchange.const_get(stock_exchange.upcase)
  opening = Time.zone.parse(opening_hours[0])
  closing = Time.zone.parse(opening_hours[1])
  t       = Time.zone.now
  weekday = t.strftime("%A")

  if weekday == "Saturday" || weekday == "Sunday"
    return false
  elsif t.between?(opening, closing)
    return true
  else
    return false
  end
end