def __init__(self, contract_id): self.state = ArbBot.STATE_WAIT_FOR_ARB self.state_lock = asyncio.Lock() self.p = pi.PredictItAPI(0).create(*load_auth()) self.contract_id = contract_id # our current market position, price and quantity self.position = (0, 0) self.strike_price = 0 # tracks the shares we own, and the price we paid for them self.portfolio = (0, 0) self.trade_id = 0
async def start(self): self.p = pi.PredictItAPI(0).create(*load_auth()) self.ws_token = self.p.negotiate_ws()['ConnectionToken'] self.start_time = self._send_start_request() self.queue = asyncio.Queue() await self.connect() self.feeds = asyncio.gather(self.run_trade_feed(), self.run_status_feed(), self._run_queue(), self.ping()) try: await self.feeds except asyncio.CancelledError: self.logger.info('Cancelled all feeds')
max_profit = profit - min(best_bids) print(f'min p/l per share: {min_profit:.2f}') print(f'max p/l per share: {max_profit:.2f}') #print(f'100 shares investment: {100 * sum(best_bids)}') #print(f'100 shares in each position is a profit range of [{100*min_profit:.2f}, {100*max_profit:.2f}]') ''' token = '' with open('auth.txt', 'r') as f: username, password = f.read().split() token = pi.PredictItAPI.get_auth_token(username, password) for market in get_all_markets()['markets']: print(market['id'], market['shortName']) p = pi.PredictItAPI(str(market['id']), token) try: best_bids = get_best_bids(p, yes=False) except (IndexError, KeyError): time.sleep(5) continue profit = sum(list(map(lambda b: (1-b) * (1 - fee), best_bids))) loss = max(best_bids) min_profit = profit - max(best_bids) max_profit = profit - min(best_bids) print(f'min p/l per share: {min_profit:.2f}') print(f'max p/l per share: {max_profit:.2f}') print('')
# To do this, we need the previous orderbook, and current orderbook ''' if type(event) == piws.ContractStatsEvent: print(f'{event.contract_id}, {event.timestamp}, {event.last_trade_price}') fifo.append(event) elif type(event) == piws.OrderbookEvent: if fifo != []: stats = fifo.pop(0) if event.contract_id in orderbook_delta: orderbook_delta[event.contract_id].update(Orderbook(event.bids, event.asks)) print(f'Change: {find_order_quantity(orderbook_delta[event.contract_id])}') else: orderbook_delta[event.contract_id] = OrderbookDelta(Orderbook(event.bids, event.asks)) ''' if type(event) == piws.OrderbookEvent: print(f'{event.contract_id}: Orderbook event') # Implement market status event here if __name__ == '__main__': p = pi.PredictItAPI('5804') p.authenticate('auth.txt') contracts = p.get_market_contract_ids() print(contracts) #for c in contracts: # orderbook_delta[c] = {} ws = piws.PredictItWebSocket() ws.contracts = contracts ws.set_queue_callback(callback) ws.set_contract_stats_filter(lambda contract: contract not in contracts) asyncio.run(ws.start())
import predictit as pi # Double check that tweetstream still works the same.. ''' I think the only necessary component is: - A tweet checker that dispatches trades when a tweet occurs ''' def load_pi_auth(filename): with open(filename, 'r') as f: return f.read().split() username, password = load_pi_auth('../etc/auth.txt') p = pi.PredictItAPI('5824').create(username, password) #short_contract = '17125' short_contract = '17124' long_contract = '' # just start with long contract -- need to double check No works # Should account for any gaps in the orderbook in the future. # TODO -- Specify whether yes or no order! def callback(tweet, count): #return global p global short_contract ob = p.get_contract_orderbook(short_contract) print(f'Fetching orderbook for contract: {short_contract}') yes_orders = list(
ws = piws.PredictItWebSocket() ws.contracts = contracts #ws.set_market_filter(lambda market_id: str(market_id) not in markets) ws.set_market_filter(lambda market_id: str(market_id) != '5782') #ws.set_contract_stats_filter(lambda contract_id: contract_id not in contracts) ws.set_contract_stats_filter(lambda contract_id: contract_id != '16894') ws.set_queue_callback(callback) #for c in contracts: # ws.subscribe_contract_orderbook(c) ws.subscribe_contract_orderbook('16894') ws.subscribe_contract_status() ws.subscribe_market_status() ''' MARKET = '5812' p = pi.PredictItAPI(MARKET) contracts = p.get_market_contract_ids() ws = piws.PredictItWebSocket() ws.set_market_filter(lambda market_id: str(market_id) != MARKET) ws.set_contract_stats_filter(lambda contract_id: contract_id not in contracts) for contract in contracts: ws.subscribe_contract_orderbook(contract) #ws.set_queue_callback(callback2) ws.set_queue_callback(callback2) ws.subscribe_contract_status() ws.subscribe_market_status() try: asyncio.run(ws.start()) except KeyboardInterrupt: