stockfighter
A Python3 library for the Stockfighter Trades.Exec() API.
Usage
Installation
pip install stockfighter-io
Configuration
Create your configuration using the config.py.example in the root of this project as an example, or copy it to config.py
, and proceed with the following:
Set your
apikey
andgm_apikey
to the correct values associated with your account. Thegm_apikey
can be found in the cookies while you're logged in on the site.Set your
levelInstance
variables. You can find them by browsing here.from config import *
This will initialize aCONF
variable that you can pass around with class initializers. This will also come with aDEFCONF
variable that will allow you to use the TESTEX exchange, allowing you to go back and forth using different instantiated objects.
Execution
Start a game
from stockfighter.api.game import Game
from config import *
g = Game(CONF, 'sell_side')
g.start_level()
Interact with an exchange
from stockfighter.exchange import Market
m = Market(CONF)
q = m.quote()
Output will be:
{
'ok': True,
'venue': 'TESTEX',
'symbol': 'FOOBAR',
'ask': 65535,
'askDepth': 15087,
'askSize': 15087,
'bid': 50,
'bidDepth': 3152,
'bidSize': 2490,
'last': 50,
'lastSize': 100,
'lastTrade': '2015-12-16T23:07:05.24319087Z',
'quoteTime': '2015-12-16T23:10:48.09910821Z'
}
Market API
Market.api_is_up()
Returns True
or False
.
Market.venue_is_up()
Returns True
or False
.
Market.stocks()
Returns a list of stocks from the exchange.
Market.orderbook()
Returns the order book.
Market.quote()
Returns a quote for the given stock.
Market.ask(cost, quantity, direction="sell", order_type='limit')
Pass in:
- cost: price you're asking for
- quantity: how many you want to sell
-
direction (optional): "buy" or "sell" (allows for generic call to
order
) - order_type (optional): may be 'limit', 'market', 'fill-or-kill', or 'immediate-or-cancel'. Default: 'limit'
Returns the order object.
Market.bid(cost, quantity, order_type='limit')
- cost: price you're asking for
- quantity: how many you want to buy
- order_type (optional): may be 'limit', 'market', 'fill-or-kill', or 'immediate-or-cancel'. Default: 'limit'
Returns the order object.
Market.status(order_id)
Pass in:
- order_id: The id passed back from in the order object.
Returns an updated copy of the order object.
Market.cancel(order_id)
Pass in:
- order_id: The id passed back from in the order object.
Returns an updated (and final) copy of the order object.
Market.quotes(callback, on_error)
Creates a Quotes websocket.
All parameters are optional. They are as follows:
-
callback: This function is called when a message is received, passing a parsed object. Defaults to
pdump
fromstockfighter/utils
to print JSON. - on_error: This function is called upon exception. Nothing gets passed.
Example:
def process(quote):
# Zero-out the missing fields
for k in [
'bid','bidSize','bidDepth',
'ask','askSize','askDepth',
'last','lastSize'
]:
quote[k] = quote[k] if k in quote else 0
# Display
print(("{bid:06d}:{bidSize:06d}:{bidDepth:06d}\t"+\
"{ask:06d}:{askSize:06d}:{askDepth:06d}\t"+\
"{last:06d}:{lastSize:06d}").format(**quote))
def err():
print("Shutting down...")
s = Market(CONF)
s.quotes(callback=process, on_error=err)
Market.executions(callback, on_error)
Creates an Executions websocket.
All parameters are optional. They are as follows:
-
callback: This function is called when a message is received, passing a parsed object. Defaults to
pdump
fromstockfighter/utils
to print JSON. - on_error: This function is called upon exception. Nothing gets passed.
Market.websocket(callback=print, on_error=none, objclass=Quotes)
Creates a generic websocket using objclass
class. Built-in classes available include the following from stockfighter.api.websocket
, and correlate directly with the stockfighter.io websocket paths:
Executions
StockExecutions
Quotes
StockQuotes
All parameters are optional. They are as follows:
-
callback: This function is called when a message is received, passing a parsed object. Defaults to
pdump
fromstockfighter/utils
to print JSON. - on_error: This function is called upon exception. Nothing gets passed.
- objclass: This is one of the classes listed above, or a similar implement.
Contributing
Please issue a pull request.
Support
Please submit questions as an issue to get a prompt response.