Build Alpha and Python
However, I have now added a python environment to give traders even more freedom. Traders now have the ability to code their own signals in python and optimize these signals in the Build Alpha strategy creation engine.
Python Example in Build Alpha
Below is a simple, step-by-step walkthrough of how to create a signal using python and test it with other pre-built signals in Build Alpha.
Step One: File >> Custom Indicator Editor
This step opens the custom indicator editor pictured above.
Step Two: Add new indicator and select Python
In the lower left of the new window that opens, please select Add and then near the top of the interface please click on Python. Both of these are circled in red below.
Step Three: Create and save the file
Hit the Create button which will open your File Explorer. Name the file that will contain your custom python script and select an appropriate file location to save the file.
You can also give your indicator a name in the top Name input box. Please note that this is the name that will appear in the main Build Alpha signal selection window.
In the example below I’ve named my custom python indicator “MyCustomIndicatorName” (green box) and named the file “CustomExample.py” (lower red box) while saving to my File Explorer.
Step Four: Add your code and save
Build Alpha then produces a simple to use template and we just need to add our custom code in the GetCustomSignal function. I have added a small moving average example below.
The only requirement is that we return a Signal list with the same length as data rows in our input data. That is, we must have a true or false signal for every bar. Do not drop NaNs.
Step Five: Custom Parameters
Build Alpha also supports custom parametric python signals. We can add variables between these comment sections like this
Then edit your code in the GetCustomSignal function to utilize your new parameter variable. Here is mine edited below. Notice I have removed the magic number 8 and replaced it with the sma_length variable which stores the value 8 at the moment.
Now save your custom python script and hit Save on the Build Alpha Custom Indicator Editor window. The indicator is now saved and will appear in your Custom Signals list of the main Strategy Builder interface.
If you click on your custom signal in the Strategy Builder you will now see the ability to edit the parameter range for any custom parameters you have added to your python script. Below I have set to optimize mine as 8, 16, 24, 32, 40, 48, 56, 64.
Running a simulation will then show all possible parameter variations for your custom python indicator. I set two max rules and required the second rule to be Not Sunday (always true) so each row has one variation of my custom python signal.
This new feature opens the door for what is possible in Build Alpha. Traders can leverage the power of python as well as BuildAlpha in extremely easy to use ways.
Installing Python Libraries for Build Alpha
Build Alpha’s python environment is a full python environment which means we can import any external python libraries. The above example imported the famous technical analysis library, talib, to create a moving average trading strategy with python.
You can add any external python library. The Build Alpha python installer includes talib, sci-kit learn, scipy, matplotlib and many other essentials.
Install Python Library for Technical Analysis Indicators
If you do not have talib installed or want to add a new python library, then open a command terminal and navigate to your python directory.
You can type ‘cmd’ into the start menu of most Windows devices. If you used the Build Alpha installer then talib is included. However, you can navigate to your python directory like this below:
Please note you can always check the folder inside Build Alpha for the most recent version of Python. At the time of writing we are using Python 3.11 so the folder name is TTPython311.
Then install talib using the following prompt below. Notice the hyphen in ta-lib.
Hit <ENTER> and you have now added a new library for use in python for Build Alpha. You can import the new library to the top of any new python script and Build Alpha will recognize it.
Algorithmic Trading Python
However, connecting to exchanges, handling live price data, and coming up with trading ideas can be a daunting task. The best part about coding is coming up with new signals. Build Alpha takes care of the heavy lifting enabling python traders to simply do the fun part: signal creation.
Summary
Build Alpha now supports the ability to import custom signals via python. This is additional functionality for programmers that want to leverage the speed and ease of Build Alpha. Reminder that this is extra and NO programming is needed to use Build Alpha’s built-in signals or drag and drop custom signal builder.
Author