one-pro.js 4.2 KB

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