使用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.pswp.cn/news/391989.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391989.shtml
英文地址,請注明出處:http://en.pswp.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…