2021-07-12 02:57:05 -07:00
|
|
|
|
# # ⚠ Warning
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|
|
|
|
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
|
# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
#
|
|
|
|
|
# [🥭 Mango Markets](https://mango.markets/) support is available at:
|
|
|
|
|
# [Docs](https://docs.mango.markets/)
|
|
|
|
|
# [Discord](https://discord.gg/67jySBhxrg)
|
|
|
|
|
# [Twitter](https://twitter.com/mangomarkets)
|
|
|
|
|
# [Github](https://github.com/blockworks-foundation)
|
|
|
|
|
# [Email](mailto:hello@blockworks.foundation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
import mango
|
2021-07-12 09:18:56 -07:00
|
|
|
|
import typing
|
2021-07-12 02:57:05 -07:00
|
|
|
|
|
2021-08-22 11:48:20 -07:00
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
2021-08-02 09:14:00 -07:00
|
|
|
|
|
2021-07-12 02:57:05 -07:00
|
|
|
|
# # 🥭 ModelState class
|
|
|
|
|
#
|
|
|
|
|
# Provides simple access to the latest state of market and account data.
|
|
|
|
|
#
|
|
|
|
|
class ModelState:
|
|
|
|
|
def __init__(self, market: mango.Market,
|
2021-08-02 09:14:00 -07:00
|
|
|
|
group_watcher: mango.Watcher[mango.Group],
|
2021-08-24 12:13:44 -07:00
|
|
|
|
account_watcher: mango.Watcher[mango.Account],
|
2021-08-02 09:14:00 -07:00
|
|
|
|
price_watcher: mango.Watcher[mango.Price],
|
|
|
|
|
placed_orders_container_watcher: mango.Watcher[mango.PlacedOrdersContainer],
|
2021-08-21 14:06:58 -07:00
|
|
|
|
inventory_watcher: mango.Watcher[mango.Inventory],
|
|
|
|
|
bids: mango.Watcher[typing.Sequence[mango.Order]],
|
|
|
|
|
asks: mango.Watcher[typing.Sequence[mango.Order]]
|
2021-07-12 02:57:05 -07:00
|
|
|
|
):
|
|
|
|
|
self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)
|
|
|
|
|
self.market: mango.Market = market
|
2021-08-02 09:14:00 -07:00
|
|
|
|
self.group_watcher: mango.Watcher[mango.Group] = group_watcher
|
2021-08-24 12:13:44 -07:00
|
|
|
|
self.account_watcher: mango.Watcher[mango.Account] = account_watcher
|
2021-08-02 09:14:00 -07:00
|
|
|
|
self.price_watcher: mango.Watcher[mango.Price] = price_watcher
|
|
|
|
|
self.placed_orders_container_watcher: mango.Watcher[
|
2021-07-29 08:19:52 -07:00
|
|
|
|
mango.PlacedOrdersContainer] = placed_orders_container_watcher
|
2021-08-02 09:14:00 -07:00
|
|
|
|
self.inventory_watcher: mango.Watcher[
|
|
|
|
|
mango.Inventory] = inventory_watcher
|
2021-08-21 14:06:58 -07:00
|
|
|
|
self.bids_watcher: mango.Watcher[typing.Sequence[mango.Order]] = bids
|
|
|
|
|
self.asks_watcher: mango.Watcher[typing.Sequence[mango.Order]] = asks
|
2021-07-12 02:57:05 -07:00
|
|
|
|
|
2021-08-26 01:19:51 -07:00
|
|
|
|
self.not_quoting: bool = False
|
|
|
|
|
self.state: typing.Dict[str, typing.Any] = {}
|
|
|
|
|
|
2021-07-12 02:57:05 -07:00
|
|
|
|
@property
|
|
|
|
|
def group(self) -> mango.Group:
|
|
|
|
|
return self.group_watcher.latest
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def account(self) -> mango.Account:
|
|
|
|
|
return self.account_watcher.latest
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def price(self) -> mango.Price:
|
|
|
|
|
return self.price_watcher.latest
|
|
|
|
|
|
2021-08-02 09:14:00 -07:00
|
|
|
|
@property
|
|
|
|
|
def placed_orders_container(self) -> mango.PlacedOrdersContainer:
|
|
|
|
|
return self.placed_orders_container_watcher.latest
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def inventory(self) -> mango.Inventory:
|
|
|
|
|
return self.inventory_watcher.latest
|
|
|
|
|
|
2021-08-21 14:06:58 -07:00
|
|
|
|
@property
|
|
|
|
|
def bids(self) -> typing.Sequence[mango.Order]:
|
|
|
|
|
return self.bids_watcher.latest
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def asks(self) -> typing.Sequence[mango.Order]:
|
|
|
|
|
return self.asks_watcher.latest
|
|
|
|
|
|
2021-09-01 10:33:38 -07:00
|
|
|
|
# The top bid is the highest price someone is willing to pay to BUY
|
2021-08-22 11:48:20 -07:00
|
|
|
|
@property
|
2021-08-25 09:12:39 -07:00
|
|
|
|
def top_bid(self) -> typing.Optional[mango.Order]:
|
2021-08-25 07:20:53 -07:00
|
|
|
|
if self.bids_watcher.latest:
|
|
|
|
|
return self.bids_watcher.latest[0]
|
|
|
|
|
else:
|
|
|
|
|
return None
|
2021-08-22 11:48:20 -07:00
|
|
|
|
|
2021-09-01 10:33:38 -07:00
|
|
|
|
# The top ask is the lowest price someone is willing to pay to SELL
|
2021-08-22 11:48:20 -07:00
|
|
|
|
@property
|
2021-08-25 09:12:39 -07:00
|
|
|
|
def top_ask(self) -> typing.Optional[mango.Order]:
|
2021-08-25 07:20:53 -07:00
|
|
|
|
if self.asks_watcher.latest:
|
|
|
|
|
return self.asks_watcher.latest[0]
|
|
|
|
|
else:
|
|
|
|
|
return None
|
2021-08-22 11:48:20 -07:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def spread(self) -> Decimal:
|
2021-08-25 07:20:53 -07:00
|
|
|
|
top_ask = self.top_ask
|
|
|
|
|
top_bid = self.top_bid
|
|
|
|
|
if top_ask is None or top_bid is None:
|
|
|
|
|
return Decimal(0)
|
|
|
|
|
else:
|
2021-08-25 09:12:39 -07:00
|
|
|
|
return top_ask.price - top_bid.price
|
2021-08-22 11:48:20 -07:00
|
|
|
|
|
2021-07-12 09:18:56 -07:00
|
|
|
|
@property
|
2021-07-29 08:19:52 -07:00
|
|
|
|
def existing_orders(self) -> typing.Sequence[mango.PlacedOrder]:
|
|
|
|
|
return self.placed_orders_container_watcher.latest.placed_orders
|
2021-07-12 09:18:56 -07:00
|
|
|
|
|
2021-07-12 02:57:05 -07:00
|
|
|
|
def __str__(self) -> str:
|
2021-08-24 12:13:44 -07:00
|
|
|
|
return f"""« 𝙼𝚘𝚍𝚎𝚕𝚂𝚝𝚊𝚝𝚎 for market '{self.market.symbol}'
|
|
|
|
|
Group: {self.group_watcher.latest.address}
|
|
|
|
|
Account: {self.account_watcher.latest.address}
|
|
|
|
|
Price: {self.price_watcher.latest}
|
|
|
|
|
Inventory: {self.inventory_watcher.latest}
|
|
|
|
|
Existing Order Count: {len(self.placed_orders_container_watcher.latest.placed_orders)}
|
|
|
|
|
Bid Count: {len(self.bids_watcher.latest)}
|
|
|
|
|
Ask Count: {len(self.bids_watcher.latest)}
|
|
|
|
|
»"""
|
2021-07-12 02:57:05 -07:00
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"{self}"
|