def get_accounts(level_venue, level_account, level_stock, start, end): fighter = Stockfighter(level_venue, level_account) global balances, j time.sleep(5) uncovered = [] for i in range(start, end): a = fighter.cancel(level_stock, i)['error'][-12:-1].replace(" ", "") if a not in j: if a[0].isupper(): j.append(a) balances[a] = {} balances[a]['cash'] = 0 balances[a]['shares'] = 0 balances[a]['trades'] = 0 balances[a]['nav'] = 0 execution_ws(a) else: account = a[1:10] j.append(account) balances[account] = {} balances[account]['cash'] = 0 balances[account]['shares'] = 0 balances[account]['trades'] = 0 balances[account]['nav'] = 0 execution_ws(account) else: continue return uncovered
row = map(str, [account, direction, id_curr, ts, price, q]) f.write(','.join(row) + '\n') f.flush() except Exception, e: print 'Could not parse execution data', str(e) #Insert your info here venue = '' account = '' stock = '' fighter = Stockfighter(venue, account) open_sockets = set() p = re.compile('[A-Z]{2,}\d+') i = 1 fname = 'data/amends.csv' f = open(fname, 'wb') f.write('account,direction,id,ts,price,qty\n') f.flush() while True: try: fighter.cancel(stock, i) except Exception, e: msg = str(e) matches = p.findall(msg) if len(matches) > 0: acc_tmp = matches[0] if acc_tmp not in open_sockets: open_sockets.add(acc_tmp) print 'Found new account: ', acc_tmp, ', total accounts: ', len(open_sockets) Stockfighter(venue, acc_tmp).execution_stock_ticker(execution_ticker, stock) i += 1
print 'Testing quote...\n', quote = fighter.quote(stock) print '\t %s...\n' % str(quote)[:40], except Stockfighter.Exception as e: print e.status_code, e.error try: print 'Testing order status...\n', status = fighter.order_status(stock, _id) print '\t %s...\n' % str(status)[:40] except Stockfighter.Exception as e: print e.status_code, e.error try: print 'Testing order cancel...\n', order = fighter.cancel(stock, _id) print '\t %s...\n' % str(order)[:40], except Stockfighter.Exception as e: print e.status_code, e.error try: print 'Testing list all orders...\n', orders = fighter.my_orders() print '\t %s...\n' % str(orders)[:40] except Stockfighter.Exception as e: print e.status_code, e.error try: import time print 'Waiting for keyboard interrupt' while True:
from stockfighter import Stockfighter fighter = Stockfighter('TESTEX', 'EXB123456')stock = 'FOOBAR' if fighter.heartbeat(): print 'API is up!' try: print 'Testing venue...', fighter.check_venue() print '%s venue is up!' % fighter.venueexcept Stockfighter.Exception as e: print e.status_code, e.error try: print 'Testing stock listing...', symbols = fighter.list_stocks() print 'Stocks available at %s:' % fighter.venue,symbolsexcept Stockfighter.Exception as e: print e.status_code, e.error try: print 'Testing orderbook...', orderbook = fighter.orderbook(stock) print '\n\t Orderbook bids:',orderbook['bids'],'\n\t Orderbook asks:',orderbook['asks']except Stockfighter.Exception as e: print e.status_code, e.error try: print 'Testing websocket tickers' def quote_ticker(quote): if quote is None: fighter.quote_stock_ticker(quote_ticker, stock) # This will auto-restart the ticker if it dies return print '\t --- Quote from ticker: %s... ---\n' % str(quote)[:40], def execution_ticker(execution): if execution is None: fighter.execution_stock_ticker(execution_ticker, stock) # This will auto-restart the ticker if it dies return print '\t --- Execution from ticker: %s... ---\n' % str(execution)[:40], fighter.quote_stock_ticker(quote_ticker, stock) fighter.execution_stock_ticker(execution_ticker, stock) except Stockfighter.Exception as e: print e.status_code, e.error try: print 'Placing ask order...\n', order = fighter.order(stock, 5100, 100, 'sell', 'limit') print '\t %s...\n' % str(order)[:40], print 'Placing bid order...\n', order = fighter.order(stock, 5100, 150, 'buy', 'limit') print '\t %s...\n' % str(order)[:40], _id = order['id']except Stockfighter.Exception as e: print e.status_code, e.error try: print 'Testing quote...\n', quote = fighter.quote(stock) print '\t %s...\n' % str(quote)[:40],except Stockfighter.Exception as e: print e.status_code, e.error try: print 'Testing order status...\n', status = fighter.order_status(stock, _id) print '\t %s...\n' % str(status)[:40]except Stockfighter.Exception as e: print e.status_code, e.error try: print 'Testing order cancel...\n', order = fighter.cancel(stock, _id) print '\t %s...\n' % str(order)[:40],except Stockfighter.Exception as e: print e.status_code, e.error try: print 'Testing list all orders...\n', orders = fighter.my_orders() print '\t %s...\n' % str(orders)[:40]except Stockfighter.Exception as e: print e.status_code, e.error try: import time print 'Waiting for keyboard interrupt' while True: time.sleep(1)except KeyboardInterrupt: pass