This documentation provides a step-by-step guide on how to integrate and utilize indicators from TA-Lib in MachinaTrader strategy code.
1. Strategy Setup
Start by importing the necessary libraries and setting up your strategy’s basic configuration:
import sys, talib, mtCommon STRAT_NAME = 'RSI MACD STOCH SMA (S)' VERSION = '1.0' STRAT_TYPE = mtCommon.SimpleStrategy NUM_CANDLES = 200
2. Parameters
Define the parameters for your strategy:
def onSendParams(): _mt.addNumericParameter('Per Trade %', .95) ... (and other parameters)
3. Market Methods
Define the methods that will be executed per market based on your MachinaTrader configuration:
def onInit(): _mt.logInfoP('onInit called [' + STRAT_NAME + ', v' + VERSION + ']') def onTick(currentDate, candles, config): calcs = Calcs(candles) ... (your trading logic)
4. Indicators
For the integration of TA-Lib indicators, make use of the onSendIndicatorModels
method. This method defines the visual representation of indicators in MachinaTrader’s interface:
def onSendIndicatorModels(candles): calcs = Calcs(candles) ... (your indicator models)
5. Calculations Class
This is a reusable module that can be utilized by both onTick
and onSendIndicatorModels
to ensure calculations are consistent:
class Calcs: def __init__(self, candles): self.onCalculate(candles, _mt) ... (your calculation methods)
6. TA-Lib Indicators
Integrate various TA-Lib indicators within your calculation class:
self.rsis = talib.func.RSI(candles.C, _mt.getParameter('RSI Period')) self.upper,self.middle,self.lower = talib.func.BBANDS(candles.C, matype=talib.MA_Type.SMA) ... (and other indicators)