| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- const OneTask = require('../libs/one-task')
- const OneInch = require('../libs/web3/1inch')
- const BinanceSpot = require('../libs/binance/binance-spot')
- const Config = require('../config/config')
- const NumKit = require('../kit/num-kit')
- const TimeKit = require('../kit/time-kit')
- const TableKit = require('../kit/table-kit')
- const orderHandler = function(context, task) {
- const fileLogger = task.fileLogger
- const tokenMap = context.tokenMap
- // 逐对处理交易信息
- Object.keys(tokenMap).forEach((tokenHash) => {
- const token = tokenMap[tokenHash]
- const pair = token.exchange.pair
- // 价格非法判定
- if (!token.BinancePrice || !token.OneInchPrice) return
- // 卖出逻辑判定
- if (token.orderPrice) {
- // TODO 对接卖出交易
- if (token.BinancePrice > token.orderPrice) {
- // 止盈逻辑
- const isStopWin = token.BinancePrice > (token.orderPrice * (100 + Config.stopWinPercentage) / 100)
- if (isStopWin) {
- fileLogger.info(`[止盈]${pair}, price: ${token.orderPrice}->${token.BinancePrice}, amount: ${token.orderAmount}.`)
- token.orderPrice = undefined
- token.prevHandleTime = new Date().getTime()
- }
- } else {
- // 止损逻辑
- const isStopLoss = token.BinancePrice < (token.orderPrice * (100 - Config.stopLossPercentage) / 100)
- if (isStopLoss) {
- fileLogger.info(`[止损]${pair}, price: ${token.orderPrice}->${token.BinancePrice}, amount: ${token.orderAmount}.`)
- token.orderPrice = undefined
- token.prevHandleTime = new Date().getTime()
- }
- }
- } else if (token.isLong) {
- // TODO 对接买入交易
- token.prevHandleTime = new Date().getTime()
- token.orderPrice = token.BinancePrice
- token.orderBaseAmount = Config.baseTokenAmount
- token.orderAmount = NumKit.getSubFloat(Config.baseTokenAmount / token.orderPrice, token.exchange.priceTick)
- fileLogger.info(`[订单]${pair}, volume: ${Config.baseTokenAmount}, price: ${token.orderPrice}, amount: ${token.orderAmount}.`)
- }
- })
- }
- const table = new TableKit(['pair', 'binance', 'order price', 'profit', 'prev time'], 20)
- const pricesHandler = function(context, task) {
- const logger = task.logger
- const fileLogger = task.fileLogger
- const tokenMap = context.tokenMap
- table.printTitles()
- Object.keys(tokenMap).forEach((tokenHash) => {
- const token = tokenMap[tokenHash]
- const pair = token.exchange.pair
- const OneInchPrice = token.OneInchPrice ? token.OneInchPrice : NaN
- 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 : NaN
- const orderPercentage = NumKit.getSubFloat(((BinancePrice - orderPrice) / orderPrice) * 100, 2)
- const prevTimeStr = token.prevHandleTime ? TimeKit.getTimeByMillisecond(token.prevHandleTime) : ''
- // 价格非法的就不处理了
- // if ((() => {
- //
- // })()) return
- const rows = [pair.toString(), BinancePrice.toString(), orderPrice.toString(), orderPercentage.toString().concat('%'), prevTimeStr]
- // 识别到在拉盘
- token.isLong = (() => {
- const percentageCondition = percentage > Config.percentageLimit
- const isNoPrevHandleTime = !token.prevHandleTime
- const isTimeCover = new Date().getTime() > token.prevHandleTime + Config.handleSpaceHours * (60 * 60 * 1000)
- const timeCondition = isNoPrevHandleTime || isTimeCover
- return percentageCondition && timeCondition
- })()
- // 打印表格
- table.showLine(rows, undefined)
- })
- table.printEndLine(logger)
- }
- const onTickFun = async function() {
- const task = this
- const context = task.context
- const tokenMap = context.tokenMap
- const tokenContractAddressList = Object.keys(tokenMap)
- // 搜集所有价格数据
- for (const tokenHash of tokenContractAddressList) {
- const toToken = tokenMap[tokenHash]
- const priceTick = toToken.exchange.priceTick
- const fromIerc20Token = Config.baseIerc20Token
- const amount = Config.baseTokenAmount
- const OneInchPrice = NumKit.getSubFloat(await OneInch.price(fromIerc20Token.contract, tokenHash, amount), priceTick)
- const BinancePrice = NumKit.getSubFloat(await BinanceSpot.realPrice(toToken.exchange.pair), priceTick)
- toToken.OneInchPrice = OneInchPrice
- toToken.BinancePrice = BinancePrice
- toToken.DiffPrice = BinancePrice - OneInchPrice
- if ((() => {
- return Math.abs(toToken.DiffPrice) < Math.pow(10, -priceTick)
- })()) {
- toToken.DiffPrice = 0
- } {
- toToken.DiffPrice = NumKit.getSubFloat(toToken.DiffPrice, priceTick)
- }
- }
- // 价格处理帧
- pricesHandler(context, task)
- // 交易、订单处理
- orderHandler(context, task)
- }
- const onePro = new OneTask('OnePro', Config.delay, OneTask.baseInit, onTickFun)
- onePro.Start()
|