Source code for mlthon.basics.defs

from typing import *

from mlthon.basics.mlt_enum import MltEnum


[docs]class Side(MltEnum): Buy = 0 Sell = 1
[docs] @staticmethod def parse(string_val: str): if string_val.lower() == 'buy': return Side.Buy elif string_val.lower() == 'sell': return Side.Sell else: raise RuntimeError("No such side: {}".format(string_val))
[docs]class OrderType(MltEnum): Unset = -1 Limit = 0
[docs] @staticmethod def parse(string_val: str): if str == "unset": return OrderType.Unset elif str == "limit": return OrderType.Limit else: raise RuntimeError("No such order type: {}".format(string_val))
[docs]class TIF(MltEnum): Unset = -1 GTC = 0 IOC = 1 # Execute immediately, even partially, the rest is cancelled
[docs] @staticmethod def parse(string_val: str): if str == "GoodTilCancel": return TIF.GTC elif str == "Unset": return TIF.Unset elif str == "ImmediateOrCancel": return TIF.IOC else: raise RuntimeError("No such time in force: {}".format(string_val))
[docs]class ExecInstructions(MltEnum): Unset = 0 PostOnly = 1
[docs] @staticmethod def parse(string_val: str): if str == "PostOnly": return ExecInstructions.PostOnly elif str == "Unset": return ExecInstructions.Unset else: raise RuntimeError("No such exec instruction: {}".format(string_val))
[docs]class Exchange(MltEnum): Blockchain = 0 Bybit = 1 Bequant = 2 Deribit = 3 Huobi = 4 Binance = 5 Ftx = 6 Lmax = 7 Binance_fut_usdt = 8 Binance_fut_coin = 9
[docs] @staticmethod def parse_str(s: str): return Exchange[s.capitalize()]
[docs] @staticmethod def try_parse_str(s: str): """ Parameters ---------- s: string to parse Returns ------- a tuple (bool,enum_val) where bool is true if parsing was a success. If so, enum_val will be the corresponding parsed value """ success = True val = None try: val = Exchange[s.capitalize()] except KeyError: success = False return success, val
[docs] @staticmethod def parse_exchange_code(exch: str): """ Parse exchange 3 character code""" exch_map = { "bet": Exchange.Bequant, "bln": Exchange.Blockchain, "byt": Exchange.Bybit, "det": Exchange.Deribit, "hui": Exchange.Huobi, "bie": Exchange.Binance, "bin": Exchange.Binance_fut_coin, "bit": Exchange.Binance_fut_usdt, "ftx": Exchange.Ftx, "lmx": Exchange.Lmax } exchange = exch_map.get(exch.lower(), None) if exchange is None: raise ValueError("Unknown exchange code: {ec}".format(ec=exch)) return exchange
@property def exchange_code(self) -> str: """Get exchange code for that exchange""" exch_map = { Exchange.Bequant.value: "bet", Exchange.Blockchain: "bln", Exchange.Bybit: "byt", Exchange.Deribit: "det", Exchange.Huobi: "hui", Exchange.Binance: "bie", Exchange.Binance_fut_coin: "bin", Exchange.Binance_fut_usdt: "bit", Exchange.Ftx: "ftx", Exchange.Lmax: "lmx" } return exch_map.get(self)
[docs]class InstrumentType(MltEnum): Spot = 0 Perpetual = 1 Future = 2
[docs] @staticmethod def parse_str(s: str): switch_table = { "spot": InstrumentType.Spot, "perp": InstrumentType.Perpetual, "fut": InstrumentType.Future, } return switch_table.get(s.lower())
[docs] @staticmethod def try_parse_str(s: str): """Try to parse InstrumentType from a string (type ID) Parameters ---------- s: str string to parse (type ID) Returns ------- a tuple (bool,enum_val) where bool is true if parsing was a success. If so, enum_val will be the corresponding parsed value """ success = True val = None try: val = InstrumentType.parse_str(s) except KeyError: success = False return success, val
@property def type_id(self) -> str: """Type ID of this InstrumentType. Returns ------- a string, type ID for that InstrumentType. """ switch_table = { InstrumentType.Spot: "spot", InstrumentType.Perpetual: "perp", InstrumentType.Future: "fut", } return switch_table.get(self)
[docs]class OrderStatus(MltEnum): Unknown = -1 Open = 0 PartiallyFilled = 1 Filled = 2 PendingCancel = 3 Cancelled = 4 Rejected = 5 Expired = 6 Suspended = 7
[docs] @staticmethod def parse_str(s: str): switch_table = { "unknown": OrderStatus.Unknown, "open": OrderStatus.Open, "partiallyfilled": OrderStatus.PartiallyFilled, "filled": OrderStatus.Filled, "pendingcancel": OrderStatus.PendingCancel, "cancelled": OrderStatus.Cancelled, "rejected": OrderStatus.Rejected, "expired": OrderStatus.Expired, "suspended": OrderStatus.Suspended } return switch_table.get(s.lower())
[docs]class GtwRejectCode(MltEnum): # Gateway related codes (in the 1000's) GtwNotReady = 1000 GtwRiskViolation = 1001 GtwRateLimitBreach = 1002 GtwCannotStackPendingRequests = 1003 GtwCannotModify = 1004 GtwPendingCancelAll = 1005 GtwOrderNotFound = 1006 GtwUnsupportedOp = 1007 GtwInvalidRequest = 1008 GtwQtyCannotBeBelowCumQty = 1009 GtwReduceMustLowerQty = 1010 GtwOngoingCancel = 1011 GtwNoRiskForSymbol = 1012 GtwRiskMaxOrderSizeViolation = 1013 GtwRiskShortPosViolation = 1014 GtwRiskLongPosViolation = 1015 GtwOrderNotOpen = 1016 # Exchange related codes (in the 2000's) ExchRateLimitBreach = 2000 ExchForbid = 2001 ExchOverload = 2002 ExchInsufficientBalance = 2003 ExchReject = 2004 ExchOrderFilled = 2005 ExchOrderNotFound = 2006 ExchDupeOrderID = 2007 ExchOrderExpired = 2008 ExchBadQty = 2009 ExchOrderCancelled = 2010 # Order already cancelled ExchOrderBelowMin = 2011 ExchQtyCannotBeBelowCumQty = 2012 ExchTooLateToCancel = 2013 ExchInvalidRequest = 2014 ExchIsClosed = 2015 ExchPostOnlyBreach = 2016 ExchTookTooLongToProcessRequest = 2017
[docs]class CancelCode(MltEnum): Unset = 0 CanceledByUser = 1 SelfTradePrevention = 2
[docs]class GtwStatus(MltEnum): Connecting = 0 ReadyToTrade = 1 Synchronizing = 2 Maintenance = 3 Disconnected = 4
[docs]class FeedHandlerStatus(MltEnum): SnapshotStart = 0 SnapshotEnd = 1 Disconnected = 2