algo_engine.base

The core layer provides C-level market data types, buffers, and memory allocation. All types are implemented in Cython and compiled to native extensions.

Market Data Types

class algo_engine.base.MarketData
address
static buffer_size(md_data_type dtype)
data_addr
dtype
classmethod from_bytes(cls, bytes data)
static from_ptr(uintptr_t addr)
market_price
market_time
owner
price
session_date
session_datetime
session_time
ticker
timestamp
to_bytes(self) bytes
topic
class algo_engine.base.TickData(str ticker, *, double timestamp, double last_price, double open_price=NAN, double prev_close=NAN, double total_traded_volume=0.0, double total_traded_notional=0.0, uint64_t total_trade_count=0, double total_bid_volume=0.0, double total_ask_volume=0.0, double weighted_bid_price=NAN, double weighted_ask_price=NAN, **kwargs)

Bases: MarketData

__init__(*args, **kwargs)
ask
ask_price
ask_volume
best_ask_price
best_ask_volume
best_bid_price
best_bid_volume
bid
bid_price
bid_volume
last_price
lite(self, bool copy=True) TickDataLite
mid_price
open_price
parse(self, dict kwargs) void
prev_close
spread
total_ask_volume
total_bid_volume
total_trade_count
total_traded_notional
total_traded_volume
weighted_ask_price
weighted_bid_price
class algo_engine.base.OrderBook
at_level(self, Py_ssize_t idx) tuple
at_price(self, double price) tuple
capacity
direction
classmethod from_buffer(cls, const unsigned char[: ] buffer)
classmethod from_bytes(cls, bytes data)
loc_volume(self, double p0, double p1)
n_orders
price
side
size
sort(self)
sorted
to_bytes(self)
to_numpy(self)
volume
class algo_engine.base.BarData(str ticker, *, double timestamp, double high_price, double low_price, double open_price, double close_price, double volume=0.0, double notional=0.0, uint64_t trade_count=0, double start_timestamp=0., bar_span=None, **kwargs)

Bases: MarketData

__init__(*args, **kwargs)
bar_end_time
bar_span
bar_span_seconds
bar_start_time
bar_type
close_price
high_price
low_price
notional
open_price
start_timestamp
trade_count
volume
vwap
class algo_engine.base.DailyBar(str ticker, *, date market_date, double high_price, double low_price, double open_price, double close_price, double volume=0.0, double notional=0.0, uint64_t trade_count=0, int bar_span=1, **kwargs)

Bases: BarData

__init__(*args, **kwargs)
bar_end_time
bar_span
bar_start_time
bar_type
market_date
market_time

Transaction Types

class algo_engine.base.TransactionData(str ticker: str, *, double timestamp, double price, double volume, md_side side, double multiplier=1.0, double notional=NAN, transaction_id=None, buy_id=None, sell_id=None, **kwargs)

Bases: MarketData

__init__(*args, **kwargs)
buy_id
classmethod merge(cls, list data_list)
multiplier
notional
notional_flow
price
sell_id
side
side_int
side_sign
transaction_id
volume
volume_flow
class algo_engine.base.OrderData(str ticker, *, double timestamp, double price, double volume, md_side side, order_id=None, md_order_type order_type=md_order_type.ORDER_GENERIC, **kwargs)

Bases: MarketData

__init__(*args, **kwargs)
flow
order_id
order_type
order_type_int
price
side
side_int
side_sign
volume
class algo_engine.base.TradeData(str ticker: str, *, double timestamp, double trade_price, double trade_volume, md_side trade_side, double multiplier=1.0, double notional=NAN, transaction_id=None, buy_id=None, sell_id=None, **kwargs)

Bases: TransactionData

__init__(*args, **kwargs)
trade_price
trade_side
trade_volume
class algo_engine.base.TransactionSide(*values)
property sign
property offset
property direction
property side_name
property offset_name
property direction_name
SIDE_LONG_OPEN = 10
SIDE_LONG_CLOSE = 18
SIDE_LONG_CANCEL = 2
SIDE_SHORT_OPEN = 8
SIDE_SHORT_CLOSE = 16
SIDE_SHORT_CANCEL = 0
SIDE_NEUTRAL_OPEN = 11
SIDE_NEUTRAL_CLOSE = 19
SIDE_BID = 6
SIDE_ASK = 4
SIDE_CANCEL = 1
SIDE_UNKNOWN = 1
SIDE_LONG = 10
SIDE_SHORT = 8
ShortOrder = 4
AskOrder = 4
Ask = 4
LongOrder = 6
BidOrder = 6
Bid = 6
ShortFilled = 16
Unwind = 16
Sell = 16
LongFilled = 10
LongOpen = 10
Buy = 10
ShortOpen = 8
Short = 8
Cover = 18
UNKNOWN = 1
CANCEL = 1
FAULTY = 255
class algo_engine.base.TransactionDirection(*values)
property sign
DIRECTION_UNKNOWN = 1
DIRECTION_SHORT = 0
DIRECTION_LONG = 2
DIRECTION_NEUTRAL = 3
class algo_engine.base.TransactionOffset(*values)
OFFSET_CANCEL = 0
OFFSET_ORDER = 4
OFFSET_OPEN = 8
OFFSET_CLOSE = 16
class algo_engine.base.OrderType(*values)
ORDER_UNKNOWN = 2
ORDER_CANCEL = 1
ORDER_GENERIC = 0
ORDER_LIMIT = 10
ORDER_LIMIT_MAKER = 11
ORDER_MARKET = 20
ORDER_FOK = 21
ORDER_FAK = 22
ORDER_IOC = 23

Trade Utilities

