Technical Indicators¶
Although you can import technical indicator libraries and use them in your strategies, QTPyLib does come bundled with some common indicators that work as Pandas Objects.
Built-In Indicators¶
ATR¶
bars['atr'] = bars.atr(window=14 [, exp=False])
...
Awesome Oscillator¶
bars['ao'] = bars.awesome_oscillator(weighted=False, fast=5, slow=34])
...
Bollinger Bands¶
bb = bars.bollinger_bands(window=20, stds=2)
bars['bb_upper'] = bb['upper']
bars['bb_lower'] = bb['lower']
bars['bb_mid'] = bb['mid']
...
Weighted Bollinger Bands¶
wbb = bars.weighted_bollinger_bands(window=20, stds=2)
bars['wbb_upper'] = wbb['upper']
bars['wbb_lower'] = wbb['lower']
bars['wbb_mid'] = wbb['mid']
...
CCI¶
bars['cci'] = bars.cci(window=14)
...
Crossed Above/Below¶
bars['sma'] = bars['close'].rolling_mean(10)
if bars['close'].crossed_above(bars['sma']):
# crossed above
...
if bars['rsi'].crossed_below(10):
# crossed below
...
if bars['close'].crossed(bars['open']):
# crossed either above or below
...
Heikin Ashi¶
# return heiken ashi ohlc based on bar's ohlc
heikinashi = bars.heikinashi()
heikinashi[['open', 'high', 'low', 'close']]
...
Hull Moving Average¶
bars['hma'] = bars.hull_moving_average(window=200 [, min_periods=None])
# also available via shorthand
# bars['hma'] = bars.hma(...)
...
IBS¶
bars['ibs'] = bars.ibs()
...
Implied Volatility¶
bars['iv'] = bars.implied_volatility(window=252)
...
Keltner Channel¶
kc = bars.keltner_channel(window=14, atrs=2)
bars['kc_upper'] = kc['upper']
bars['kc_lower'] = kc['lower']
bars['kc_mid'] = kc['mid']
...
MACD¶
macd = bars.macd(fast=3, slow=10, smooth=16)
bars['macd'] = macd['macd']
bars['macd_signal'] = macd['signal']
bars['macd_hist'] = macd['histogram']
...
Moving Average: Simple¶
Shorthand for bars.rolling_mean(...)
bars['sma'] = bars.sma(window=200 [, min_periods=None])
...
Moving Average: Weighted¶
Shorthand for bars.rolling_weighted_mean(...)
bars['wma'] = bars.wma(window=200 [, min_periods=None])
...
Moving Average: Hull¶
Shorthand for bars.hull_moving_average(...)
bars['hma'] = bars.hma(window=200 [, min_periods=None])
...
Median Price¶
# (High + Low) / 2
bars['mid'] = bars.mid_price()
...
Typical Price¶
# (High + Low + Close) / 3
bars['typical'] = bars.typical_price()
...
Traders Dynamic Index (TDI)¶
bars['typical'] = bars['close'].tdi([rsi_len=13, bollinger_len=34,
rsi_smoothing=2, rsi_signal_len=7, bollinger_std=1.6185])
...
Price Volume Trend¶
bars['pvt'] = bars.pvt()
...
Rolling Minimum¶
bars['min'] = bars.rolling_min(window=14 [, min_periods=None])
...
Rolling Maximum¶
bars['max'] = bars.rolling_max(window=14 [, min_periods=None])
...
Rolling Mean¶
bars['sma'] = bars.rolling_mean(window=200 [, min_periods=None])
# also available via shorthand
# bars['sma'] = bars.sma(...)
...
Rolling Standard Deviation¶
bars['std'] = bars.rolling_std(window=200 [, min_periods=None])
...
Rolling VWAP¶
bars['rvwap'] = bars.rolling_vwap(window=200 [, min_periods=None])
...
Rolling Weighted Mean¶
bars['wma'] = bars.rolling_weighted_mean(window=200 [, min_periods=None])
# also available via shorthand
# bars['wma'] = bars.wma(...)
...
Rolling Returns¶
bars['returns'] = bars.returns()
...
Rolling Log Returns¶
bars['log_returns'] = bars.log_returns()
...
ROC¶
bars['roc'] = bars.roc(window=14)
...
RSI¶
bars['rsi'] = bars.rsi(window=14)
...
Session¶
This isn’t an indicator, but rather a utility that trims the bars to a specified “Session” (useful when wanting to work, for example, with the most recent PIT or GLOBEX session to calculate VWAP, etc.).
# make sure to specity timezone="US/Central" for your algo
# otherwise, the default timezone is UTC
# pit session
bars = bars.session(start='08:30', end='15:15')
# globex session
bars = bars.session(start='17:00', end='16:00')
...
Stochastics¶
bars['stoch'] = bars.stoch([window=14, d=3, k=3, fast=True])
...
True Range¶
bars['tr'] = bars.true_range()
...
VWAP¶
bars['vwap'] = bars.vwap(bars)
...
Z-Score¶
bars['zscore'] = bars.zscore(window=20, stds=1, col='close')
...
TA-Lib Integration¶
QTPyLib also offers full integration with TA-Lib.
All the TA-Lib methods are available via the talib_indicators
modules and
automatically extracts and prepare the relevant data your strategy’s bars
or ticks
.
To use the TA-Lib integration, you’ll need to have TA-Lib installed on your system,
and import the talib_indicators
module into your strategies:
# strategy.py
from qtpylib import talib_indicators as ta
...
def on_bar(self, instrument):
# get OHLCV bars
bars = instrument.get_bars()
# add 14-period ATR column
bars['atr'] = ta.ATR(bars, timeperiod=14)
# same result using Vanilla TA-Lib:
# bars['atr'] = talib.ATR(bars['high'].values, bars['low'].values, bars['close'].values, timeperiod=14)
...
For more information on all available TA-Lib methods/indicators, please visit TA-Lib’s website.