-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetatrader5.py
More file actions
63 lines (48 loc) · 2.62 KB
/
metatrader5.py
File metadata and controls
63 lines (48 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#merge request update1
from datetime import datetime
import MetaTrader5 as mt5
import pandas
import pytz
from loguru import logger
logger.add('debug.log',format="{time} {level} {message}", level="DEBUG",
rotation="100 KB", compression="zip" )
barsAmount = 99000
timezone = pytz.timezone("Etc/UTC")
EUR_USD = 'EURUSD'
HIGH = 'high'
LOW = 'low'
round_num = 5
def get_prices_for_whole_period():
now = datetime.now(timezone)
rates_whole_period = mt5.copy_rates_from(EUR_USD, mt5.TIMEFRAME_H1, now, barsAmount)
rates_frame = pandas.DataFrame(rates_whole_period)
high_price = rates_frame[[HIGH]]
low_price = rates_frame[[LOW]]
bar_size = high_price[HIGH] - low_price[LOW]
logger.debug(f'Максимальная свеча за весь период: {round(bar_size.max(), round_num)}')
logger.debug(f'Минимальная свеча за весь период: {round(bar_size.min(), round_num)}')
def get_prices_for_quarter(year, time_from, time_to, quarter):
rates_in_range = mt5.copy_rates_range(EUR_USD, mt5.TIMEFRAME_H1, time_from, time_to)
rates_frame = pandas.DataFrame(rates_in_range)
high_price = rates_frame[[HIGH]]
low_price = rates_frame[[LOW]]
bar_size = high_price[HIGH] - low_price[LOW]
logger.debug(f'Максимальная свеча за {quarter} квартал {year} года: {round(bar_size.max(), round_num)}')
logger.debug(f'Минимальная свеча за {quarter} квартал {year} года: {round(bar_size.min(), round_num)}')
def get_prices_for_quarters():
year = 1970
while year < 2021:
get_prices_for_quarter(year, datetime(year, 1, 1, tzinfo=timezone), datetime(year, 3, 31, tzinfo=timezone), 1)
get_prices_for_quarter(year, datetime(year, 4, 1, tzinfo=timezone), datetime(year, 6, 30, tzinfo=timezone), 2)
get_prices_for_quarter(year, datetime(year, 7, 1, tzinfo=timezone), datetime(year, 9, 30, tzinfo=timezone), 3)
get_prices_for_quarter(year, datetime(year, 10, 1, tzinfo=timezone), datetime(year, 12, 31, tzinfo=timezone), 4)
year += 1
get_prices_for_quarter(year, datetime(year, 1, 1, tzinfo=timezone), datetime(year, 3, 31, tzinfo=timezone), 1)
if not mt5.initialize():
logger.debug('Ошибка инициализации MetaTrader5: {mt5.last_error()}')
quit()
logger.debug('Найти максимальную и минимальную свечи на весь период по EURUSD')
get_prices_for_whole_period()
logger.debug('\nНайти максимальную и минимальную свечи за каждый квартал по EURUSD')
get_prices_for_quarters()
mt5.shutdown()