In one of my last posts, I showed how to create graphics using the Plotly library. To do this, we import data from MetaTrader in a ‘raw’ way without automation. Today, we will learn how to automate this process and plot a heatmap graph of the correlations of different assets in just a few lines of code.
在上一篇文章中,我展示了如何使用Plotly库创建图形。 为此,我们无需自动化即可以“原始”方式从MetaTrader导入数据。 今天,我们将学习如何自动执行此过程,并仅用几行代码就可以绘制出不同资产的相关性的热图图。
How to integrate Python and MetaTrader? I follow the following steps:
如何集成Python和MetaTrader? 我遵循以下步骤:
- Having installed MetaTrader 5 and Python 3.8 on your machine 在计算机上安装了MetaTrader 5和Python 3.8
- Installing the Python libraries: MetaTrader5, matplotlib, and pandas 安装Python库:MetaTrader5,matplotlib和pandas
- Importing the data 导入数据
- Plot the Graph 绘制图
安装库 (Installing the Libraries)
If you already have Python installed on your computer, open the terminal and install the necessary libraries with the command:
如果您已经在计算机上安装了Python,请打开终端并使用以下命令安装必要的库:
pip install MetaTrader5
pip install pandas
pip install matplotlib
Have in mind that you must have installed the latest version of MetaTrader on your computer for the integration to work.
请记住,您必须在计算机上安装最新版本的MetaTrader才能进行集成。
收集资料 (Collecting the Data)
We arrived at the interesting part. We will start the development of our small data collection program.
我们到达了有趣的部分。 我们将开始开发小型数据收集程序。
The first step is to import the necessary libraries:
第一步是导入必要的库:
import MetaTrader5 as mt5
import pandas as pd
import matplotlib.pyplot as plt
After, we initialize the MetaTrader terminal with the code:
之后,我们使用以下代码初始化MetaTrader终端:
mt5.initialize()
We define the symbols of the assets that we want to analyze in an array. I am Brazilian, and I trade on the Brazilian stock exchange. Thus, the assets described in this article will not work in other brokerages.
我们在数组中定义要分析的资产的符号。 我是巴西人,我在巴西证券交易所交易。 因此,本文所述的资产将无法在其他经纪公司中使用。
symbols = ['GOAU4','WEGE3','VVAR3','PRIO3','MRFG3']
data = pd.DataFrame()
For each symbol in the array, we collect the data defining the time of each bar and the quantity. Then, we feed the data frame with the closing prices of each request:
对于数组中的每个符号,我们收集定义每个柱形时间和数量的数据。 然后,我们向数据框提供每个请求的收盘价:
for i in symbols:
rates = mt5.copy_rates_from_pos(i, mt5.TIMEFRAME_D1, 0, 1000)
data[i] = [y['close'] for y in rates]
We will now close the communication with MetaTrader, as we already have the data for analysis.
由于我们已经有要分析的数据,因此我们现在将关闭与MetaTrader的通信。
mt5.shutdown()
计算退货 (Calculating Returns)
Calculating returns is quite easy. Just call the dataframe’s pct_change () method, and you’re good to go.
计算收益非常容易。 只需调用数据框的pct_change()方法,就可以了。
retornos = data.pct_change()
相关计算 (Correlation Calculation)
Like returns, correlations can also be easily calculated by calling the dataframe’s corr () method.
像返回一样,通过调用数据框的corr()方法也可以轻松计算相关性。
corr = data.corr()
绘制HeatMap (Plotting the HeatMap)
To build the heat graph, we will use the matplotlib library. So:
要构建热图,我们将使用matplotlib库。 所以:
plt.figure(figsize=(10,10))
plt.imshow(corr, cmap = 'RdYlGn', interpolation='none', aspect='auto')
plt.colorbar()
plt.xticks(range(len(corr)), corr.columns, rotation = 'vertical')
plt.yticks(range(len(corr)), corr.columns)
plt.suptitle('MAPA de CALOR - ATIVOS', fontsize = 15, fontweight = 'bold')
plt.show()
结论 (Conclusion)
In this post, we saw how to connect Python and MetaTrader 5, how to import the data of the assets we want to analyze, and how to create a heatmap of the correlations of the returns of these assets.
在本文中,我们看到了如何连接Python和MetaTrader 5,如何导入要分析的资产的数据,以及如何为这些资产的收益相关建立热图。
Thanks for reading, see you next time! Let me know if you have questions. Cheers!
感谢您的阅读,下次见! 如果您有任何问题,请告诉我。 干杯!
Gain Access to Expert View — Subscribe to DDI Intel
获得访问专家视图的权限- 订阅DDI Intel
翻译自: https://medium.com/datadriveninvestor/build-your-trading-strategies-in-5-minutes-with-python-and-metatrader-3e9fd5c62956
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391989.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!