简体   繁体   English

如何使用 mplfinance 填充颜色?

[英]How to fill color using mplfinance?

I plotted the candlestick chart for the stock price along with some indicators using the mplfinance package.我使用 mplfinance 包绘制了股票价格的烛台图以及一些指标。 I bought and sold the stock at different prices for many times.我多次以不同的价格买卖股票。 Sometimes I win and somethimes I lose.有时我赢了,有时我输了。 I want to fill the region where I win with green color and the regin where I lose with red color on the main plot.我想在主情节上用绿色填充我获胜的区域和我用红色失败的区域。 How can I do it?我该怎么做?

For instance, how to fill the region between 2022-04-07 and 2022-04-21 with red and the region between 2022-04-27 and 2022-05-10 with green?例如,如何用红色填充 2022-04-07 和 2022-04-21 之间的区域,以及用绿色填充 2022-04-27 和 2022-05-10 之间的区域? 在此处输入图像描述

import mplfinance as mpf

kwargs = dict(
type='candle', 
mav=(5,10,30), 
volume=True, 
title='%s'%(stock_df.iloc[0,0]),    
ylabel='Price', 
ylabel_lower='Volume', 
#figratio=(1200/72,480/60), 
figscale=3,
datetime_format='%Y-%m-%d',
xrotation=0
)

add_plot = [
mpf.make_addplot(rsi.tail(60),panel=2,ylabel='RSI'),
mpf.make_addplot(slowk.tail(60),panel=3,color='darkslateblue',ylabel='KDJ'),
mpf.make_addplot(slowd.tail(60),panel=3,color='limegreen'),
mpf.make_addplot(slowj.tail(60),panel=3,color='orangered')
]

mpf.plot(
        stock_df.tail(60),  # the stock price 
        **kwargs,
        addplot=add_plot,
        style='yahoo'
    )

You can use kwarg fill_between when calling mpf.plot() or when calling mpf.make_addplot() .您可以在调用mpf.plot()或调用mpf.make_addplot() ) 时使用 kwarg fill_between Both methods accept fill_between .两种方法都接受fill_between

Do whichever is more convenient.做哪个更方便。 The difference is that, when passing a dict for you fill_between value, mpf.plot() will accept panel as one of the keys in the dict (to place the fill_between on a particular panel), whereas, since mpf.make_addplot() has its own panel kwarg, there is no need to include it in the fill_between dict.不同之处在于,当为您的fill_between值传递dict时, mpf.plot()将接受panel作为 dict 中的键之一(将 fill_between 放置在特定面板上),而由于mpf.make_addplot()有它自己的panel kwarg,无需将其包含在fill_between字典中。

To accomplish what you want, coloring only between particular dates, you must pass, a where value within the fill_between dict.为了完成想要的,只在特定日期之间着色,你必须在fill_between字典中传递一个where值。 This where value must be a boolean list, equal in length to your dataframe, where True indicates that color should be placed at that point, and False indicates no color. where的 value 必须是一个布尔列表,长度与您的数据框相等,其中True表示应将颜色放置在该点,而False表示没有颜色。

For specific examples, see the fill_between tutorial in the mplfinance documentation.有关具体示例,请参阅 mplfinance 文档中的fill_between教程

Note: kwarg fill_betweenwas recently enhanced with version 0.12.9b0 , adding support for make_addplot and allowing a list of fill_between dicts to be passed.注意:kwarg fill_between最近在 0.12.9b0 版本中得到了增强,增加了对make_addplot的支持并允许传递一个fill_between字典list Make sure you have the latest version of mplfinance: pip install --upgrade mplfinance .确保您拥有最新版本的 mplfinance: pip install --upgrade mplfinance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM