如何使用 mplfinance 和 pandas ta 绘制指标线?
我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
工作代码:
working code: