소스 검색

虚拟交易逻辑

龚成明 2 년 전
부모
커밋
688ebc8afd
2개의 변경된 파일36개의 추가작업 그리고 5개의 파일을 삭제
  1. 6 2
      config/config.js
  2. 30 3
      scripts/one-pro.js

+ 6 - 2
config/config.js

@@ -1,11 +1,15 @@
 class Config {}
 
-// 至少需要多少利润百分比才会触发
+// 至少需要多少差价百分比才会触发
 Config.percentageLimit = 0.25
 // 至少间隔多少小时才触发一次
 Config.handleSpaceHours = 4
+// 止盈百分比
+Config.stopWinPercentage = 2
+// 止损百分比
+Config.stopLossPercentage = 2
 // 打算售出的baseToken数量
-Config.baseTokenAmount = 500
+Config.baseTokenAmount = 50
 // 轮询时间
 Config.delay = 5000
 // 限制源

+ 30 - 3
scripts/one-pro.js

@@ -13,10 +13,36 @@ const orderHandler = function(context, task) {
   Object.keys(tokenMap).forEach((tokenHash) => {
     const token = tokenMap[tokenHash]
     const pair = token.exchange.pair
+
+    if (token.orderPrice) {
+      // TODO 对接卖出交易
+      if (token.BinancePrice > token.orderPrice) {
+        // 止盈逻辑
+        const isStopWin = token.BinancePrice > (token.orderPrice * (100 + Config.stopWinPercentage) / 100)
+        if (isStopWin) {
+          token.orderPrice = undefined
+          fileLogger.info(`[止盈]${pair}, price: ${token.orderPrice}, amount: ${token.orderAmount}.`)
+        }
+      } else {
+        // 止损逻辑
+        const isStopLoss = token.BinancePrice < (token.orderPrice * (100 - Config.stopLossPercentage) / 100)
+        if (isStopLoss) {
+          token.orderPrice = undefined
+          fileLogger.info(`[止损]${pair}, price: ${token.orderPrice}, amount: ${token.orderAmount}.`)
+        }
+      }
+    } else {
+      // TODO 对接买入交易
+      token.orderPrice = token.BinancePrice
+      token.orderBaseAmount = Config.baseTokenAmount
+      token.orderAmount = NaN
+
+      fileLogger.info(`[订单]${pair}, price: ${token.orderPrice}, amount: ${token.orderAmount}.`)
+    }
   })
 }
 
-const table = new TableKit(['pair', 'binance'], 25)
+const table = new TableKit(['pair', 'binance', 'order price'], 25)
 
 const pricesHandler = function(context, task) {
   const logger = task.logger
@@ -32,13 +58,14 @@ const pricesHandler = function(context, task) {
     const BinancePrice = token.BinancePrice ? token.BinancePrice : NaN
     const DiffPrice = token.DiffPrice
     const percentage = NumKit.getSubFloat((DiffPrice / OneInchPrice) * 100, 2)
+    const orderPrice = token.orderPrice ? token.orderPrice : 0
 
     // 价格非法的就不处理了
     // if ((() => {
     //
     // })()) return
 
-    const rows = [pair.toString(), BinancePrice.toString()]
+    const rows = [pair.toString(), BinancePrice.toString(), orderPrice]
 
     // 识别到在拉盘
     token.isLong = (() => {
@@ -55,7 +82,7 @@ const pricesHandler = function(context, task) {
       token.prevHandleTime = new Date().getTime()
     }
     // 打印表格
-    table.showLine(rows, token.isLong ? fileLogger : undefined)
+    table.showLine(rows, undefined)
   })
   table.printEndLine(logger)
 }