def tmp_set_parameters(chat_id, candle_interval): if "monitor_other" in candle_interval: return set_other_companies_monitoring(chat_id, candle_interval) possible_candles_intervals = load_yaml("parameters_map.yaml") candle_interval = "1min" if candle_interval is None else candle_interval if candle_interval not in possible_candles_intervals.keys(): return "Impossible candle_interval: {}. Please choose from: {}".format( candle_interval, possible_candles_intervals, ) tmp_common_parameters = possible_candles_intervals[candle_interval] file_path = "{}.yaml".format(chat_id) if os.path.isfile(file_path): file_data = load_yaml(file_path) is_monitor_other_companies = file_data["common_parameters"][ "is_monitor_other_companies"] file_data["common_parameters"] = tmp_common_parameters file_data["common_parameters"][ "is_monitor_other_companies"] = is_monitor_other_companies else: file_data = {"common_parameters": tmp_common_parameters} save_yaml(file_path, file_data) return "New interval '{}' is set up.".format(candle_interval)
def set_other_companies_monitoring(chat_id, candle_interval): answer = "Something wrong with other companies monitoring flag setting." if "no_monitor_other" in candle_interval: answer = "No monitor other companies is set up." is_monitor_other_companies = False elif "monitor_other" in candle_interval: answer = "Monitor other companies is set up." is_monitor_other_companies = True else: return answer possible_candles_intervals = load_yaml("parameters_map.yaml") tmp_common_parameters = possible_candles_intervals["1min"] tmp_common_parameters[ "is_monitor_other_companies"] = is_monitor_other_companies file_path = "{}.yaml".format(chat_id) if os.path.isfile(file_path): file_data = load_yaml(file_path) common_parameters = file_data.get("common_parameters", None) if common_parameters is not None: file_data["common_parameters"][ "is_monitor_other_companies"] = is_monitor_other_companies else: file_data["common_parameters"] = tmp_common_parameters else: file_data = {"common_parameters": tmp_common_parameters} save_yaml(file_path, file_data) return answer
def make_other_companies(chat_id): global TMP_OTHER_COMPANIES if TMP_OTHER_COMPANIES: return user_companies = load_yaml("{}.yaml".format(chat_id)) all_companies = load_yaml("ticker_to_figi_map.yaml") TMP_OTHER_COMPANIES = {name: s for name, s in all_companies.items() if name not in user_companies.keys()}
def delete_company_from_monitoring(chat_id, company_figi): monitoring_list = load_yaml("{}.yaml".format(chat_id)) is_company_monitoring = monitoring_list.pop(company_figi, False) if is_company_monitoring: save_yaml("{}.yaml".format(chat_id), monitoring_list) return True else: return False
def tmp_get_parameter(chat_id, parameter_name): file_path = "{}.yaml".format(chat_id) if os.path.isfile(file_path): file_data = load_yaml(file_path) if file_data is None: return None else: return file_data["common_parameters"].get(parameter_name, None) else: return None
def monitor_companies(chat_id, user_queue): companies = load_yaml("{}.yaml".format(chat_id)) common_parameters = companies.pop("common_parameters") for company_name, settings in companies.items(): signal = monitor_company({**settings, **common_parameters}) if signal is not None: user_queue.put( (company_name, "{}. {}".format(company_name, signal)), block=False, )
def add_company_to_monitoring(chat_id, company_figi, company_name): new_company_data = { company_name: { "figi": company_figi, } } file_path = "{}.yaml".format(chat_id) if os.path.isfile(file_path): file_data = load_yaml(file_path) if file_data is None: tmp_set_parameters(chat_id, "1min") file_data = load_yaml(file_path) file_data.update(new_company_data) else: file_data.update(new_company_data) else: tmp_set_parameters(chat_id, "1min") file_data = load_yaml(file_path) file_data.update(new_company_data) save_yaml(file_path, file_data)
def make_figi_ticker_dictionary(): stocks = get_stocks() russian_stocks = load_yaml("russian_tickers.yaml")["russian_tickers"] ticker_to_figi_map = dict() for stock in stocks: if stock["ticker"] in russian_stocks: continue ticker_to_figi_map[stock["ticker"]] = { "figi": stock["figi"], } save_yaml(NAME_FIGI_TICKER_MAP_PATH, ticker_to_figi_map)
def tmp_monitor_other_companies(chat_id, other_companies_queue): global TMP_OTHER_COMPANIES make_other_companies(chat_id) other_companies_settings = load_yaml("parameters_map.yaml")["other_companies"] tmp_counter = 50 while tmp_counter != 0: if not TMP_OTHER_COMPANIES: break company_name, settings = TMP_OTHER_COMPANIES.popitem() signal = monitor_company({**settings, **other_companies_settings}) if signal is not None: other_companies_queue.put( (company_name, "{}. {}".format(company_name, signal)), block=False, ) tmp_counter -= 1 print("Other companies to check: {}".format(len(TMP_OTHER_COMPANIES)))
def find_company_by_ticker(company_name): name_figi_ticker_map = load_yaml(NAME_FIGI_TICKER_MAP_PATH) return name_figi_ticker_map.get(company_name.upper(), dict()).get("figi", None)
HEADERS = { "accept": "application/json", "Authorization": "Bearer {}".format(TOKEN), } def daterange(start_datetime, end_datetime): for n in range(int((end_datetime - start_datetime).days)): yield end_datetime - timedelta(n), end_datetime - timedelta( n) + timedelta(1) if __name__ == "__main__": all_figi = [ v["figi"] for v in load_yaml("ticker_to_figi_map.yaml").values() ] for figi in all_figi: start_date = datetime(2000, 3, 15) end_date = datetime(2021, 3, 17) with open("{}_candles".format(figi), "w") as file_: file_.write("time open close high low volume\n") counter = 0 days_without_candles = 0 for from_, to in daterange(start_date, end_date): if counter >= 200: sleep(60) counter = 0