Path: blob/master/ invest-robot-contest_sirius-master/services/history_test_data.py
5933 views
import logging1import time2from datetime import timedelta34from api_calls.get_info import get_candles_by_figi5from services.instruments_info_cache import get_instrument_info6from utils.datetime_utils import date_minus_days, current_date, string_to_date7from utils.settings import settings8from utils.util import read_dict_from_file, write_dict_to_file, pretty_dict910# методы используются в режиме history_test1112# тестовые даты старта и остановки. Определяются как самая ранняя/поздняя дата свечей при инициализации13# данных в init_history_test_data()14test_current_date = None15test_stop_date = None16poll_interval_minutes = int(settings()['TRADE']['poll_interval_minutes'])171819def init_history_test_data():20global test_current_date21global test_stop_date22res = read_dict_from_file('data/test_history')23if res is None:24return res2526for info in res:27start_date = string_to_date(info['candles'][0]['time'])28stop_date = string_to_date(info['candles'][-1]['time'])29if test_current_date is None or start_date < test_current_date:30test_current_date = start_date31if test_stop_date is None or stop_date > test_stop_date:32test_stop_date = stop_date3334return res353637test_info = init_history_test_data()383940def get_test_current_date():41return test_current_date424344# сдвинуть тестовую дату на poll_interval45def get_next_test_current_date():46global test_current_date47test_current_date = test_current_date + timedelta(minutes=poll_interval_minutes)48return test_current_date495051def get_test_data(exchange, ticker):52for instrument in test_info:53if instrument['exchange'] == exchange and instrument['ticker'] == ticker:54return instrument55return None565758# TODO speed up search59def test_get_candles_by_date(exchange, ticker, from_date_time_timestamp, to_date_time_timestamp):60ticker_test_info = get_test_data(exchange, ticker)61if ticker_test_info is None:62return []6364ticker_candles = ticker_test_info['candles']65res = []66for candle in ticker_candles:67candle_date = candle['datetime']68if candle_date < from_date_time_timestamp:69continue70elif candle_date > to_date_time_timestamp:71break72res.append(candle)73return res747576# условие остановки при тестовом режиме77def test_get_stop():78global test_stop_date79return get_test_current_date() > test_stop_date808182def prepare_history_file(days):83instruments = read_dict_from_file('instruments_history_test')8485logging.info("Preparing history test for {} days for following instruments:\n{}"86.format(days, pretty_dict(instruments)))8788for instrument in instruments:89instrument_info = get_instrument_info(instrument['exchange'], instrument['ticker'])90figi = instrument_info['figi']91candles = []9293for i in range(0, days):94candles.extend(get_candles_by_figi(figi, date_minus_days(current_date(), days - i),95date_minus_days(current_date(), days - i - 1),96'CANDLE_INTERVAL_1_MIN')['candles'])97for candle in candles:98candle['datetime'] = string_to_date(candle['time']).timestamp()99instrument['candles'] = candles100logging.info("Candles for one day for instrument = {} {} has been prepared"101.format(instrument['exchange'], instrument['ticker']))102103time.sleep(2) # to avoid rate limit break104105write_dict_to_file('data/test_history', instruments)106107logging.info("History file successfully prepared")108109110