- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bt.plot(): TypeError: float() argument must be a string or a real number, not 'Timestamp' #906
Comments
Does one of your indicators return an array of timestamps? |
Oh ok, thx, yes i had... Was using
so I changed it to this to get the values I need
|
Maybe backtesting.py/backtesting/backtesting.py Line 135 in 0ce24d8
|
I could take a look at this
option 1. Can someone refresh my memory on the format we are hoping 'Strategy.I' is to keep or use for 'np.asarray', if I understand correctly the proposed solution is to convert the 'Strategy.I' class.function to return the value of 'np.asarray' to floats else fail... but fail if it cannot convert ['int','timestamp'] to a float. I'm still a noob to the 'Backtesting' package so I haven't read through and memorized this function let alone finished the whole codebase but I had an idea and was curious if this would help. The places I did find that might make sense were these two screen shots, somewhere starting at line 416 option 2. Would it make sense, and I'm just brainstorming an idea, but what if we added a clean data filter as an assert in '/test/_test.py' that if the data didn't fit the patter 'you specify' the data gets passed to a function that first runs a data_type check and if its wrong it cleans or converts it then passes it was always formatted correctly before being passed to 'Strategy.I'? or under 'TestUtil' Class with something like 'self.assertdatetime.fromisoformat('2011-11-04T00:05:23+04:00')' per Python 3.11.2 |
@datatalking Sorry for the late reply (awfully busy these days), and thanks for showing interest. As you can see in the original issue report above, the error happens during plotting, on line: backtesting.py/backtesting/_plotting.py Line 573 in 0ce24d8
and that only when one of the indicator functions (wrapped in Strategy.I ) returns a series of Timestamp values, which then fail since float(mean_timestamp_value) is not defined.
I proposed coercing any indicator-returned values to floats exactly here: backtesting.py/backtesting/backtesting.py Line 135 in 0ce24d8
(i.e. np.asarray(..., dtype=float, ...) ), but I now think it's better to coerce later, on the faulty plotting line itself:backtesting.py/backtesting/_plotting.py Line 573 in 0ce24d8
(i.e. pd.Series(arr).astype(int).mean()) or some such) so the series of timestamps can remain to be used as such within strategy logic.
In either case, I don't think this has anything to do with tests, other than needing a test to cover this use case when the issue is fixed. 🤔 |
Expected Behavior
bt.plot()
Actual Behavior
stats = bt.run()
works fine.. not sure why I can't plotThe text was updated successfully, but these errors were encountered: