Skip to content

Commit b189642

Browse files
committed
Generalize AccelerationDecelerationOscillator and add test
1 parent 465a816 commit b189642

2 files changed

Lines changed: 67 additions & 14 deletions

File tree

backtrader/indicators/accdecoscillator.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,38 @@
2222
unicode_literals)
2323

2424
import backtrader as bt
25-
from . import SMA
26-
from . import AO
25+
from . import MovAv, AwesomeOscillator
26+
27+
28+
__all__ = ['AccelerationDecelerationOscillator', 'AccDeOsc']
2729

2830

2931
class AccelerationDecelerationOscillator(bt.Indicator):
3032
'''
31-
Acceleration/Deceleration Technical Indicator (AC) measures acceleration and
32-
deceleration of the current driving force. This indicator will change
33+
Acceleration/Deceleration Technical Indicator (AC) measures acceleration
34+
and deceleration of the current driving force. This indicator will change
3335
direction before any changes in the driving force, which, it its turn, will
3436
change its direction before the price.
3537
3638
Formula:
37-
- MEDIAN PRICE = (HIGH + LOW) / 2
38-
- AO = SMA (MEDIAN PRICE, 5) - SMA (MEDIAN PRICE, 34)
39-
- AC = AO - SMA (AO, 5)
39+
- AcdDecOsc = AwesomeOscillator - SMA(AwesomeOscillator, period)
4040
4141
See:
4242
- https://www.metatrader5.com/en/terminal/help/indicators/bw_indicators/ao
4343
- https://www.ifcmarkets.com/en/ntx-indicators/ntx-indicators-accelerator-decelerator-oscillator
4444
4545
'''
46-
alias = ('AC', )
47-
lines = ('ac', )
46+
alias = ('AccDeOsc',)
47+
lines = ('accde', )
4848

49-
plotlines = dict(ac=dict(_method='bar'))
49+
params = (
50+
('period', 5),
51+
('movav', MovAv.SMA),
52+
)
5053

51-
def __init__(self):
54+
plotlines = dict(accde=dict(_method='bar', alpha=0.50, width=1.0))
5255

53-
ao = AO()
54-
#self.l.ac = ao - SMA(ao, 5)
55-
self.l.ac = ao - SMA(ao, period=5)
56+
def __init__(self):
57+
ao = AwesomeOscillator()
58+
self.l.accde = ao - self.p.movav(ao, period=self.p.period)
59+
super(AccelerationDecelerationOscillator, self).__init__()

tests/test_ind_accdecosc.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
28+
chkdatas = 1
29+
chkvals = [
30+
['-2.097441', '14.156647', '30.408335']
31+
]
32+
33+
chkmin = 38
34+
chkind = bt.ind.AccelerationDecelerationOscillator
35+
36+
37+
def test_run(main=False):
38+
datas = [testcommon.getdata(i) for i in range(chkdatas)]
39+
testcommon.runtest(datas,
40+
testcommon.TestStrategy,
41+
main=main,
42+
plot=main,
43+
chkind=chkind,
44+
chkmin=chkmin,
45+
chkvals=chkvals)
46+
47+
48+
if __name__ == '__main__':
49+
test_run(main=True)

0 commit comments

Comments
 (0)