Strategy vault / Market making
Dynamic Balance Market Making Strategy
JavaScript strategy by FawkesPan.
JavaScriptMarket making
The original spec has no written description. Read the source below.
Parameters
| Name | Default | What it controls |
|---|---|---|
| DELAY | 5 | Balance interval |
| PRICE_INCREMENT | 1e-06 | Minimum price movement |
| AMOUNT_INCREMENT | 0.01 | Minimum quantity movement |
| AMOUNT_MINIMUM | 0.01 | Minimum trading quantity |
| SPREAD | 0.3 | Order price interval |
Source JavaScript
/*
Rebalance With MarketMake
Copyright 2018 FawkesPan
Contact : [email protected] / Telegram@FawkesPan
GNU General Public License v3.0
*/
var Coin = ''
var Fiat = ''
var RefreshLoop = 0
var Account = ''
var Depth = ''
var Orders = ''
var BuyWeighted = 0
var SellWeighted = 0
var MidPrice = 0
var InitialBalance = exchange.GetAccount().Balance + exchange.GetAccount().Stocks * exchange.GetDepth().Bids[0].Price
function CancelPendingOrders() {
for (var j = 0; j < Orders.length; j++) {
exchange.CancelOrder(Orders[j].Id, Orders[j])}
}
function UpdateAll() {
Account = exchange.GetAccount()
Depth = exchange.GetDepth()
Orders = exchange.GetOrders()
//Log("UpdateAll")
}
function UpdatePrice() {
//MidPrice = (Depth.Asks[0].Price+Depth.Bids[0].Price)/2
BuyWeighted = Depth.Asks[0].Price * (1+SPREAD/100)
SellWeighted = Depth.Bids[0].Price * (1-SPREAD/100)
//Log("UpdatePrice")
}
function onTick(){
// Refresh account balance and market data
//Log("UpdateAll")
UpdateAll()
var Buy = Depth.Asks[0].Price
var Sell = Depth.Bids[0].Price
// Calculate weighted price
//Log("UpdatePrice")
UpdatePrice()
// Check existing orders
if (Orders.length==2){
return
}
if (Orders.length>2){
CancelPendingOrders()
UpdateAll()
}
// Process single order
if (Orders.length==1){
Order = Orders[0]
Price=Order.Price
if (Order.Type==0){
if (Price/(1-SPREAD/100) > Sell){
return
}else{
CancelPendingOrders()
UpdateAll()
}
}else{
if (Price/(1+SPREAD/100) < Buy){
return
}else{
CancelPendingOrders()
UpdateAll()
}
}
}
// Calculate the value of current positions
var ValueByBuy = Account.Stocks * BuyWeighted
var ValueBySell = Account.Stocks * SellWeighted
Log(Coin + " Value By Weighted Ask - " + Fiat +" Balance : " + (ValueByBuy-Account.Balance))
Log(Fiat + " Balance - " + Coin +" Value By Weighted Bid : " +(Account.Balance-ValueBySell))
//Log(ValueByBuy)
//Log(ValueBySell)
// The value of the currency held is higher than the pricing currency. Sell the currency held.
if (ValueByBuy > Account.Balance){
ToBeSold = (ValueByBuy - Account.Balance)/2/BuyWeighted
if (ToBeSold > AMOUNT_MINIMUM){
ToBeSold = _N(Math.ceil(ToBeSold / AMOUNT_INCREMENT) * AMOUNT_INCREMENT)
Log("To be sold " + Coin + " Quantity:" + ToBeSold)
exchange.Sell(BuyWeighted,ToBeSold)
}
}
// The value of the pricing currency held is higher than the currency held. Buy the currency
if (ValueBySell < Account.Balance){
ToBeBought = (Account.Balance - ValueBySell)/2/SellWeighted
if (ToBeBought > AMOUNT_MINIMUM){
ToBeBought = _N(Math.ceil(ToBeBought / AMOUNT_INCREMENT) * AMOUNT_INCREMENT)
Log("To be purchased " + Coin + " Quantity:" + ToBeBought)
exchange.Buy(SellWeighted,ToBeBought)
}
}
Log(Fiat + " Quantity:" + Account.Balance)
Log(Coin + " Quantity:" + Account.Stocks)
RefreshLoop = RefreshLoop+1
if (RefreshLoop>60) {
var Profit = _N(Account.Stocks*Sell) + _N(Account.Balance) - _N(InitialBalance)
LogProfit(Profit)
RefreshLoop = 0
}
}
function main(){
var Pair = exchange.GetCurrency()
LogReset()
LogProfitReset()
LogProfit(0)
UpdateAll()
CancelPendingOrders()
Coin = Pair.split("_")[0]
Fiat = Pair.split("_")[1]
while (true){
try {
onTick()
} catch (err) {
Log(err)
}
Sleep(DELAY*1000)
}
}Published by FawkesPan on the FMZ strategy square · last modified 2018-09-18. 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/Dynamic-Balance-Market-Making-Strategy.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
- Simple Bitcoin High Frequency Strategy Bot from 2014JavaScript · Market making
- Market Making Strategy of Grabbing the Market Position High Frequency Approaching TypeJavaScript · Market making
- Market Making Strategy of Seizing the Market CounterattackJavaScript · Market making