DCAv2/profits/dump_old_profits_to_db.py

77 lines
2.4 KiB
Python

'''
Format to dump: (pair,timestamp,profit,exchange_name,order_id,order_history)
'''
import os
from datetime import datetime
import json
import sqlite3
import sys
#Make a dictionary with the format "pair":"exchange" instead of this garbage
if sys.argv[1]=="--testnet":
exchanges = ["binance_testnet"]
else:
exchanges = ["binance","gateio","kucoin","okex"]
recognized_pairs = {}
profits_path = "../profits/"
configs_path = "../configs/"
results = []
dates_already_present = []
order_id_list = []
amount_of_files_to_process = 0
#Load pair information from the exchange(s) configuration file(s)
for exchange_file in exchanges:
with open(f"{configs_path}{exchange_file}.json") as f:
coins_in_exchange_config = json.load(f)["pairs"]
for coin in coins_in_exchange_config:
recognized_pairs[coin] = exchange_file
#Connect to db
connection = sqlite3.connect('profits_database.db')
cursor = connection.cursor()
#Extract order_id data from db
cursor.execute('SELECT * FROM profits_table')
rows = cursor.fetchall()
for row in rows:
order_id_list.append(row[4])
for archivo in os.listdir(profits_path):
if archivo.endswith(".profits"):
amount_of_files_to_process+=1
#Format data
count = 1
for archivo in os.listdir(profits_path):
if archivo.endswith(".profits"):
coin = archivo.split(".")[0]
coin = f"{coin[-50:-4]}/{coin[-4:]}"
print(f"Processing {count} out of {amount_of_files_to_process} files ({coin})")
with open(f"{profits_path}{archivo}", "r") as csvfile:
for x in csvfile:
date,amount,order_id = x.split(",")
unix_time = int(datetime.strptime(date,"%Y-%m-%d").timestamp())
while unix_time in dates_already_present:
unix_time+=1
dates_already_present.append(unix_time)
exchange = recognized_pairs[coin] if coin in recognized_pairs else "unknown"
stripped_order_id = order_id.strip()
complete_line = (unix_time,coin,float(amount),exchange,stripped_order_id,"")
if stripped_order_id not in order_id_list:
results.append(complete_line)
count+=1
#Dump data to db
#for row in results:
# cursor.execute('INSERT INTO profits_table VALUES(?, ?, ?, ?, ?, ?)', row)
#connection.commit()
#connection.close()
print(f"Done. Added {len(results)} rows")