如何使用 mplfinance 和 pandas ta 绘制指标线?

发布于 2025-01-09 04:04:07 字数 2048 浏览 1 评论 0原文

我有以下代码:

import MetaTrader5 as mt5
import pandas as pd
import time
import pandas_ta as ta
import mplfinance as mpf

pd.set_option('display.max_columns', 500) 
pd.set_option('display.width', 1500)      

if not mt5.initialize():
    print("initialize failed")
    mt5.shutdown()

account="FOO"
authorized=mt5.login(account, server="FOO")
if authorized:
    print("Authorized")
else:
    print("failed to connect at account #{}, error code: {}".format(account, mt5.last_error()))

cndl_72 = mt5.copy_rates_from_pos("BTCUSD", mt5.TIMEFRAME_M1, 1, 72)
cndl_72_df = pd.DataFrame(cndl_72) 
cndl_72_df['time']=pd.to_datetime(cndl_72_df['time'], unit='s') 
cndl_72_df.rename(columns={'time': 'date', 'tick_volume' : 'volume'}, inplace=True)
cndl_72_df.set_index('date', inplace=True)
cndl_72_df.drop(['spread','real_volume'], axis=1, inplace=True)
mpf.plot(cndl_72_df, type='candle')

目标是让 MACD 指标出现。

编辑#1:请参阅评论中的尝试#1。更改会引发关键错误。
编辑#2:查看更改 https://pastebin.com/qAiu1PkS,这会引发一个新错误:

编辑# 3:类型错误已修复,现在我可以从我的数据帧成功计算 MACD 并在必要时附加它。现在我需要弄清楚图表...

编辑#4:图表完成,请参阅评论。 MACD 图看起来很相似,因此数据可能是准确的。只是需要使它更漂亮......

Authorized
Traceback (most recent call last):
  File "C:\Users\hello\Documents\projects\alerter_venv\main.py", line 38, in <module>
    mpf.plot(cndl_72_df, type='candle', addplot=plots)
  File "C:\Users\hello\Documents\projects\alerter_venv\lib\site-packages\mplfinance\plotting.py", line 707, in plot
    ax = _addplot_columns(panid,panels,ydata,apdict,xdates,config)
  File "C:\Users\hello\Documents\projects\alerter_venv\lib\site-packages\mplfinance\plotting.py", line 997, in _addplot_columns
    yd = [y for y in ydata if not math.isnan(y)]
  File "C:\Users\hello\Documents\projects\alerter_venv\lib\site-packages\mplfinance\plotting.py", line 997, in <listcomp>
    yd = [y for y in ydata if not math.isnan(y)]
TypeError: must be real number, not str

据我所知,没有任何字符串可以产生这个。我如何找到来源?

I have the following code:

import MetaTrader5 as mt5
import pandas as pd
import time
import pandas_ta as ta
import mplfinance as mpf

pd.set_option('display.max_columns', 500) 
pd.set_option('display.width', 1500)      

if not mt5.initialize():
    print("initialize failed")
    mt5.shutdown()

account="FOO"
authorized=mt5.login(account, server="FOO")
if authorized:
    print("Authorized")
else:
    print("failed to connect at account #{}, error code: {}".format(account, mt5.last_error()))

cndl_72 = mt5.copy_rates_from_pos("BTCUSD", mt5.TIMEFRAME_M1, 1, 72)
cndl_72_df = pd.DataFrame(cndl_72) 
cndl_72_df['time']=pd.to_datetime(cndl_72_df['time'], unit='s') 
cndl_72_df.rename(columns={'time': 'date', 'tick_volume' : 'volume'}, inplace=True)
cndl_72_df.set_index('date', inplace=True)
cndl_72_df.drop(['spread','real_volume'], axis=1, inplace=True)
mpf.plot(cndl_72_df, type='candle')

The objective is to get an MACD indicator to appear.

EDIT #1: see attempt #1 in comments. changes throw a key error.
EDIT #2: see changes https://pastebin.com/qAiu1PkS, which throws a new error:

EDIT #3: type error fixed, now I can successfully calculate MACD from my dataframe and append it if necessary. Now I need to figure out charting...

EDIT #4: charting done, see comments. The MACD plot looks similar so the data could be accurate. Just need to make it prettier...

