MetaTrader 5 Python User Group - how to use Python in Metatrader - page 74

 

Good afternoon everyone!

I don't know if I'm writing in the right thread, but I hope you can correct me. And one more thing to clarify - I'm a total zero in programming!

So, I've found a code of a small program written in PYTHON that takes a quote of EUR/USD pair from well-known site and writes it in A1 cell of EUR_USD_QUOTE.csv file.

If someone has not mind, please improve this code so that a quote is automatically updated in the same cell A1 of EUR_USD_QUOTE.csv file every hour.

Sincerely, Vladimir.

import csv
import requests
from bs4 import BeautifulSoup
import os


def get_data(url_to_scrap='https://ru.investing.com/currencies/streaming-forex-rates-majors',
             file=None, save_file="Name.csv"):
    if url_to_scrap is not None:
        header = {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                  'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'}
        r = requests.get(url_to_scrap, headers=header)
        data = BeautifulSoup(r.content, 'html.parser')
    else:
        data = BeautifulSoup(open(file), 'html.parser')
    table = data.find('tr', id='pair_1')
    table = table.find_all(class_='pid-1-bid')
    row_data = []
    for row in table:
        row_data.append([row.get_text()])
    with open(save_file, 'w') as save:
        for row in row_data:
            writer = csv.writer(save, delimiter=';')
            writer.writerow(row)
    os.startfile(save_file)


get_data(save_file='EUR_USD_QUOTE.csv')
Пара EUR/USD: технический анализ, новости Forex, фундаментальный анализ - Блоги трейдеров и аналитика финансовых рынков
Пара EUR/USD: технический анализ, новости Forex, фундаментальный анализ - Блоги трейдеров и аналитика финансовых рынков
  • www.mql5.com
Валютная пара EUR/USD — самая ликвидная, поскольку в ней участвуют первая и вторая по значимости мировые резервные валюты. Это подтверждается и статистически: доллар и евро самые крупные по объему
 
MrBrooklin:

Good afternoon everyone!

I don't know if I'm writing in the right thread, but I hope you can correct me. And one more thing to clarify - I'm a total zero in programming!

So, I've found a code of a small program written in PYTHON that takes a quote of EUR/USD pair from well-known site and writes it in A1 cell of EUR_USD_QUOTE.csv file.

If someone has not mind, please improve this code so that a quote is automatically updated in the same cell A1 of EUR_USD_QUOTE.csv file every hour.

Sincerely, Vladimir.

import csv
import requests
from bs4 import BeautifulSoup
import os


def get_data(url_to_scrap='https://ru.investing.com/currencies/streaming-forex-rates-majors',
             file=None, save_file="Name.csv"):
    if url_to_scrap is not None:
        header = {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                  'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'}
        r = requests.get(url_to_scrap, headers=header)
        data = BeautifulSoup(r.content, 'html.parser')
    else:
        data = BeautifulSoup(open(file), 'html.parser')
    table = data.find('tr', id='pair_1')
    table = table.find_all(class_='pid-1-bid')
    row_data = []
    for row in table:
        row_data.append([row.get_text()])
    with open(save_file, 'w') as save:
        for row in row_data:
            writer = csv.writer(save, delimiter=';')
            writer.writerow(row)
    os.startfile(save_file)


from datetime import datetime
import time


t = datetime.now().hour
t2 = t

while True:
    t = datetime.now().hour
    if(t2 != t):
        get_data(save_file='EUR_USD_QUOTE.csv')
        t2 = t
    time.sleep(10)

 
Maxim Dmitrievsky:

Thanks, Maxim!

I've launched the finalized code in PyCharm development environment, but nothing happens yet, only the "Rerun" button is activated. I can't understand yet - does the code work or not?

There is some additional information displayed in the "Problems" tab:

  1. PEP 8: E402 module level import not at top of file: 28 - this concerns the line with the code from datetime import datetime
  2. PEP 8: E402 module level import not at top of file: 29 - this concerns the line import time
  3. Remove redundant parentheses- when I removed these parentheses from the code, the message disappeared.

Maxim, can you temporarily make the quote in the file update now at least in 1 minute to understand whether the code is working or not, otherwise I have to wait a whole hour...?

One more thing. After I had launched the initial code that I had published earlier, file EUR_USD_QUOTE.csv was immediately opened with current quote, while the modified code cannot open this file.

Maybe I'm doing something wrong or I don't understand something? Please, advise me.

Regards, Vladimir.
 
MrBrooklin:
Thank you, Maxim!

I've run the finalized code in PyCharm development environment, but nothing happens yet, only the "Rerun" button is activated. I can't understand yet - does the code work or not?

There is also additional information displayed in the "Problems" tab:

  1. PEP 8: E402 module level import not at top of file: 28 - this concerns the line with the code from datetime import datetime
  2. PEP 8: E402 module level import not at top of file: 29 - this refers to the line import time
  3. Remove redundant parentheses- when I removed these parentheses from the code, the message disappeared.

Maxim, can you temporarily make the quote in the file update now at least in 1 minute to understand whether the code is working or not, otherwise I have to wait a whole hour...?

One more thing. After I had launched the initial code that I had published earlier, file EUR_USD_QUOTE.csv was immediately opened with current quote, while the modified code cannot open this file.

Maybe I'm doing something wrong or I don't understand something? Please, advise me.

Regards, Vladimir.

1st 2 errors - just move this to the very top of the file

from datetime import datetime
import time

This is just a programming style warning, not an error

For a quick check, replace both fields

hour

to

second

the file should be written every 10 seconds because the check timer is set to 10 seconds.

P.S. Just don't get too carried away or you'll get banned for too frequent requests.

t = datetime.now().hour
t2 = t

while True:
    t = datetime.now().hour
    if(t2 != t):
        get_data(save_file='EUR_USD_QUOTE.csv')
        t2 = t
    time.sleep(10)

on parentheses I do not have any errors

This code waits until the next hour and then only saves

 

I do not have these modules installed

import requests
from bs4 import BeautifulSoup

so haven't checked

 
Maxim Dmitrievsky:

Maxim, I changed your code a little by "scientific experiment" method (please excuse me beforehand) and it worked out to open EUR_USD_QUOTE.csv file with current quote in 1 minute after launching.

from datetime import datetime
import time
t = datetime.now().minute  # здесь поставил минуту
t2 = t

while True:
    t = datetime.now().minute  # здесь поставил минуту
    if t2 != t:
        get_data(save_file='EUR_USD_QUOTE.csv')
        t2 = t
    # Code executed here
    time.sleep(1)  # и здесь 10 изменил на 1.

But if I don't close EUR_USD_QUOTE.csv file during next minute, I get this message:

Traceback (most recent call last):
File "C:/Users/Vladimir/PycharmProjects/My_Python_Project/8.py", line 36, in <module>
get_data(save_file='EUR_USD_QUOTE.csv')
File "C:/Users/Vladimir/PycharmProjects/My_Python_Project/8.py", line 21, in get_data
with open(save_file, 'w') as save:
PermissionError: [Errno 13] Permission denied: 'EUR_USD_QUOTE.csv'

Process finished with exit code 1

How can I make the process continue without human intervention after the program code has been executed?

Regards, Vladimir.

 
MrBrooklin:

Maxim, I changed your code a little by "scientific experiment" method (please excuse me beforehand) and it was possible to open EUR_USD_QUOTE.csv file with current quote in 1 minute after launching.


But if I don't close EUR_USD_QUOTE.csv file during next minute, I get this message:

Traceback (most recent call last):
File "C:/Users/Vladimir/PycharmProjects/My_Python_Project/8.py", line 36, in <module>
get_data(save_file='EUR_USD_QUOTE.csv')
File "C:/Users/Vladimir/PycharmProjects/My_Python_Project/8.py", line 21, in get_data
with open(save_file, 'w') as save:
PermissionError: [Errno 13] Permission denied: 'EUR_USD_QUOTE.csv'

Process finished with exit code 1

How can I make the process continue without human intervention after the program code has been executed?

Regards, Vladimir.

ah yes, because the process is busy os.startfile() for some reason, delete this line

os.startfile(save_file)

и

import os

can be removed

Checked it, it works.

 
Maxim Dmitrievsky:

and yes, because the process is busy os.startfile() for some reason, delete that line

и

can be deleted.

Checked it, it works.

Maxim, did everything as you advised. No more problems reported. The program has started but now how can I view the quote if EUR_USD_QUOTE.csv file is closed and when I open it and leave it open I get the same message that PermissionError: [Errno 13] Permission denied: 'EUR_USD_QUOTE.csv' and the quote in the open file is no longer updated?

Once again, please excuse me, I've never concealed that I am a total zero in programming.

Regards, Vladimir.

 
MrBrooklin:

Maxim, I have done everything as you advised. There are no more messages about problems. Now how can I view the quote if EUR_USD_QUOTE.csv file is closed and when I open it and leave it open, I get the same message that PermissionError: [Errno 13] Permission denied: 'EUR_USD_QUOTE.csv' and the quote in the open file is no longer updated?

Please excuse me once again, I've never concealed that I am a total zero in programming.

Regards, Vladimir.

Do you open the file with Excel? It means it opens the file monopolistically. You have to allow other applications/users to modify the file.

Maybe http://blog.depit.ru/odnovremennaya-rabota-v-e xcel/ will help.

In general, you can't modify a file opened in another program.

Одновременная работа нескольких пользователей с таблицами Excel
Одновременная работа нескольких пользователей с таблицами Excel
  • blog.depit.ru
Возможности системы Microsoft Excel позволяют работать по наполнению одного файла электронных таблиц одновременно нескольким пользователям. Для того, чтобы начать совместную работу с документом, достаточно в документе Microsoft Excel перейти в меню « Рецензирование » — « Доступ к книге » и выбрать « Разрешить изменять файл нескольким...
 
Maxim Dmitrievsky:

You open the file with Excel? So it opens the file monopolistically. You have to allow other applications/users to modify the file somewhere in the settings.

Maybe http://blog.depit.ru/odnovremennaya-rabota-v-e xcel/ will help.

You can't modify a file opened in another program.

Yes, I'm opening it in Excel. I still cannot update the quote in the open EUR_USD_QUOTE.csv file in real time.

It was the idea to look at the table and see the changes in the quote.

Regards, Vladimir.