Class: ChockABlock::Broker
- Inherits:
-
Object
- Object
- ChockABlock::Broker
- Includes:
- Errors
- Defined in:
- lib/chock_a_block/broker.rb
Instance Method Summary collapse
-
#cancel_order(id) ⇒ Hash
Cancel specific order details API Consumer.
-
#get_order(id) ⇒ Hash
Get specific order details API Consumer.
-
#get_orders ⇒ Hash
Get all the orders of a specific stock API Consumer.
-
#initialize(api_key, account, venue, symbol) ⇒ Broker
constructor
Initialize a Broker to interact with API placing orders and getting order stats…
-
#place_order(price, qty) ⇒ Hash
Place an order API Consumer.
Constructor Details
#initialize(api_key, account, venue, symbol) ⇒ Broker
Initialize a Broker to interact with API placing orders and getting order stats… etc.
12 13 14 15 16 17 |
# File 'lib/chock_a_block/broker.rb', line 12 def initialize(api_key, account, venue, symbol) @api_key = api_key @account = account @venue = venue @symbol = symbol end |
Instance Method Details
#cancel_order(id) ⇒ Hash
Cancel specific order details API Consumer
77 78 79 80 81 82 83 |
# File 'lib/chock_a_block/broker.rb', line 77 def cancel_order(id) response = HTTP['X-Starfighter-Authorization' => @api_key].delete( BASE_URL + "/venues/#{@venue}/stocks/#{@symbol}/orders/#{id}" ) fail AuthenticationError, @api_key if response.code == 401 JSON.parse response.body end |
#get_order(id) ⇒ Hash
Get specific order details API Consumer
63 64 65 66 67 68 69 |
# File 'lib/chock_a_block/broker.rb', line 63 def get_order(id) response = HTTP['X-Starfighter-Authorization' => @api_key].get( BASE_URL + "/venues/#{@venue}/stocks/#{@symbol}/orders/#{id}" ) fail AuthenticationError, @api_key if response.code == 401 JSON.parse response.body end |
#get_orders ⇒ Hash
Get all the orders of a specific stock API Consumer
49 50 51 52 53 54 55 |
# File 'lib/chock_a_block/broker.rb', line 49 def get_orders response = HTTP['X-Starfighter-Authorization' => @api_key].get( BASE_URL + "/venues/#{@venue}/accounts/#{@account}/stocks/#{@symbol}/orders" ) fail AuthenticationError, @api_key if response.code == 401 JSON.parse(response.body)['orders'] end |
#place_order(price, qty) ⇒ Hash
Place an order API Consumer
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/chock_a_block/broker.rb', line 26 def place_order(price, qty) response = HTTP['X-Starfighter-Authorization' => @api_key].post( BASE_URL + "/venues/#{@venue}/stocks/#{@symbol}/orders", json: { account: @account, venue: @venue, stock: @symbol, price: price, qty: qty, direction: :buy, orderType: :limit } ) response_body = JSON.parse response.body fail AuthenticationError, @api_key if response.code == 401 fail OrderError, response_body['error'] unless response_body['ok'] response_body end |