Strategy vault / Market making
Market Making Strategy of Seizing the Market Counterattack
Market making strategy, the most basic market-making strategy: buy-sell-one-grab order to capture the market open, earning the price difference between buy and sell.
Market making strategy, the most basic market-making strategy: buy-sell-one-grab order to capture the market open, earning the price difference between buy and sell.
Parameters
| Name | Default | What it controls |
|---|---|---|
| Interval | 2000 | Error retry interval (milliseconds) |
| LoopInterval | true | Polling interval (seconds) |
| SlidePrice | 0.01 | slippage |
| MaxDiff | 0.8 | Minimum spread on the market |
| Lot | 0.2 | Number of lots |
| MinStock | 0.0001 | Minimum transaction volume |
Source JavaScript
function CancelPendingOrders(orderType) {
while (true) {
var orders = _C(exchange.GetOrders);
var count = 0;
if (typeof(orderType) != 'undefined') {
for (var i = 0; i < orders.length; i++) {
if (orders[i].Type == orderType) {
count++;
}
}
} else {
count = orders.length;
}
if (count == 0) {
return;
}
for (var j = 0; j < orders.length; j++) {
if (typeof(orderType) == 'undefined' || (orderType == orders[j].Type)) {
exchange.CancelOrder(orders[j].Id, orders[j]);
if (j < (orders.length-1)) {
Sleep(Interval);
}
}
}
}
}
function updateProfit(accountInit, accountNow, ticker) {
var netNow = accountNow.Balance + accountNow.FrozenBalance + ((accountNow.Stocks + accountNow.FrozenStocks) * ticker.Buy);
var netInit = accountInit.Balance + accountInit.FrozenBalance + ((accountInit.Stocks + accountInit.FrozenStocks) * ticker.Buy);
LogProfit(netNow - netInit);
}
var InitAccount = null;
var LastBuyPrice = 0;
var LastSellPrice = 0;
function onTick() {
var ticker = _C(exchange.GetTicker);
var BuyPrice = ticker.Buy + SlidePrice;
var SellPrice = ticker.Sell - SlidePrice;
// Profit Disappears
if ((SellPrice - BuyPrice) <= MaxDiff) {
CancelPendingOrders();
return;
}
var cancelType = null;
if (LastBuyPrice > 0 && (ticker.Buy - LastBuyPrice) > SlidePrice) {
cancelType = ORDER_TYPE_BUY;
}
if (LastSellPrice > 0 && (LastSellPrice - ticker.Sell) > SlidePrice) {
if (cancelType == null) {
cancelType = ORDER_TYPE_SELL;
} else {
cancelType = -1;
}
}
if (cancelType == -1) {
CancelPendingOrders();
} else if (cancelType != null) {
CancelPendingOrders(cancelType);
}
var orders = _C(exchange.GetOrders);
if (orders.length == 2) {
return;
}
var account = _C(exchange.GetAccount);
var amountBuy = _N(Math.min(account.Balance / BuyPrice, Lot));
var amountSell = Math.min(account.Stocks, Lot);
if (amountBuy >= MinStock) {
if (orders.length == 0 || orders[0].Type == ORDER_TYPE_SELL) {
if (orders.length > 0) {
updateProfit(InitAccount, account, ticker);
}
exchange.Buy(BuyPrice, amountBuy);
LastBuyPrice = BuyPrice;
}
}
if (amountSell >= MinStock) {
if (orders.length == 0 || orders[0].Type == ORDER_TYPE_BUY) {
if (orders.length > 0) {
updateProfit(InitAccount, account, ticker);
}
exchange.Sell(SellPrice, amountSell);
LastSellPrice = SellPrice;
}
}
}
function onexit() {
CancelPendingOrders();
}
function main() {
InitAccount = _C(exchange.GetAccount);
Log(InitAccount);
SetErrorFilter("502:|503:|unexpected|network|timeout|WSARecv|Connect|GetAddr|no such|reset|received|EOF");
exchange.SetRate(1);
LoopInterval = Math.max(LoopInterval, 1);
Lot = Math.max(MinStock, Lot);
while (true) {
onTick();
Sleep(LoopInterval * 1000);
}
}
Published by Zero on the FMZ strategy square · last modified 2018-03-27. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a market making bot with inventory skew limits and order deduplication from strategies/Market-Making-Strategy-of-Seizing-the-Market-Counterattack.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Dynamic Balance Market Making Strategy Xiao GaoJavaScript · Market making
- Short Unlocking Strategy on FMEX Starting from 1 BTC Bearish OutlookJavaScript · Market making
- Long Unlocking Strategy on FMEX Starting from 1 BTC Bullish OutlookJavaScript · Market making
- Dynamic Balance Market Making StrategyJavaScript · Market making
- Simple Bitcoin High Frequency Strategy Bot from 2014JavaScript · Market making
- Market Making Strategy of Grabbing the Market Position High Frequency Approaching TypeJavaScript · Market making