瀏覽代碼

优化余额获取逻辑、新增余额显示

龚成明 2 年之前
父節點
當前提交
2679c09002
共有 3 個文件被更改,包括 32 次插入9 次删除
  1. 1 3
      kit/table-kit.js
  2. 12 4
      libs/binance/test/binance-spot-test.js
  3. 19 2
      scripts/one-pro.js

+ 1 - 3
kit/table-kit.js

@@ -51,9 +51,7 @@ module.exports = class TableKit {
     if (fileLogger) fileLogger.info(infoLine)
   }
 
-  printEndLine(logger=undefined) {
+  printEndLine() {
     console.info(this.splitLine)
-
-    if (logger) logger.info('')
   }
 }

+ 12 - 4
libs/binance/test/binance-spot-test.js

@@ -35,15 +35,23 @@ async function orderTest(context) {
 
   const binanceSpot = context.binanceSpot
 
-  const orderRst = await binanceSpot.buy('DOGEBUSD', -1, 15)
+  const orderRst = await binanceSpot.buy('HOOKBUSD', -1, Config.baseTokenAmount)
+  logger.info(orderRst)
 
   if (orderRst.executedQty) {
     const cummulativeQuoteQty = orderRst.cummulativeQuoteQty
-    const executedQty = orderRst.executedQty
+    let executedQty = 0
+    for (const fill of orderRst.fills) {
+      executedQty += parseFloat(fill.qty)
+      // 扣除支付的手续费
+      if (fill.commissionAsset === 'HOOK') {
+        executedQty -= parseFloat(fill.commission)
+      }
+    }
     const price = cummulativeQuoteQty / executedQty
 
     logger.info(`消耗${Config.baseIerc20Token.symbol} ${cummulativeQuoteQty}, 买入${executedQty}, 均价${price}.`)
-    logger.info(await binanceSpot.sell('DOGEBUSD', -1, executedQty))
+    logger.info(await binanceSpot.sell('HOOKBUSD', -1, executedQty))
   } else {
     logger.error(orderRst)
   }
@@ -57,7 +65,7 @@ async function main() {
   // await realPriceTest()
   // await exchangeInfoTest()
   // await accountInfoTest(context)
-  // await orderTest(context)
+  await orderTest(context)
 }
 
 main()

+ 19 - 2
scripts/one-pro.js

@@ -21,8 +21,11 @@ const orderHandler = async function(context, task) {
     // 价格非法判定
     if (!token.BinancePrice || !token.OneInchPrice) continue;
 
+    // 更新token的实际余额
+    token.orderAmount = NumKit.getSubFloat(accountAssetMap[token.exchange.symbol], token.exchange.lotSize)
+
     // 卖出逻辑判定
-    if (token.orderPrice) {
+    if (token.orderPrice && token.orderAmount > Math.pow(10, -token.exchange.lotSize)) {
       if (token.BinancePrice > token.orderPrice) {
         // 止盈逻辑
         const isStopWin = token.BinancePrice > (token.orderPrice * (100 + Config.stopWinPercentage) / 100)
@@ -83,7 +86,16 @@ const orderHandler = async function(context, task) {
 
       if (buyRst.executedQty) {
         const cummulativeQuoteQty = NumKit.getSubFloat(buyRst.cummulativeQuoteQty, 6)
-        const executedQty = NumKit.getSubFloat(buyRst.executedQty, token.exchange.lotSize)
+        // 计算扣除手续费后的实际成交数量
+        let executedQty = 0
+        for (const fill of orderRst.fills) {
+          executedQty += parseFloat(fill.qty)
+          // 扣除支付的手续费
+          if (fill.commissionAsset === 'HOOK') {
+            executedQty -= parseFloat(fill.commission)
+          }
+        }
+        executedQty = NumKit.getSubFloat(executedQty, token.exchange.lotSize)
 
         token.orderAmount = executedQty
         token.orderPrice = cummulativeQuoteQty / executedQty
@@ -102,6 +114,7 @@ const table = new TableKit(['pair', '1inch', 'binance', 'order price', 'profit(%
 const showInfo = function(context, task) {
   const logger = task.logger
   const tokenMap = context.tokenMap
+  const accountAssetMap = context.accountAssetMap
 
   table.printTitles()
 
@@ -139,6 +152,10 @@ const showInfo = function(context, task) {
     table.showLine(rows, undefined)
   })
   table.printEndLine(logger)
+
+  logger.info(`base asset amount: ${accountAssetMap[Config.baseIerc20Token.symbol]}.`)
+  logger.info('')
+
 }
 
 const priceHandler = async function(context, task) {