Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ununiformly spaced data #393

Open
fxmarty opened this issue May 16, 2021 · 10 comments
Open

Support for ununiformly spaced data #393

fxmarty opened this issue May 16, 2021 · 10 comments
Labels
enhancement New feature or request

Comments

@fxmarty
Copy link

fxmarty commented May 16, 2021

Hello,

Today I tried to plot data for which the time column was not uniformly spaced, there is the result:

mpl-bug

Corresponding data:

                         Open      High       Low     Close        Volume
Date                                                                     
2021-01-10 16:43:50       NaN       NaN       NaN       NaN      0.000000
2021-01-10 16:49:40       NaN       NaN       NaN       NaN      0.000000
2021-01-10 16:57:28  0.339755  0.339755  0.339755  0.339755   2489.423910
2021-01-10 17:05:17  0.337448  0.337448  0.334059  0.334627  15050.000000
2021-01-10 17:10:20  0.335243  0.336025  0.335243  0.336025   3213.676925
2021-01-10 17:17:21  0.336025  0.336025  0.324992  0.324992  28470.776438
2021-01-10 17:24:09  0.326430  0.326905  0.326430  0.326905   1989.542713
2021-01-10 17:31:11       NaN       NaN       NaN       NaN      0.000000
2021-01-10 17:36:33  0.323282  0.324741  0.318373  0.323247  46563.285561
2021-01-10 17:43:19  0.323247  0.328985  0.323247  0.328985  19099.188282
2021-01-10 17:50:08       NaN       NaN       NaN       NaN      0.000000
2021-01-10 17:54:13       NaN       NaN       NaN       NaN      0.000000
2021-01-10 18:00:22  0.335203  0.336286  0.334394  0.334394  26723.586340
2021-01-10 18:07:07  0.333296  0.333296  0.331800  0.331800   6210.287422
2021-01-10 18:14:16  0.334205  0.334205  0.334205  0.334205   5037.527533
2021-01-10 18:21:39  0.333101  0.333101  0.333101  0.333101   1308.452131
2021-01-10 18:28:06  0.331582  0.331582  0.331582  0.331582   5000.000000
2021-01-10 18:34:04  0.328742  0.328742  0.328742  0.328742   6832.647206
2021-01-10 18:42:57  0.330514  0.330514  0.330514  0.330514   6000.000000
2021-01-10 18:49:21       NaN       NaN       NaN       NaN      0.000000

We can see two problems:

  • The candles' widths are not proportional to the duration of each row.
  • The volumes widths are not proportional to the duration of each row, and worse, the volume bars may overlap with their neighbors.

It would be nice to be able to handle this case natively with mplfinance.

Note: The use case is to inspect transactions inside blocks in a given blockchain (e.g. Bitcoin, Ethereum). The mining time is not constant.

Thank you!

@fxmarty fxmarty added the enhancement New feature or request label May 16, 2021
@fxmarty fxmarty changed the title Feature Request: Support for uniformly spaced data Feature Request: Support for un-uniformly spaced data May 16, 2021
@fxmarty fxmarty changed the title Feature Request: Support for un-uniformly spaced data Feature Request: Support for ununiformly spaced data May 16, 2021
@fxmarty fxmarty changed the title Feature Request: Support for ununiformly spaced data Support for ununiformly spaced data May 16, 2021
@DanielGoldfarb
Copy link
Collaborator

Marty,
(let me know if ok to call you that).

  • Please set kwarg show_nontrading=True. This will allow the time axis to be linear and evenly spaced.

By definition, show_nontrading=False (the default value) means:

  • any missing time periods are considered as non-trading periods, and
  • you don't want to see them, so they are skipped over and not displayed on the plot.

@fxmarty
Copy link
Author

fxmarty commented May 16, 2021

Thanks a lot for your answer! My options are the following for plotting above:

...
mpf.plot(dataset, type="candle", volume=True, show_nontrading=True,
         style="charles", title=title, addplot=plots_list)
...

I acknowledge it is a very edgy case. To me it looks like I would rather need to change this value

width_config['volume_width' ] = 0.5*avg_dist_between_points
, and pass e.g. a list of variable widths if I want to have an adaptative width w.r.t. the time length of one row.

