|
|
@@ -1,8 +1,6 @@
|
|
|
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 PrivateConfig = require('../PrivateConfig')
|
|
|
const NumKit = require('../kit/num-kit')
|
|
|
const TimeKit = require('../kit/time-kit')
|
|
|
const TableKit = require('../kit/table-kit')
|
|
|
@@ -10,13 +8,14 @@ const BinanceKit = require("../libs/binance/binance-kit");
|
|
|
const assert = require("assert");
|
|
|
|
|
|
const holdingHandler = async function(context, task, token, pair) {
|
|
|
+ const config = context.config
|
|
|
const fileLogger = task.fileLogger
|
|
|
const binanceSpot = context.binanceSpot
|
|
|
const accountAssetMap = context.accountAssetMap
|
|
|
|
|
|
// 这两个逻辑只有可能满足一个
|
|
|
- let isStopWin = token.BinancePrice > (token.orderPrice * (100 + PrivateConfig.stopWinPercentage) / 100)
|
|
|
- let isStopLoss = token.BinancePrice < (token.orderPrice * (100 - PrivateConfig.stopLossPercentage) / 100)
|
|
|
+ let isStopWin = token.BinancePrice > (token.orderPrice * (100 + config.stopWinPercentage) / 100)
|
|
|
+ let isStopLoss = token.BinancePrice < (token.orderPrice * (100 - config.stopLossPercentage) / 100)
|
|
|
|
|
|
assert.notEqual(isStopWin && isStopLoss, true, '止盈止损逻辑错误,请认真检查。')
|
|
|
|
|
|
@@ -25,7 +24,7 @@ const holdingHandler = async function(context, task, token, pair) {
|
|
|
const orderAmount = token.orderAmount
|
|
|
|
|
|
token.orderPrice = undefined
|
|
|
- token.nextHandleTime = new Date().getTime() + PrivateConfig.stopLossHandleSpaceHours * (60 * 60 * 1000)
|
|
|
+ token.nextHandleTime = new Date().getTime() + config.stopLossHandleSpaceHours * (60 * 60 * 1000)
|
|
|
|
|
|
try {
|
|
|
const sellMarketRst = await binanceSpot.sellMarket(pair, orderAmount)
|
|
|
@@ -43,24 +42,25 @@ const holdingHandler = async function(context, task, token, pair) {
|
|
|
}
|
|
|
|
|
|
const noHoldingHandler = async function(context, task, token, pair) {
|
|
|
+ const config = context.config
|
|
|
const fileLogger = task.fileLogger
|
|
|
const binanceSpot = context.binanceSpot
|
|
|
const accountAssetMap = context.accountAssetMap
|
|
|
|
|
|
// 1. 获取base资产数量
|
|
|
- const baseAssetAmount = accountAssetMap[Config.baseIerc20Token.symbol]
|
|
|
+ const baseAssetAmount = accountAssetMap[config.baseIerc20Token.symbol]
|
|
|
|
|
|
// 2. 判断余额是否够下单
|
|
|
- if (PrivateConfig.baseTokenAmount > baseAssetAmount) {
|
|
|
- fileLogger.info(`[余额不足]${pair}, 需要: ${PrivateConfig.baseTokenAmount}, 剩余: ${baseAssetAmount}.`)
|
|
|
- token.nextHandleTime = new Date().getTime() + PrivateConfig.stopLossHandleSpaceHours * (60 * 60 * 1000)
|
|
|
+ if (config.baseTokenAmount > baseAssetAmount) {
|
|
|
+ fileLogger.info(`[余额不足]${pair}, 需要: ${config.baseTokenAmount}, 剩余: ${baseAssetAmount}.`)
|
|
|
+ token.nextHandleTime = new Date().getTime() + config.stopLossHandleSpaceHours * (60 * 60 * 1000)
|
|
|
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 3. 下单获取成交信息
|
|
|
try {
|
|
|
- const buyMarketRst = await binanceSpot.buyMarket(pair, PrivateConfig.baseTokenAmount)
|
|
|
+ const buyMarketRst = await binanceSpot.buyMarket(pair, config.baseTokenAmount)
|
|
|
|
|
|
token.cummulativeQuoteQty = NumKit.getSubFloat(buyMarketRst.cummulativeQuoteQty, 6)
|
|
|
token.orderAmount = NumKit.getSubFloat(buyMarketRst.executedQty, token.exchange.lotSize)
|
|
|
@@ -68,7 +68,7 @@ const noHoldingHandler = async function(context, task, token, pair) {
|
|
|
|
|
|
fileLogger.info(`[单]${pair}, 成交额${token.cummulativeQuoteQty}, 买入${token.orderAmount}个, 均价${token.orderPrice}.`)
|
|
|
|
|
|
- accountAssetMap[Config.baseIerc20Token.symbol] -= token.cummulativeQuoteQty
|
|
|
+ accountAssetMap[config.baseIerc20Token.symbol] -= token.cummulativeQuoteQty
|
|
|
} catch (e) {
|
|
|
fileLogger.error(`[下单失败]${pair}, ${e}`)
|
|
|
}
|
|
|
@@ -99,6 +99,7 @@ const tradeLogic = async function(context, task) {
|
|
|
|
|
|
const table = new TableKit(['pair', '1inch', 'binance', 'order price', 'profit(%)', 'next time'], 20)
|
|
|
const showInfo = function(context, task) {
|
|
|
+ const config = context.config
|
|
|
const logger = task.logger
|
|
|
const tokenMap = context.tokenMap
|
|
|
const accountAssetMap = context.accountAssetMap
|
|
|
@@ -126,7 +127,7 @@ const showInfo = function(context, task) {
|
|
|
|
|
|
// 识别到在拉盘
|
|
|
token.isLong = (() => {
|
|
|
- const percentageCondition = percentage > PrivateConfig.percentageLimit
|
|
|
+ const percentageCondition = percentage > config.percentageLimit
|
|
|
|
|
|
const isNoPrevHandleTime = !token.nextHandleTime
|
|
|
const isTimeCover = new Date().getTime() > token.nextHandleTime
|
|
|
@@ -140,20 +141,21 @@ const showInfo = function(context, task) {
|
|
|
})
|
|
|
table.printEndLine(logger)
|
|
|
|
|
|
- logger.info(`${Config.baseIerc20Token.symbol} asset amount: ${accountAssetMap[Config.baseIerc20Token.symbol]}.`)
|
|
|
+ logger.info(`${config.baseIerc20Token.symbol} asset amount: ${accountAssetMap[config.baseIerc20Token.symbol]}.`)
|
|
|
logger.info('')
|
|
|
|
|
|
}
|
|
|
|
|
|
const priceHandler = async function(context, task) {
|
|
|
+ const config = context.config
|
|
|
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 = PrivateConfig.baseTokenAmount
|
|
|
+ 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)
|
|
|
@@ -191,6 +193,8 @@ const onTickFun = async function() {
|
|
|
await tradeLogic(context, task)
|
|
|
}
|
|
|
|
|
|
-const onePro = new OneTask('OnePro', PrivateConfig.delay, OneTask.baseInit, onTickFun)
|
|
|
+const onePro = new OneTask('OnePro', OneTask.baseInit, onTickFun)
|
|
|
|
|
|
-onePro.Start()
|
|
|
+onePro.setDelayTime(onePro.context.config.delay)
|
|
|
+
|
|
|
+onePro.Start().then(() => {})
|