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

Corrected engine.js to include held assets in calculations for buy_max_amt and buy_pct #1111

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = function container (get, set, clear) {
s.product_id = so.selector.product_id
s.asset = so.selector.asset
s.currency = so.selector.currency
s.asset_capital = 0

if (typeof so.period_length == 'undefined')
so.period_length = so.period
Expand Down Expand Up @@ -192,18 +193,19 @@ module.exports = function container (get, set, clear) {
s.exchange.getBalance({currency: s.currency, asset: s.asset}, function (err, balance) {
if (err) return cb(err)
s.balance = balance
if (!s.start_capital) {
s.exchange.getQuote({product_id: s.product_id}, function (err, quote) {
if (err) return cb(err)
s.exchange.getQuote({product_id: s.product_id}, function (err, quote) {
if (err) return cb(err)

if (!s.start_capital) {
s.start_price = n(quote.ask).value()
s.start_capital = n(s.balance.currency).add(n(s.balance.asset).multiply(quote.ask)).value()

pushMessage('Balance ' + s.exchange.name.toUpperCase(), 'sync balance ' + s.start_capital + ' ' + s.currency + '\n')

cb()
})
}
else cb()
}
s.asset_capital = n(s.balance.asset).multiply(quote.ask).value()
cb()
})
})
}

Expand Down Expand Up @@ -435,11 +437,14 @@ module.exports = function container (get, set, clear) {
if (!size) {
if (so.mode === 'live') {
var buy_pct = so.buy_pct
if(so.buy_max_amt){
var buy_max_as_pct = n(so.buy_max_amt).divide(s.balance.currency).multiply(100)
if(buy_max_as_pct < buy_pct){
buy_pct = buy_max_as_pct
}
if(so.buy_max_amt){ // account for held assets as buy_max
var adjusted_buy_max_amt = n(so.buy_max_amt).subtract(s.asset_capital).value()
var buy_max_as_pct = n(adjusted_buy_max_amt).divide(s.balance.currency).multiply(100).value()
buy_pct = buy_max_as_pct
}else{ // account for held assets as %
var held_pct = n(s.asset_capital).divide(s.balance.currency).multiply(100).value()
var to_buy_pct = n(so.buy_pct).subtract(held_pct).value()
buy_pct = to_buy_pct
}
if (so.order_type === 'maker') {
size = n(s.balance.currency).multiply(buy_pct).divide(100).multiply(s.exchange.makerFee / 100).format('0.00000000')
Expand Down
Loading