How to Code a Market Making Algorithm: The Basics"
How to Code a Market Making Algorithm: The Basics
"Is Market Making just placing Limit Orders?"
Market Making is the holy grail of algorithmic trading. It is the strategy of providing liquidity: buying at the Bid and selling at the Ask, capturing the spread. It is theoretically risk-free, but in practice, it is the most difficult strategy to engineer.
At Virexan Capital, we build market making engines for crypto exchanges and illiquid stocks. Here is how the logic works.
The Core Concept: Inventory Risk
A market maker's nightmare is Inventory Exposure.
- li> You quote Bid: ₹100, Ask: ₹101.
- Someone hits your Bid. You are Long 100 shares.
- Price drops to ₹90. You lose ₹1000.
The Math: Skewing Quotes
To manage inventory, you must Skew your quotes.
- li> Neutral: Bid ₹100 / Ask ₹101.
- Long Inventory: You have too many shares. You want to sell.
* You lower your Bid (discourage buying) and lower your Ask (encourage selling).
The Formula (Avellaneda-Stoikov Model):
Professional market makers use stochastic calculus to determine the optimal spread based on:
- li> Volatility: Higher volatility = Wider spread (more risk).
- Inventory: More inventory = More skew.
Coding a Simple Maker (Python Concept)
class MarketMaker:
def init(self, max_pos=100):
self.inventory = 0
self.maxinventory = maxpos
self.mid_price = 100.0
self.spread = 0.50
def calculate_quotes(self):
# Calculate Skew based on Inventory
inventoryfactor = (self.inventory / self.maxinventory) * 0.10
# If Long +20 shares, skew prices DOWN to sell
# If Short -20 shares, skew prices UP to buy
bidprice = self.midprice - (self.spread / 2) - inventory_factor
askprice = self.midprice + (self.spread / 2) - inventory_factor
return bidprice, askprice
The Infrastructure Challenge
Market Making is a latency game.
- li> If the market moves faster than you can update your quotes, you get "Picked Off" (Adverse Selection).
- Virexan Standard: We place our servers in the exchange colocation facility (Colo).
- Code: C++ is mandatory here. Python is too slow for the tick-to-trade loop (often < 10 microseconds).
Dealing with Toxic Flow
"Toxic Flow" is when informed traders (who know something you don't) hit your quotes.
- li> Example: A hedge fund knows earnings are bad. They dump 100k shares into your Bid.
- Detection: We use Order Flow Imbalance (OFI) metrics. If we see massive selling pressure in the L2 order book, we widen our spreads or pull quotes entirely.
Why You Need Custom Software
Retail platforms cannot run market making logic.
- li> Rate Limits: You need to cancel/replace orders 50 times a second. Most retail APIs limit you to 10 requests/second.
- L2 Data: You need full depth of book to calculate micro-price. Retail feeds only give Top of Book.
- Risk Controls: You need a "Kill Switch" that liquidates everything if inventory exceeds limits.
Conclusion: The Ultimate Test
Market Making is the pinnacle of quantitative engineering. It combines high-speed data, complex math, and extreme risk management.
If you are a fund looking to provide liquidity for a token or stock, do not rely on a generic bot. Build a custom engine with Virexan Capital.
Ready to provide liquidity?
---
Related Insights
Need This Logic in Your Portfolio?
We don't just write about algorithms; we build them. Hire **Virexan Capital** to engineer your custom trading infrastructure.