Class: ChockABlock::Stock

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/chock_a_block/stock.rb

Instance Method Summary collapse

Constructor Details

#initialize(venue, symbol) ⇒ Stock

Initialize a Stock resource API consumer

Parameters:

  • venue (String)

    venue name

  • symbol (String)

    stock symbol



9
10
11
12
# File 'lib/chock_a_block/stock.rb', line 9

def initialize(venue, symbol)
  @venue  = venue
  @symbol = symbol
end

Instance Method Details

#best_biddingHash

Get the best bidding currently available on the orderbook

or retry if best_bid is nil

Returns:

  • (Hash)

    representing the best current price/qty offered by bots



39
40
41
42
43
44
45
46
47
48
# File 'lib/chock_a_block/stock.rb', line 39

def best_bidding
  order_book = get_orderbook
  best_bid = order_book['bids']&.last

  return best_bid unless best_bid.nil?

  # retryl few seconds later
  sleep 2
  best_bidding
end

#get_orderbookHash

Get the orderbook for specific STOCK_SYMBOL in a specific VENUE API Consumer

Returns:

  • (Hash)

    representing the response of the stock orderbook



18
19
20
21
22
23
# File 'lib/chock_a_block/stock.rb', line 18

def get_orderbook
  response = HTTP.get(BASE_URL + "/venues/#{@venue}/stocks/#{@symbol}")
  fail StockNotFoundError, @symbol if response.code == 500
  fail VenueNotFoundError, @venue  if response.code == 404
  JSON.parse response.body
end

#get_quoteHash

Get the qoute for a specific STOCK_SYMBOL in a specifc VENUE API Consumer

Returns:

  • (Hash)

    representing the response of the stock quote



29
30
31
32
33
# File 'lib/chock_a_block/stock.rb', line 29

def get_quote
  response = HTTP.get(BASE_URL + "/venues/#{@venue}/stocks/#{@symbol}/quote")
  fail StockNotFoundError, @symbol if response.code == 404
  JSON.parse response.body
end