-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (55 loc) · 2.05 KB
/
main.py
File metadata and controls
70 lines (55 loc) · 2.05 KB
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
import pandas as pd
from python_bitvavo_api.bitvavo import Bitvavo
import datetime as dt
import os
CSV_INPUT = "purchases.csv"
CSV_OUTPUT = "analysis.csv"
def get_current_rates():
"""
Get latest rates for all markets available in Bitvavo
:return: {market: rate (as float)}
"""
bitvavo = Bitvavo()
responses = bitvavo.tickerPrice({})
return {entry["market"]: float(entry["price"]) for entry in responses}
def get_24h_rates(market_name):
"""
Return 24h details for one market
:param market_name: market name, e.g. BCN-EUR
:return: dictionary with rate-, volume- and bid/ask information
"""
bitvavo = Bitvavo()
response = bitvavo.ticker24h({"market": market_name})
return response
def two_digits(number):
if pd.isnull(number):
number = 0
return round(number, 2)
def main():
in_pycharm = "RUNNING_INSIDE_PYCHARM" in os.environ
now = dt.datetime.now()
date = now.strftime("%d-%m-%Y")
df = pd.read_csv(CSV_INPUT)
if df.empty:
print(f"Please enter your purchases in '{CSV_INPUT}'")
return
for index, row in df.iterrows():
market = f"{row.currency}-{row.base}"
rates = get_24h_rates(market)
df.at[index, "opening_rate"] = float(rates["open"])
df.at[index, "current_price"] = float(rates["last"])
df["purchase_rate"] = (df.investment / df.number)
df["current_value"] = (df.number * df.current_price).apply(two_digits)
df["increase"] = (df.current_value - df.investment).apply(two_digits)
df["percentage"] = (df.increase / df.investment * 100).apply(two_digits)
df["day_delta"] = (100 * (df.current_price - df.opening_rate) / df.current_price).apply(two_digits)
df["date"] = date
df["time"] = now.strftime("%H:%M:%S")
print("\n")
print(df[["currency", "current_value", "increase", "percentage"]])
print(f"\nVirtual result €{df.increase.sum():.2f}")
df.to_csv("%s" % CSV_OUTPUT, index=False)
if not in_pycharm:
input("\n\nPress enter to continue")
if __name__ == '__main__':
main()