@@ -20,6 +20,7 @@ export type GridBotConfig = {
2020 gridQuantity : number ;
2121 balanceQuote : number ;
2222 fee : number ;
23+ precision ?: number ;
2324} ;
2425
2526type Grid = {
@@ -33,37 +34,49 @@ type Grid = {
3334export class GridBot {
3435 balanceQuote : number ;
3536 balanceAsset : number ;
36- quotePerGrid : number ;
37+ assetPerGrid : number ;
3738 // eslint-disable-next-line @typescript-eslint/no-explicit-any
3839 orderHistory : any [ ] ;
3940 grids : Grid [ ] ;
4041 exchange : SandExchange ;
4142 syncedOrders : Set < number > ;
4243 private readonly fee : number ;
44+ private readonly precision : number ;
4345
4446 constructor ( config : GridBotConfig ) {
4547 this . orderHistory = [ ] ;
4648 this . syncedOrders = new Set ( ) ;
4749 this . balanceQuote = config . balanceQuote ;
4850 this . balanceAsset = 0 ;
4951 this . fee = config . fee ;
52+ this . precision = config . precision || DEFAULT_PRECISION ;
5053
5154 this . exchange = new SandExchange ( {
5255 balanceAsset : this . balanceAsset ,
5356 balanceQuote : this . balanceQuote ,
5457 fee : this . fee ,
58+ precision : this . precision ,
5559 } ) ;
5660
57- // Get last money partition ignore round-robin parts
58- this . quotePerGrid = this . balanceQuote / config . gridQuantity ;
61+ const pricePerStep = ( config . priceHigh - config . priceLow ) / config . gridQuantity ;
62+ const totalPriceStep = ( config . gridQuantity ** 2 + config . gridQuantity ) / 2 ; // n * n + n / 2
63+
64+ this . assetPerGrid = this . balanceQuote / ( config . gridQuantity * config . priceLow + totalPriceStep * pricePerStep ) ;
65+ this . assetPerGrid = floor ( this . assetPerGrid , DEFAULT_PRECISION ) ;
5966
6067 this . grids = [ ...Array ( config . gridQuantity ) ] . map ( ( _ , i ) => {
6168 const priceLow = config . priceLow + ( ( config . priceHigh - config . priceLow ) / config . gridQuantity ) * i ;
6269 const priceHigh = config . priceLow + ( ( config . priceHigh - config . priceLow ) / config . gridQuantity ) * ( i + 1 ) ;
63- const maxQuantity = floor ( this . quotePerGrid / priceLow , DEFAULT_PRECISION ) ;
70+
6471 const ownedQuantity = 0 ;
6572
66- return { priceLow, priceHigh, maxQuantity, ownedQuantity, activeOrderId : null } ;
73+ return {
74+ priceLow,
75+ priceHigh,
76+ maxQuantity : this . assetPerGrid ,
77+ ownedQuantity,
78+ activeOrderId : null ,
79+ } ;
6780 } ) ;
6881 }
6982
@@ -82,7 +95,7 @@ export class GridBot {
8295 const grindIndex = this . grids . findIndex ( grid => grid . activeOrderId === order . orderId ) ;
8396
8497 if ( order . side === OrderSide . BUY ) {
85- const bookedQuantity = order . executedQty * ( 1 - this . fee ) ;
98+ const bookedQuantity = floor ( order . executedQty * ( 1 - this . fee ) , this . precision ) ;
8699 this . balanceAsset += bookedQuantity ;
87100 this . balanceQuote -= order . executedQty * order . price ;
88101
@@ -102,6 +115,7 @@ export class GridBot {
102115 } ;
103116 }
104117
118+ this . orderHistory . push ( order ) ;
105119 this . syncedOrders . add ( order . orderId ) ;
106120 }
107121 } ) ;
0 commit comments