Skip to content

Commit 71c89f9

Browse files
committed
Allow for reversal values in range [1,9]
1 parent 9b95dca commit 71c89f9

File tree

3 files changed

+570
-33
lines changed

3 files changed

+570
-33
lines changed

examples/price-movement_plots.ipynb

Lines changed: 22 additions & 22 deletions
Large diffs are not rendered by default.

examples/scratch_pad/pnf_reversal.ipynb

Lines changed: 541 additions & 0 deletions
Large diffs are not rendered by default.

src/mplfinance/_utils.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def _valid_pnf_kwargs():
390390
'Validator' : lambda value: isinstance(value,(float,int)) or value == 'atr' },
391391
'atr_length' : { 'Default' : 14,
392392
'Validator' : lambda value: isinstance(value,int) or value == 'total' },
393-
'reversal' : { 'Default' : 2,
393+
'reversal' : { 'Default' : 1,
394394
'Validator' : lambda value: isinstance(value,int) }
395395
}
396396

@@ -936,25 +936,21 @@ def _construct_pointnfig_collections(dates, highs, lows, volumes, config_pointnf
936936
atr_length = pointnfig_params['atr_length']
937937
reversal = pointnfig_params['reversal']
938938

939-
# box_size upper limit (also used for calculating upper limit of reversal)
940-
upper_limit = (max(closes) - min(closes)) / 2
941-
942939
if box_size == 'atr':
943940
if atr_length == 'total':
944941
box_size = _calculate_atr(len(closes)-1, highs, lows, closes)
945942
else:
946943
box_size = _calculate_atr(atr_length, highs, lows, closes)
947944
else: # is an integer or float
945+
upper_limit = (max(closes) - min(closes)) / 2
948946
lower_limit = 0.01 * _calculate_atr(len(closes)-1, highs, lows, closes)
949947
if box_size > upper_limit:
950948
raise ValueError("Specified box_size may not be larger than (50% of the close price range of the dataset) which has value: "+ str(upper_limit))
951949
elif box_size < lower_limit:
952950
raise ValueError("Specified box_size may not be smaller than (0.01* the Average True Value of the dataset) which has value: "+ str(lower_limit))
953951

954-
if reversal < 2:
955-
raise ValueError("Specified reversal may not be smaller than 2")
956-
elif reversal*box_size > upper_limit*0.6:
957-
raise ValueError("Product of specified box_size and reversal which has value: "+ str(reversal*box_size) + " may not exceed (30% of the close price range of the dataset) which has value: "+ str(upper_limit*0.6))
952+
if reversal < 1 or reversal > 9:
953+
raise ValueError("Specified reversal must be an integer in the range [1,9]")
958954

959955
alpha = marketcolors['alpha']
960956

@@ -1024,16 +1020,16 @@ def _construct_pointnfig_collections(dates, highs, lows, volumes, config_pointnf
10241020
rolling_change = 0
10251021
volume_cache = 0
10261022

1027-
#Clean data to account for reversal size (added to allow overriding the default reversal of 2)
1023+
#Clean data to account for reversal size (added to allow overriding the default reversal of 1)
10281024
for i in range(1, len(adjusted_boxes)):
10291025

10301026
# Add to rolling_change and volume_cache which stores the box and volume values
10311027
rolling_change += adjusted_boxes[i]
10321028
volume_cache += temp_volumes[i]
10331029

1034-
# Add to new list if the rolling change is >= the reversal - 1
1030+
# Add to new list if the rolling change is >= the reversal
10351031
# The -1 is because we have already subtracted 1 from the box values in the previous loop
1036-
if abs(rolling_change) >= reversal-1:
1032+
if abs(rolling_change) >= reversal:
10371033

10381034
# if rolling_change is the same sign as the previous # of boxes then combine
10391035
if rolling_change*boxes[-1] > 0:

0 commit comments

Comments
 (0)