For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Sharpe Ratio analyzer causing massive RAM usage during backtesting/optimization runs on Linux



  • Currently I'm using the get_analysis method like so:

    def get_analysis(self):
         return {"sharperatio": self.ratio}
    

    Then I am doing this after an optimization run:

        sharpeopt = pd.DataFrame({str(r[0].params.emaperiod)+","+str(r[0].params.lookbacktrail): r[0].analyzers.sharperatio.get_analysis() for r in cerebroreturn}).T.loc[:,'sharperatio']
    

    Therefore I have the data for each parameter combination (here using 2 parameters one called "lookbacktrail" and one called "emaperiod") in a dataframe which I can use or manipulate as I wish.

    The issue is that I run all my compute intensive tasks on a Amazon EC2 instance running a linux OS - everything seems to work fine on my Windows PC but on linux I get a massive increase in RAM usage during the run (it increases by like 300mb per second or something crazy like that) and then eventually freezes the entire run due to memory being complete full.

    The problem seems to be just with the Sharpe Ratio analyzer - all other analyzers I use this method with work perfectly and the run sits comfortably under 2gb of mem usage. Anyone got any ideas what could be the cause? To me it seems obvious that there is some data being continuously (maybe exponentially) replicated/held on to for each run and hence why the memory usage ramps way up. Just wondering if anyone else has had this problem before? Also super strange that it seems to work fine on Windows but not on Ubuntu...



  • Found out what it was, I'll post the solution in case anyone else has similar problems:

    By process of elimination it seems the part of the Sharpe Ratio analyzer that my Linux instance didnt like was:

    self.anret = AnnualReturn()

    The instantiation/usage of the annual return Analyzer, for reasons that are beyond me, was causing a lot of excess data to be dumped into RAM. Not sure why, but I created a replacement analyzer to do a similar job to get the annual returns for the sharpe ratio and my memory usage is at a comfortable 1.5gb.


  • administrators

    See ... the problem with posting things and no code and no "nothing" is that nothing can be concluded.

    You then add
    @Gox said in Sharpe Ratio analyzer causing massive RAM usage during backtesting/optimization runs on Linux:

    By process of elimination it seems the part of the Sharpe Ratio analyzer that my Linux instance didnt like was:

    self.anret = AnnualReturn()
    

    If one looks at the documentation ... and the code, the conclusion is that AnnualReturn will only be used if the parameter legacyannual is set to True, because the AnnualReturn analyzer was deprecated ages ago in favor of TimeReturn.

    I can give you the solution ... don't use legacyannual=True and save yourself the effort of creating any replacement analyzer.



  • @backtrader Unfortunately the same problem was present whether or not legacyannual=True, when I tried to figure out what was causing it I used the code for legacyannual because there was less code to go through. Using legacyannual = True/False created the same memory problems - but when I created a custom Sharpe Ratio analyzer without using AnnualReturn there were no RAM issues.

    To clarify instead I used a loop in range(len(self.data)) within my custom analzyer for return data and when I did that it resulted in memory issues being solved.

    Again I think that this issue is idiosyncratic to Ubuntu - there were no problems with legacyannual = True/False on Windows.

    Just trying to help - if you would like me to post specific code samples I am happy to do so. Just wanted to report that this is what worked for me for my specific configuration (AWS EC2 compute instance).

    Either way your package is my favorite for algorithmic trading in python. I mean no disrespect.


Log in to reply
 

});