From ebac725951a999ac0b14826b1acf2f7052cc0e10 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 23 Sep 2022 12:01:03 +0300 Subject: [PATCH] created script save shares data it loads our shares stats, gets the most liquid ones and starts gathering them aggresively --- tools/save_shares_data.py | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tools/save_shares_data.py diff --git a/tools/save_shares_data.py b/tools/save_shares_data.py new file mode 100644 index 0000000..fcfc5b6 --- /dev/null +++ b/tools/save_shares_data.py @@ -0,0 +1,41 @@ +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 +