matplotlib / matplotlib/mplfinance

Plotting a Swing chart over the generic OHLC chart

Open
#541 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
4.4k
Forks
678
PR merge metrics
No merged PRs in 30d

Description

I'm want to visualize a swings/price movement chart over a normal OHLC chart, e.g., like the attachment below.

![image](https://user-images.githubusercontent.com/66440980/175025853-e3eba223-2d5c-4362-b802-68d051d5cf2b.png)

I have written the following code:
```py
def get_swing_dataframe(daily: DataFrame):
last_high = -999999999
last_low = abs(last_high)
raw_dftb = []
dip = True

for row in daily.iterrows():
close = row[1]["Close"]
mark = False

if dip:
if close > last_high:
dip = False
mark = True
last_high = close
else:
if close < last_low:
dip = True
mark = True
last_low = close

if mark:
raw_dftb.append(row[1])

df = pd.DataFrame(raw_dftb)
df.index.name = "Date"
return df

def main():
daily = pd.read_csv("ES___CCB.csv", index_col=0, parse_dates=True)
daily.index.name = "Date"
swing = get_swing_dataframe(daily)
mpf.plot(
daily,
style="binance",
type="ohlc",
addplot=[mpf.make_addplot(swing, type="line", color="g", panel=1)],
)

if __name__ == "__main__":
main()
```
The issue here is that the "Swing" dataframe does not have a data entry for every single date, it only marks where and when the price changes, spitting out the following error:
```
File "D:\Andaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 1605, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "D:\Andaconda3\lib\site-packages\matplotlib\axes\_base.py", line 315, in __call__
yield from self._plot_args(this, kwargs)
File "D:\Andaconda3\lib\site-packages\matplotlib\axes\_base.py", line 501, in _plot_args
raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (6232,) and (23,)
```
How can I achieve this?
Recap: I want to visualize a swings/price movement chart over a normal OHLC chart, e.g., like the attachment below.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the mpf.plot and mpf.make_addplot calls in the example, then reproduce the mismatch using the daily and swing DataFrames described. Trace how addplot data are aligned with the OHLC x-axis; done means a sparse swing series can be overlaid on the normal OHLC chart without the x/y dimension error and matches the requested swing visualization.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.