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

Simulations get super slow and consume 100% cpu #594

Closed
Corstiaan84 opened this issue Oct 6, 2017 · 11 comments · Fixed by #1896
Closed

Simulations get super slow and consume 100% cpu #594

Corstiaan84 opened this issue Oct 6, 2017 · 11 comments · Fixed by #1896
Labels

Comments

@Corstiaan84
Copy link

Corstiaan84 commented Oct 6, 2017

Hi,

Im on node v8.0.0.

When starting a simulation it's starts very swiftly and runs through the first batch of periods quickly. However, soon after it starts grinding to a very slow pace of about a period per second while consuming 100% cpu.

It also depends on the used strategy. E.g. macd runs smoothly but srsi_macd is dead slow.

Any ideas?

@Corstiaan84
Copy link
Author

Corstiaan84 commented Oct 6, 2017

After digging around in the indicator and strategy code I think this slow perf has to do with the lookback and/or/in the for loops of the indicator?

@DeviaVir DeviaVir added bug and removed question labels Feb 28, 2018
@mikitymike
Copy link

mikitymike commented Mar 5, 2018

I also have this issue when simulating for more than 40 days, regardless of period (between 100 seconds and 30m) with a custom strategy and even with noop strategy. Changing keep_lookback_periods does not seem to affect this.

If I simulate for 60 days and let it run slowly, after about 10 minutes the program crashes and it says it cannot allocate any more memory:

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

@simclem77
Copy link

same for me

@basedalexander
Copy link

In my case I can't run simulation of MACD strategy on gdax.BTC-USD for more than 60 days, then it goes very slowly, about 1 day period a minute.

@DeviaVir any thoughts why is that?

@leozzo
Copy link

leozzo commented Oct 22, 2018 via email

@basedalexander
Copy link

@leozzo The problem is not the memory, my CPU get's 100% load from the process.

@Partiolainen
Copy link

Seems on sim command lookback and trades arrays are not reduced with any keep_lookback_periods option

I'm not spec, but something like this in sim.js will help:

function updatePeriod(trade) {
    s.period.high = Math.max(trade.price, s.period.high)
    s.period.low = Math.min(trade.price, s.period.low)
    s.period.close = trade.price
    s.period.volume += trade.size
    s.period.latest_trade_time = trade.time
    s.strategy.calculate(s)
    s.vol_since_last_blink += trade.size
    if (s.trades && s.last_trade_id !== trade.trade_id) {
      s.trades.push(trade)
      s.last_trade_id = trade.trade_id
    }
    //--- memory leak fix ---
    if(s.lookback.length > 1000){
      s.lookback.splice(-1,1)
    }

    if(s.trades.length > 1000){
      s.trades.splice(-1,1)
    }
  }

@mmdiego
Copy link
Contributor

mmdiego commented Jun 23, 2019

I was trying to run a long simulation (more than a month) of my custom strategy on gdax.BTC-USD at 1m period and I faced this same problem, after 15 days the simulation turned very slow and cpu went to 100%.
And then tried the fix above and it worked perfectly (which by the way must be done in engine.js, not sim.js), however limiting the size of s.lookback and s.trades affects the final simulation graph (which for me was not a problem)
So, it seems the size of these two arrays are a problem for long simulations, and I think it will be a problem as well for long live trading.
Is it neccessary to keep all the past information? Can this be implemented as a permanent fix? How can it be done for simulations for the final chart to be complete?

@mmdiego
Copy link
Contributor

mmdiego commented Jun 23, 2019

Reading issue #440, I saw that live trading limits the amount of data through the keep_lookback_periods param, so I answed myself that in live trading it shouldn't be a problem.
Maybe a similar parameter can be added for simulation, or some option to avoid the final graph in the sim's results so that data can be discarded for long simulations?

@mmdiego
Copy link
Contributor

mmdiego commented Jun 23, 2019

It seems s.trades is not useful at all for simulations.
It's only used in exitSim() in:
options_output.simresults.last_assest_value = s.trades[s.trades.length-1].price
which I think can be replaced by:
options_output.simresults.last_assest_value = s.period.close
and save a lot of memory in sims.
To get rid of s.trades, it can be done with:
if (so.mode !== 'sim' && conf.output.api.on) {

What do you think?

@mmdiego
Copy link
Contributor

mmdiego commented Jun 23, 2019

I've tried running the two month sim again without the size limitation on s.lookback and it worked well, so the speed problem was mainly caused by s.trades.
The fix I've proposed should improve sim performance a lot. I'll PR the changes.

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

Successfully merging a pull request may close this issue.

8 participants