From d8e2d8eb77b646eb3558dd7bb09ad65e4d819487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Tue, 17 Dec 2024 10:59:28 -0300 Subject: [PATCH] fetch_pair_volatility changes --- utils/fetch_pair_volatility.py | 75 +++++++++++++++++----------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/utils/fetch_pair_volatility.py b/utils/fetch_pair_volatility.py index eebe9ed..13be1d6 100644 --- a/utils/fetch_pair_volatility.py +++ b/utils/fetch_pair_volatility.py @@ -17,7 +17,7 @@ def write_to_log(message): Writes a message to the log file ''' with open("log.txt", "a") as f: - f.write(message) + f.write(f"{message}\n") def yang_zhang(candles): @@ -111,56 +111,41 @@ def get_pair_list(broker, inclusions = ["/USDT"], exclusions = []): return pair_list -def fetch_data(broker: str, pair_list: list, timeframe: str, samples: int): +def fetch_data(broker: str, pair: str, timeframe: str, samples: int): """ Fetch data from exchange """ - - global volatilities - - wait_time = .5 #Sleep time between requests + index = 0 - exchange = getattr(ccxt, broker) - exchange = exchange({ - "apiKey": "", - "secret": "", - "password": "", - "timeout": 30000, - "enableRateLimit": True - }) + trading_volume = 0 + index += 1 + try: + data = exchange.fetch_ohlcv(pair,timeframe=timeframe,limit=samples) + except Exception as e: + write_to_log(str(e)) + return None + try: + parkinson_volatility = parkinson(data) + yangzhang_volatility = yang_zhang(data) + except Exception as e: + write_to_log(str(e)) + return None + for item in data: + trading_volume += item[4]*item[5] - for pair in pair_list: - trading_volume = 0 - index += 1 - try: - data = exchange.fetch_ohlcv(pair,timeframe=timeframe,limit=samples) - except Exception as e: - write_to_log(str(e)) - continue - try: - parkinson_volatility = parkinson(data) - yangzhang_volatility = yang_zhang(data) - except Exception as e: - write_to_log(str(e)) - continue - for item in data: - trading_volume += item[4]*item[5] - volatilities[pair] = [round(yangzhang_volatility*100,2),round(parkinson_volatility*100,2),int(trading_volume)] - print(f"{pair} on {broker} ready, {len(pair_list)-index} pairs remaining.") - time.sleep(wait_time) - - return 0 + print(f"{pair} on {broker} ready, {len(pair_list)-index} pairs remaining.") + return [round(yangzhang_volatility*100,2),round(parkinson_volatility*100,2),int(trading_volume)] if __name__=="__main__": threads = [] - pair_list = [] volatilities = {} samples = 288 timeframe = "5m" minimum_volume = 0 + wait_time = .5 #Create database if it does not exist database_connection = sqlite3.connect(f"{get_credentials('VOL_DB_PATH')['path']}{sys.argv[1]}.db") @@ -175,8 +160,22 @@ if __name__=="__main__": database_connection.commit() database_connection.close() - - fetch_data(sys.argv[1], get_pair_list(sys.argv[1]), timeframe, samples) + exchange = getattr(ccxt, sys.argv[1]) + exchange = exchange({ + "apiKey": "", + "secret": "", + "password": "", + "timeout": 30000, + "enableRateLimit": True + }) + + pair_list = get_pair_list(sys.argv[1],inclusions = ["/USDT"], exclusions = []) + + for pair in pair_list: + data = fetch_data(exchange, pair, timeframe, samples) + if data is not None: + volatilities[pair] = data + time.sleep(wait_time) write_to_db(sys.argv[1],volatilities)