Skip to content

Commit 38f9501

Browse files
committed
dv2 rewrite, literature addition and import
1 parent 284f1d8 commit 38f9501

3 files changed

Lines changed: 72 additions & 8 deletions

File tree

backtrader/indicators/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
from .crossover import *
5656
from .dpo import *
5757
from .directionalmove import *
58-
from .dv2 import *
5958
from .envelope import *
6059
from .macd import *
6160
from .momentum import *
@@ -72,6 +71,8 @@
7271
from .williams import *
7372

7473

74+
from .dv2 import * # depends on percentrank
75+
7576
# Depends on Momentum
7677
from .kst import *
7778

backtrader/indicators/dv2.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,34 @@
2121
from __future__ import (absolute_import, division, print_function,
2222
unicode_literals)
2323

24-
from . import Indicator
24+
25+
from . import Indicator, SMA, PercentRank
26+
27+
28+
__all__ = ['DV2']
2529

2630

2731
class DV2(Indicator):
2832
'''
2933
RSI(2) alternative
3034
Developed by David Varadi of http://cssanalytics.wordpress.com/
35+
36+
This seems to be the *Bounded* version.
37+
38+
See also:
39+
40+
- http://web.archive.org/web/20131216100741/http://quantingdutchman.wordpress.com/2010/08/06/dv2-indicator-for-amibroker/
41+
3142
'''
32-
params = (('rperiod', 252),)
43+
params = (
44+
('period', 252),
45+
('maperiod', 2),
46+
('_movav', SMA),
47+
)
3348
lines = ('dv2',)
3449

3550
def __init__(self):
36-
self.addminperiod(self.p.rperiod)
37-
avg = (self.data.high + self.data.low) / 2
38-
self.dvu = btind.SMA((self.data.close/avg), period=2)
39-
self.lines.dv2 = btind.PctRank(self.dvu, period=self.p.rperiod) * 100
51+
chl = self.data.close / ((self.data.high + self.data.low) / 2.0)
52+
dvu = self.p._movav(chl, period=self.p.maperiod)
53+
self.lines.dv2 = PercentRank(dvu, period=self.p.period) * 100
4054
super(DV2, self).__init__()
41-

tests/test_ind_dv2.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+
['0.900000', '0.880000', '0.980000'],
32+
]
33+
34+
chkmin = 50
35+
chkind = btind.DV2
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)