Files
aiomql/Untitled.ipynb
T
2024-08-27 05:57:07 +01:00

15 KiB

In [5]:
# import shelve
# import pickle
# import zlib
# import lzma
# import pytz
from datetime import datetime, timedelta
from aiomql import MetaTrader, TimeFrame, AccountInfo, TimeFrame, CopyTicks, Account, Symbol
from MetaTrader5 import SymbolInfo
import pandas as pd
from pandas import DataFrame
import pytz
In [2]:
res = await Account().sign_in()
print(res)
True
In [19]:
tz = pytz.timezone('Etc/UTC')
sym = Symbol(name='EURUSD')
start = datetime(day=22, month=8, year=2024, tzinfo=tz)
end = datetime(day=26, month=8, year=2024, hour=12, tzinfo=tz)
# rates = await sym.mt5.copy_rates_from(symbol='EURUSD', date_from=end, count=5, timeframe=TimeFrame.H12)
rates = await sym.mt5.copy_rates_from_pos(symbol='EURUSD', start_pos=0, count=5, timeframe=TimeFrame.H12)
df = DataFrame(rates)
df['time'] = pd.to_datetime(df['time'], unit='s')
df.index[-1]
Out [19]:
4
In [30]:
df.loc[2:6]
Out [30]:
time open high low close tick_volume spread real_volume
2 2024-08-25 12:00:00 1.11869 1.11947 1.11849 1.11894 4570 1 0
3 2024-08-26 00:00:00 1.11894 1.12016 1.11628 1.11652 43583 0 0
4 2024-08-26 12:00:00 1.11652 1.11790 1.11501 1.11618 45034 0 0
In [ ]:
now = datetime.now()
st = now.replace(hour=0, minute=0, second=0, day=1, month=1, year=2023)
et = now.replace(hour=9, minute=0, second=0)
diff = et - st
secs = int(diff.total_seconds())
# st = now.replace(hour=0, day=16)
# et = now.replace(hour=9)
# start = now.replace(day=now.day-3, tzinfo=tz)
# end = now.replace(day=now.day-1, tzinfo=tz)
In [ ]:
symbols = {'Volatility 10 Index', 'Volatility 100 (1s) Index', 'Volatility 25 Index'}
timeframes = {TimeFrame.M5, TimeFrame.H1, TimeFrame.M1, TimeFrame.H4, TimeFrame.M30, TimeFrame.M15}
gd = GetData(st, et, timeframes, symbols, name='data')
await gd.fail()
In [ ]:
symbols = {'Volatility 10 Index', 'Volatility 100 (1s) Index', 'Volatility 25 Index'}
timeframes = {TimeFrame.M5, TimeFrame.H1, TimeFrame.M1, TimeFrame.H4, TimeFrame.M30, TimeFrame.M15}
async with MetaTester(st, et, timeframes, symbols, name='data') as mt:
    # res = await mt.copy_ticks_range('Volatility 100 Index', st, et, CopyTicks.ALL)
    # print(res)
    await mt.get_and_save_data()
In [ ]:
df = pd.DataFrame(res)
In [ ]:
# df.set_index(list(range(secs)))
df.drop_duplicates(subset=['time'], keep='last', )
In [ ]:
df = df.set_index('time', drop=False, verify_integrity=True)
In [ ]:
bg = int(st.timestamp())
en = int(secs) + bg
index = range(bg, en)
In [ ]:
bf = len(df.index)
df = df.reindex(index=index, method='nearest')
In [ ]:
last = df.iloc[-1].time
print(datetime.fromtimestamp(last, tz=tz), et)
In [ ]:
acc = AccountInfo(balance=500)
data = MetaTester(start, end).load_data('data')
td = TestData(acc, data)
In [ ]:
ticks = data['ticks']['Volatility 10 Index']
In [ ]:
ticks[-1]
In [ ]:
y = TimeFrame.M5
y.time
In [ ]:
end.timestamp()
In [ ]:
len(ticks)
In [ ]:
type(ticks[0])
In [ ]:
ticks.reshape(8, 86160)
In [ ]:
import numpy as np
In [ ]:
res = np.reshape(ticks, (-1, 8))
In [ ]:
ticks.shape = (86160, 8)
In [ ]:
res = np.hstack(ticks)
In [ ]:
v = np.array((*ticks[0]))
In [ ]:
r = next(iter(ticks))
In [ ]:
v.shape
In [ ]:
ar = list(range(4))
t = np.array(ar)
t.shape = (1, 4)
t
In [ ]:
mt.config.login
In [ ]:
acc = Account()
await acc.sign_in()
In [ ]:
mt5 = MetaTrader()
sym = await mt5.symbol_info('Volatility 100 (1s) Index')
print(sym._asdict)
In [ ]:
n = datetime.now()
start = n.replace(hour=0, day=1, year=2020, month=1)
end = n.replace(hour=15)
res = await mt.copy_rates_range('Volatility 25 Index', TimeFrame.M1, start, end)
In [ ]:
res = pd.DataFrame(res)
len(res.index)
In [ ]:
print(start)
In [ ]:
data = shelve.open('./data/01-08-24_17-08-24', writeback=True)
In [ ]:
data = dict(data)
In [ ]:
data = pickle.dumps(data)
In [ ]:
data = zlib.compress(data, level=9)
In [ ]:
_data = lzma.compress(data)
In [ ]:
fh = lzma.open('./data/ldata.xz', 'w')
fh.write(_data)
fh.close()
In [ ]:
rb = lzma.open('./data/ldata.xz')
rbb = rb.read()
rb.close()
In [ ]:
rbd = lzma.decompress(rbb)
In [ ]:
rdata = pickle.loads(rbd)
In [ ]:
data.keys()
In [ ]:
fh = open('./data/pdata', 'wb')
pickle.dump(rdata, fh)
In [ ]:
fh.close()
In [ ]: