Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

Commit

Permalink
Introduce Trade Class (#1968)
Browse files Browse the repository at this point in the history
* subtract candle length from start time to calculate isPremature

* Move startTimeMinusCandleSize inside realtime if check

* return the indicator after adding it

* develop trade class

* make fixes after live market test

* line spacing

* rollback package-lock.json to develop branch

* link to discussion:

* move methods from portfolio manager into trade class and new portfolio class

* cleanup logging, comments
  • Loading branch information
ansonphong authored and askmike committed Apr 1, 2018
1 parent 07a4d67 commit 2627b47
Show file tree
Hide file tree
Showing 3 changed files with 475 additions and 346 deletions.
111 changes: 111 additions & 0 deletions plugins/trader/portfolio.js
@@ -0,0 +1,111 @@
/*
The Portfolio class holds the most recent data about the portfolio and ticker
*/

var _ = require('lodash')
var util = require('../../core/util')
var dirs = util.dirs()
var events = require('events')
var log = require(dirs.core + 'log')
var async = require('async')

class Portfolio{
constructor(conf,exchange){
_.bindAll(this)
this.conf = conf
this.exchange = exchange
this.portfolio = {}
this.fee = null
this.ticker = null
}

getBalance(fund) {
return this.getFund(fund).amount;
}

// return the [fund] based on the data we have in memory
getFund(fund) {
return _.find(this.portfolio, function(f) { return f.name === fund});
}

// convert into the portfolio expected by the performanceAnalyzer
convertPortfolio(asset,currency) { // rename?
var asset = _.find(this.portfolio, a => a.name === this.conf.asset).amount;
var currency = _.find(this.portfolio, a => a.name === this.conf.currency).amount;

return {
currency,
asset,
balance: currency + (asset * this.ticker.bid)
}
}

logPortfolio() {
log.info(this.exchange.name, 'portfolio:');
_.each(this.portfolio, function(fund) {
log.info('\t', fund.name + ':', parseFloat(fund.amount).toFixed(12));
});
};

setPortfolio(callback) {
let set = (err, fullPortfolio) => {
if(err)
util.die(err);

// only include the currency/asset of this market
const portfolio = [ this.conf.currency, this.conf.asset ]
.map(name => {
let item = _.find(fullPortfolio, {name});

if(!item) {
log.debug(`unable to find "${name}" in portfolio provided by exchange, assuming 0.`);
item = {name, amount: 0};
}

return item;
});

this.portfolio = portfolio;

if(_.isEmpty(this.portfolio))
this.emit('portfolioUpdate', this.convertPortfolio(this.conf.asset,this.conf.currency,this.ticker.bid));

if(_.isFunction(callback))
callback();

}

this.exchange.getPortfolio(set);
}

setFee(callback) {
let set = (err, fee) => {
this.fee = fee;

if(err)
util.die(err);

if(_.isFunction(callback))
callback();
}
this.exchange.getFee(set);
}

setTicker(callback) {
let set = (err, ticker) => {
this.ticker = ticker;

if(err)
util.die(err);

if(_.isFunction(callback))
callback();
}
this.exchange.getTicker(set);
}

}

util.makeEventEmitter(Portfolio)

module.exports = Portfolio

1 comment on commit 2627b47

@ansonphong
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@askmike Perhaps you can have a look at this, Issue: #2086

It's possibly that this PR broke the emit. I am not fully familiar with the specifics of the event emitting architecture in Gekko.

Please sign in to comment.