import market_trade.constants import tinkoff_grpc import json import time import grpc if __name__ == '__main__': # loading shares liquidity stats gathered with open(market_trade.constants.SHARES_STATS_PATH, encoding="utf-8", mode="r") as shares_stats_file: sorted_shares_liquidity_stats = json.load(shares_stats_file) # getting sorted list by liquidity and extracting first n, where n is subscription limit sorted_shares_figis = list(sorted_shares_liquidity_stats.keys()) most_liquid_figis_limited = sorted_shares_figis[:market_trade.constants.MARKETDATA_CONNECTION_LIMIT_SUBSCRIPTIONS] # instantiating a channel api_address = market_trade.constants.TINKOFF_API_ADDRESS token = market_trade.constants.TINKOFF_BEARER_TOKEN authorization_field = market_trade.constants.TINKOFF_AUTHORIZATION_HEADER with tinkoff_grpc.Channel(api_address=api_address, token=token, authorization_field=authorization_field) as tinkoff_channel: # getting all share objects list instrument_service = tinkoff_grpc.InstrumentsService(tinkoff_channel) shares = instrument_service.get_shares(market_trade.constants.DEFAULT_INSTRUMENT_STATUS) # getting final object of shares shares_to_gather = [share for share in shares if share.figi in most_liquid_figis_limited] # gathering shares data while True: time.sleep(1) try: tinkoff_trades_saver = tinkoff_grpc.savers.TradesSaver(channel=tinkoff_channel, instruments=shares_to_gather, filepath=market_trade.constants.SHARES_TRADES_PATH) tinkoff_trades_saver.start() except grpc.RpcError as grpc_error: pass