backtrader2 / backtrader

Python Backtesting library for trading strategies
https://www.backtrader.com
GNU General Public License v3.0
227 stars 54 forks source link

Pandas numeric columns causing .lower errors. Issue #43 #56

Closed neilsmurphy closed 3 years ago

neilsmurphy commented 3 years ago

In 2017 the numeric check was dropped from the code. However, the function (colsnumeric) for checking if the dataframe headers were numeric and not string was still in the code. But there was no use of this function any longer.

Consequently, the current workflow sees the numeric headers not being properly identified and heading into the section where the text headers get sorted and identified. The numeric headers hits a x.lower() designed for strings and an error is thrown, hence this issue.

I have reinstated now the use of the colsnumeric. I have added back in similar fashion (but not exactly the same) code that will set the self._colmapping dict which gets used when iterating through the pandas lines to create the backtrader lines.

The order of the columns is obviously important since there is no text to determine the lines. I have checked the historical codes as well as the docs and set the columns accordingly:

{'datetime': None, 'open': 0, 'high': 1, 'low': 2, 'close': 3, 'volume': 4, 'openinterest': 5}

datetime being index of the dataframe.

The self.colsnumeric bool qualifier is carried forward and used in the Transform names section in start(). This will ensure that numeric headers skip the .lower() error and use the second conditional in

# Transform names (valid for .ix) into indices (good for .iloc)
        if self.p.nocase and not self.colsnumeric:
            colnames = [x.lower() for x in self.p.dataname.columns.values]
        else:
            colnames = [x for x in self.p.dataname]

I will create some test cases but this is working so far. I have run this with numeric and text headers.

neilsmurphy commented 3 years ago

Added test file. (tests/test_data_pandas.py)

neilsmurphy commented 3 years ago

The modification is working for regular pandas feed, but not for extended feeds. I'm losing the name in the mapping to the additional lines. Will review.

The travis error I'm receiving is Travis can't find Pandas module. However, to test this I require a pandas module. Any thoughts on how I can deal with this?

neilsmurphy commented 3 years ago

Everything seems working now with the latest commit. Still unsure how to test in Travis with the extra Pandas module. Travis seems to reject that.

neilsmurphy commented 3 years ago

@vladisld Seems all I had to do was modify the travis.yml file to include pandas in the build. All passed. Since I wrote I'll let you authorize. Thanks.

neilsmurphy commented 3 years ago

@vladisld Are we good to go with this one?

neilsmurphy commented 3 years ago

@vladisld I adjusted the travis file for the conflict. Are we still good to go?