Help in Accessing MT5 terminal data with Python

 

Hi guys,

Thank you in advance for your time and effort.

I am very new to MT5/MQL5 and Python, trying to migrate from Pinescript to MT5 using Python for Auto-trading. I dont have much coding knowledge. Been using internet resources and some ChatGPT.

I have come across a problem which i need help.

Given my below code I cant seem to fetch the 3 Min required data, what am i missing?

I am using the Anaconda Environment with the Jupyter Notebook

I have pip installed MetaTrade5

Thank you for your help!

import pandas as pd

import numpy as np
import matplotlib.pyplot as plt
import MetaTrader5 as mt5
from datetime import datetime

import pytz

path = "C:\Program Files\MT5\terminal64.exe"

login = 22548462

password = "************"

server = "FOREX.comGlobal-Demo531"

timeout = 10000

portable = False

if mt5.initialize(path=path, login=login, password=password, server=server, timeout=timeout, portable=portable):

    print ("Initialization successful")

mt5.initialize()

True

symbol = "AAPL"

number_of_data = 1500

timeframe = mt5.TIMEFRAME_M3


from_date = datetime.now()

rates = mt5.copy_rates_from(symbol, timeframe, from_date, number_of_data)

print(rates)

None

Improperly formatted code edited by moderator.

Documentation on MQL5: Python Integration / copy_rates_from
Documentation on MQL5: Python Integration / copy_rates_from
  • www.mql5.com
copy_rates_from - Python Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

I edited your improperly formatted code, but I'm not a Python programmer so please check it.

Please, always use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
//+------------------------------------------------------------------+
//|                                                   SMABot.mq5     |
//|                        Copyright MetaQuotes Ltd.                |
//|                https://www.mql5.com/en/users/metaquotes         |
//+------------------------------------------------------------------+
#property copyright "Copyright MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1  clrRed
#property indicator_color2  clrGreen
#property indicator_color3  clrBlue
#property indicator_width1  1
#property indicator_width2  1
#property indicator_width3  1

//--- indicator buffers
double SMA7Buffer[];
double SMA14Buffer[];
double SMA28Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--- indicator buffers mapping
   SetIndexBuffer(0, SMA7Buffer);
   SetIndexBuffer(1, SMA14Buffer);
   SetIndexBuffer(2, SMA28Buffer);

   //--- set plot styles using PlotIndexSetInteger
   PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_LINE);

   PlotIndexSetInteger(0, PLOT_LINE_WIDTH, 1); // Line width for SMA7
   PlotIndexSetInteger(1, PLOT_LINE_WIDTH, 1); // Line width for SMA14
   PlotIndexSetInteger(2, PLOT_LINE_WIDTH, 1); // Line width for SMA28

   //--- set index labels
   IndicatorSetString(INDICATOR_SHORTNAME, "SMA 7, 14, 28");
   PlotIndexSetString(0, PLOT_LABEL, "SMA7");
   PlotIndexSetString(1, PLOT_LABEL, "SMA14");
   PlotIndexSetString(2, PLOT_LABEL, "SMA28");

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    // Loop through each bar to calculate SMAs
    for(int i = 0; i < rates_total; i++)
    {

        // Calculate SMA7 for the i-th bar
        SMA7Buffer[i] = iMA(_Symbol, _Period, 7, 0, MODE_SMA, PRICE_CLOSE);

        // Calculate SMA14 for the i-th bar
        SMA14Buffer[i] = iMA(_Symbol, _Period, 14, 0, MODE_SMA, PRICE_CLOSE);

        // Calculate SMA28 for the i-th bar
        SMA28Buffer[i] = iMA(_Symbol, _Period, 28, 0, MODE_SMA, PRICE_CLOSE);
    }
 
    return(rates_total);
}
hi again, I was not able to find a solution to my previous problem, so here is a new problem that I am getting, I want to plot 3 SMA, 7, 14, 28, I am not getting any compiling errors, but I cannot for the life of me see the values or the lines on the chart, the value just reads 10 for the SMA7, any help would be greatly appreciated, thank you.
MetaQuotes
  • 2023.12.15
  • www.mql5.com
Trader's profile