Skip to content

Commit

Permalink
feature:run success without mongodb
Browse files Browse the repository at this point in the history
fix:update config and save to file
  • Loading branch information
rainfu committed May 4, 2023
1 parent 247fa7f commit fe9831a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 35 deletions.
2 changes: 1 addition & 1 deletion extensions/output/panacea/api/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const updateConfig = (message, cb, s) => {
var target2 = require('path').resolve(__dirname, '../../../../data/config/last_config.json')
require('fs').writeFileSync(target2, JSON.stringify(newConfig, null, 2))
if (message.restart) {
console.log('\nSome Core Param changed .should restart process...'.orange)
debug.msg('\nSome Core Param changed .should restart process...'.cyan)
var target3 = require('path').resolve(__dirname, '../../../../data/pm2/restart_' + s.options.exchange + "_" + (s.options.name || '') + '.json')
// console.log('wrote config', message.restart, target3)
require('fs').writeFileSync(target3, JSON.stringify({ event: 'updateConfig', time: moment().format('MMDD HH:mm:ss') }, null, 2))
Expand Down
2 changes: 1 addition & 1 deletion extensions/output/panacea/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ module.exports = function panacea(s, conf, engine, keyboard) {
if (!this.wss) return
if (s.status.status !== 'work') return
if (!this.wss.clients.size) {
console.log("\nWebsocket no client".red)
debug.msg("\nWebsocket no client".red)
return
}
this.wss.clients.forEach(client => {
Expand Down
29 changes: 17 additions & 12 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,24 @@ module.exports = function core(s, conf, engine) {
s.status.status = 'finished'
}
var bots = collectionServiceInstance.getBots()
let bot = Object.assign(getBacktestData(s), {
id: tb(s.status.startTime).resize(s.options.period).toString(),
time: (new Date()).getTime()
})
// console.log('save bot', bot)
bots.replaceOne({ _id: bot.id }, bot, { upsert: true }, function (err) {
if (err) {
console.error('\n' + moment().format('YYYY-MM-DD HH:mm:ss') + ' - error saving bot')
console.error(err)
}
// console.log('save bot ok', s.status)
if (bots.err) {
debug.msg('Save bot error'.cyan + bots.message.red)
if (cb) cb()
})
} else {
let bot = Object.assign(getBacktestData(s), {
id: tb(s.status.startTime).resize(s.options.period).toString(),
time: (new Date()).getTime()
})
// console.log('save bot', bot)
bots.replaceOne({ _id: bot.id }, bot, { upsert: true }, function (err) {
if (err) {
console.error('\n' + moment().format('YYYY-MM-DD HH:mm:ss') + ' - error saving bot')
console.error(err)
}
// console.log('save bot ok', s.status)
if (cb) cb()
})
}
}
function buy(cb, symbol, position_side = 'LONG', size = null, is_taker = false) {
if (position_side === 'SHORT') {
Expand Down
4 changes: 2 additions & 2 deletions lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,8 @@ module.exports = function (s, conf) {
}
// console.log('executeSignal sell..', symbol.product_id, s.symbols[symbol.product_id].asset_amount, size, s.symbols[symbol.product_id].asset_amount_hold)
if (n(s.symbols[symbol.product_id].asset_amount).subtract(s.symbols[symbol.product_id].asset_amount_hold || 0).value() < n(size).value()) {
debug.msg('sell delayed: ' + formatPercent(n(s.symbols[symbol.product_id].asset_amount_hold || 0).divide(s.symbols[symbol.product_id].asset_amount).value()) + ' of funds (' + formatAsset(s.symbols[symbol.product_id].asset_amount_hold || 0, symbol.asset) + ') on hold')
logger.warn('sell delayed: ' + formatPercent(n(s.symbols[symbol.product_id].asset_amount_hold || 0).divide(s.symbols[symbol.product_id].asset_amount).value()) + ' of funds (' + formatAsset(s.symbols[symbol.product_id].asset_amount_hold || 0, symbol.asset) + ') on hold')
debug.msg('sell delayed: asset_amount ' + s.symbols[symbol.product_id].asset_amount || 0 + " - asset_amount_hold " + s.symbols[symbol.product_id].asset_amount_hold || 0 + ' < sell size ' + n(ssize).value())
logger.warn('sell delayed: asset_amount ' + s.symbols[symbol.product_id].asset_amount || 0 + " - asset_amount_hold " + s.symbols[symbol.product_id].asset_amount_hold || 0 + ' < sell size ' + n(ssize).value())
return setTimeout(function () {
if (s.symbols[symbol.product_id].last_signal === signal) {
executeSignal(signal, cb, size, true, false, false, symbol)
Expand Down
44 changes: 36 additions & 8 deletions lib/mongo-service.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
module.exports = function (conf) {
return {
getBots: () => {
conf.db.mongo.collection('bots').createIndex({ time: -1 })
return conf.db.mongo.collection('bots')
try {
conf.db.mongo.collection('bots').createIndex({ time: -1 })
return conf.db.mongo.collection('bots')
} catch (e) {
return {
err: true,
message: e.toString()
}
}
},
getSims: () => {
conf.db.mongo.collection('sim').createIndex({ bid: 1, time: 1 })
return conf.db.mongo.collection('sim')
try {
conf.db.mongo.collection('sim').createIndex({ bid: 1, time: 1 })
return conf.db.mongo.collection('sim')
} catch (e) {
return {
err: true,
message: e.toString()
}
}
},
getTrades: () => {
conf.db.mongo.collection('trades').createIndex({ selector: 1, time: 1 })
return conf.db.mongo.collection('trades')
try {
conf.db.mongo.collection('trades').createIndex({ selector: 1, time: 1 })
return conf.db.mongo.collection('trades')
} catch (e) {
return {
err: true,
message: e.toString()
}
}
},
getTickers: () => {
conf.db.mongo.collection('tickers').createIndex({ selector: 1, time: 1 })
return conf.db.mongo.collection('tickers')
try {
conf.db.mongo.collection('tickers').createIndex({ selector: 1, time: 1 })
return conf.db.mongo.collection('tickers')
} catch (e) {
return {
err: true,
message: e.toString()
}
}
},
}
}
Expand Down
21 changes: 10 additions & 11 deletions lib/ws_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ const webSocket = require('ws')
, helpers = require('../lib/helpers')

module.exports = function wsTickers(conf, exchange, so) {
var dbTickers
let startTime = (new Date()).getTime()
let insertCount = 0
let totalCount = 0
let klineScanCount = 0
let getHotProductsTime = startTime + 86400 * 1000
let collectionServiceInstance = collectionService(conf)
let dbTickers = collectionServiceInstance.getTickers()
var wsServer = {
wss: null,
run: function () {
Expand Down Expand Up @@ -420,17 +421,15 @@ module.exports = function wsTickers(conf, exchange, so) {
let shouldInsertTrades = trades.filter(t => shouldInsertPairs.some(p => t.selector === p))
// console.log('shouldInsertTrades', shouldInsertTrades)
if (shouldInsertTrades && shouldInsertTrades.length) {
try {
insertCount += shouldInsertTrades.length
if (!dbTickers) {
var collectionServiceInstance = collectionService(conf)
dbTickers = collectionServiceInstance.getTickers()
}
if (dbTickers.err) {
debug.msg('Insert tickers error '.cyan + dbTickers.message.red)
} else {
await dbTickers.insertMany(shouldInsertTrades)
let insertRate = (insertCount / diff)
process.stdout.write(moment().format('MMDD HH:mm:ss').green + ' ' + exchange.name.cyan + ' from ' + moment(startTime).format('MMDD HH:mm:ss').green
+ ' total:'.green + insertCount.toString().cyan + '/' + totalCount.toString().cyan + ' user time:'.green + ('' + diff).cyan + 's insert rate:'.green + (insertRate).toFixed(2).cyan + '/s')
} catch (err) { console.log('insert error..', err) }
insertCount += shouldInsertTrades.length
}
let insertRate = (insertCount / diff)
process.stdout.write(moment().format('MMDD HH:mm:ss').green + ' ' + exchange.name.cyan + ' from ' + moment(startTime).format('MMDD HH:mm:ss').green
+ ' total:'.green + insertCount.toString().cyan + '/' + totalCount.toString().cyan + ' user time:'.green + ('' + diff).cyan + 's insert rate:'.green + (insertRate).toFixed(2).cyan + '/s')
}
// console.timeEnd('db')
}
Expand Down

0 comments on commit fe9831a

Please sign in to comment.