Skip to content

Commit 38a16dd

Browse files
Merge pull request #373 from DanielGoldfarb/retpnfdata
fix pnf return calc values per stackoverflow 66991998
2 parents 385b27c + f7e3520 commit 38a16dd

File tree

4 files changed

+1043
-20
lines changed

4 files changed

+1043
-20
lines changed

examples/scratch_pad/price-movement_ret_calc_vals.ipynb

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

src/mplfinance/_utils.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,9 @@ def _construct_renko_collections(dates, highs, lows, volumes, config_renko_param
855855
edgecolors=edge_colors,
856856
linewidths=lw
857857
)
858-
return [rectCollection,], new_dates, new_volumes, brick_values, brick_size
858+
calculated_values = dict(dates=new_dates,volumes=new_volumes,
859+
values=brick_values,size=brick_size)
860+
return [rectCollection,], calculated_values
859861

860862

861863
def _construct_pointnfig_collections(dates, highs, lows, volumes, config_pointnfig_params, closes, marketcolors=None):
@@ -998,7 +1000,7 @@ def _construct_pointnfig_collections(dates, highs, lows, volumes, config_pointnf
9981000
box_values = [] # y values for the boxes
9991001
circle_patches = [] # list of circle patches to be used to create the cirCollection
10001002
line_seg = [] # line segments that make up the Xs
1001-
1003+
10021004
for index, difference in enumerate(boxes):
10031005
diff = abs(difference)
10041006

@@ -1007,9 +1009,9 @@ def _construct_pointnfig_collections(dates, highs, lows, volumes, config_pointnf
10071009

10081010
x = [index] * (diff)
10091011
y = [curr_price + (i * box_size * sign) for i in range(start_iteration, diff+start_iteration)]
1010-
1012+
10111013
curr_price += (box_size * sign * (diff))
1012-
box_values.append(sum(y) / len(y))
1014+
box_values.append( y )
10131015

10141016
for i in range(len(x)): # x and y have the same length
10151017
height = box_size * 0.85
@@ -1036,7 +1038,9 @@ def _construct_pointnfig_collections(dates, highs, lows, volumes, config_pointnf
10361038
linewidths=lw,
10371039
antialiaseds=useAA
10381040
)
1039-
return [cirCollection, xCollection], new_dates, new_volumes, box_values, box_size
1041+
calculated_values = dict(dates=new_dates,counts=boxes,values=box_values,
1042+
volumes=new_volumes,size=box_size)
1043+
return [cirCollection, xCollection], calculated_values
10401044

10411045

10421046
def _construct_aline_collections(alines, dtix=None):

src/mplfinance/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
version_info = (0, 12, 7, 'alpha', 11)
2+
version_info = (0, 12, 7, 'alpha', 12)
33

44
_specifier_ = {'alpha': 'a','beta': 'b','candidate': 'rc','final': ''}
55

src/mplfinance/plotting.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -400,16 +400,25 @@ def plot( data, **kwargs ):
400400
collections =_construct_mpf_collections(ptype,dates,xdates,opens,highs,lows,closes,volumes,config,style)
401401

402402
if ptype in VALID_PMOVE_TYPES:
403-
collections, new_dates, volumes, brick_values, size = collections
404-
formatter = IntegerIndexDateTimeFormatter(new_dates, fmtstring)
405-
xdates = np.arange(len(new_dates))
403+
collections, calculated_values = collections
404+
volumes = calculated_values['volumes']
405+
pmove_dates = calculated_values['dates']
406+
pmove_values = calculated_values['values']
407+
if all([isinstance(v,(list,tuple)) for v in pmove_values]):
408+
pmove_avgvals = [sum(v)/len(v) for v in pmove_values]
409+
else:
410+
pmove_avgvals = pmove_values
411+
pmove_size = calculated_values['size']
412+
pmove_counts = calculated_values['counts'] if 'counts' in calculated_values else None
413+
formatter = IntegerIndexDateTimeFormatter(pmove_dates, fmtstring)
414+
xdates = np.arange(len(pmove_dates))
406415

407416
if collections is not None:
408417
for collection in collections:
409418
axA1.add_collection(collection)
410419

411420
if ptype in VALID_PMOVE_TYPES:
412-
mavprices = _plot_mav(axA1,config,xdates,brick_values)
421+
mavprices = _plot_mav(axA1,config,xdates,pmove_avgvals)
413422
else:
414423
mavprices = _plot_mav(axA1,config,xdates,closes)
415424

@@ -428,8 +437,8 @@ def plot( data, **kwargs ):
428437
_lows = lows
429438
_highs = highs
430439
else:
431-
_lows = brick_values
432-
_highs = [brick+size for brick in brick_values]
440+
_lows = pmove_avgvals
441+
_highs = [value+pmove_size for value in pmove_avgvals]
433442

434443
miny = np.nanmin(_lows)
435444
maxy = np.nanmax(_highs)
@@ -458,13 +467,17 @@ def plot( data, **kwargs ):
458467

459468
if config['return_calculated_values'] is not None:
460469
retdict = config['return_calculated_values']
461-
if ptype in VALID_PMOVE_TYPES:
462-
prekey = ptype
463-
retdict[prekey+'_bricks'] = brick_values
464-
retdict[prekey+'_dates'] = mdates.num2date(new_dates)
465-
retdict[prekey+'_size'] = size
466-
if config['volume']:
467-
retdict[prekey+'_volumes'] = volumes
470+
if ptype == 'renko':
471+
retdict['renko_bricks' ] = pmove_values
472+
retdict['renko_dates' ] = mdates.num2date(pmove_dates)
473+
retdict['renko_size' ] = pmove_size
474+
retdict['renko_volumes'] = volumes if config['volume'] else None
475+
elif ptype == 'pnf':
476+
retdict['pnf_dates' ] = mdates.num2date(pmove_dates)
477+
retdict['pnf_counts' ] = pmove_counts
478+
retdict['pnf_values' ] = pmove_values
479+
retdict['pnf_size' ] = pmove_size
480+
retdict['pnf_volumes' ] = volumes if config['volume'] else None
468481
if config['mav'] is not None:
469482
mav = config['mav']
470483
if len(mav) != len(mavprices):
@@ -480,7 +493,7 @@ def plot( data, **kwargs ):
480493
# Note: these are NOT mutually exclusive, so the order of this
481494
# if/elif is important: VALID_PMOVE_TYPES must be first.
482495
if ptype in VALID_PMOVE_TYPES:
483-
dtix = pd.DatetimeIndex([dt for dt in mdates.num2date(new_dates)])
496+
dtix = pd.DatetimeIndex([dt for dt in mdates.num2date(pmove_dates)])
484497
elif not config['show_nontrading']:
485498
dtix = data.index
486499
else:

0 commit comments

Comments
 (0)