|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8; py-indent-offset:4 -*- |
| 3 | +############################################################################### |
| 4 | +# |
| 5 | +# Copyright (C) 2015, 2016, 2017 Daniel Rodriguez |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +# |
| 20 | +############################################################################### |
| 21 | +from __future__ import (absolute_import, division, print_function, |
| 22 | + unicode_literals) |
| 23 | + |
| 24 | + |
| 25 | +import backtrader as bt |
| 26 | +from . import EMA |
| 27 | + |
| 28 | + |
| 29 | +class TrueStrengthIndicator(bt.Indicator): |
| 30 | + alias = ('TSI',) |
| 31 | + params = ( |
| 32 | + ('period1', 25), |
| 33 | + ('period2', 13), |
| 34 | + ) |
| 35 | + lines = ('tsi',) |
| 36 | + |
| 37 | + def __init__(self): |
| 38 | + pc = self.data - self.data(-1) |
| 39 | + |
| 40 | + sm1 = bt.ind.EMA(pc, period=self.p.period1) |
| 41 | + sm12 = bt.ind.EMA(sm1, period=self.p.period2) |
| 42 | + |
| 43 | + sm2 = bt.ind.EMA(abs(pc), period=self.p.period1) |
| 44 | + sm22 = bt.ind.EMA(sm2, period=self.p.period2) |
| 45 | + |
| 46 | + self.lines.tsi = 100.0 * (sm12 / sm22) |
0 commit comments