feat: Add support for python md plugins

This commit is contained in:
John Colvin committed 2024-09-18 14:02:39 +00:00
1 parent 2b62668df8
commit 2e9b4c78f3
46 files changed
+2265 -11

No files matched your search

@@ -0,0 +1,122 @@
import python_md_module as pymd
from typing import Optional
def _get_security(
venue: str,
name: str,
asset: str,
group: str,
currency: str,
expiration: Optional[str] = None,
option_kind: Optional[str] = None,
strike: Optional[float] = None,
):
sec = pymd.PySecurity()
sec.set_venue(venue)
sec.set_exchange_id(name)
sec.set_name(name)
sec.set_currency(currency)
sec.set_asset(asset)
sec.set_group(group)
is_spot = expiration is None and option_kind is None and strike is None
is_future = expiration is not None and option_kind is None and strike is None
is_option = (
expiration is not None and option_kind is not None and strike is not None
)
if is_spot:
sec.set_spot(pymd.PySpot())
elif is_future:
fut = pymd.PyFuture()
fut.set_expiration(expiration)
sec.set_future(fut)
elif is_option:
opt = pymd.PyOption()
opt.set_expiration(expiration)
opt.set_is_call(option_kind.lower().startswith("c"))
opt.set_strike(strike)
sec.set_option(opt)
else:
raise ValueError("Invalid parameters (unable to deduce security type)")
return sec
def _get_book_side(levels: list[(float, float)], is_bid_side: bool):
book_side = pymd.PyBookSide()
book_side.set_is_ascending(not is_bid_side)
for price, qty in levels:
book_level = pymd.PyBookLevel()
book_level.set_price(price)
book_level.set_quantity(qty)
book_side.set_level(book_level)
return book_side
def _get_trade(
venue: str,
exchange_security_id: str,
price: float,
quantity: float,
aggressor_side: str,
):
trade = pymd.PyTrade()
trade.set_venue(venue)
trade.set_exchange_security_id(exchange_security_id)
trade.set_price(price)
trade.set_quantity(quantity)
trade.set_aggressor_side(aggressor_side)
return trade
class DummyMdProvider(pymd.MdProvider):
def __init__(self):
super(DummyMdProvider, self).__init__()
pass
def start(self, sink: pymd.MdSink):
# start subscriptions, bind callbacks etc.
def add_fake_securities():
sec_list = pymd.PySecurityList()
sec_list.append(
_get_security(
venue="Glbx",
name="ESH5",
asset="ES",
group="ES",
currency="USD",
expiration="2025-03-28",
)
)
sink.on_securities(sec_list)
def add_fake_books():
bids = _get_book_side([(99.75, 8), (99.50, 11)], is_bid_side=True)
asks = _get_book_side([(100.25, 3)], is_bid_side=False)
book = pymd.PyBook()
book.set_venue("Glbx")
book.set_exchange_security_id("ESH5")
book.set_bids(bids)
book.set_asks(asks)
# pass book to sink
book_list = pymd.PyBookList()
book_list.append(book)
sink.on_books(book_list)
def add_fake_trades():
trade = _get_trade(
venue="Glbx",
exchange_security_id="ESH5",
price=100,
quantity=2,
aggressor_side="Buy",
)
trade_list = pymd.PyTradeList()
trade_list.append(trade)
sink.on_trades(trade_list)
add_fake_securities()
add_fake_books()
add_fake_trades()
def stop(self):
print("Stopping python MdProviderImpl")
@@ -0,0 +1,31 @@
from typing import Optional
import python_md_module as pymd
def get_book_side(levels: list[(float, float)], is_bid_side: bool):
book_side = pymd.PyBookSide()
book_side.set_is_ascending(not is_bid_side)
for price, qty in levels:
book_level = pymd.PyBookLevel()
book_level.set_price(price)
book_level.set_quantity(qty)
book_side.set_level(book_level)
return book_side
def get_trade(
price: Optional[float] = None,
quantity: Optional[float] = None,
aggressor: Optional[str] = None,
security_id: Optional[int] = None,
):
trade = pymd.PyTrade()
if price is not None:
trade.set_price(100)
if quantity is not None:
trade.set_quantity(2)
if aggressor is not None:
trade.set_aggressor(aggressor)
if security_id is not None:
trade.set_security_id(security_id)
return trade