-
Notifications
You must be signed in to change notification settings - Fork 165
/
price.py
401 lines (338 loc) · 14.1 KB
/
price.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# -*- coding: utf-8 -*-
from fractions import Fraction
from .account import Account
from .amount import Amount
from .asset import Asset
from .exceptions import InvalidAssetException
from .instance import BlockchainInstance
from .utils import assets_from_string, formatTimeString, parse_time
from graphenecommon.price import Price as GraphenePrice
@BlockchainInstance.inject
class Price(GraphenePrice):
"""
This class deals with all sorts of prices of any pair of assets to simplify dealing
with the tuple::
(quote, base)
each being an instance of :class:`bitshares.amount.Amount`. The
amount themselves define the price.
.. note::
The price (floating) is derived as ``base/quote``
:param list args: Allows to deal with different representations of a price
:param bitshares.asset.Asset base: Base asset
:param bitshares.asset.Asset quote: Quote asset
:param bitshares.bitshares.BitShares blockchain_instance: BitShares instance
:returns: All data required to represent a price
:rtype: dict
Way to obtain a proper instance:
* ``args`` is a str with a price and two assets
* ``args`` can be a floating number and ``base`` and ``quote`` being instances of :class:`bitshares.asset.Asset`
* ``args`` can be a floating number and ``base`` and ``quote`` being instances of ``str``
* ``args`` can be dict with keys ``price``, ``base``, and ``quote`` (*graphene balances*)
* ``args`` can be dict with keys ``base`` and ``quote``
* ``args`` can be dict with key ``receives`` (filled orders)
* ``args`` being a list of ``[quote, base]`` both being instances of :class:`bitshares.amount.Amount`
* ``args`` being a list of ``[quote, base]`` both being instances of ``str`` (``amount symbol``)
* ``base`` and ``quote`` being instances of :class:`bitshares.asset.Amount`
This allows instanciations like:
* ``Price("0.315 USD/BTS")``
* ``Price(0.315, base="USD", quote="BTS")``
* ``Price(0.315, base=Asset("USD"), quote=Asset("BTS"))``
* ``Price({"base": {"amount": 1, "asset_id": "1.3.0"}, "quote": {"amount": 10, "asset_id": "1.3.106"}})``
* ``Price({"receives": {"amount": 1, "asset_id": "1.3.0"}, "pays": {"amount": 10, "asset_id": "1.3.106"}}, base_asset=Asset("1.3.0"))``
* ``Price(quote="10 GOLD", base="1 USD")``
* ``Price("10 GOLD", "1 USD")``
* ``Price(Amount("10 GOLD"), Amount("1 USD"))``
* ``Price(1.0, "USD/GOLD")``
Instances of this class can be used in regular mathematical expressions
(``+-*/%``) such as:
.. code-block:: python
>>> from bitshares.price import Price
>>> Price("0.3314 USD/BTS") * 2
0.662600000 USD/BTS
"""
def define_classes(self):
self.amount_class = Amount
self.asset_class = Asset
@property
def market(self):
"""
Open the corresponding market.
:returns: Instance of :class:`bitshares.market.Market` for the
corresponding pair of assets.
"""
from .market import Market
return Market(
base=self["base"]["asset"],
quote=self["quote"]["asset"],
blockchain_instance=self.blockchain,
)
class Order(Price):
"""
This class inherits :class:`bitshares.price.Price` but has the ``base`` and
``quote`` Amounts not only be used to represent the price (as a ratio of base and
quote) but instead has those amounts represent the amounts of an actual order!
:param bitshares.bitshares.BitShares blockchain_instance: BitShares instance
.. note::
If an order is marked as deleted, it will carry the
'deleted' key which is set to ``True`` and all other
data be ``None``.
"""
def __init__(self, *args, **kwargs):
# This class does not have @BlockchainInstance.inject because of MRO, so we need
# to init BlockchainInstance manually! Fixes
# https://github.com/bitshares/python-bitshares/issues/234
BlockchainInstance.__init__(self, **kwargs)
if len(args) == 1 and isinstance(args[0], str):
"""Load from id."""
order = self.blockchain.rpc.get_objects([args[0]])[0]
if order:
Price.__init__(
self, order["sell_price"], blockchain_instance=self.blockchain
)
self.update(order)
self["deleted"] = False
else:
self["id"] = args[0]
self["deleted"] = True
self["quote"] = None
self["base"] = None
self["price"] = None
self["seller"] = None
elif len(args) == 1 and isinstance(args[0], dict) and "sell_price" in args[0]:
"""Load from object 1.7.xxx."""
# Take all the arguments with us
self.update(args[0])
Price.__init__(
self, args[0]["sell_price"], blockchain_instance=self.blockchain
)
elif (
len(args) == 1
and isinstance(args[0], dict)
and "min_to_receive" in args[0]
and "amount_to_sell" in args[0]
):
"""Load from an operation."""
# Take all the arguments with us
self.update(args[0])
Price.__init__(
self,
Amount(args[0]["min_to_receive"], blockchain_instance=self.blockchain),
Amount(args[0]["amount_to_sell"], blockchain_instance=self.blockchain),
)
else:
# Try load Order as Price
Price.__init__(self, *args, **kwargs)
if "for_sale" in self:
self["for_sale"] = Amount(
{"amount": self["for_sale"], "asset_id": self["base"]["asset"]["id"]},
blockchain_instance=self.blockchain,
)
@property
def for_sale(self):
if "for_sale" in self:
return Amount(
{"amount": self["for_sale"], "asset_id": self["base"]["asset"]["id"]},
blockchain_instance=self.blockchain,
)
@property
def price(self):
return self["price"]
@property
def to_buy(self):
if "for_sale" in self:
return Amount(
float(self["for_sale"]) / self["price"],
self["quote"]["asset"],
blockchain_instance=self.blockchain,
)
def __repr__(self):
if "deleted" in self and self["deleted"]:
return "deleted order %s" % self["id"]
else:
t = ""
if "time" in self and self["time"]:
t += "(%s) " % self["time"]
if "type" in self and self["type"]:
t += "%s " % str(self["type"])
if "for_sale" in self and self["for_sale"]:
t += "buy {} for {} ".format(
str(
Amount(
float(self["for_sale"]) / self["price"],
self["quote"]["asset"],
blockchain_instance=self.blockchain,
)
),
str(self["for_sale"]),
)
elif "amount_to_sell" in self:
t += "sell {} for {} ".format(
str(
Amount(
self["amount_to_sell"], blockchain_instance=self.blockchain
)
),
str(
Amount(
self["min_to_receive"], blockchain_instance=self.blockchain
)
),
)
elif "quote" in self and "base" in self:
t += "{} for {} ".format(
str(
Amount(
{
"amount": self["quote"],
"asset_id": self["quote"]["asset"]["id"],
},
blockchain_instance=self.blockchain,
)
),
str(
Amount(
{
"amount": self["base"],
"asset_id": self["base"]["asset"]["id"],
},
blockchain_instance=self.blockchain,
)
),
)
return t + "@ " + Price.__repr__(self)
__str__ = __repr__
class FilledOrder(Price):
"""
This class inherits :class:`bitshares.price.Price` but has the ``base`` and
``quote`` Amounts not only be used to represent the price (as a ratio of base and
quote) but instead has those amounts represent the amounts of an actually filled
order!
:param bitshares.bitshares.BitShares blockchain_instance: BitShares instance
.. note:: Instances of this class come with an additional ``time`` key
that shows when the order has been filled!
"""
def copy(self):
return self.__class__(
self.order, base=self["base"].copy(), quote=self["quote"].copy()
)
def __init__(self, order, **kwargs):
self.order = order
if isinstance(order, dict) and "price" in order:
Price.__init__(
self,
order.get("price"),
base=kwargs.get("base"),
quote=kwargs.get("quote"),
)
self.update(order)
self["time"] = formatTimeString(order["date"])
elif isinstance(order, dict):
# filled orders from account history
if "op" in order:
if isinstance(order["op"], (list, set)):
order = order["op"][1]
elif isinstance(order["op"], dict):
order = order["op"]
base_asset = kwargs.get("base_asset", order["receives"]["asset_id"])
Price.__init__(self, order, base_asset=base_asset)
# To be on the save side, store the entire order object in this
# dict as well
self.update(order)
# Post-Process some additional stuff
if "time" in order:
self["time"] = formatTimeString(order["time"])
if "account_id" in order:
self["account_id"] = order["account_id"]
else:
raise ValueError("Couldn't parse 'Price'.")
def __repr__(self):
t = ""
if "time" in self and self["time"]:
t += "(%s) " % self["time"]
if "type" in self and self["type"]:
t += "%s " % str(self["type"])
if "quote" in self and self["quote"]:
t += "%s " % str(self["quote"])
if "base" in self and self["base"]:
t += "%s " % str(self["base"])
return t + "@ " + Price.__repr__(self)
__str__ = __repr__
class UpdateCallOrder(Price):
"""This class inherits :class:`bitshares.price.Price` but has the ``base``
and ``quote`` Amounts not only be used to represent the **call
price** (as a ratio of base and quote).
:param bitshares.bitshares.BitShares blockchain_instance: BitShares instance
"""
def __init__(self, call, **kwargs):
BlockchainInstance.__init__(self, **kwargs)
if isinstance(call, dict) and "call_price" in call:
Price.__init__(
self,
call.get("call_price"),
base=call["call_price"].get("base"),
quote=call["call_price"].get("quote"),
)
else:
raise ValueError("Couldn't parse 'Call'.")
def __repr__(self):
t = "Margin Call: "
if "quote" in self and self["quote"]:
t += "%s " % str(self["quote"])
if "base" in self and self["base"]:
t += "%s " % str(self["base"])
return t + "@ " + Price.__repr__(self)
__str__ = __repr__
@BlockchainInstance.inject
class PriceFeed(dict):
"""
This class is used to represent a price feed consisting of.
* a witness,
* a symbol,
* a core exchange rate,
* the maintenance collateral ratio,
* the max short squeeze ratio,
* a settlement price, and
* a date
:param bitshares.bitshares.BitShares blockchain_instance: BitShares instance
"""
def __init__(self, feed, **kwargs):
if len(feed) == 2:
dict.__init__(
self,
{
"producer": Account(
feed[0], lazy=True, blockchain_instance=self.blockchain
),
"date": parse_time(feed[1][0]),
"maintenance_collateral_ratio": feed[1][1][
"maintenance_collateral_ratio"
],
"maximum_short_squeeze_ratio": feed[1][1][
"maximum_short_squeeze_ratio"
],
"settlement_price": Price(
feed[1][1]["settlement_price"],
blockchain_instance=self.blockchain,
),
"core_exchange_rate": Price(
feed[1][1]["core_exchange_rate"],
blockchain_instance=self.blockchain,
),
},
)
else:
dict.__init__(
self,
{
"maintenance_collateral_ratio": feed[
"maintenance_collateral_ratio"
],
"maximum_short_squeeze_ratio": feed["maximum_short_squeeze_ratio"],
"settlement_price": Price(
feed["settlement_price"], blockchain_instance=self.blockchain
),
"core_exchange_rate": Price(
feed["core_exchange_rate"], blockchain_instance=self.blockchain
),
},
)