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

Sim.js simulation end balance broken #2527

Closed
yellowgh0st opened this issue Dec 2, 2020 · 4 comments
Closed

Sim.js simulation end balance broken #2527

yellowgh0st opened this issue Dec 2, 2020 · 4 comments
Labels

Comments

@yellowgh0st
Copy link
Contributor

yellowgh0st commented Dec 2, 2020

System information

  • Have I written custom code (as opposed to using zenbot vanilla): zero
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Fedora 33
  • Zenbot version (commit ref, or version): main
  • Zenbot branch: main
  • NodeJS version: 6.13.4
  • Python version (when using a python script): 3.9
  • Exact command to reproduce (include everything): zenbot.sh sim
  • Did I make any changes to conf-sample.js?: Basic stuff

Describe the problem

It looks like the end result calculation is wrong. It doesn't reflect asset end balance, instead, it just wrongly multiplies currency balance. For unknown reasons, the asset balance is always zero. Also, the formula used for live calculation should be checked too.

Source code / Error logs

Sim.js

I'm currently working on fix up/rewrite.

---
function exitSim () {
        console.log()
        if (!s.period) {
          console.error('no trades found! try running `zenbot backfill ' + so.selector.normalized + '` first')
          process.exit(1)
        }
        var option_keys = Object.keys(so)
        var output_lines = []
        option_keys.sort(function (a, b) {
          if (a < b) return -1
          return 1
        })
        var options = {}
        option_keys.forEach(function (k) {
          options[k] = so[k]
        })

        let options_output = options
        options_output.simresults = {}

        if (s.my_trades.length) {
          s.my_trades.push({
            price: s.period.close,
            size: s.balance.asset,
            type: 'sell',
            time: s.period.time
          })
        }
        s.balance.currency = n(s.net_currency).add(n(s.period.close).multiply(s.balance.asset)).format('0.00000000')

        s.balance.asset = 0
        s.lookback.unshift(s.period)
        var profit = s.start_capital ? n(s.balance.currency).subtract(s.start_capital).divide(s.start_capital) : n(0)
        output_lines.push('end balance: ' + n(s.balance.currency).format('0.00000000').yellow + ' (' + profit.format('0.00%') + ')')
        //console.log('start_capital', s.start_capital)
        //console.log('start_price', n(s.start_price).format('0.00000000'))
        //console.log('close', n(s.period.close).format('0.00000000'))
        var buy_hold = s.start_price ? n(s.period.close).multiply(n(s.start_capital).divide(s.start_price)) : n(s.balance.currency)
        //console.log('buy hold', buy_hold.format('0.00000000'))
        var buy_hold_profit = s.start_capital ? n(buy_hold).subtract(s.start_capital).divide(s.start_capital) : n(0)
        output_lines.push('buy hold: ' + buy_hold.format('0.00000000').yellow + ' (' + n(buy_hold_profit).format('0.00%') + ')')
        output_lines.push('vs. buy hold: ' + n(s.balance.currency).subtract(buy_hold).divide(buy_hold).format('0.00%').yellow)
        output_lines.push(s.my_trades.length + ' trades over ' + s.day_count + ' days (avg ' + n(s.my_trades.length / s.day_count).format('0.00') + ' trades/day)')
        var last_buy
        var losses = 0, sells = 0
        s.my_trades.forEach(function (trade) {
          if (trade.type === 'buy') {
            last_buy = trade.price
          }
          else {
            if (last_buy && trade.price < last_buy) {
              losses++
            }
            sells++
          }
        })
        if (s.my_trades.length) {
          output_lines.push('win/loss: ' + (sells - losses) + '/' + losses)
          output_lines.push('error rate: ' + (sells ? n(losses).divide(sells).format('0.00%') : '0.00%').yellow)
        }
        options_output.simresults.start_capital = s.start_capital
        options_output.simresults.last_buy_price = s.last_buy_price
        options_output.simresults.last_assest_value = s.period.close
        options_output.net_currency = s.net_currency
        options_output.simresults.asset_capital = s.asset_capital
        options_output.simresults.currency = n(s.balance.currency).value()
        options_output.simresults.profit = profit.value()
        options_output.simresults.buy_hold = buy_hold.value()
        options_output.simresults.buy_hold_profit = buy_hold_profit.value()
        options_output.simresults.total_trades = s.my_trades.length
        options_output.simresults.length_days = s.day_count
        options_output.simresults.total_sells = sells
        options_output.simresults.total_losses = losses
        options_output.simresults.vs_buy_hold = n(s.balance.currency).subtract(buy_hold).divide(buy_hold).value() * 100.00

        let options_json = JSON.stringify(options_output, null, 2)
        if (so.show_options) {
          output_lines.push(options_json)
        }

        output_lines.forEach(function (line) {
          console.log(line)
        })
...

I'm currently working on fix up/rewrite.

@DeviaVir DeviaVir added the bug label Dec 2, 2020
@tlcointrack
Copy link

Any update? I am experiencing the same issue at this time, let me know how I can support

@k3ntinhu
Copy link
Contributor

k3ntinhu commented Jan 1, 2021

From discord channel
xwolfyxNLY said:

If you revert:
if (!clock && so.mode !== 'live' && so.mode !== 'paper') clock = lolex.install(
{
shouldAdvanceTime: false,
now: trade.time,
toFake: [
'setTimeout'
]
})

to this:
if (!clock && so.mode !== 'live' && so.mode !== 'paper') clock = lolex.install({ shouldAdvanceTime: false, now: trade.time })

in the lib\engine.js the sims are correct again.

@xwolfyxNL
Copy link
Contributor

xwolfyxNL commented Jan 3, 2021

Specifically undoing the changes in the commit (#2425) fixes the issue with the wrong results in the simulation.

Have been running a lot of sims the past few days and I didn't encounter any issues after undoing the changes.

Made a pull request #2600

@yellowgh0st
Copy link
Contributor Author

Thanks a lot! My plan was different, but you got the issue, so I'm closing!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants