Make watchlist app retrieve quotes from the broker daemon
							parent
							
								
									73ef95f42a
								
							
						
					
					
						commit
						4898459bcd
					
				| 
						 | 
					@ -7,7 +7,6 @@ Launch with ``piker watch <watchlist name>``.
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
from itertools import chain
 | 
					from itertools import chain
 | 
				
			||||||
from types import ModuleType
 | 
					from types import ModuleType
 | 
				
			||||||
from functools import partial
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import trio
 | 
					import trio
 | 
				
			||||||
from kivy.uix.boxlayout import BoxLayout
 | 
					from kivy.uix.boxlayout import BoxLayout
 | 
				
			||||||
| 
						 | 
					@ -21,7 +20,6 @@ from kivy.core.window import Window
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from ..log import get_logger
 | 
					from ..log import get_logger
 | 
				
			||||||
from .pager import PagerView
 | 
					from .pager import PagerView
 | 
				
			||||||
from ..brokers.core import poll_tickers
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
log = get_logger('watchlist')
 | 
					log = get_logger('watchlist')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -49,7 +47,7 @@ _kv = (f'''
 | 
				
			||||||
#:kivy 1.10.0
 | 
					#:kivy 1.10.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<Cell>
 | 
					<Cell>
 | 
				
			||||||
    font_size: 18
 | 
					    font_size: 20
 | 
				
			||||||
    # text_size: self.size
 | 
					    # text_size: self.size
 | 
				
			||||||
    size: self.texture_size
 | 
					    size: self.texture_size
 | 
				
			||||||
    color: {colorcode('gray')}
 | 
					    color: {colorcode('gray')}
 | 
				
			||||||
| 
						 | 
					@ -386,77 +384,77 @@ async def _async_main(name, tickers, brokermod, rate):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    This is started with cli command `piker watch`.
 | 
					    This is started with cli command `piker watch`.
 | 
				
			||||||
    '''
 | 
					    '''
 | 
				
			||||||
    queue = trio.Queue(1000)
 | 
					    # setup ticker stream
 | 
				
			||||||
 | 
					    from ..brokers.core import StreamQueue
 | 
				
			||||||
 | 
					    queue = StreamQueue(await trio.open_tcp_stream('127.0.0.1', 1616))
 | 
				
			||||||
 | 
					    await queue.put(tickers)  # initial request for symbols price streams
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # get initial symbol data
 | 
				
			||||||
    async with brokermod.get_client() as client:
 | 
					    async with brokermod.get_client() as client:
 | 
				
			||||||
        async with trio.open_nursery() as nursery:
 | 
					        # get long term data including last days close price
 | 
				
			||||||
            # get long term data including last days close price
 | 
					        sd = await client.symbol_data(tickers)
 | 
				
			||||||
            sd = await client.symbol_data(tickers)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            nursery.start_soon(
 | 
					    async with trio.open_nursery() as nursery:
 | 
				
			||||||
                partial(poll_tickers, client, brokermod.quoter, tickers, queue,
 | 
					        # get first quotes response
 | 
				
			||||||
                        rate=rate)
 | 
					        quotes = await queue.get()
 | 
				
			||||||
            )
 | 
					        first_quotes = [
 | 
				
			||||||
 | 
					            brokermod.format_quote(quote, symbol_data=sd)[0]
 | 
				
			||||||
 | 
					            for quote in quotes.values()]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # get first quotes response
 | 
					        if first_quotes[0].get('last') is None:
 | 
				
			||||||
            quotes = await queue.get()
 | 
					            log.error("Broker API is down temporarily")
 | 
				
			||||||
            first_quotes = [
 | 
					            nursery.cancel_scope.cancel()
 | 
				
			||||||
                brokermod.format_quote(quote, symbol_data=sd)[0]
 | 
					            return
 | 
				
			||||||
                for quote in quotes.values()]
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if first_quotes[0].get('last') is None:
 | 
					        # build out UI
 | 
				
			||||||
                log.error("Broker API is down temporarily")
 | 
					        Window.set_title(f"watchlist: {name}\t(press ? for help)")
 | 
				
			||||||
                nursery.cancel_scope.cancel()
 | 
					        Builder.load_string(_kv)
 | 
				
			||||||
                return
 | 
					        box = BoxLayout(orientation='vertical', padding=5, spacing=5)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # build out UI
 | 
					        # define bid-ask "stacked" cells
 | 
				
			||||||
            Window.set_title(f"watchlist: {name}\t(press ? for help)")
 | 
					        # (TODO: needs some rethinking and renaming for sure)
 | 
				
			||||||
            Builder.load_string(_kv)
 | 
					        bidasks = brokermod._bidasks
 | 
				
			||||||
            box = BoxLayout(orientation='vertical', padding=5, spacing=5)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # define bid-ask "stacked" cells
 | 
					        # add header row
 | 
				
			||||||
            # (TODO: needs some rethinking and renaming for sure)
 | 
					        headers = first_quotes[0].keys()
 | 
				
			||||||
            bidasks = brokermod._bidasks
 | 
					        header = Row(
 | 
				
			||||||
 | 
					            {key: key for key in headers},
 | 
				
			||||||
 | 
					            headers=headers,
 | 
				
			||||||
 | 
					            bidasks=bidasks,
 | 
				
			||||||
 | 
					            is_header_row=True,
 | 
				
			||||||
 | 
					            size_hint=(1, None),
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        box.add_widget(header)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # add header row
 | 
					        # build grid
 | 
				
			||||||
            headers = first_quotes[0].keys()
 | 
					        grid = TickerTable(
 | 
				
			||||||
            header = Row(
 | 
					            cols=1,
 | 
				
			||||||
                {key: key for key in headers},
 | 
					            size_hint=(1, None),
 | 
				
			||||||
                headers=headers,
 | 
					        )
 | 
				
			||||||
                bidasks=bidasks,
 | 
					        for ticker_record in first_quotes:
 | 
				
			||||||
                is_header_row=True,
 | 
					            grid.append_row(ticker_record, bidasks=bidasks)
 | 
				
			||||||
                size_hint=(1, None),
 | 
					        # associate the col headers row with the ticker table even though
 | 
				
			||||||
            )
 | 
					        # they're technically wrapped separately in containing BoxLayout
 | 
				
			||||||
            box.add_widget(header)
 | 
					        header.table = grid
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # build grid
 | 
					        # mark the initial sorted column header as bold and underlined
 | 
				
			||||||
            grid = TickerTable(
 | 
					        sort_cell = header.get_cell(grid.sort_key)
 | 
				
			||||||
                cols=1,
 | 
					        sort_cell.bold = sort_cell.underline = True
 | 
				
			||||||
                size_hint=(1, None),
 | 
					        grid.last_clicked_col_cell = sort_cell
 | 
				
			||||||
            )
 | 
					 | 
				
			||||||
            for ticker_record in first_quotes:
 | 
					 | 
				
			||||||
                grid.append_row(ticker_record, bidasks=bidasks)
 | 
					 | 
				
			||||||
            # associate the col headers row with the ticker table even though
 | 
					 | 
				
			||||||
            # they're technically wrapped separately in containing BoxLayout
 | 
					 | 
				
			||||||
            header.table = grid
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # mark the initial sorted column header as bold and underlined
 | 
					        # set up a pager view for large ticker lists
 | 
				
			||||||
            sort_cell = header.get_cell(grid.sort_key)
 | 
					        grid.bind(minimum_height=grid.setter('height'))
 | 
				
			||||||
            sort_cell.bold = sort_cell.underline = True
 | 
					        pager = PagerView(box, grid, nursery)
 | 
				
			||||||
            grid.last_clicked_col_cell = sort_cell
 | 
					        box.add_widget(pager)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # set up a pager view for large ticker lists
 | 
					        widgets = {
 | 
				
			||||||
            grid.bind(minimum_height=grid.setter('height'))
 | 
					            # 'anchor': anchor,
 | 
				
			||||||
            pager = PagerView(box, grid, nursery)
 | 
					            'root': box,
 | 
				
			||||||
            box.add_widget(pager)
 | 
					            'grid': grid,
 | 
				
			||||||
 | 
					            'box': box,
 | 
				
			||||||
            widgets = {
 | 
					            'header': header,
 | 
				
			||||||
                # 'anchor': anchor,
 | 
					            'pager': pager,
 | 
				
			||||||
                'root': box,
 | 
					        }
 | 
				
			||||||
                'grid': grid,
 | 
					        nursery.start_soon(run_kivy, widgets['root'], nursery)
 | 
				
			||||||
                'box': box,
 | 
					        nursery.start_soon(
 | 
				
			||||||
                'header': header,
 | 
					            update_quotes, brokermod, widgets, queue, sd, quotes)
 | 
				
			||||||
                'pager': pager,
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            nursery.start_soon(run_kivy, widgets['root'], nursery)
 | 
					 | 
				
			||||||
            nursery.start_soon(
 | 
					 | 
				
			||||||
                update_quotes, brokermod, widgets, queue, sd, quotes)
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue