我有以下代码:
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
据我所知,没有任何可能产生这种情况的字符串。我怎样才能找到源头?
发布于 2022-02-23 17:16:25
工作代码:
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)
https://stackoverflow.com/questions/71213315
复制相似问题