Authorized
Traceback (most recent call last):
  File "C:\Users\hello\Documents\projects\alerter_venv\main.py", line 38, in <module>
    mpf.plot(cndl_72_df, type='candle', addplot=plots)
  File "C:\Users\hello\Documents\projects\alerter_venv\lib\site-packages\mplfinance\plotting.py", line 707, in plot
    ax = _addplot_columns(panid,panels,ydata,apdict,xdates,config)
  File "C:\Users\hello\Documents\projects\alerter_venv\lib\site-packages\mplfinance\plotting.py", line 997, in _addplot_columns
    yd = [y for y in ydata if not math.isnan(y)]
  File "C:\Users\hello\Documents\projects\alerter_venv\lib\site-packages\mplfinance\plotting.py", line 997, in <listcomp>
    yd = [y for y in ydata if not math.isnan(y)]
TypeError: must be real number, not str

As far as I can tell there aren't any strings that could be producing this. How do I find the source?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2025-01-16 04:04:07

工作代码:

import MetaTrader5 as mt5
import pandas as pd
import pandas_ta as ta
import mplfinance as mpf
 
pd.set_option('display.max_columns', 500) 
pd.set_option('display.width', 1500) 
 
if not mt5.initialize():
    print("initialize failed")
    mt5.shutdown()
 
account="HIDDEN"
authorized=mt5.login(account, server="HIDDEN")
if authorized:
    print("Authorized")
else:
    print("failed to connect at account #{}, error code: {}".format(account, mt5.last_error()))
 
cndl_72 = mt5.copy_rates_from_pos("BTCUSD", mt5.TIMEFRAME_M5, 1, 144)
cndl_72_df = pd.DataFrame(cndl_72) 
cndl_72_df['time']=pd.to_datetime(cndl_72_df['time'], unit='s') 
cndl_72_df.rename(columns={'time': 'date', 'tick_volume' : 'volume'}, inplace=True)
cndl_72_df.set_index('date', inplace=True)
cndl_72_df.drop(['spread','real_volume'], axis=1, inplace=True)
 
macd = cndl_72_df.ta.macd(close='close', fast=12, slow=26, signal=9)
 
macd_plot = mpf.make_addplot(macd["MACD_12_26_9"], panel=1, color='fuchsia', title="MACD")
macd_hist_plot = mpf.make_addplot(macd["MACDh_12_26_9"], type='bar', panel=1) 
macd_signal_plot = mpf.make_addplot(macd["MACDs_12_26_9"], panel=1, color='b')
plots = [macd_plot, macd_signal_plot, macd_hist_plot]
 
mpf.plot(cndl_72_df, type='candle', style='yahoo', addplot=plots)

working code:

import MetaTrader5 as mt5
import pandas as pd
import pandas_ta as ta
import mplfinance as mpf
 
pd.set_option('display.max_columns', 500) 
pd.set_option('display.width', 1500) 
 
if not mt5.initialize():
    print("initialize failed")
    mt5.shutdown()
 
account="HIDDEN"
authorized=mt5.login(account, server="HIDDEN")
if authorized:
    print("Authorized")
else:
    print("failed to connect at account #{}, error code: {}".format(account, mt5.last_error()))
 
cndl_72 = mt5.copy_rates_from_pos("BTCUSD", mt5.TIMEFRAME_M5, 1, 144)
cndl_72_df = pd.DataFrame(cndl_72) 
cndl_72_df['time']=pd.to_datetime(cndl_72_df['time'], unit='s') 
cndl_72_df.rename(columns={'time': 'date', 'tick_volume' : 'volume'}, inplace=True)
cndl_72_df.set_index('date', inplace=True)
cndl_72_df.drop(['spread','real_volume'], axis=1, inplace=True)
 
macd = cndl_72_df.ta.macd(close='close', fast=12, slow=26, signal=9)
 
macd_plot = mpf.make_addplot(macd["MACD_12_26_9"], panel=1, color='fuchsia', title="MACD")
macd_hist_plot = mpf.make_addplot(macd["MACDh_12_26_9"], type='bar', panel=1) 
macd_signal_plot = mpf.make_addplot(macd["MACDs_12_26_9"], panel=1, color='b')
plots = [macd_plot, macd_signal_plot, macd_hist_plot]
 
mpf.plot(cndl_72_df, type='candle', style='yahoo', addplot=plots)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文