Skip to content

Commit c8fd69e

Browse files
committed
first tsi indicator implementation and test case
1 parent 9053e11 commit c8fd69e

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

backtrader/indicators/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
from .rsi import *
7070
from .stochastic import *
7171
from .trix import *
72+
from .tsi import *
7273
from .ultimateoscillator import *
7374
from .williams import *
7475

backtrader/indicators/tsi.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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)

tests/test_ind_tsi.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8; py-indent-offset:4 -*-
3+
###############################################################################
4+
#
5+
# Copyright (C) 2015, 2016 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+
import testcommon
25+
26+
import backtrader as bt
27+
import backtrader.indicators as btind
28+
29+
chkdatas = 1
30+
chkvals = [
31+
['16.012364', '22.866307', '4.990750']
32+
]
33+
34+
chkmin = 38
35+
chkind = bt.ind.TSI
36+
37+
38+
def test_run(main=False):
39+
datas = [testcommon.getdata(i) for i in range(chkdatas)]
40+
testcommon.runtest(datas,
41+
testcommon.TestStrategy,
42+
main=main,
43+
plot=main,
44+
chkind=chkind,
45+
chkmin=chkmin,
46+
chkvals=chkvals)
47+
48+
49+
if __name__ == '__main__':
50+
test_run(main=True)

0 commit comments

Comments
 (0)