日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/11/29 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用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 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 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 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 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

總結

以上是生活随笔為你收集整理的使用Python和MetaTrader在5分钟内开始构建您的交易策略的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。