Skip to content

Commit ccdc804

Browse files
authored
[jealousGH-130] CR window is not in the name. (jealous#131)
When user specify a customized window for the energy index, it should appear in the column name. The customized column should not overwrite the column with the default window size.
1 parent f4e841b commit ccdc804

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![codecov](https://codecov.io/gh/jealous/stockstats/branch/master/graph/badge.svg?token=IFMD1pVJ7T)](https://codecov.io/gh/jealous/stockstats)
55
[![pypi](https://img.shields.io/pypi/v/stockstats.svg)](https://pypi.python.org/pypi/stockstats)
66

7-
VERSION: 0.5.0
7+
VERSION: 0.5.1
88

99
## Introduction
1010

stockstats.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -759,17 +759,22 @@ def _get_kdj_default(self):
759759
self['kdjd'] = self['kdjd_{}'.format(self.KDJ_WINDOW)]
760760
self['kdjj'] = self['kdjj_{}'.format(self.KDJ_WINDOW)]
761761

762-
def _get_cr(self, window=26):
762+
def _get_cr(self, windows=None):
763763
""" Energy Index (Intermediate Willingness Index)
764764
765765
https://support.futunn.com/en/topic167/?lang=en-us
766766
Use the relationship between the highest price, the lowest price and
767767
yesterday's middle price to reflect the market's willingness to buy
768768
and sell.
769769
770-
:param window: window of the moving sum
770+
:param windows: window of the moving sum
771771
:return: None
772772
"""
773+
if windows is None:
774+
window = 26
775+
else:
776+
window = self.get_int_positive(windows)
777+
773778
middle = self._tp()
774779
last_middle = self._shift(middle, -1)
775780
ym = self._shift(middle, -1)
@@ -779,10 +784,22 @@ def _get_cr(self, window=26):
779784
p2_m = pd.concat((last_middle, low), axis=1).min(axis=1)
780785
p1 = self._mov_sum(high - p1_m, window)
781786
p2 = self._mov_sum(ym - p2_m, window)
782-
self['cr'] = cr = p1 / p2 * 100
783-
self['cr-ma1'] = self._shifted_cr_sma(cr, self.CR_MA1)
784-
self['cr-ma2'] = self._shifted_cr_sma(cr, self.CR_MA2)
785-
self['cr-ma3'] = self._shifted_cr_sma(cr, self.CR_MA3)
787+
788+
if windows is None:
789+
cr = 'cr'
790+
cr_ma1 = 'cr-ma1'
791+
cr_ma2 = 'cr-ma2'
792+
cr_ma3 = 'cr-ma3'
793+
else:
794+
cr = 'cr_{}'.format(window)
795+
cr_ma1 = 'cr_{}-ma1'.format(window)
796+
cr_ma2 = 'cr_{}-ma2'.format(window)
797+
cr_ma3 = 'cr_{}-ma3'.format(window)
798+
799+
self[cr] = cr = p1 / p2 * 100
800+
self[cr_ma1] = self._shifted_cr_sma(cr, self.CR_MA1)
801+
self[cr_ma2] = self._shifted_cr_sma(cr, self.CR_MA2)
802+
self[cr_ma3] = self._shifted_cr_sma(cr, self.CR_MA3)
786803

787804
def _shifted_cr_sma(self, cr, window):
788805
cr_sma = self._sma(cr, window)
@@ -1108,7 +1125,10 @@ def _get_kama(self, column, windows, fasts=None, slows=None):
11081125

11091126
# start with simple moving average
11101127
kama = self._sma(col, window)
1111-
last_kama = kama.iloc[window - 1]
1128+
if len(kama) >= window:
1129+
last_kama = kama.iloc[window - 1]
1130+
else:
1131+
last_kama = 0.0
11121132
for i in range(window, len(kama)):
11131133
cur = smoothing.iloc[i] * (col.iloc[i] - last_kama) + last_kama
11141134
kama.iloc[i] = cur

test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ def test_cr(self):
161161
assert_that(stock['cr-ma2'].loc[20110331], near_to(117.1488))
162162
assert_that(stock['cr-ma3'].loc[20110331], near_to(111.5195))
163163

164+
stock.get('cr_26')
165+
assert_that(stock['cr_26'].loc[20110331], near_to(178.1714))
166+
assert_that(stock['cr_26-ma1'].loc[20110331], near_to(120.0364))
167+
assert_that(stock['cr_26-ma2'].loc[20110331], near_to(117.1488))
168+
assert_that(stock['cr_26-ma3'].loc[20110331], near_to(111.5195))
169+
164170
def test_column_permutation(self):
165171
stock = self.get_stock_20day()
166172
amount_p = stock['volume_-1_d_-3,-2,-1_p']

0 commit comments

Comments
 (0)