from pprint import pprint import asyncio import aiostream import aiohttp import kalshi from nadex import NadexSession from decimal import Decimal as D, ROUND_CEILING import os import sys from utils import * from piecewise import * from decimal import Decimal async def check(ops): cps = [] for o, q in ops: if (q>0 and not o.asks) or (q<0 and not o.bids): return False cps.append(o.asks[0][0] if q>0 else o.bids[0][0]) cc = min_sum_pc(cps) global best if cc>best: updlog(D(cc)/D(100)) u = [o.asks if q>0 else o.bids for o, q in ops] for i, (o, q) in enumerate(ops): updlog([o.low, o.high, u[i][0], o.exchange, o.market_id], q) updlog() best = cc if cc>15000: await asyncio.gather(*[ o.exchange.execute_order(o.market_id, u[i][0][-1], 1, q>0) for i, (o, q) in enumerate(ops) ]) return True return False async def main(): async with aiohttp.ClientSession() as kcs, aiohttp.ClientSession() as ncs: ks = kalshi.KalshiSession(kcs) ns = NadexSession(ncs) await asyncio.gather(ks.login(), ns.login()) global best best = -10000000 upd = 0 async with aiostream.stream.ziplatest( ks.orderbook_stream("INXD-23MAR15", kalshi.convert_sp_rulebook, "ESTM23.CME"), ns.orderbook_stream(160809 if DEMO else 157993), partial=False ).stream() as streamer: async for kbook, nbook in streamer: upd +=1 if upd%100==0: updlog(upd) for k1 in kbook: for n1 in nbook: for ord in [(-1, 1), (1, -1), (1, 1), (-1, -1)]: if await check([(k1, ord[0]), (n1, ord[1])]): return for n2 in nbook: for ord in [(-1, 1, -1), (-1, -1, 1)]: if await check([(k1, ord[0]), (n1, ord[1]), (n2, ord[2])]): return if __name__=="__main__": asyncio.run(main())