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###############################################################################
21from __future__ import (absolute_import, division, print_function,
22                        unicode_literals)
23
24import backtrader as bt
25from . import SMA, ROC100
26
27
28class KnowSureThing(bt.Indicator):
29    '''
30    It is a "summed" momentum indicator. Developed by Martin Pring and
31    published in 1992 in Stocks & Commodities.
32
33    Formula:
34      - rcma1 = MovAv(roc100(rp1), period)
35      - rcma2 = MovAv(roc100(rp2), period)
36      - rcma3 = MovAv(roc100(rp3), period)
37      - rcma4 = MovAv(roc100(rp4), period)
38
39      - kst = 1.0 * rcma1 + 2.0 * rcma2 + 3.0 * rcma3 + 4.0 * rcma4
40      - signal = MovAv(kst, speriod)
41
42    See:
43      - http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:know_sure_thing_kst
44
45    Params
46
47      - ``rma1``, ``rma2``, ``rma3``, ``rma4``: for the MovingAverages on ROCs
48      - ``rp1``, ``rp2``, ``rp3``, ``rp4``: for the ROCs
49      - ``rsig``: for the MovingAverage for the signal line
50      - ``rfactors``: list of factors to apply to the different MovAv(ROCs)
51      - ``_movav`` and ``_movavs``, allows to change the Moving Average type
52        applied for the calculation of kst and signal
53
54    '''
55    alias = ('KST',)
56    lines = ('kst', 'signal',)
57    params = (
58        ('rp1', 10), ('rp2', 15), ('rp3', 20), ('rp4', 30),
59        ('rma1', 10), ('rma2', 10), ('rma3', 10), ('rma4', 10),
60        ('rsignal', 9),
61        ('rfactors', [1.0, 2.0, 3.0, 4.0]),
62        ('_rmovav', SMA),
63        ('_smovav', SMA),
64    )
65
66    plotinfo = dict(plothlines=[0.0])
67
68    def __init__(self):
69        rcma1 = self.p._rmovav(ROC100(period=self.p.rp1), period=self.p.rma1)
70        rcma2 = self.p._rmovav(ROC100(period=self.p.rp2), period=self.p.rma2)
71        rcma3 = self.p._rmovav(ROC100(period=self.p.rp3), period=self.p.rma3)
72        rcma4 = self.p._rmovav(ROC100(period=self.p.rp4), period=self.p.rma4)
73        self.l.kst = sum([rfi * rci for rfi, rci in
74                          zip(self.p.rfactors, [rcma1, rcma2, rcma3, rcma4])])
75
76        self.l.signal = self.p._smovav(self.l.kst, period=self.p.rsignal)
77        super(KnowSureThing, self).__init__()
78