class algo_engine.base.OrderState(*values)
property is_working
property is_placed
property is_done
property state_name
STATE_UNKNOWN = 0
STATE_REJECTED = 1
STATE_INVALID = 2
STATE_PENDING = 3
STATE_SENT = 4
STATE_PLACED = 5
STATE_PARTFILLED = 6
STATE_FILLED = 7
STATE_CANCELING = 8
STATE_CANCELED = 9
UNKNOWN = 0
Rejected = 1
Invalid = 2
Pending = 3
Sent = 4
Placed = 5
PartFilled = 6
Filled = 7
Canceling = 8
Canceled = 9
class algo_engine.base.TradeReport(str ticker, *, double timestamp, double price, double volume, md_side side, double notional=NAN, double multiplier=1., double fee=0., order_id=None, trade_id=NO_DEFAULT, **kwargs)
__init__(*args, **kwargs)
fee
classmethod from_json(cls, json_data)
multiplier
notional
notional_flow
order_id
price
reset_order_id(self, order_id=NO_DEFAULT) TradeReport
reset_trade_id(self, trade_id=NO_DEFAULT) TradeReport
side
side_int
side_sign
to_json(self, str fmt='str', **kwargs)
to_trade(self) TransactionData
trade_id
trade_time
volume
volume_flow
class algo_engine.base.TradeInstruction(str ticker, *, double timestamp, md_side side, double volume, md_order_type order_type=md_order_type.ORDER_GENERIC, double limit_price=NAN, double multiplier=1., order_id=NO_DEFAULT, **kwargs)
__init__(*args, **kwargs)
add_trade(self, TradeReport trade_report) TradeInstruction
average_price
cancel_order(self, double timestamp=NAN) TradeInstruction
canceled(self, double timestamp=NAN) TradeInstruction
canceled_time
canceled_ts
fee
fill(self, TradeReport trade_report) TradeInstruction
filled_notional
filled_volume
finished_time
finished_ts
classmethod from_json(cls, json_data)
is_done
is_placed
is_working
limit_price
multiplier
order_id
order_state
order_state_int
order_type
order_type_int
placed_time
placed_ts
reset(self) TradeInstruction
reset_order_id(self, order_id=NO_DEFAULT) TradeInstruction
set_order_state(self, md_order_state order_state, double timestamp=NAN) TradeInstruction
side
side_int
side_sign
start_time
to_json(self, str fmt='str', **kwargs)
trades
volume
working_volume

Data Buffers

class algo_engine.base.MarketDataBuffer
buf
cache(self)
data_capacity
data_tail
classmethod from_bytes(cls, bytes data)
get(self, Py_ssize_t idx)
is_sorted
ptr_capacity
ptr_tail
put(self, MarketData market_data)
sort(self)
to_bytes(self)
class algo_engine.base.MarketDataRingBuffer
data_capacity
data_tail
get(self, int idx: int)
is_empty
listen(self, bool block=True, double timeout=0.0)
ptr_capacity
ptr_head
ptr_tail
put(self, MarketData market_data, bool block=True, double timeout=0.0)
class algo_engine.base.MarketDataConcurrentBuffer
disable_worker(self, size_t worker_id)
enable_worker(self, size_t worker_id)
is_empty(self)
is_full(self)
is_worker_empty(self, size_t worker_id)
listen(self, size_t worker_id, bool block=True, double timeout=0.0)
put(self, MarketData market_data, bool block=True, double timeout=0)
class algo_engine.base.MarketDataBufferCache
capacity
clear(self)
get(self, Py_ssize_t idx)
parent
put(self, MarketData market_data)
size

Configuration

algo_engine.base.CONFIG = mappingproxy({'market_data': mappingproxy({'DEBUG': False, 'BOOK_SIZE': 10, 'ID_SIZE': 16, 'LONG_ID_SIZE': 128, 'MAX_WORKERS': 128, 'MID_ALLOW_INT64': 1, 'MID_ALLOW_INT128': 1, 'LONG_MID_ALLOW_INT64': 1, 'LONG_MID_ALLOW_INT128': 1, 'DTYPE_MIN_SIZE': 80, 'DTYPE_MAX_SIZE': 392, 'MD_FILTER_ALL': 254}), 'market_data_buffer': mappingproxy({'MD_BUF_PTR_DEFAULT_CAP': 16, 'MD_BUF_DATA_DEFAULT_CAP': 1024}), 'exchange_profile': mappingproxy({'SECONDS_PER_DAY': 86400.0, 'SECONDS_PER_HOUR': 3600.0, 'SECONDS_PER_MINUTE': 60.0, 'NANOS_PER_SECOND': 1000000000.0, 'MICROS_PER_SECOND': 1000000.0, 'UNIX_EPOCH_ORDINAL': 719163, 'EX_PROFILE_ID_SIZE': 64, 'EX_PROFILE_MIN_YEAR': 1, 'EX_PROFILE_MAX_YEAR': 9999, 'EX_PROFILE_MAX_ORDINAL': 3652059})})

Read-only proxy of a mapping.

algo_engine.base.USE_CYTHON = True

Returns True when the argument is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.

Financial Utilities

class algo_engine.base.FinancialDecimal(value: Real | str = 0.0, /, k: int = None, tick: int = None)[source]
classmethod from_float(f: float, tick=None)[source]
as_integer_ratio() tuple[int, int][source]

Return a pair of integers, whose ratio is exactly equal to the original float.

The ratio is in lowest terms and has a positive denominator. Raise OverflowError on infinities and a ValueError on NaNs.

>>> (10.0).as_integer_ratio()
(10, 1)
>>> (0.0).as_integer_ratio()
(0, 1)
>>> (-.25).as_integer_ratio()
(-1, 4)
property k
property numerator
property tick
property denominator