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

Bug Report: Multiprocessing bug #161

Open
kenter1 opened this issue Jun 9, 2020 · 3 comments
Open

Bug Report: Multiprocessing bug #161

kenter1 opened this issue Jun 9, 2020 · 3 comments
Labels
bug Something isn't working

Comments

@kenter1
Copy link

kenter1 commented Jun 9, 2020

Describe the bug
When using multiprocessing the images generated have varying plot sizes and adding a lock to the plot function or plotting in a loop works fine.

To Reproduce
Steps to reproduce the behavior:
Plot multiple charts using
python multiprocessing.Pool
map, apply, etc

Expected behavior
The generated charts should be the same as the charts generated in a non multiproc

@kenter1 kenter1 added the bug Something isn't working label Jun 9, 2020
@DanielGoldfarb
Copy link
Collaborator

To save me time looking into this, can you please post exact code you used to reproduce this, and some screen shots of the varying plot sizes? Please also provide the data to input. Thank you.

@kenter1
Copy link
Author

kenter1 commented Jun 9, 2020

To reproduce
image

using a loop

import pandas as pd
import mplfinance as mpf
from multiprocessing import Pool

def generate_candle_plot(date, n_candles=30):
    mc = mpf.make_marketcolors(
        up='green',
        down='red',
        edge={'up':'green','down':'red'},
        wick={'up':'white','down':'orange'},
        inherit=True,
        alpha=1.0
    )

    s  = mpf.make_mpf_style(base_mpf_style='classic', facecolor='white', marketcolors=mc)

    _ = mpf.plot(df[date < df.index][:n_candles],
        axisoff=True,
        type = 'candle',
        style = s,
        volume = False,
        show_nontrading=False,
        savefig = dict(fname="{}_loop.png".format(str(date)), bboxes='tight'),
        figscale=.5
    )
    
    return 
    
r = requests.get('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=IBM&outputsize=full&apikey=demo')
df = pd.DataFrame.from_records(r.json()["Time Series (Daily)"]).T[["1. open","2. high","3. low","4. close","6. volume"]]
df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
df = df.astype(float)
df.index = pd.to_datetime(df.index)

items = df.index[:1000]

for item in items:
    generate_candle_plot(item)

using multiprocessing

import requests 
import pandas as pd
import mplfinance as mpf
from multiprocessing import Pool

def generate_candle_plot(date, n_candles=30):
    mc = mpf.make_marketcolors(
        up='green',
        down='red',
        edge={'up':'green','down':'red'},
        wick={'up':'white','down':'orange'},
        inherit=True,
        alpha=1.0
    )

    s  = mpf.make_mpf_style(base_mpf_style='classic', facecolor='white', marketcolors=mc)

    _ = mpf.plot(df[date < df.index][:n_candles],
        axisoff=True,
        type = 'candle',
        style = s,
        volume = False,
        show_nontrading=False,
        savefig = dict(fname="{}_multi.png".format(str(date)), bboxes='tight'),
        figscale=.5
    )
    
    return 
    
r = requests.get('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=IBM&outputsize=full&apikey=demo')
df = pd.DataFrame.from_records(r.json()["Time Series (Daily)"]).T[["1. open","2. high","3. low","4. close","6. volume"]]
df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
df = df.astype(float)
df.index = pd.to_datetime(df.index)

items = df.index[:1000]

with Pool(12) as pool:
    pool.map(generate_candle_plot, items)

@DanielGoldfarb
Copy link
Collaborator

@kenter1
Thanks. Will take a look in the next day or so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants