mplfinance icon indicating copy to clipboard operation
mplfinance copied to clipboard

PNF Box Dates & Renko reversal size

Open Pipsopath opened this issue 1 year ago • 7 comments

First of all my big appreciation to Daniel and all other contributors for this great resource!

I have a request regarding the 'pnf' resp. 'renko' chart:

  1. Is it possible to include to the return_calculated_values an additional array, that contains the dates for each box (e.g. pnf_box_date), that is matching the pnf_values? I guess it can be created easily in the same step when the pnf_values is processed.

  2. I tried to get the dates from the renko return_calculated_values, but since there is no reversal size considered and therefore not equivalent to the 'pnf' chart, it was not possible. This would lead to my 2nd proposal to add reversal brick according to the 'pnf' logic.

Pipsopath avatar Jun 07 '23 09:06 Pipsopath

Please take a look at the Output from cell In [6] and from cell In [9] in the following notebook: https://github.com/matplotlib/mplfinance/blob/master/examples/scratch_pad/price-movement_ret_calc_vals.ipynb

Is that not what you are looking for??

DanielGoldfarb avatar Jun 07 '23 12:06 DanielGoldfarb

Hi Daniel, thanks for the quick reply.

In [9] gives the Dates for each column (start of the trend), but I am looking for the dates within each column for the X/O boxes. Currently there is the pnf_values and my requested dates would give the time of reaching these pnf_values. I tried to work around by generate these dates by using the dates from the return_calculated_values of renko charts In[6] (which should in theory be working), but as renko does not consider reverse parameter, I can not pass over same set of parameters for renko and pnf.

From performance point of view it would also consume more calcuation time to run pnf and renko. I guess a solution would be, where there is the trigger to fill the pnf_values array, in the same time the requested pnf_box_date array can be filled with the date of the tick. The overall idea is to have the exact dates of signals that can be then used to transfer to a candlestick chart.

Nevertheless, an reversal parameter in the renko would also be great, like it was added to the pnf.

Pipsopath avatar Jun 07 '23 12:06 Pipsopath

OK. I did some playing around with the dates, and I've added a new key to the return_calculated_values dict for PNF.

Try this: pip install "git+https://github.com/DanielGoldfarb/mplfinance.git@issue623_pnf"

The new key is called pnf_detail. It contains the same information as pnf_values but also contains a timestamp for each box in each column. Let me know if that's what you had in mind.

Regarding dates for renko, they are already there, under the key renko_dates. Renko bricks do not stack up vertically (as do pnf boxes) therefore there is exactly one date for each brick.

As a general rule, to see what is in the calculated values dict, you can use code similar to this:

cv= {}
mpf.plot( df, type='renko', return_calculated_values=cv, volume=True)

print('\ncv.keys()=',cv.keys(),'\n')

import pprint
pp = pprint.PrettyPrinter()
pp.pprint(cv)

The above will show you everthing in the return_calculated_values dict, so there is no need to remember what key each item is listed under.

DanielGoldfarb avatar Jun 07 '23 20:06 DanielGoldfarb

Many thanks for this wonderful resource! I have a query regarding the atr used in Renko (and PnF). Say, I plot data from 100 to 175 bars as a Renko chart. Which atr is used to calculate the box size? (The atr for the first brick, i.e. bar number 100, or the atr for the last brick, i.e. bar number 175 or atr for the previous brick or some other method?) Thanks in advance.

SSTrader100 avatar Jul 24 '23 15:07 SSTrader100

@SSTrader100 The ATR used is that calculated for the most recent data point.

DanielGoldfarb avatar Jul 24 '23 17:07 DanielGoldfarb

Thanks for the clarification. I am plotting 5 minute bars as Renko and I refresh the chart every five minutes. The starting date and time do not change. So, after refresh, often the previous Renko bars change since the latest ATR has changed. After your clarification, I have used pandas_ta to calculate the ATR for the bar just before my starting price bar. In order to avoid unnecessary calculations, I am creating a small DF t get this single ATR value. this ATR does not change during refresh and even when I plot the data from the same starting price bar anytime. This is the code I used:

Calculate the ATR based on the df_for_atr using pandas_ta

        atr_series = ta.atr(df_for_atr['High'], df_for_atr['Low'], df_for_atr['Close'], length=atr_length)
        atr = atr_series.iloc[-1]
        brick_size = atr if brick_size == 'atr' else brick_size

SSTrader100 avatar Jul 25 '23 02:07 SSTrader100

@SSTrader100 As you have apparently noticed, mplfinance allows you to pass in the brick size. Calculating the ATR outside of mplfinance and passing it in as the brick size is perfectly fine. Alternatively, if you want, you can allow mplfinance to calculate the ATR as it makes the initial plot, and then pass it in on subsequent calls when updating the plot. To access the 'renko_size' that mplfinance calculated, use the return_calculated_values kwarg when calling mpf.plot(). Just wanted you to be aware of this alternative; either technique is fine; do whichever you feel works best for your code.

DanielGoldfarb avatar Jul 25 '23 15:07 DanielGoldfarb