74 lines
2.2 KiB
Markdown
74 lines
2.2 KiB
Markdown
# trading: order matching
|
|
|
|
###### Sprint: ?
|
|
|
|
|
|
###DOC
|
|
* in case of reading first part processor please skip this part.
|
|
|
|
for matching and executing orders openware creates a flow that manage by RabbitMQ and its consumers.
|
|
we have three consumers that works on trades:
|
|
1. order processor:<br >
|
|
does preliminary calculations and works on order like locking order<br >
|
|
2. matching:<br >
|
|
match orders and pass them to executor
|
|
3. trade executor:<br >
|
|
execute trades that matching creates
|
|
|
|
|
|
```mermaid
|
|
stateDiagram-v2
|
|
[*] --> API
|
|
API --> RabbitMQ
|
|
RabbitMQ --> OrderProcessor
|
|
OrderProcessor --> RabbitMQ
|
|
RabbitMQ --> Matching
|
|
Matching --> RabbitMQ
|
|
RabbitMQ --> Executor
|
|
Executor --> Notify
|
|
Notify --> [*]
|
|
```
|
|
|
|
#### Order matching
|
|
##### what does order matching do step by step:
|
|
1. submit order ro engine
|
|
2. match method
|
|
1. get orderbooks
|
|
2. loop
|
|
1. is order filled?(all amount)
|
|
2. get top order of opposite order book(top means order with best price)
|
|
* for better and faster searching they use rbtree. for more information [click here](https://www.geeksforgeeks.org/red-black-tree-set-1-introduction-2/)
|
|
3. check can they create a trade
|
|
4. is trade valid?(trade validation):<br >
|
|
* not zero,calculation problems
|
|
5. fill both orders:<br >
|
|
* decrease order volume(if the opposite order is filled, we remove it from orderbook)
|
|
* filled order means that order completely get its needed volume
|
|
6. send to trade executor
|
|
|
|
```mermaid
|
|
graph TD
|
|
A[RabbitMQ] -->|submit payload| B(Matching)
|
|
B -->|submit engine|C(match method)
|
|
C --> D(get orderbooks)
|
|
D --> E(loop)
|
|
E --> F{order filled?}
|
|
F --> |yes|X(break)
|
|
F --> |no|G{opposit orderbook is blanked}
|
|
G --> |yes|H{is it limit order?}
|
|
H --> |yes|I(add to orderbook)
|
|
I --> X
|
|
H --> |no|J(cancel order)
|
|
J --> X
|
|
G --> |no|K(get top of opposit orderbook)
|
|
K --> L(make trade with top of opposit)
|
|
L --> |created trade|M{trade.blank?}
|
|
M --> |yes|H
|
|
M --> |no|N(validate trade)
|
|
N --> O(fill order)
|
|
O --> P(fill opposit order)
|
|
O --> |publish to executor|A
|
|
O --> E
|
|
X --> A
|
|
```
|