Bläddra i källkod

整理代码逻辑

龚成明 2 år sedan
förälder
incheckning
d37f899405
1 ändrade filer med 15 tillägg och 10 borttagningar
  1. 15 10
      scripts/one-pro.js

+ 15 - 10
scripts/one-pro.js

@@ -41,21 +41,22 @@ const orderHandler = function(context, task) {
     } else if (token.isLong) {
       // TODO 对接买入交易
       token.prevHandleTime = new Date().getTime()
-      token.orderPrice = token.BinancePrice
       token.orderBaseAmount = Config.baseTokenAmount
 
+      // 1. 获取base资产数量
+      // 2. 下单获取成交数量以及平均价格
+      token.orderPrice = token.BinancePrice
       token.orderAmount = NumKit.getSubFloat(Config.baseTokenAmount / token.orderPrice, token.exchange.priceTick)
 
+      // 3. 下单成功后打印日志
       fileLogger.info(`[订单]${pair}, volume: ${Config.baseTokenAmount}, price: ${token.orderPrice}, amount: ${token.orderAmount}.`)
     }
   })
 }
 
 const table = new TableKit(['pair', 'binance', 'order price', 'profit', 'prev time'], 20)
-
-const pricesHandler = function(context, task) {
+const showInfo = function(context, task) {
   const logger = task.logger
-  const fileLogger = task.fileLogger
   const tokenMap = context.tokenMap
 
   table.printTitles()
@@ -95,13 +96,10 @@ const pricesHandler = function(context, task) {
   table.printEndLine(logger)
 }
 
-const onTickFun = async function() {
-  const task = this
-  const context = task.context
+const priceHandler = async function(context, task) {
   const tokenMap = context.tokenMap
   const tokenContractAddressList = Object.keys(tokenMap)
 
-  // 搜集所有价格数据
   for (const tokenHash of tokenContractAddressList) {
     const toToken = tokenMap[tokenHash]
     const priceTick = toToken.exchange.priceTick
@@ -123,9 +121,16 @@ const onTickFun = async function() {
       toToken.DiffPrice = NumKit.getSubFloat(toToken.DiffPrice, priceTick)
     }
   }
+}
 
-  // 价格处理帧
-  pricesHandler(context, task)
+const onTickFun = async function() {
+  const task = this
+  const context = task.context
+
+  // 搜集所有价格数据
+  await priceHandler(context, task)
+  // 展示关键信息
+  showInfo(context, task)
   // 交易、订单处理
   orderHandler(context, task)
 }