aboutsummaryrefslogtreecommitdiff
path: root/portfolio.py
diff options
context:
space:
mode:
Diffstat (limited to 'portfolio.py')
-rw-r--r--portfolio.py50
1 files changed, 22 insertions, 28 deletions
diff --git a/portfolio.py b/portfolio.py
index b0f9256..e1ee1f8 100644
--- a/portfolio.py
+++ b/portfolio.py
@@ -1,5 +1,6 @@
1import ccxt 1import ccxt
2import time 2import time
3from decimal import Decimal as D
3# Put your poloniex api key in market.py 4# Put your poloniex api key in market.py
4from market import market 5from market import market
5 6
@@ -37,7 +38,9 @@ class Portfolio:
37 except Exception: 38 except Exception:
38 return 39 return
39 try: 40 try:
40 cls.data = json.loads(r.data) 41 cls.data = json.loads(r.data,
42 parse_int=D,
43 parse_float=D)
41 except json.JSONDecodeError: 44 except json.JSONDecodeError:
42 cls.data = None 45 cls.data = None
43 46
@@ -83,22 +86,15 @@ class Portfolio:
83class Amount: 86class Amount:
84 MAX_DIGITS = 18 87 MAX_DIGITS = 18
85 88
86 def __init__(self, currency, value, int_val=None, linked_to=None, ticker=None): 89 def __init__(self, currency, value, linked_to=None, ticker=None):
87 self.currency = currency 90 self.currency = currency
88 if int_val is None: 91 self.value = D(value)
89 self._value = int(value * 10**self.MAX_DIGITS)
90 else:
91 self._value = int_val
92 self.linked_to = linked_to 92 self.linked_to = linked_to
93 self.ticker = ticker 93 self.ticker = ticker
94 94
95 self.ticker_cache = {} 95 self.ticker_cache = {}
96 self.ticker_cache_timestamp = time.time() 96 self.ticker_cache_timestamp = time.time()
97 97
98 @property
99 def value(self):
100 return self._value / 10 ** self.MAX_DIGITS
101
102 def in_currency(self, other_currency, market, action="average"): 98 def in_currency(self, other_currency, market, action="average"):
103 if other_currency == self.currency: 99 if other_currency == self.currency:
104 return self 100 return self
@@ -106,20 +102,19 @@ class Amount:
106 if asset_ticker is not None: 102 if asset_ticker is not None:
107 return Amount( 103 return Amount(
108 other_currency, 104 other_currency,
109 0, 105 self.value * asset_ticker[action],
110 int_val=int(self._value * asset_ticker[action]),
111 linked_to=self, 106 linked_to=self,
112 ticker=asset_ticker) 107 ticker=asset_ticker)
113 else: 108 else:
114 raise Exception("This asset is not available in the chosen market") 109 raise Exception("This asset is not available in the chosen market")
115 110
116 def __abs__(self): 111 def __abs__(self):
117 return Amount(self.currency, 0, int_val=abs(self._value)) 112 return Amount(self.currency, abs(self.value))
118 113
119 def __add__(self, other): 114 def __add__(self, other):
120 if other.currency != self.currency and other._value * self._value != 0: 115 if other.currency != self.currency and other.value * self.value != 0:
121 raise Exception("Summing amounts must be done with same currencies") 116 raise Exception("Summing amounts must be done with same currencies")
122 return Amount(self.currency, 0, int_val=self._value + other._value) 117 return Amount(self.currency, self.value + other.value)
123 118
124 def __radd__(self, other): 119 def __radd__(self, other):
125 if other == 0: 120 if other == 0:
@@ -128,25 +123,22 @@ class Amount:
128 return self.__add__(other) 123 return self.__add__(other)
129 124
130 def __sub__(self, other): 125 def __sub__(self, other):
131 if other.currency != self.currency and other._value * self._value != 0: 126 if other.currency != self.currency and other.value * self.value != 0:
132 raise Exception("Summing amounts must be done with same currencies") 127 raise Exception("Summing amounts must be done with same currencies")
133 return Amount(self.currency, 0, int_val=self._value - other._value) 128 return Amount(self.currency, self.value - other.value)
134
135 def __int__(self):
136 return self._value
137 129
138 def __mul__(self, value): 130 def __mul__(self, value):
139 if type(value) != int and type(value) != float: 131 if type(value) != int and type(value) != float and type(value) != D:
140 raise TypeError("Amount may only be multiplied by numbers") 132 raise TypeError("Amount may only be multiplied by numbers")
141 return Amount(self.currency, 0, int_val=(self._value * value)) 133 return Amount(self.currency, self.value * value)
142 134
143 def __rmul__(self, value): 135 def __rmul__(self, value):
144 return self.__mul__(value) 136 return self.__mul__(value)
145 137
146 def __floordiv__(self, value): 138 def __floordiv__(self, value):
147 if type(value) != int: 139 if type(value) != int and type(value) != float and type(value) != D:
148 raise TypeError("Amount may only be multiplied by integers") 140 raise TypeError("Amount may only be multiplied by integers")
149 return Amount(self.currency, 0, int_val=(self._value // value)) 141 return Amount(self.currency, self.value / value)
150 142
151 def __truediv__(self, value): 143 def __truediv__(self, value):
152 return self.__floordiv__(value) 144 return self.__floordiv__(value)
@@ -154,14 +146,14 @@ class Amount:
154 def __lt__(self, other): 146 def __lt__(self, other):
155 if self.currency != other.currency: 147 if self.currency != other.currency:
156 raise Exception("Comparing amounts must be done with same currencies") 148 raise Exception("Comparing amounts must be done with same currencies")
157 return self._value < other._value 149 return self.value < other.value
158 150
159 def __eq__(self, other): 151 def __eq__(self, other):
160 if other == 0: 152 if other == 0:
161 return self._value == 0 153 return self.value == 0
162 if self.currency != other.currency: 154 if self.currency != other.currency:
163 raise Exception("Comparing amounts must be done with same currencies") 155 raise Exception("Comparing amounts must be done with same currencies")
164 return self._value == other._value 156 return self.value == other.value
165 157
166 def __str__(self): 158 def __str__(self):
167 if self.linked_to is None: 159 if self.linked_to is None:
@@ -266,7 +258,7 @@ class Trade:
266 def invert(ticker): 258 def invert(ticker):
267 return { 259 return {
268 "inverted": True, 260 "inverted": True,
269 "average": (float(1/ticker["bid"]) + float(1/ticker["ask"]) ) / 2, 261 "average": (1/ticker["bid"] + 1/ticker["ask"]) / 2,
270 "original": ticker, 262 "original": ticker,
271 } 263 }
272 def augment_ticker(ticker): 264 def augment_ticker(ticker):
@@ -416,6 +408,8 @@ class Order:
416 408
417def print_orders(market, base_currency="BTC"): 409def print_orders(market, base_currency="BTC"):
418 Balance.prepare_trades(market, base_currency=base_currency) 410 Balance.prepare_trades(market, base_currency=base_currency)
411 for currency, balance in Balance.known_balances.items():
412 print(balance)
419 for currency, trade in Trade.trades.items(): 413 for currency, trade in Trade.trades.items():
420 print(trade) 414 print(trade)
421 for order in trade.orders: 415 for order in trade.orders: