-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from bitrinjani/feature/analytics
Analytics plugin system
- Loading branch information
Showing
63 changed files
with
1,011 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ config.json | |
.idea | ||
.python-version | ||
datastore/ | ||
reports/ | ||
reports/ | ||
sandbox/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const _ = require('lodash'); | ||
const ss = require('simple-statistics'); | ||
const { getLogger } = require('@bitr/logger'); | ||
|
||
const precision = 3; | ||
|
||
class SimpleSpreadStatHandler { | ||
// Constructor is called when initial snapshot of spread stat history has arrived. | ||
constructor(history) { | ||
this.log = getLogger(this.constructor.name); | ||
const profitPercentHistory = history.map(x => x.bestCase.profitPercentAgainstNotional); | ||
this.sampleSize = profitPercentHistory.length; | ||
this.profitPercentMean = this.sampleSize != 0 ? ss.mean(profitPercentHistory) : 0; | ||
this.profitPercentVariance = this.sampleSize != 0 ? ss.sampleVariance(profitPercentHistory) : 0; | ||
} | ||
|
||
// The method is called each time new spread stat has arrived, by default every 3 seconds. | ||
// Return value: part of ConfigRoot or undefined. | ||
// If part of ConfigRoot is returned, the configuration will be merged. If undefined is returned, no update will be made. | ||
async handle(spreadStat) { | ||
const newData = spreadStat.bestCase.profitPercentAgainstNotional; | ||
// add new data to mean | ||
this.profitPercentMean = ss.addToMean(this.profitPercentMean, this.sampleSize, newData); | ||
// add new data to variance | ||
this.profitPercentVariance = ss.combineVariances( | ||
this.profitPercentVariance, | ||
this.profitPercentMean, | ||
this.sampleSize, | ||
0, | ||
newData, | ||
1 | ||
); | ||
|
||
this.sampleSize++; | ||
|
||
// set μ + σ to minTargetProfitPercent | ||
const n = this.sampleSize; | ||
const mean = this.profitPercentMean; | ||
const standardDeviation = Math.sqrt(this.profitPercentVariance * n/(n-1)); | ||
const minTargetProfitPercent = _.round(mean + standardDeviation, precision); | ||
if (_.isNaN(minTargetProfitPercent)) { | ||
return undefined; | ||
} | ||
this.log.info( | ||
`μ: ${_.round(mean, precision)}, σ: ${_.round( | ||
standardDeviation, | ||
precision | ||
)}, n: ${n} => minTargetProfitPercent: ${minTargetProfitPercent}` | ||
); | ||
const config = { minTargetProfitPercent }; | ||
return config; | ||
} | ||
} | ||
|
||
module.exports = SimpleSpreadStatHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,85 @@ | ||
import { injectable } from 'inversify'; | ||
import { ConfigStore, ConfigRoot } from './types'; | ||
import { getConfigRoot } from './configUtil'; | ||
import { ConfigStore, ConfigRoot, ConfigRequest } from './types'; | ||
import { getConfigRoot, getConfigPath } from './configUtil'; | ||
import ConfigValidator from './ConfigValidator'; | ||
import { setTimeout } from 'timers'; | ||
import { socket, Socket } from 'zeromq'; | ||
import { configStoreSocketUrl } from './constants'; | ||
import { parseBuffer } from './util'; | ||
import * as _ from 'lodash'; | ||
import * as fs from 'fs'; | ||
import { promisify } from 'util'; | ||
import { getLogger } from '@bitr/logger'; | ||
|
||
const writeFile = promisify(fs.writeFile); | ||
|
||
@injectable() | ||
export default class JsonConfigStore implements ConfigStore { | ||
private TTL = 5 * 1000; | ||
private readonly log = getLogger(this.constructor.name); | ||
private timer: NodeJS.Timer; | ||
private readonly server: Socket; | ||
private readonly TTL = 5 * 1000; | ||
private cache?: ConfigRoot; | ||
|
||
constructor(private readonly configValidator: ConfigValidator) {} | ||
constructor(private readonly configValidator: ConfigValidator) { | ||
this.server = socket('rep'); | ||
this.server.on('message', message => this.messageHandler(message)); | ||
this.server.bindSync(configStoreSocketUrl); | ||
} | ||
|
||
get config(): ConfigRoot { | ||
if (this.cache) { | ||
return this.cache; | ||
} | ||
const config = getConfigRoot(); | ||
this.configValidator.validate(config); | ||
this.cache = config; | ||
setTimeout(() => (this.cache = undefined), this.TTL); | ||
this.updateCache(config); | ||
return config; | ||
} | ||
|
||
async set(config: ConfigRoot) { | ||
this.configValidator.validate(config); | ||
await writeFile(getConfigPath(), JSON.stringify(config, undefined, 2)); | ||
this.updateCache(config); | ||
} | ||
|
||
close() { | ||
this.server.close(); | ||
} | ||
|
||
private async messageHandler(message: Buffer) { | ||
const parsed = parseBuffer<ConfigRequest>(message); | ||
if (parsed === undefined) { | ||
this.log.debug(`Invalid message received. Message: ${message.toString()}`); | ||
this.server.send(JSON.stringify({ success: false, reason: 'invalid message' })); | ||
return; | ||
} | ||
switch (parsed.type) { | ||
case 'set': | ||
try { | ||
const newConfig = parsed.data; | ||
await this.set(_.merge({}, getConfigRoot(), newConfig)); | ||
this.server.send(JSON.stringify({ success: true })); | ||
this.log.debug(`Config updated with ${JSON.stringify(newConfig)}`); | ||
} catch (ex) { | ||
this.server.send(JSON.stringify({ success: false, reason: 'invalid config' })); | ||
this.log.warn(`Failed to update config. Error: ${ex.message}`); | ||
this.log.debug(ex.stack); | ||
} | ||
break; | ||
case 'get': | ||
this.server.send(JSON.stringify({ success: true, data: getConfigRoot() })); | ||
break; | ||
default: | ||
this.log.warn(`ConfigStore received an invalid message. Message: ${parsed}`); | ||
this.server.send(JSON.stringify({ success: false, reason: 'invalid message type' })); | ||
break; | ||
} | ||
} | ||
|
||
private updateCache(config: ConfigRoot) { | ||
this.cache = config; | ||
clearTimeout(this.timer); | ||
this.timer = setTimeout(() => (this.cache = undefined), this.TTL); | ||
} | ||
} /* istanbul ignore next */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.