one-pro.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const OneTask = require('../libs/one-task')
  2. const OneInch = require('../libs/web3/1inch')
  3. const BinanceSpot = require('../libs/binance/binance-spot')
  4. const Config = require('../config/config')
  5. const NumKit = require('../kit/num-kit')
  6. const TimeKit = require('../kit/time-kit')
  7. const TableKit = require('../kit/table-kit')
  8. const orderHandler = function(context, task) {
  9. const fileLogger = task.fileLogger
  10. const tokenMap = context.tokenMap
  11. // 逐对处理交易信息
  12. Object.keys(tokenMap).forEach((tokenHash) => {
  13. const token = tokenMap[tokenHash]
  14. const pair = token.exchange.pair
  15. if (token.orderPrice) {
  16. // TODO 对接卖出交易
  17. if (token.BinancePrice > token.orderPrice) {
  18. // 止盈逻辑
  19. const isStopWin = token.BinancePrice > (token.orderPrice * (100 + Config.stopWinPercentage) / 100)
  20. if (isStopWin) {
  21. fileLogger.info(`[止盈]${pair}, price: ${token.orderPrice}->${token.BinancePrice}, amount: ${token.orderAmount}.`)
  22. token.orderPrice = undefined
  23. token.prevHandleTime = new Date().getTime()
  24. }
  25. } else {
  26. // 止损逻辑
  27. const isStopLoss = token.BinancePrice < (token.orderPrice * (100 - Config.stopLossPercentage) / 100)
  28. if (isStopLoss) {
  29. fileLogger.info(`[止损]${pair}, price: ${token.orderPrice}->${token.BinancePrice}, amount: ${token.orderAmount}.`)
  30. token.orderPrice = undefined
  31. token.prevHandleTime = new Date().getTime()
  32. }
  33. }
  34. } else if (token.isLong) {
  35. // TODO 对接买入交易
  36. token.prevHandleTime = new Date().getTime()
  37. token.orderPrice = token.BinancePrice
  38. token.orderBaseAmount = Config.baseTokenAmount
  39. token.orderAmount = NaN
  40. fileLogger.info(`[订单]${pair}, price: ${token.orderPrice}, amount: ${token.orderAmount}.`)
  41. }
  42. })
  43. }
  44. const table = new TableKit(['pair', 'binance', 'order price', 'profit', 'prev time'], 20)
  45. const pricesHandler = function(context, task) {
  46. const logger = task.logger
  47. const fileLogger = task.fileLogger
  48. const tokenMap = context.tokenMap
  49. table.printTitles()
  50. Object.keys(tokenMap).forEach((tokenHash) => {
  51. const token = tokenMap[tokenHash]
  52. const pair = token.exchange.pair
  53. const OneInchPrice = token.OneInchPrice ? token.OneInchPrice : NaN
  54. const BinancePrice = token.BinancePrice ? token.BinancePrice : NaN
  55. const DiffPrice = token.DiffPrice
  56. const percentage = NumKit.getSubFloat((DiffPrice / OneInchPrice) * 100, 2)
  57. const orderPrice = token.orderPrice ? token.orderPrice : NaN
  58. const orderPercentage = NumKit.getSubFloat(((BinancePrice - orderPrice) / orderPrice) * 100, 2)
  59. const prevTimeStr = token.prevHandleTime ? TimeKit.getTimeByMillisecond(token.prevHandleTime) : ''
  60. // 价格非法的就不处理了
  61. // if ((() => {
  62. //
  63. // })()) return
  64. const rows = [pair.toString(), BinancePrice.toString(), orderPrice.toString(), orderPercentage.toString().concat('%'), prevTimeStr]
  65. // 识别到在拉盘
  66. token.isLong = (() => {
  67. const percentageCondition = percentage > Config.percentageLimit
  68. const isNoPrevHandleTime = !token.prevHandleTime
  69. const isTimeCover = new Date().getTime() > token.prevHandleTime + Config.handleSpaceHours * (60 * 60 * 1000)
  70. const timeCondition = isNoPrevHandleTime || isTimeCover
  71. return percentageCondition && timeCondition
  72. })()
  73. // 打印表格
  74. table.showLine(rows, undefined)
  75. })
  76. table.printEndLine(logger)
  77. }
  78. const onTickFun = async function() {
  79. const task = this
  80. const context = task.context
  81. const tokenMap = context.tokenMap
  82. const tokenContractAddressList = Object.keys(tokenMap)
  83. // 搜集所有价格数据
  84. for (const tokenHash of tokenContractAddressList) {
  85. const toToken = tokenMap[tokenHash]
  86. const priceTick = toToken.exchange.priceTick
  87. const fromIerc20Token = Config.baseIerc20Token
  88. const amount = Config.baseTokenAmount
  89. const OneInchPrice = NumKit.getSubFloat(await OneInch.price(fromIerc20Token.contract, tokenHash, amount), priceTick)
  90. const BinancePrice = NumKit.getSubFloat(await BinanceSpot.realPrice(toToken.exchange.pair), priceTick)
  91. toToken.OneInchPrice = OneInchPrice
  92. toToken.BinancePrice = BinancePrice
  93. toToken.DiffPrice = BinancePrice - OneInchPrice
  94. if ((() => {
  95. return Math.abs(toToken.DiffPrice) < Math.pow(10, -priceTick)
  96. })()) {
  97. toToken.DiffPrice = 0
  98. } {
  99. toToken.DiffPrice = NumKit.getSubFloat(toToken.DiffPrice, priceTick)
  100. }
  101. }
  102. // 价格处理帧
  103. pricesHandler(context, task)
  104. // 交易、订单处理
  105. orderHandler(context, task)
  106. }
  107. const onePro = new OneTask('OnePro', Config.delay, OneTask.baseInit, onTickFun)
  108. onePro.Start()