Procházet zdrojové kódy

交易信息持久化

龚成明 před 2 roky
rodič
revize
4763553e5b
4 změnil soubory, kde provedl 66 přidání a 3 odebrání
  1. 13 2
      libs/test/token-test.js
  2. 36 1
      libs/token.js
  3. 7 0
      scripts/one-pro.js
  4. 10 0
      test/level-db-test.js

+ 13 - 2
libs/test/token-test.js

@@ -8,8 +8,19 @@ const BinanceSpot = require('../binance/binance-spot')
 
 describe('token-test', () => {
   const context = new Context()
+  const token = new Token('DOGE', 5, 0, 'DOGEBURGER', 18)
 
-  it('normal-test', () => {
-    logger.info('normal')
+  it('save-test', async () => {
+    token.orderAmount = 1000
+    token.orderPrice = 0.001
+    token.nextHandleTime = 1
+
+    await token.save()
+  })
+
+  it('load-test', async () => {
+    await token.load()
+
+    logger.info(token)
   })
 })

+ 36 - 1
libs/token.js

@@ -1,9 +1,15 @@
 const IERC20 = require("./web3/ierc20-token");
+const { Level } = require("level");
+const db = new Level('app.db', { valueEncoding: 'json' })
 
 module.exports = class Token {
   exchange = {}
   ierc20 = {}
 
+  orderPrice = 0
+  orderAmount = 0
+  nextHandleTime = 0
+
   constructor(exchangeSymbol, exchangePriceTick, exchangeLotSize, exchangePair, ierc20Decimals) {
     this.exchange.symbol = exchangeSymbol
     this.exchange.priceTick = exchangePriceTick
@@ -13,8 +19,30 @@ module.exports = class Token {
     this.ierc20.decimals = ierc20Decimals
   }
 
+  async save() {
+    const localData = {
+      orderPrice: this.orderPrice,
+      orderAmount: this.orderAmount,
+      nextHandleTime: this.nextHandleTime
+    }
+
+    await db.put(this.exchange.pair, JSON.stringify(localData))
+  }
+
+  async load() {
+    try {
+      const localDataJsonStr = await db.get(this.exchange.pair)
+      const localData = JSON.parse(localDataJsonStr)
+
+      this.orderAmount = localData.orderAmount
+      this.orderPrice = localData.orderPrice
+      this.nextHandleTime = localData.nextHandleTime
+    } catch (e) {}
+  }
+
   static async init(context, tokenHash, priceTickFilterMap, lotSizeFilterMap) {
     const config = context.config
+    const logger = context.logger
 
     // token初始化
     const ierc20Decimals = parseInt(await IERC20.getDecimals(tokenHash))
@@ -23,7 +51,14 @@ module.exports = class Token {
     const exchangePriceTick = priceTickFilterMap[exchangePair]
     const exchangeLotSize = lotSizeFilterMap[exchangePair]
 
-    context.tokenMap[tokenHash] = new Token(exchangeSymbol, exchangePriceTick, exchangeLotSize, exchangePair, ierc20Decimals)
+    const token = new Token(exchangeSymbol, exchangePriceTick, exchangeLotSize, exchangePair, ierc20Decimals)
+
+    // 载入时持久化
+    await token.load()
+
+    context.tokenMap[tokenHash] = token
+
+    logger.info(`${token.exchange.pair}初始化完毕,orderPrice:${token.orderPrice}, orderAmount:${token.orderAmount}.`)
   }
 
   static async batchInit(context, ierc20TokenAddressList, priceTickFilterMap, lotSizeFilterMap) {

+ 7 - 0
scripts/one-pro.js

@@ -25,6 +25,7 @@ const holdingHandler = async function(context, task, token, pair) {
     const orderAmount = token.orderAmount
 
     token.orderPrice = undefined
+    token.orderAmount = undefined
     token.nextHandleTime = new Date().getTime() + config.stopLossHandleSpaceHours * (60 * 60 * 1000)
 
     try {
@@ -33,6 +34,9 @@ const holdingHandler = async function(context, task, token, pair) {
       const price = NumKit.getSubFloat(sellMarketRst.avgPrice, token.exchange.priceTick)
       const cummulativeQuoteQty = NumKit.getSubFloat(sellMarketRst.cummulativeQuoteQty, 6)
 
+      // 交易数据持久化
+      await token.save()
+
       fileLogger.info(`[${isStopWin ? '盈+' : '损-'}]${pair}, `
         .concat(`成交额${cummulativeQuoteQty}, 均价${orderPrice}->${price}, 卖出${orderAmount}个.`)
       )
@@ -67,6 +71,9 @@ const noHoldingHandler = async function(context, task, token, pair) {
     token.orderAmount = NumKit.getSubFloat(buyMarketRst.executedQty, token.exchange.lotSize)
     token.orderPrice = NumKit.getSubFloat(buyMarketRst.avgPrice, token.exchange.priceTick)
 
+    // 关键数据持久化
+    await token.save()
+
     fileLogger.info(`[单]${pair}, 成交额${token.cummulativeQuoteQty}, 买入${token.orderAmount}个, 均价${token.orderPrice}.`)
 
     accountAssetMap[config.baseIerc20Token.symbol] -= token.cummulativeQuoteQty

+ 10 - 0
test/level-db-test.js

@@ -17,4 +17,14 @@ describe('level-db', () => {
     logger.info(`key:${key}, value:${value}, type:${typeof value}, rst type:${typeof rst}.`)
     assert.equal(rst, value, 'test failed.')
   })
+
+  it('get null', async () => {
+    const nullKey = 'null'
+
+    try {
+      logger.info(await db.get(nullKey))
+    } catch (e) {
+      logger.info(`不存在的键:${nullKey}.`)
+    }
+  })
 })