使用Python和MetaTrader在5分钟内开始构建您的交易策略

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()
Image for post
Image by author — Close prices from stocks
图片由作者提供—股票的收盘价

计算退货 (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()
Image for post
Image by author — Stock Daily Returns
图片由作者—图库照片

相关计算 (Correlation Calculation)

Like returns, correlations can also be easily calculated by calling the dataframe’s corr () method.

像返回一样,通过调用数据框的corr()方法也可以轻松计算相关性。

corr = data.corr()
Image for post
Image by Author — Stocks Correlation
图片由作者—图库照片相关

绘制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()
Image for post
Image by Author — HeatMap Stocks Correlations
图片由作者提供— HeatMap股票的相关性

结论 (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,一经查实,立即删除!

相关文章

卷积神经网络 手势识别_如何构建识别手语手势的卷积神经网络

卷积神经网络 手势识别by Vagdevi Kommineni通过瓦格德维科米尼(Vagdevi Kommineni) 如何构建识别手语手势的卷积神经网络 (How to build a convolutional neural network that recognizes sign language gestures) Sign language has been a major boon for people who are h…

spring—第一个spring程序

1.导入依赖 <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.9.RELEASE</version></dependency>2.写一个接口和实现 public interface dao {public void save(); }…

请对比html与css的异同,css2与css3的区别是什么?

css主要有三个版本&#xff0c;分别是css1、css2、css3。css2使用的比较多&#xff0c;因为css1的属性比较少&#xff0c;而css3有一些老式浏览器并不支持&#xff0c;所以大家在开发的时候主要还是使用css2。CSS1提供有关字体、颜色、位置和文本属性的基本信息&#xff0c;该版…

基础 之 数组

shell中的数组 array (1 2 3) array ([1]ins1 [2]ins2 [3]ins3)array ($(命令)) # 三种定义数组&#xff0c;直接定义&#xff0c;键值对&#xff0c;直接用命令做数组的值。${array[*]}${array[]}${array[0]} # 输出数组中的0位置的值&#xff0c;*和…

Linux_异常_08_本机无法访问虚拟机web等工程

这是因为防火墙的原因&#xff0c;把响应端口开启就行了。 # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m st…

Building a WAMP Dev Environment [3/4] - Installing and Configuring PHP

Moved to http://blog.tangcs.com/2008/10/27/wamp-installing-configuring-php/转载于:https://www.cnblogs.com/WarrenTang/archive/2008/10/27/1320069.html

ipywidgets_未来价值和Ipywidgets

ipywidgetsHow to use Ipywidgets to visualize future value with different interest rates.如何使用Ipywidgets可视化不同利率下的未来价值。 There are some calculations that even being easy becoming better with a visualization of his terms. Moreover, the sooner…

2019 css 框架_宣布CSS 2019调查状态

2019 css 框架by Sacha Greif由Sacha Greif 宣布#StateOfCSS 2019调查 (Announcing the #StateOfCSS 2019 Survey) 了解JavaScript状况之后&#xff0c;帮助我们确定最新CSS趋势 (After the State of JavaScript, help us identify the latest CSS trends) I’ve been using C…

计算机主机后面辐射大,电脑的背面辐射大吗

众所周知&#xff0c;电子产品的辐射都比较大&#xff0c;而电脑是非常常见的电子产品&#xff0c;它也存在着一定的辐射&#xff0c;那么电脑的背面辐射大吗?下面就一起随佰佰安全网小编来了解一下吧。有资料显示&#xff0c;电脑后面的辐射比前面大&#xff0c;长期近距离在…

spring— Bean标签scope配置和生命周期配置

scope配置 singleton 默认值&#xff0c;单例的prototype 多例的request WEB 项目中&#xff0c;Spring 创建一个 Bean的对象&#xff0c;将对象存入到 request 域中session WEB 项目中&#xff0c;Spring 创建一个 Bean 的对象&#xff0c;将对象存入session 域中global sess…

装饰器3--装饰器作用原理

多思考&#xff0c;多记忆&#xff01;&#xff01;&#xff01; 转载于:https://www.cnblogs.com/momo8238/p/7217345.html

用folium模块画地理图_使用Folium表示您的地理空间数据

用folium模块画地理图As a part of the Data Science community, Geospatial data is one of the most crucial kinds of data to work with. The applications are as simple as ‘Where’s my food delivery order right now?’ and as complex as ‘What is the most optim…

Windows下安装Python模块时环境配置

“Win R”打开cmd终端&#xff0c;如果直接在里面使用pip命令的时候&#xff0c;要么出现“syntax invalid”&#xff0c;要么出现&#xff1a; pip is not recognized as an internal or external command, operable program or batch file. 此时需要将C:\Python27\Scripts添加…

播客2008

http://blog.tangcs.com/2008/12/29/year-2008/转载于:https://www.cnblogs.com/WarrenTang/articles/1364465.html

linear在HTML的作用,CSS3里的linear-gradient()函数

linear-gradient() 函数用于创建一个线性渐变的 "图像"。为了创建一个线性渐变&#xff0c;你需要设置一个起始点和一个方向(指定为一个角度)的渐变效果。你还要定义终止色。终止色就是你想让Gecko去平滑的过渡&#xff0c;并且你必须指定至少两种&#xff0c;当然也…

golang底层深入_带有Golang的GraphQL:从基础到高级的深入研究

golang底层深入by Ridham Tarpara由里德姆塔帕拉(Ridham Tarpara) 带有Golang的GraphQL&#xff1a;从基础到高级的深入研究 (GraphQL with Golang: A Deep Dive From Basics To Advanced) GraphQL has become a buzzword over the last few years after Facebook made it ope…

spring—Bean实例化三种方式

1&#xff09; 使用无参构造方法实例化 它会根据默认无参构造方法来创建类对象&#xff0c;如果bean中没有默认无参构造函数&#xff0c;将会创建失败 <?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.o…

bzoj 3439: Kpm的MC密码

Description 背景 想Kpm当年为了防止别人随便进入他的MC&#xff0c;给他的PC设了各种奇怪的密码和验证问题&#xff08;不要问我他是怎么设的。。。&#xff09;&#xff0c;于是乎&#xff0c;他现在理所当然地忘记了密码&#xff0c;只能来解答那些神奇的身份验证问题了。。…

python创建类统计属性_轻松创建统计数据的Python包

python创建类统计属性介绍 (Introduction) Sometimes you may need a distribution figure for your slide or class. Since you are not using data, you want a quick solution.有时&#xff0c;您的幻灯片或课程可能需要一个分配图。 由于您不使用数据&#xff0c;因此需要快…

pytorch深度学习_在本完整课程中学习在PyTorch中应用深度学习

pytorch深度学习In this complete course from Fawaz Sammani you will learn the key concepts behind deep learning and how to apply the concepts to a real-life project using PyTorch. 在Fawaz Sammani的完整课程中&#xff0c;您将学习深度学习背后的关键概念&#x…