Python
Cybotrade Models

Models

As a communication medium between the Python layer and the underlying Rust runtime, the data being passed through and forth are being made into various types of models. In other words, Python's dataclasses.

The following is an extensive list of all models used for the library.

Enums

RuntimeMode

The mode for the runtime.

class RuntimeMode(Enum):
    Backtest = 0
    Paper = 1
    Live = 2
    LiveTestnet = 3

Exchange

The supported exchange.

class Exchange(Enum):
    BybitLinear = 0
    BinanceLinear = 1
    OkxLinear = 2
    BitgetLinear = 3
    ZoomexLinear = 4

Environment

The exchange's environment to run the Strategy in.

class Environment(Enum):
    Testnet = 0
    Demo = 1
    Mainnet = 2

OrderSide

The side of an order, it's either buy or sell.

class OrderSide(Enum):
    Buy = 0
    Sell = 1

OrderType

The type of an order, it's either market or sell.

class OrderType(Enum):
    Market = 0
    Limit = 1

OrderStatus

The status for an order returned when the runtime invokes on_order_update.

class OrderStatus(Enum):
    Created = 0
    PartiallyFilled = 1
    PartiallyFilledCancelled = 2
    Filled = 3
    Cancelled = 4
    Rejected = 5
    CancelRejected = 6

TimeInForce

The time in force (opens in a new tab) for an order.

class TimeInForce(Enum):
    GTC = 0
    IOC = 1
    FOK = 2
    PostOnly = 3

PositionSide

The side of a position on the exchange.

class PositionSide(Enum):
    Closed = 0
    OneWayLong = 1
    OneWayShort = 2
    HedgeLong = 3
    HedgeShort = 4

PositionMargin

The margin type for an entered position, it's either cross or isolated.

class PositionMargin(Enum):
    Cross = 0
    Isolated = 1

LevelAction

The action for the specified level of the OrderBook snapshot.

@dataclass
class LevelAction(Enum):
    Add = 0
    Remove = 1
    Update = 2

Dataclasses

RuntimeConfig

The configuration for the underlying runtime.

@dataclass
class RuntimeConfig:
    mode: RuntimeMode
    candle_topics: List[str]
    datasource_topics: List[str]
 
    # interval denoted in seconds, represented in whole numbers, used for polling any Active Orders in `Live` or `LiveTestnet`.
    # Refer to additional notes below.
    active_order_interval: int
 
    data_count: Optional[int]
 
    # permutation_id is only relevant when `mode` is [`RuntimeMode.Backtest`]
    permutation_id: Optional[str]
 
    # initial_capital is only relevant when `mode` is [`RuntimeMode.Backtest`]
    initial_capital: Optional[float]
 
    # start_time is required when `mode` is [`RuntimeMode.Backtest`]
    start_time: Optional[datetime]
 
    # end_time is required when `mode` is [`RuntimeMode.Backtest`]
    end_time: Optional[datetime]
 
    # initial_capital is required when `mode` is [`RuntimeMode.Backtest`]
    initial_capital: Optional[float]
 
    # api_id is required when running the runtime locally. 
    api_key: Optional[str]
 
    # api_secret is required when running the runtime locally. 
    api_secret: Optional[str]
 
    # exchange_keys is required when running the runtime locally.
    exchange_keys: Optional[str]
 

Notes

data_count

The data_count represents the number of historical candles the runtime will manage for you. e.g data_count = 1000, For candles, this will maintain 1000 candles and also retrieve 1000 historical candles, for example, using the candle topic 'candles-1h-BTC/USDT-bybit', the runtime will retrieve the previous 1000 1h candles. For Data Source, this will maintain up to 1000 DataSource(s) but no historical data is retrieved.

When 'maintain' is mentioned it refers to the length of the List in the data_map struct. Refer to additional notes below

If you set data_count=1000, the List in data_map will be locked to a length of 1000. The List will follow a LIFO (Last In First Out) method of maintenance when the length has reached 1000, refer to the image below. data_count example


active_order_interval

The active_order_interval parameter is denoted in seconds. Refer to this guide for more details.

active_order_interval=1 # active orders will be checked after every 1 second have elapsed.
active_order_interval=7000 # active orders will be checked after every 7000 seconds have elapsed.

OrderResponse

The response when placing an order with Strategy.order() and Strategy.open().

@dataclass
class OrderResponse:
    exchange: Exchange
    exchange_order_id: str
    client_order_id: str

Symbol

Representation of a trading pair.

class Symbol:
    base: str 
    quote: str

OrderUpdate

The update for an order.

@dataclass
class OrderUpdate:
    symbol: Symbol
    order_type: OrderType
    side: OrderSide
    time_in_force: TimeInForce
    exchange_order_id: str
    order_time: datetime
    updated_time: datetime
    size: float
    filled_size: float
    remain_size: float
    price: float
    client_order_id: str
    status: OrderStatus
    exchange: Exchange
    is_reduce_only: bool
    is_hedge_mode: bool

OrderParams

The parameters for an order request.

@dataclass
class OrderParams:
    id: str
    side: OrderSide
    quantity: float
    is_hedge_mode: bool
    is_post_only: bool
    limit: Optional[float]
    stop: Optional[StopParams]
    reduce: Optional[bool]
    market_price: Optional[float]

Notes

If the limit parameter is not passed in, the order will be placed as a market order, else it will be a limit order.

# This will create a limit order on exchange that will be filled when price reaches 47500.0
limit=47500.0 

If the stop parameter is passed in, the order will be placed as either a limit market order, else it will be a stop limit order

StopParams

The parameters for a Stop Order request.

class StopParams:
    trigger_direction: Direction
    trigger_price: float
Notes

The trigger_price refers to the price at which the stop order will be triggered and placed.

The trigger_direction refers to where the trigger_price is, relative to the current price.
For example,

Current Price: 46500.0
Trigger Price: 48000.0

Here the trigger_price is set above the current price, as such the trigger_direction should be set to Up and vice versa.

Direction

The Direction set for StopParams

class Direction(enum):
  Up,
  Down

Position

The current position.

@dataclass
class Position:
    symbol: Symbol
    long: PositionData
    short: PositionData

PositionData

Data class that holds the position data.

@dataclass
class PositionData:
    quantity: float
    avg_price: float

ActiveOrder

The data returned for Unfilled/PartiallyFilled orders when the Runtime invoked on_active_order_interval.

@dataclass
class ActiveOrder:
    params: ActiveOrderParams, 
    client_order_id: str

ActiveOrderParams

The parameters of the Order that has not been Filled when the Runtime invokes on_active_order_interval.

@dataclass
class ActiveOrderParams:
  quantity: float
  take_profit: Optional[float]
  stop_loss: Optional[float]
  side: OrderSide
  exchange: Exchange
  price: float 
  is_reduce_only: bool

Level

 
The parameters of OrderBook ***snapshot*** level returned when the Runtime invokes `get_order_book`. 
 
@dataclass
class Level:
    price: float
    quantity: float
    price_action: LevelAction

OrderBookSnapshot

The parameters of the OrderBook snapshot returned when the Runtime invokes get_order_book.

@dataclass 
class OrderBookSnapshot:
    symbol: Symbol
    last_update_time: datetime
    last_update_id: int | None
    bids: List[Level]
    asks: List[Level]
    exchange: Exchange

Balance

The parameters of the Balance data returned when the Runtime invokes get_balance_data

@dataclass 
class Balance:
    exchange: Exchange
    coin: str
    wallet_balance: float
    available_balance: float
    initial_margin: Optional[float]
    margin_balance: Optional[float]
    maintenance_margin: Optional[float]