Class: Stockfighter::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/stockfighter/api.rb

Constant Summary collapse

BASE_URL =
"https://api.stockfighter.io/ob/api"

Instance Method Summary collapse

Constructor Details

#initialize(key:, account:, symbol:, venue:) ⇒ Api

Returns a new instance of Api.



7
8
9
10
11
12
# File 'lib/stockfighter/api.rb', line 7

def initialize(key:, account:, symbol:, venue:)
  @api_key = key
  @account = 
  @symbol = symbol
  @venue = venue
end

Instance Method Details

#cancel_order(order_id) ⇒ Object



33
34
35
# File 'lib/stockfighter/api.rb', line 33

def cancel_order(order_id)
  HTTParty.delete("#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/orders/#{order_id}", headers: {"X-Starfighter-Authorization" => @api_key})
end

#get_quoteObject



14
15
16
# File 'lib/stockfighter/api.rb', line 14

def get_quote
  HTTParty.get("#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/quote", {"X-Starfighter-Authorization" => @api_key}).parsed_response
end

#order_bookObject



41
42
43
# File 'lib/stockfighter/api.rb', line 41

def order_book
  HTTParty.get("#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}", headers: {"X-Starfighter-Authorization" => @api_key}).parsed_response
end

#order_status(order_id) ⇒ Object



37
38
39
# File 'lib/stockfighter/api.rb', line 37

def order_status(order_id)
  HTTParty.get("#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/orders/#{order_id}", :headers => {"X-Starfighter-Authorization" => @api_key}).parsed_response
end

#place_order(price:, quantity:, direction:, order_type:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stockfighter/api.rb', line 18

def place_order(price:, quantity:, direction:, order_type:)
  order = {
    "account" => @account,
    "venue" => @venue,
    "symbol" => @symbol,
    "price" => price,
    "qty" => quantity,
    "direction" => direction,
    "orderType" => order_type
  }

  HTTParty.post("#{BASE_URL}/venues/#{@venue}/stocks/#{@symbol}/orders", body: JSON.dump(order),
  headers: {"X-Starfighter-Authorization" => @api_key}).parsed_response
end

#status_allObject



50
51
52
# File 'lib/stockfighter/api.rb', line 50

def status_all
  HTTParty.get("#{BASE_URL}/venues/#{@venue}/accounts/#{@account}/orders", headers: {"X-Starfighter-Authorization" => @api_key})
end

#venue_up?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/stockfighter/api.rb', line 45

def venue_up?
  response = HTTParty.get("#{BASE_URL}/venues/#{@venue}/heartbeat", headers: {"X-Starfighter-Authorization" => @api_key}).parsed_response
  response["ok"]
end