This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Asset.py
150 lines (112 loc) · 3.99 KB
/
Asset.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from enum import Enum
from Exchanges import findExchange
from InfoProvider import getStockStaticData
import pycountry
import gettext
class Asset:
def __init__(self, ticker, name):
self.ticker = ticker
self.name = name
@property
def country(self):
return None
def __str__(self):
return str(self.ticker)
def __eq__(self, other):
return type(self) == type(other) and self.ticker == other.ticker;
def __hash__(self):
return hash(type(self)) + hash(self.ticker)
class Currency(Asset):
def __init__(self, ticker, name):
super().__init__(ticker, name)
@property
def type(self):
return "Currency"
class Stock(Asset):
def __init__(self, isin, ticker, exchange, currency, name):
super().__init__(ticker, name)
self.isin = isin
self.exchange = exchange
self.currency = currency
self._type = 'Equity'
@property
def country(self):
return self.exchange.iso if self.exchange and self.exchange.iso else None
@property
def type(self):
return self._type
def __eq__(self, other):
return type(self) == type(other) and self.ticker == other.ticker and self.exchange == other.exchange and self.currency == other.currency
def __hash__(self):
return hash(type(self)) + hash(self.ticker) + hash(self.exchange) + hash(self.currency)
def __str__(self):
return str(self.ticker) if not self.exchange else "%s.%s" % (str(self.ticker), self.exchange)
class AssetDatabase:
def __init__(self):
self._currency = {}
self._stocks = []
self._changes = {}
def updateData(self):
for x in self._stocks:
if x.exchange and isinstance(x.exchange, str):
x.exchange = findExchange(x.exchange)
for x in self._stocks:
if not x.isin:
continue
data = getStockStaticData(x.isin, x.currency)
if data:
x.name = data['name']
x._type = data['type']
def getCurrency(self, ticker):
if ticker in self._currency:
return self._currency[ticker]
name = ticker
try:
pl = gettext.translation('iso4217', pycountry.LOCALES_DIR, languages=['pl'])
name = pycountry.currencies.lookup(ticker).name
except:
return None
c = Currency(ticker, name)
self._currency[c.ticker] = c
return c
def getStock(self, isin = None, ticker = None, exchange = None, currency = None, name = None):
t = ticker
e = exchange
while t and e and (t, e) in self._changes:
w = self._changes[(t, e)]
t = w[0]
e = w[1]
for s in self._stocks:
if isin and s.isin and s.isin != isin:
continue
if t and s.ticker and s.ticker != t:
continue
if e and s.exchange and e != s.exchange:
continue
if currency and s.currency and currency != s.currency:
continue
if name and s.name and name != s.name:
continue
if isin and not s.isin:
s.isin = isin
if t and not s.ticker:
s.ticker = t
if e and not s.exchange:
s.exchange = e
if currency and not s.currency:
s.currency = currency
if name and not s.name:
s.name = name
return s
s = Stock(isin, ticker, e, currency, name)
self._stocks.append(s)
return s
def changeName(self, oldTicker, oldExchange, newTicker, newExchange):
oldE = oldExchange
newE = newExchange
for x in self._stocks:
if x.ticker == oldTicker and x.exchange:
x.ticker = newTicker
x.exchange = newE
break
self._changes[(oldTicker, oldE)] = (newTicker, newE)