highfestiva/finplot

[BUG] fplt.horiz_time_volume returned a blank window

DieterFishLi opened this issue · 9 comments

Requirements (place an x in each of the [ ])**

  • I realize finplot is not a web lib. (Hint: it's native!)
  • I've read the snippets and not found what I'm looking for.
  • I've searched for any related issues and avoided creating a duplicate issue.
  • I've updated finplot (pip install -U finplot).
  • I've supplied the required data to run my code below.

Code to reproduce

I used the code from the Volume Profile.py example and replaced the Binance data with my own one.

import finplot as fplt
import pandas as pd
# skipped some function definitions 
df = pd.read_csv('data.csv', parse_dates=['time'])
time_volume_profile = calc_volume_profile(df, period='W', bins=20) # try fewer/more horizontal bars (graphical resolution only)
vwap = calc_vwap(period='W')
fplt.create_plot('Binance BTC futures weekly volume profile')
fplt.plot(df.time, df.close, legend='Price')
fplt.plot(df.time, vwap, style='--', legend='VWAP')
fplt.horiz_time_volume(time_volume_profile, draw_va=0.5, draw_poc=1.0)
fplt.show()

appendix data:
data.csv

Describe the bug

fplt.horiz_time_volume returned a blank window

Expected behavior

line plot of close, vwap and horizontal volume profile

Screenshots

Snipaste_2023-11-09_14-50-41

Reproducible in:

OS: Windows 10
finplot version:1.9.3
pyqtgraph version: 0.13.3
pyqt version:5.15.9

Ah, interesting. I've found one bug, but here be dragons. This will take a few days to sort out unfortch.

See fix in e12fd2a. This plots something with your data:

from collections import defaultdict
import finplot as fplt
import pandas as pd


def calc_volume_profile(df, period, bins):
    data = []
    df['hlc3'] = (df.high + df.low + df.close) / 3 # assume this is volume center per each 1m candle
    _,all_bins = pd.cut(df.hlc3, bins, right=False, retbins=True)
    for _,g in df.groupby(pd.Grouper(key='time', freq=period)):
        if len(g) == 0:
            continue
        t = g.time.iloc[0]
        volbins = pd.cut(g.hlc3, all_bins, right=False)
        price2vol = defaultdict(float)
        for iv,vol in zip(volbins, g.volume):
            price2vol[iv.left] += vol
        data.append([t, sorted(price2vol.items())])
    return data


def calc_vwap(period):
    vwap = pd.Series([], dtype = 'float64')
    df['hlc3v'] = df['hlc3'] * df.volume
    for _,g in df.groupby(pd.Grouper(key='time', freq=period)):
        if len(g) == 0:
            continue
        i0,i1 = g.index[0],g.index[-1]
        vwap = pd.concat([vwap, g.hlc3v.loc[i0:i1].cumsum() / df.volume.loc[i0:i1].cumsum()])
    return vwap


df = pd.read_csv('data.csv', parse_dates=['time'])
time_volume_profile = calc_volume_profile(df, period='W', bins=100) # try fewer/more horizontal bars (graphical resolution only)
vwap = calc_vwap(period='W')
fplt.create_plot('Binance BTC futures weekly volume profile')
fplt.plot(df.time, df.close, legend='Price')
fplt.plot(df.time, vwap, style='--', legend='VWAP')
fplt.horiz_time_volume(time_volume_profile, draw_va=0.7, draw_poc=1.0)
fplt.show()

Sorry but I forgot to mention that earlier I corrected the IndexError both in calc_volume_profile, calc_vwap. The calc_volume_profile returns extactly the structured data expected by horiz_time_volume.
Snipaste_2023-11-10_09-15-06

I ran your new code om my computer. It still yielded a blank window. Everything works well except the fplt.horiz_time_volume.
Snipaste_2023-11-10_09-15-34

I need a complete example to replicate the bug you're having.