An alternative could be to keep all bars of the same width (including the candles), but to distort the time axis to take into account this non-uniformity.

Edit: Most probably the plot I gave above is misleading to carry my point. The issue is not with 0 volumes rows (rows with NaN values), but rather stems from the fact that each row have time points which are not equally spaced.

@DanielGoldfarb
Copy link
Collaborator

So you actually want the candle widths and volume bar widths to vary? Wider for a larger time period since the previous candle and narrower for a smaller time period?

@DanielGoldfarb
Copy link
Collaborator

Please confirm the above question.
I will play around with the data you posted, and see what I can come up with.

@fxmarty
Copy link
Author

fxmarty commented May 17, 2021

Thanks a lot! Yes, when this case arises, I would either expect:

  • A warning message saying the non-uniform case is not supported
  • Width-adaptative candles / volume
  • Width-fixed candles / volume, but with a distorted time axis

In the evening I can post a proper file for the data if you want to have a look.

@fxmarty
Copy link
Author

fxmarty commented May 17, 2021

Here is a reproducible example: https://pastebin.com/4Ft7Md04

@DanielGoldfarb
Copy link
Collaborator

  • Please note that for the case of show_nontrading=False you do have "Width-fixed candles/volume, but distorted time axis."

As noted in your PR, Non-trading periods are defined as rows that are completely missing from the dataframe: that is, both the data and the datetime index entry are missing for a given date/time where trading does not occur.

However a row that exists but is filled with float('nan') is not considered non-trading, but rather missing trade data.

@DanielGoldfarb
Copy link
Collaborator

@fxmarty
By the way, can you point me to an example of a candlestick chart where the candle widths vary? I'm thinking it will be relatively easy to implement, and I'm willing to do so if my hunch is right that it will be easy to do without disturbing the code around it, but I have never actually seen nor even heard of such a thing.

As I understand it, one of the central ideas in candlestick charts is that each candle represents a set chunk of time, be it 1 minute per candle, or 15 minutes per candle, or an hour, a day, a week, etc. This enables one to visualization not only where the market is moving, but also how fast the market is moving. Since each candle represents the same amount of time, then taller candles represent fast moving markets, while shorter candles represent slow moving markets.

So I would be very interested to learn if some people routinely use a type of candlestick chart where the width of each candle varies. Can you point to an example and/or article online somewhere?

Thanks. --Daniel

@fxmarty
Copy link
Author

fxmarty commented May 19, 2021

Thanks a lot for your thoughtful remarks! Actually this got me curious and I did not know it, but it looks like this kind of chart with width-variable candles are used as a type of candle called "CandleVolume": see https://school.stockcharts.com/doku.php?id=chart_analysis:candlevolume and https://cmtassociation.org/kb/candlevolume-chart-definition/ . It looks a bit exotic though, but funny to see.

As I said a bit above, the use case I was thinking about is a bit different: for blockchains which record transactions (swaps) and do not have a fixed block time. Say block 0 is minted at 5:46:05, block 1 is minted at 5:48:23, and block 2 is minted at 5:52:12. Say we want to plot a chart with a 5 min resolution. Does block 2 belong to the time frame 5:45-5:50 or to the time frame 5:50-5:55? Obviously we can do an arbitrary choice (and that is what I came up to do in the end), but an other approach, if you are not so much concerned about time but more concerned about the blocks themselves, would be to have on your x axis the block number.

A third approach would be to keep time on the x axis, but either 1/ to distort the time axis, or 2/ have candles of variable width.

@DanielGoldfarb
Copy link
Collaborator

@fxmarty
Thanks. Will take a look at the CandleVolume when I get a chance. Those I have heard of, but don't know much about them.

Regarding your block chain use-case. My inclination would be that block 2 definitely goes into the 5:45-5:50 block. You may find it useful to use Pandas resampling to bucket your data into even time periods. If you are not familiar, you can see some examples here. (And some additional references can be found here.)

You may also be interested in Renko charts where the x-axis represents blocks of price movement (instead of time). Just use type=renko in mpf.plot()

I don't understand how distorting the time axis would work or be helpful.
I do appreciate, however, the idea of candles of variable width. That might work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants