fetch_pair_volatility changes

This commit is contained in:
Nicolás Sánchez 2024-12-17 10:59:28 -03:00
parent c46b184627
commit d8e2d8eb77
1 changed files with 37 additions and 38 deletions

View File

@ -17,7 +17,7 @@ def write_to_log(message):
Writes a message to the log file Writes a message to the log file
''' '''
with open("log.txt", "a") as f: with open("log.txt", "a") as f:
f.write(message) f.write(f"{message}\n")
def yang_zhang(candles): def yang_zhang(candles):
@ -111,56 +111,41 @@ def get_pair_list(broker, inclusions = ["/USDT"], exclusions = []):
return pair_list 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 Fetch data from exchange
""" """
global volatilities
wait_time = .5 #Sleep time between requests
index = 0 index = 0
exchange = getattr(ccxt, broker) trading_volume = 0
exchange = exchange({ index += 1
"apiKey": "", try:
"secret": "", data = exchange.fetch_ohlcv(pair,timeframe=timeframe,limit=samples)
"password": "", except Exception as e:
"timeout": 30000, write_to_log(str(e))
"enableRateLimit": True 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: print(f"{pair} on {broker} ready, {len(pair_list)-index} pairs remaining.")
trading_volume = 0 return [round(yangzhang_volatility*100,2),round(parkinson_volatility*100,2),int(trading_volume)]
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
if __name__=="__main__": if __name__=="__main__":
threads = [] threads = []
pair_list = []
volatilities = {} volatilities = {}
samples = 288 samples = 288
timeframe = "5m" timeframe = "5m"
minimum_volume = 0 minimum_volume = 0
wait_time = .5
#Create database if it does not exist #Create database if it does not exist
database_connection = sqlite3.connect(f"{get_credentials('VOL_DB_PATH')['path']}{sys.argv[1]}.db") 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.commit()
database_connection.close() database_connection.close()
exchange = getattr(ccxt, sys.argv[1])
fetch_data(sys.argv[1], get_pair_list(sys.argv[1]), timeframe, samples) 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) write_to_db(sys.argv[1],volatilities)