one-pro.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. fileLogger.info(`[止盈]${pair}, price: ${token.orderPrice}->${token.BinancePrice}, amount: ${token.orderAmount}.`)
  21. token.orderPrice = undefined
  22. }
  23. } else {
  24. // 止损逻辑
  25. const isStopLoss = token.BinancePrice < (token.orderPrice * (100 - Config.stopLossPercentage) / 100)
  26. if (isStopLoss) {
  27. fileLogger.info(`[止损]${pair}, price: ${token.orderPrice}->${token.BinancePrice}, amount: ${token.orderAmount}.`)
  28. token.orderPrice = undefined
  29. }
  30. }
  31. } else if (token.isLong) {
  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', 'profit'], 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 : NaN
  54. const orderPercentage = NumKit.getSubFloat(((BinancePrice - orderPrice) / orderPrice) * 100, 2)
  55. // 价格非法的就不处理了
  56. // if ((() => {
  57. //
  58. // })()) return
  59. const rows = [pair.toString(), BinancePrice.toString(), orderPrice.toString(), orderPercentage.toString().concat('%')]
  60. // 识别到在拉盘
  61. token.isLong = (() => {
  62. const percentageCondition = percentage > Config.percentageLimit
  63. const isNoPrevHandleTime = !token.prevHandleTime
  64. const isTimeCover = new Date().getTime() > token.prevHandleTime + Config.handleSpaceHours * (60 * 60 * 1000)
  65. const timeCondition = isNoPrevHandleTime || isTimeCover
  66. return percentageCondition && timeCondition
  67. })()
  68. if (token.isLong) {
  69. token.prevHandleTime = new Date().getTime()
  70. }
  71. // 打印表格
  72. table.showLine(rows, undefined)
  73. })
  74. table.printEndLine(logger)
  75. }
  76. const onTickFun = async function() {
  77. const task = this
  78. const context = task.context
  79. const tokenMap = context.tokenMap
  80. const tokenContractAddressList = Object.keys(tokenMap)
  81. // 搜集所有价格数据
  82. for (const tokenHash of tokenContractAddressList) {
  83. const toToken = tokenMap[tokenHash]
  84. const priceTick = toToken.exchange.priceTick
  85. const fromIerc20Token = Config.baseIerc20Token
  86. const amount = Config.baseTokenAmount
  87. const OneInchPrice = NumKit.getSubFloat(await OneInch.price(fromIerc20Token.contract, tokenHash, amount), priceTick)
  88. const BinancePrice = NumKit.getSubFloat(await BinanceSpot.realPrice(toToken.exchange.pair), priceTick)
  89. toToken.OneInchPrice = OneInchPrice
  90. toToken.BinancePrice = BinancePrice
  91. toToken.DiffPrice = BinancePrice - OneInchPrice
  92. if ((() => {
  93. return Math.abs(toToken.DiffPrice) < Math.pow(10, -priceTick)
  94. })()) {
  95. toToken.DiffPrice = 0
  96. } {
  97. toToken.DiffPrice = NumKit.getSubFloat(toToken.DiffPrice, priceTick)
  98. }
  99. }
  100. // 价格处理帧
  101. pricesHandler(context, task)
  102. // 交易、订单处理
  103. orderHandler(context, task)
  104. }
  105. const onePro = new OneTask('OnePro', Config.delay, OneTask.baseInit, onTickFun)
  106. onePro.Start()