Here is a python script which runs in a terminal and downloads/updates scheduled news events found in the myfxbook economic calendar. Events are parsed in python to make it easy to work with the data in MQL by using simple casting.
Prerequisites:
Prerequisites:
- Python 3.7+
- requests library (pip install requests)
Instructions:
- Login to myfxbook with chrome.
- Make sure the time-zone matches your broker in the myfxbook account settings
- Go to the myfxb economic calendar and change the filter settings for your desired data e.g. Impact, currencies, etc.
- With the chrome window selected hit F12 (inspect)
- Click on the network tab in the inspection window
- reload the calendar page
- click on the first item in the "Name" window
- Click the headers tab and copy the headers under request headers and paste/save to "headers.txt" in the same path as the script
- run script -- python script.py
- The first run you will be prompted for the path to the MQL common files directory... paste directory and press enter.
- The events will be parsed and saved to a TSV (tab [\t] sep file) named "news_events.tsv" which is found in the common dir
Inserted Code
import csv import datetime import json import re import sys import time from pathlib import Path import requests URL = 'https://www.myfxbook.com/calendar_statement.csv' def myfx_to_mql_time(time): dt = datetime.datetime.strptime(time, '%Y, %B %d, %H:%M') return dt.strftime('%Y.%m.%d %H:%M') def str_to_float(num): is_percent = False if '%' in num: is_percent = True p = re.compile(r'(-?).*?([0-9.]+)') m = p.search(num) if m: res = float(''.join(m.groups())) if is_percent: res = round(res / 100.0, 4) return res return None def get_config(): conf = Path() / 'news_conf.json' if conf.exists(): conf_d = json.loads(conf.read_text()) else: common = Path(input("MQL common path >>> ")) if not common.exists(): raise Exception(f'{common} does not exits') update_interval = int(input('update interval in seconds >>> ')) conf_d = { 'common' : str(common.absolute()), 'interval': update_interval, } conf.write_text(json.dumps(conf_d)) return conf_d def get_headers(): cookies = Path('headers.txt') if not cookies.exists(): raise FileNotFoundError('NO HEADERS FILE') headers = {} with open(cookies) as f: p = re.compile(r'(.*?):\s?(.*)') for line in f: m = p.match(line) if m: key, val = m.group(1), m.group(2) headers[key] = val else: raise Exception(f'Error in {cookies}') return headers def get_csv(): r = requests.get(URL, headers=get_headers()) reader = csv.reader(r.text.split('\n')) headers = next(reader) results = [dict(zip(headers, row)) for row in reader] if not headers or not reader: raise Exception() for res in results: for k, v in res.items(): if k == 'Date': res['Date'] = myfx_to_mql_time(res['Date']) elif k in ['Previous', 'Consensus', 'Actual', 'Currency']: new_val = str_to_float(v) if new_val is not None: res[k] = new_val return results def save_csv(event_csv, save_path): fields = 'Date Event Impact Previous Consensus Actual Currency'.split() with open(save_path, 'w', newline='\n') as f: writer = csv.DictWriter(f, fieldnames=fields, delimiter='\t') writer.writeheader() writer.writerows(event_csv) def main(): config = get_config() save_path = Path(config['common']) / 'news_events.tsv' interval = config['interval'] while True: try: event_csv = get_csv() save_csv(event_csv, save_path) print('updated:', datetime.datetime.now()) except PermissionError: print('CANNOT OPEN FILE. OPEN with flags=FILE_SHARE...') except KeyboardInterrupt: sys.exit('exiting now...') except Exception: raise time.sleep(interval) if __name__ == '__main__': main()