-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalize_tradehistory.py
114 lines (99 loc) · 4.32 KB
/
normalize_tradehistory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python
import sys, os;
import argparse;
from os.path import expanduser;
import pandas as pd;
import math;
from datetime import datetime as dt;
from datetime import timedelta;
__author__ = "Jeetesh Mangwani"
def main():
parser = argparse.ArgumentParser(description="This script normalizes the Binance trade history to a simpler format for other scripts to process");
parser.add_argument("-th", "--tradehistory", type=str, help="The input xlsx file cotaining your Binance trade history", required=False, default = './trade-history.xlsx');
parser.add_argument("-nh", "--normalizedhistory", type=str, help="The output xlsx file cotaining your normalized trade history", required=False, default = './trade-history-normalized.xlsx');
parser.add_argument("-v", "--verbose", help="Whether to output verbose output messages", required=False, default=False);
args = parser.parse_args();
print("Input Buy History file: ", args.tradehistory);
print("Output Normalized History file: ", args.normalizedhistory);
print("Verbosity of log messages: ", args.verbose);
tradeHistoryDfs = pd.read_excel(args.tradehistory, sheet_name="sheet1")
outputDfs = pd.DataFrame({
'dateTime': pd.Series([], dtype='str'),
'asset': pd.Series([], dtype='str'),
'type': pd.Series([], dtype='str'),
'amount': pd.Series([], dtype='float'),
'pricePerUnit': pd.Series([], dtype='float'),
'totalCost': pd.Series([], dtype='float'),
#'txnFee': pd.Series([], dtype='float'),
})
for index, row in tradeHistoryDfs.iterrows():
#ts = int(row['UnixTimestamp'])
#txnDateTime = dt.utcfromtimestamp(ts).isoformat()
txnDateTime = dt.fromisoformat(str(row['Date(UTC)']))
mainCoin, baseCoin = splitCoinPair(row['Market'])
buyOrSell = str(row['Type'])
if (buyOrSell == "BUY"):
# bought main coin
# sold base coin
baseCoinPrice = float(row['Base Coin Unit Price'])
mainCoinAmount = float(row['Amount'])
mainCoinPrice = baseCoinPrice * float(row['Price'])
totalCost = mainCoinAmount * mainCoinPrice
baseCoinAmount = totalCost / baseCoinPrice
outputDfs = outputDfs.append({
'dateTime': txnDateTime,
'asset': mainCoin,
'type': 'BUY',
'amount': mainCoinAmount,
'pricePerUnit': mainCoinPrice,
'totalCost': totalCost}, ignore_index=True);
outputDfs = outputDfs.append({
'dateTime': txnDateTime,
'asset': baseCoin,
'type': 'SELL',
'amount': baseCoinAmount,
'pricePerUnit': baseCoinPrice,
'totalCost': totalCost}, ignore_index=True);
elif (buyOrSell == "SELL"):
# sold main coin
# bought base coin
baseCoinPrice = float(row['Base Coin Unit Price'])
mainCoinAmount = float(row['Amount'])
mainCoinPrice = baseCoinPrice * float(row['Price'])
totalCost = mainCoinAmount * mainCoinPrice
baseCoinAmount = totalCost / baseCoinPrice
outputDfs = outputDfs.append({
'dateTime': txnDateTime,
'asset': mainCoin,
'type': 'SELL',
'amount': mainCoinAmount,
'pricePerUnit': mainCoinPrice,
'totalCost': totalCost}, ignore_index=True);
outputDfs = outputDfs.append({
'dateTime': txnDateTime,
'asset': baseCoin,
'type': 'BUY',
'amount': baseCoinAmount,
'pricePerUnit': baseCoinPrice,
'totalCost': totalCost}, ignore_index=True);
else:
raise Exception("Invalid trade type:" + row)
outputDfs = outputDfs.sort_values(by=['dateTime'])
print(outputDfs);
outputDfs.to_excel(args.normalizedhistory)
def splitCoinPair(coinPair):
baseCoins = [
"USDT",
"BNB",
"ETH",
"USDC",
"BTC",
"BUSD"
];
coinPair = str(coinPair);
for baseCoin in baseCoins:
if coinPair.endswith(baseCoin):
mainCoin = coinPair.replace(baseCoin, "")
return mainCoin, baseCoin
raise Exception("Invalid coin pair: " + coinPair)
main();