Closed neilsmurphy closed 4 years ago
Is everyone ok with updating functions.py with this? Or should we seek to make an indicator? @backtrader2/admins
Functions.py makes more sense if it's just a simple operator
On Tuesday, July 28, 2020, 03:48:43 AM PDT, Neil Murphy <notifications@github.com> wrote:
Is everyone ok with updating functions.py with this? Or should we seek to make an indicator? @backtrader2/admins
— You are receiving this because you are on a team that was mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.
Sounds good. Adding a function instead of indicator may potentially enable real vectorizing in the future (as the logic could be recursively mapped to numpy operators).
Agree, functions.py is better place.
This is ready to go for quite some time, but I'm shy to mess up the main backtrader repository using cherry pick, since I don't know how to do it.
This issue seeks to add new scalar math functionality such as math.log to the available functions in Backtrader.
The functionality of And/Max/Min is located in functions.py.
They use the math library in conjunction with a class MultiLogic. This is designed to work with two or more lines.
Essentially what the MultiLogic does is iterate through the lines at a particular bar, create a list, and then apply the math function to the list. Of course, Max and And and the like are looking for an iterable.
When we try to do this with a single line like math.log, then it breaks because math.log is looking for a scalar but is getting a list.
We could adjust multilogic to find out if the list is only length of one, and then select the item [0] to make it a scalar. This does work but I don't like it because we are playing with a built in class and I would prefer to leave it alone.
So instead I created a new class called SingleLogic.
With this single logic class we can now use all of the math library functions that require a scalar. Here are some examples:
Then I used them in a basic strategy like this:
And get output of this:
It is very easy now to create new functions from the math library. We just have to pick the ones we want to implement. And of course we'll need some testing and documentation.