From 12657d5598b8a2b3e380d021eee3582f10db9f69 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 19 Sep 2022 14:12:14 +0300 Subject: [PATCH] moved scripts --- tools/check_shares.py | 15 ++++++ tools/collect_part_of_shares_trades.py | 68 ++++++++++++++++++++++++++ tools/save_trades_data.py | 40 +++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 tools/check_shares.py create mode 100644 tools/collect_part_of_shares_trades.py create mode 100644 tools/save_trades_data.py diff --git a/tools/check_shares.py b/tools/check_shares.py new file mode 100644 index 0000000..b34377b --- /dev/null +++ b/tools/check_shares.py @@ -0,0 +1,15 @@ +import tinkoff_grpc +import market_trade.constants + +if __name__ == '__main__': + 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: + instrument_service = tinkoff_grpc.InstrumentsService(tinkoff_channel) + shares = instrument_service.get_shares(market_trade.constants.DEFAULT_INSTRUMENT_STATUS) + print(len(shares)) + for share in shares: + print(share.is_api_tradeable) diff --git a/tools/collect_part_of_shares_trades.py b/tools/collect_part_of_shares_trades.py new file mode 100644 index 0000000..880de37 --- /dev/null +++ b/tools/collect_part_of_shares_trades.py @@ -0,0 +1,68 @@ +""" +This script is controlled by constant SHARE_PART_NUM and designed to collect shares object, to decide and check which +ones are more liquid +""" + +import tinkoff_grpc +import time +import grpc +import argparse +import market_trade.constants + + +# initialized argument parser +arguments_parser = argparse.ArgumentParser(description="A simple shares data collector") + +# added part number controller +arguments_parser.add_argument("-p", "--part-number", type=int, action="store", + dest=market_trade.constants.PART_NUMBER_FIELD, + default=market_trade.constants.DEFAULT_PART_NUMBER) + + +def get_share_part(part_number, channel): + """ + This function returns list of shares based on limit and controlling constant `SHARE_PART_NUM` + :param part_number: int, a number indicating which shares part should we collect now + :param channel: tinkoff_grpc.Channel object, which helps to initialize the service + :return: shares, list of Share objects + """ + + # initializing the service + instrument_service = tinkoff_grpc.InstrumentsService(channel) + + # getting list of all shares + shares = instrument_service.get_shares(market_trade.constants.DEFAULT_INSTRUMENT_STATUS) + + # getting part indices, based on the constant PART NUM + start_share_idx = part_number * market_trade.constants.MARKETDATA_CONNECTION_LIMIT_SUBSCRIPTIONS + end_share_idx = (part_number+1) * market_trade.constants.MARKETDATA_CONNECTION_LIMIT_SUBSCRIPTIONS + + # if the length of shares is smaller than end index, make it just the last share element + if end_share_idx > len(shares): + end_share_idx = -1 + + # getting our shares + shares_to_check = shares[start_share_idx: end_share_idx] + return shares_to_check + + +if __name__ == '__main__': + # getting our arguments parsed and converting them to dict + cli_args = vars(arguments_parser.parse_args()) + + 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 target share part + shares_part = get_share_part(cli_args[market_trade.constants.PART_NUMBER_FIELD], channel=tinkoff_channel) + while True: + time.sleep(1) + try: + tinkoff_trades_saver = tinkoff_grpc.savers.TradesSaver(channel=tinkoff_channel, instruments=shares_part, + filepath=market_trade.constants.SHARES_TRADES_PATH) + tinkoff_trades_saver.start() + except grpc.RpcError as grpc_error: + pass diff --git a/tools/save_trades_data.py b/tools/save_trades_data.py new file mode 100644 index 0000000..6610ab0 --- /dev/null +++ b/tools/save_trades_data.py @@ -0,0 +1,40 @@ +import time +import grpc +import market_trade.constants +import tinkoff_grpc.src.instruments +import tinkoff_grpc.src.marketdata +import tinkoff_grpc.src.channel + + +def get_all_currencies(channel: tinkoff_grpc.src.channel.Channel): + """ + This function takes channel and gets all currencies objects in channel + :param channel: channel.Channel object + :return: array of Currency object + """ + # initializing service + instruments_service = tinkoff_grpc.src.instruments.InstrumentsService(channel) + + # getting currencies + currencies = instruments_service.get_currencies( + instrument_status_name=market_trade.constants.DEFAULT_INSTRUMENT_STATUS + ) + return currencies + + + + +if __name__ == '__main__': + 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.src.channel.Channel(api_address=api_address, token=token, authorization_field=authorization_field) as tinkoff_channel: + currencies = get_all_currencies(tinkoff_channel) + while True: + time.sleep(1) + try: + tinkoff_trades_saver = tinkoff_grpc.savers.TradesSaver(channel=tinkoff_channel, instruments=currencies, + filepath=market_trade.constants.CURRENCIES_TRADES_PATH) + tinkoff_trades_saver.start() + except grpc.RpcError as grpc_error: + pass