Class: MarketDataFaker::StockExchange

Inherits:
Object
  • Object
show all
Defined in:
lib/market_data_faker/stock_exchange.rb

Constant Summary collapse

XETRA =
["7:00", "19:00"]
FRANKFURT =
["7:00", "19:00"]
LSE =
["8:00", "16:30"]
EURONEXT =
["8:00", "16:30"]
NYSE =
["14:30", "21:00"]
NASDAQ =
["14:30", "21:00"]

Class Method Summary collapse

Class Method Details

.exchange(symbol, fake_it = true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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