Skip to content

Commit 612cdba

Browse files
committed
Fix test_exceptions test and adjust reversal size logic
Previous implementation didn't account for the movement after the last reversal. This led to issues with both the final box value and the final column volume data. This change adds logic to track any further movement (volume and box) in the same signed direction after the final reversal and adds it to the last box column after the clean data loop.
1 parent e5ae865 commit 612cdba

File tree

3 files changed

+162
-45
lines changed

3 files changed

+162
-45
lines changed

examples/scratch_pad/pnf_reversal.ipynb

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

src/mplfinance/_utils.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,7 @@ def _construct_pointnfig_collections(dates, highs, lows, volumes, config_pointnf
10191019

10201020
rolling_change = 0
10211021
volume_cache = 0
1022+
biggest_difference = 0 # only used for the last column
10221023

10231024
#Clean data to account for reversal size (added to allow overriding the default reversal of 1)
10241025
for i in range(1, len(adjusted_boxes)):
@@ -1027,8 +1028,12 @@ def _construct_pointnfig_collections(dates, highs, lows, volumes, config_pointnf
10271028
rolling_change += adjusted_boxes[i]
10281029
volume_cache += temp_volumes[i]
10291030

1031+
# if rolling_change is the same sign as the previous box and the abs value is bigger than the
1032+
# abs value of biggest_difference then we should replace biggest_difference with rolling_change
1033+
if rolling_change*boxes[-1] > 0 and abs(rolling_change) > abs(biggest_difference):
1034+
biggest_difference = rolling_change
1035+
10301036
# Add to new list if the rolling change is >= the reversal
1031-
# The -1 is because we have already subtracted 1 from the box values in the previous loop
10321037
if abs(rolling_change) >= reversal:
10331038

10341039
# if rolling_change is the same sign as the previous # of boxes then combine
@@ -1044,7 +1049,14 @@ def _construct_pointnfig_collections(dates, highs, lows, volumes, config_pointnf
10441049

10451050
# reset rolling_change and volume_cache once we've used them
10461051
rolling_change = 0
1047-
volume_cache = 0
1052+
volume_cache = 0
1053+
1054+
# reset biggest_difference as we start from the beginning every time there is a reversal
1055+
biggest_difference = 0
1056+
1057+
# Adjust the last box column if the left over rolling_change is the same sign as the column
1058+
boxes[-1] += biggest_difference
1059+
new_volumes[-1] += volume_cache
10481060

10491061
curr_price = closes[0]
10501062
box_values = [] # y values for the boxes

tests/test_exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ def test_reversal_box_size_bounds(bolldata):
8787
buf = io.BytesIO()
8888
mpf.plot(df,type='pnf',pnf_params=dict(box_size=3, reversal=3), volume=True, savefig=buf)
8989
with pytest.raises(ValueError) as ex:
90-
mpf.plot(df,type='pnf',pnf_params=dict(box_size=3, reversal=4), volume=True, savefig=buf)
91-
assert 'Product of specified box_size and reversal which has value: 12 may not exceed (30% of the close price range of the dataset)' in str(ex.value)
90+
mpf.plot(df,type='pnf',pnf_params=dict(box_size=3, reversal=10), volume=True, savefig=buf)
91+
assert 'Specified reversal must be an integer in the range [1,9]' in str(ex.value)

0 commit comments

Comments
 (0)