This repository has been archived by the owner on Feb 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
07a4d67
commit 2627b47
Showing
3 changed files
with
475 additions
and
346 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 |
---|---|---|
@@ -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 |
Oops, something went wrong.
2627b47
There was a problem hiding this comment.
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.