Include symbol deats in feed init message from ib
Async spawn a deats getter task whenever we load a symbol data feed. Pass these symbol details in the first message delivered by the feed at open. Move stream loop into a new func.basic_orders
							parent
							
								
									5327d7be5e
								
							
						
					
					
						commit
						10e47e349c
					
				| 
						 | 
				
			
			@ -426,12 +426,15 @@ class Client:
 | 
			
		|||
 | 
			
		||||
        """
 | 
			
		||||
        contract = await self.find_contract(symbol)
 | 
			
		||||
 | 
			
		||||
        details_fute = self.ib.reqContractDetailsAsync(contract)
 | 
			
		||||
        ticker: Ticker = self.ib.reqMktData(
 | 
			
		||||
            contract,
 | 
			
		||||
            snapshot=True,
 | 
			
		||||
        )
 | 
			
		||||
        ticker = await ticker.updateEvent
 | 
			
		||||
        return contract, ticker
 | 
			
		||||
        details = (await details_fute)[0]
 | 
			
		||||
        return contract, ticker, details
 | 
			
		||||
 | 
			
		||||
    # async to be consistent for the client proxy, and cuz why not.
 | 
			
		||||
    async def submit_limit(
 | 
			
		||||
| 
						 | 
				
			
			@ -440,7 +443,7 @@ class Client:
 | 
			
		|||
        symbol: str,
 | 
			
		||||
        price: float,
 | 
			
		||||
        action: str,
 | 
			
		||||
        size: int = 100,
 | 
			
		||||
        size: int,
 | 
			
		||||
    ) -> int:
 | 
			
		||||
        """Place an order and return integer request id provided by client.
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -870,6 +873,7 @@ async def stream_quotes(
 | 
			
		|||
    symbols: List[str],
 | 
			
		||||
    shm_token: Tuple[str, str, List[tuple]],
 | 
			
		||||
    loglevel: str = None,
 | 
			
		||||
 | 
			
		||||
    # compat for @tractor.msg.pub
 | 
			
		||||
    topics: Any = None,
 | 
			
		||||
    get_topics: Callable = None,
 | 
			
		||||
| 
						 | 
				
			
			@ -885,7 +889,8 @@ async def stream_quotes(
 | 
			
		|||
    # TODO: support multiple subscriptions
 | 
			
		||||
    sym = symbols[0]
 | 
			
		||||
 | 
			
		||||
    contract, first_ticker = await _trio_run_client_method(
 | 
			
		||||
    async with trio.open_nursery() as n:
 | 
			
		||||
        contract, first_ticker, details = await _trio_run_client_method(
 | 
			
		||||
            method='get_quote',
 | 
			
		||||
            symbol=sym,
 | 
			
		||||
        )
 | 
			
		||||
| 
						 | 
				
			
			@ -896,8 +901,8 @@ async def stream_quotes(
 | 
			
		|||
        symbol=sym,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    async with aclosing(stream):
 | 
			
		||||
 | 
			
		||||
    shm = None
 | 
			
		||||
    async with trio.open_nursery() as ln:
 | 
			
		||||
        # check if a writer already is alive in a streaming task,
 | 
			
		||||
        # otherwise start one and mark it as now existing
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -908,7 +913,6 @@ async def stream_quotes(
 | 
			
		|||
        # maybe load historical ohlcv in to shared mem
 | 
			
		||||
        # check if shm has already been created by previous
 | 
			
		||||
        # feed initialization
 | 
			
		||||
        async with trio.open_nursery() as ln:
 | 
			
		||||
        if not writer_already_exists:
 | 
			
		||||
            _local_buffer_writers[key] = True
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -945,9 +949,23 @@ async def stream_quotes(
 | 
			
		|||
            delay_s = times[-1] - times[times != times[-1]][-1]
 | 
			
		||||
            subscribe_ohlc_for_increment(shm, delay_s)
 | 
			
		||||
 | 
			
		||||
        # pass back some symbol info like min_tick, trading_hours, etc.
 | 
			
		||||
        # con = asdict(contract)
 | 
			
		||||
        # syminfo = contract
 | 
			
		||||
        symdeats = asdict(details)
 | 
			
		||||
        symdeats.update(symdeats['contract'])
 | 
			
		||||
 | 
			
		||||
        # TODO: for loop through all symbols passed in
 | 
			
		||||
        init_msgs = {
 | 
			
		||||
            # pass back token, and bool, signalling if we're the writer
 | 
			
		||||
            # and that history has been written
 | 
			
		||||
            await ctx.send_yield((shm_token, not writer_already_exists))
 | 
			
		||||
            sym: {
 | 
			
		||||
                'is_shm_writer': not writer_already_exists,
 | 
			
		||||
                'shm_token': shm_token,
 | 
			
		||||
                'symbol_info': symdeats,
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        await ctx.send_yield(init_msgs)
 | 
			
		||||
 | 
			
		||||
        # check for special contract types
 | 
			
		||||
        if type(first_ticker.contract) not in (ibis.Commodity, ibis.Forex):
 | 
			
		||||
| 
						 | 
				
			
			@ -988,6 +1006,7 @@ async def stream_quotes(
 | 
			
		|||
        # with trio.move_on_after(10) as cs:
 | 
			
		||||
        # wait for real volume on feed (trading might be closed)
 | 
			
		||||
 | 
			
		||||
        async with aclosing(stream):
 | 
			
		||||
            async for ticker in stream:
 | 
			
		||||
 | 
			
		||||
                # for a real volume contract we rait for the first
 | 
			
		||||
| 
						 | 
				
			
			@ -1009,9 +1028,34 @@ async def stream_quotes(
 | 
			
		|||
                    # ``aclosing()`` above?
 | 
			
		||||
                    break
 | 
			
		||||
 | 
			
		||||
            # if cs.cancelled_caught:
 | 
			
		||||
            #     await tractor.breakpoint()
 | 
			
		||||
            # enter stream loop
 | 
			
		||||
            try:
 | 
			
		||||
                await stream_and_write(
 | 
			
		||||
                    stream=stream,
 | 
			
		||||
                    calc_price=calc_price,
 | 
			
		||||
                    topic=topic,
 | 
			
		||||
                    writer_already_exists=writer_already_exists,
 | 
			
		||||
                    shm=shm,
 | 
			
		||||
                    suffix=suffix,
 | 
			
		||||
                    ctx=ctx,
 | 
			
		||||
                )
 | 
			
		||||
            finally:
 | 
			
		||||
                if not writer_already_exists:
 | 
			
		||||
                    _local_buffer_writers[key] = False
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def stream_and_write(
 | 
			
		||||
    stream,
 | 
			
		||||
    calc_price: bool,
 | 
			
		||||
    topic: str,
 | 
			
		||||
    writer_already_exists: bool,
 | 
			
		||||
    suffix: str,
 | 
			
		||||
    ctx: tractor.Context,
 | 
			
		||||
    shm: Optional['SharedArray'],  # noqa
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Core quote streaming and shm writing loop; optimize for speed!
 | 
			
		||||
 | 
			
		||||
    """
 | 
			
		||||
    # real-time stream
 | 
			
		||||
    async for ticker in stream:
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1073,12 +1117,16 @@ async def stream_quotes(
 | 
			
		|||
        ticker.ticks = []
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@tractor.msg.pub
 | 
			
		||||
@tractor.msg.pub(
 | 
			
		||||
    send_on_connect={'local_trades': 'start'}
 | 
			
		||||
)
 | 
			
		||||
async def stream_trades(
 | 
			
		||||
    loglevel: str = None,
 | 
			
		||||
    get_topics: Callable = None,
 | 
			
		||||
) -> AsyncIterator[Dict[str, Any]]:
 | 
			
		||||
 | 
			
		||||
    global _trades_stream_is_live
 | 
			
		||||
 | 
			
		||||
    # XXX: required to propagate ``tractor`` loglevel to piker logging
 | 
			
		||||
    get_console_log(loglevel or tractor.current_actor().loglevel)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1086,9 +1134,6 @@ async def stream_trades(
 | 
			
		|||
        method='recv_trade_updates',
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    # startup msg
 | 
			
		||||
    yield {'local_trades': 'start'}
 | 
			
		||||
 | 
			
		||||
    async for event_name, item in stream:
 | 
			
		||||
 | 
			
		||||
        # XXX: begin normalization of nonsense ib_insync internal
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue