module StockFighter::ApiMixin::TradingApi

Private Class Methods

create_partified_module() click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 61
def self.create_partified_module
  Module.new do
    include HTTParty

    base_uri 'https://api.stockfighter.io/ob/api'
    format :json
    headers 'Content-Type' => 'application/json'
  end
end

Public Instance Methods

cancel_order(venue, stock, order) click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 39
def cancel_order venue, stock, order
  trading_http.delete "/venues/#{venue}/stocks/#{stock}/orders/#{order}"
end
get_orderbook(venue, stock) click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 22
def get_orderbook venue, stock
  trading_http.get "/venues/#{venue}/stocks/#{stock}"
end
get_quote(venue, stock) click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 43
def get_quote venue, stock
  trading_http.get "/venues/#{venue}/stocks/#{stock}/quote"
end
list_account_orders(venue, account) click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 47
def list_account_orders venue, account
  trading_http.get "/venues/#{venue}/accounts/#{account}/orders"
end
list_account_stock_orders(venue, account, stock) click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 51
def list_account_stock_orders venue, account, stock
  trading_http.get "/venues/#{venue}/accounts/#{account}/stocks/#{stock}/orders"
end
list_stocks(venue) click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 18
def list_stocks venue
  trading_http.get "/venues/#{venue}/stocks"
end
place_order(venue, stock, account, price:, qty:, direction: "buy", order_type: "limit") click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 26
def place_order venue, stock, account, price:, qty:, direction: "buy", order_type: "limit"
  params = method(__method__).parameters.map(&:last)
  body = params.map { |p| [p, eval(p.to_s)] }.to_h

  body[:orderType] = body.delete(:order_type)

  trading_http.post "/venues/#{venue}/stocks/#{stock}/orders", body: JSON.dump(body)
end
send_heartbeat() click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 10
def send_heartbeat
  trading_http.get "/heartbeat"
end
send_venue_heartbeat(venue) click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 14
def send_venue_heartbeat venue
  trading_http.get "/venues/#{venue}/heartbeat"
end
set_api_key(api_key) click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 6
def set_api_key api_key
  trading_http.headers "X-Starfighter-Authorization" => api_key
end
show_order(venue, stock, order) click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 35
def show_order venue, stock, order
  trading_http.get "/venues/#{venue}/stocks/#{stock}/orders/#{order}"
end

Private Instance Methods

trading_http() click to toggle source
# File lib/stock_fighter/api_mixin/trading_api.rb, line 56
def trading_http
  # TODO: how do we create module specific vars?
  @trading_http_delegator ||= TradingApi.create_partified_module
end