-
Notifications
You must be signed in to change notification settings - Fork 6
Strategies
To create a new strategy, use the manage.py's strategy create
command.
The manage.py tool is a command line tool that allows you to create and manage strategies. You can use it with python, like so:
python manage.py
When creating a strategy with the CLI tool, the strategy name must be in pascal case.
This is because the strategy name is used to create the strategy class name, which must be in pascal case. (it is also converted to snake case for the file name)
# create a new strategy
python manage.py strategy create MyNewStrategy
# results in an error
python manage.py strategy create my_new_strategy
python manage.py strategy create My New Strategy 2
The strategy class is created in the strategies
directory, and is named after the strategy name you provided.
While the main focus of a strategy class is to utilize the on_step
decorator to create functions that run on every "step" of the data, often you will need to create data series that are used in multiple places or within the on_step
functions. This is referred to as "pre-compiled" data series. This is done to help reduce the amount of time it takes to run the strategy.
Here is an example of a pre-compiled data series, with a simple Moving Averages strategy:
class SMACrossOver(Strategy):
sma_fast_length = Parameter(10)
sma_slow_length = Parameter(60)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
all_close = self.data.all('close')
self.sma_fast = ta.sma(all_close, int(self.sma_fast_length))
self.sma_slow = ta.sma(all_close, int(self.sma_slow_length))
...
Note, you can only pre-compile data within the __init__()
method. This is due to how python handles initializing classes.