Skip to content

Instantly share code, notes, and snippets.

@GuntharDeNiro
Created September 28, 2021 16:23
Show Gist options
  • Save GuntharDeNiro/b4585fd2853e387c345381e656700f88 to your computer and use it in GitHub Desktop.
Save GuntharDeNiro/b4585fd2853e387c345381e656700f88 to your computer and use it in GitHub Desktop.
getITB() {
const quoteBalance = parseFloat(this.getLedger().quoteBalance) || 0
const keepQuote = parseFloat(this.strategy_config.KEEP_QUOTE) || 0
let boughtAmount = 0
let detectedITB = 0
var orders = this.getLedger().orders;
// try if we can establish a useful ITB value with order history data, only for usage in abp
// iterate through most recent orders, sum amounts and stop when a sell happens after the previous order history cumulated a similar amount to currentQty
for (let i = 0; i < orders.length; i++) {
if (orders[i].type === 'buy') {
boughtAmount = boughtAmount + parseFloat(orders[i].amount)
}
else if (orders[i].type === 'sell') {
if (boughtAmount > (quoteBalance * 0.98) - keepQuote) {
detectedITB = parseFloat(orders[i].time)
break
}
boughtAmount = boughtAmount - parseFloat(orders[i].amount)
}
}
if (!_.isNil(this.strategy_config.IGNORE_TRADES_BEFORE) && parseFloat(this.strategy_config.IGNORE_TRADES_BEFORE) > 0) {
return math.max(parseFloat(this.strategy_config.IGNORE_TRADES_BEFORE), detectedITB)
}
else if (this.strategy_config.RT_ENABLED === true) {
// expecting a mess when we try to establish itb in these cases, set ITB 0 instead
return 0
}
return detectedITB
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment