Files
spark/src/lib_python_md

lib_python_md

This library binds c++ market data providers to python modules which can be loaded at runtime.

Dependencies

Notes

  • Because this is python, market data enums (SecurityType, Venue, Side, etc.) are represented as strings

    • Parsing is NOT case sensitive (though it is conventional to use UPPER_CASE strings)
    • Type name prefixes (SECURITY_TYPE_, VENUE_, SIDE_ etc.) are NOT required
    • Having said that, we recommend UPPER_CASE strings without the type prefix. Here are a few examples:
    Enum Examples
    SecurityType "SPOT", "FUTURE", "OPTION"
    Venue "BYBIT", "GATEIO", "DERIBIT", "COINBASE"
    Side "BUY", "SELL"
  • All .py files in the python_md_providers dir are copied to a special plugins directory at build time, so they may be loaded at runtime.

  • For intellisense, create symlinks to the .so and .pyi files in the python_md_providers dir

    These files are excluded from source control via .gitignore, so don't worry about accidentally checking them in

# Run in src/lib_python_md/python_md_providers
ln -s ../../../build/sparkd-debug-clang/src/lib_python_md/stubs/python_md_module.pyi .
ln -s ../../../build/sparkd-debug-clang/src/lib_python_md/python_md_module.cpython-310-x86_64-linux-gnu.so .

Example

import time
import python_md_module as pymd


class MdProviderExample(pymd.MdProvider):
    def __init__(self):
        super(MdProviderExample, self).__init__()
        pass

    def start(self, sink: pymd.MdSink):
        print("Starting MdProviderExample")
        # An actual implementation would subscribe to market data here.
        # This is just an example to show you how to pass market data
        # back to the c++ Spark api.

        def add_dummy_security():
            # This example populates a dummy security for demonstrative purposes.
            # Of course, an actual implementation would populate the security list 
            # with data received from the exchange (or some other provider).
            sec = pymd.PySecurity()
            sec.set_venue("GLBX")
            sec.set_name("ESH8")
            sec.set_asset("ES")
            fut = pymd.PyFuture()
            fut.set_expiration("2025-03-28")
            sec.set_future(fut)
            # Note: the example only adds 1 sec, but batch adds are supported.
            sec_list = pymd.PySecurityList()
            sec_list.append(sec)
            sink.on_securities(sec_list)

        def add_dummy_trade():
            trade = pymd.PyTrade()
            trade.set_venue("GLBX")
            trade.set_security_type("FUTURE")
            trade.set_exchange_security_id("ESH8")
            trade.set_price(5684.25)
            trade.set_quantity(1)
            trade.set_aggressor_side("BUY")
            trade.set_timestamp(time.time_ns())

        add_dummy_security()
        add_dummy_trade()

    def stop(self):
        print("Stopping MdProviderExample")