---
# System prepended metadata

title: pure market making

---

# pure market making

## Every tick
```
proposal = None
if self._create_timestamp <= self._current_timestamp:
    # 1. Create base order proposals
    proposal = self.c_create_base_proposal()
    # 2. Apply functions that limit numbers of buys and sells proposal
    self.c_apply_order_levels_modifiers(proposal)
    # 3. Apply functions that modify orders price
    self.c_apply_order_price_modifiers(proposal)
    # 4. Apply functions that modify orders size
    self.c_apply_order_size_modifiers(proposal)
    # 5. Apply budget constraint, i.e. can't buy/sell more than what you have.
    self.c_apply_budget_constraint(proposal)

    if not self._take_if_crossed:
        self.c_filter_out_takers(proposal)


self.c_cancel_active_orders_on_max_age_limit()
self.c_cancel_active_orders(proposal)
self.c_cancel_orders_below_min_spread()
if self.c_to_create_orders(proposal):
    self.c_execute_orders_proposal(proposal)
```

### c_create_base_proposal
(不考慮 order_override / inventory_cost_price_delegate)

1. 根據 `get_price()` 取出的價格
2. buy_price = buy_reference_price * (Decimal("1") - self._bid_spread - (level * self._order_level_spread))
3. buy_size = self._order_amount + (self._order_level_amount * level)
4. sell 端同理


Note: 
* c_quantize_order_amount: 讓 amount fit 交易所的最小單元
* c_quantize_order_price: 讓 price fit 交易所的最小單元
* inventory_cost_price_delegate: 從 db 取得 inventory price
    * price = record.quote_volume / record.base_volume


### c_apply_order_levels_modifiers
1. _price_ceiling /  _price_floor 調整上下限
2. _ping_pong_enabled -> 刪除已經 filled 的單 i.e 只放沒成交的那邊

### c_apply_order_price_modifiers
#### _order_optimization_enabled -> c_apply_order_optimization
根據目前最好的 bid / ask 調整 price (向前一格)

相關參數
* _bid_order_optimization_depth: 要跨越多少的 volume 後的價格
    * ex 1000，代表要跨越累進 1000U 的 order
    

```
price_above_bid = (ceil(top_bid_price / price_quantum) + 1) * price_quantum
lower_buy_price = min(proposal.buys[0].price, price_above_bid)
for i, proposed in enumerate(proposal.buys):
    proposal.buys[i].price = market.c_quantize_order_price(self.trading_pair, lower_buy_price) * (1 - self.order_level_spread * i)
```

#### _add_transaction_costs_to_orders -> c_apply_add_transaction_costs
```
fee = market.c_get_fee(self.base_asset, self.quote_asset,
                       self._limit_order_type, TradeType.BUY, buy.size, buy.price)
price = buy.price * (Decimal(1) - fee.percent)
```

### c_apply_order_size_modifiers
#### _inventory_skew_enabled -> c_apply_inventory_skew
根據庫存調整 order amount，讓庫存不要太快用完

### c_apply_budget_constraint
根據剩餘 budget 調整 order，當剩餘 budget 不夠的時候要砍掉多餘的單

### !_take_if_crossed  -> c_filter_out_takers
!_take_if_crossed，將超過上下限的 order 砍掉

```
c_get_price: :returns: Top bid/ask price for a specific trading pair
proposal.buys = [buy for buy in proposal.buys if buy.price < top_ask]
proposal.sells = [sell for sell in proposal.sells if sell.price > top_bid]
```


### filled_order_delay
Event trigger, if order filled, it will triger `c_did_complete_sell_order`, in which:
```
# delay order creation by filled_order_dalay (in seconds)
self._create_timestamp = self._current_timestamp + self._filled_order_delay
self._cancel_timestamp = min(self._cancel_timestamp, self._create_timestamp)
```
## Market Event Interfaces

The `StrategyBase` class comes with a set of interface functions for handling market events from associated `MarketBase` objects, which may be overridded by child classes to receive and process market events.


## The event interface functions are as follows:

- `cdef c_did_create_buy_order(self, object order_created_event)`

    A buy order has been created. Argument is a [`BuyOrderCreatedEvent`](https://github.com/CoinAlpha/hummingbot/blob/master/hummingbot/core/event/events.py) object.

- `cdef c_did_create_sell_order(self, object order_created_event)`

    A sell order has been created. Argument is a [`SellOrderCreatedEvent`](https://github.com/CoinAlpha/hummingbot/blob/master/hummingbot/core/event/events.py) object.

- `cdef c_did_fill_order(self, object order_filled_event)`

    An order has been filled in the market. Argument is a [`OrderFilledEvent`](https://github.com/CoinAlpha/hummingbot/blob/master/hummingbot/core/event/events.py) object.

- `cdef c_did_fail_order(self, object order_failed_event)`

    An order has failed in the market. Argument is a [`MarketOrderFailureEvent`](https://github.com/CoinAlpha/hummingbot/blob/master/hummingbot/core/event/events.py) object.

- `cdef c_did_cancel_order(self, object cancelled_event)`

    An order has been cancelled. Argument is a [`OrderCancelledEvent`](https://github.com/CoinAlpha/hummingbot/blob/master/hummingbot/core/event/events.py) object.

- `cdef c_did_expire_order(self, object expired_event)`

    An order has expired. Argument is a [`OrderExpiredEvent`](https://github.com/CoinAlpha/hummingbot/blob/master/hummingbot/core/event/events.py) object.

- `cdef c_did_complete_buy_order(self, object order_completed_event)`

    A buy order has been completely filled. Argument is a [`BuyOrderCompletedEvent`](https://github.com/CoinAlpha/hummingbot/blob/master/hummingbot/core/event/events.py) object.

- `cdef c_did_complete_sell_order(self, object order_completed_event)`

    A sell order has been completely filled. Argument is a [`SellOrderCompletedEvent`](https://github.com/CoinAlpha/hummingbot/blob/master/hummingbot/core/event/events.py) object.