mirror of
https://github.com/Ichinga-Samuel/aiomql.git
synced 2026-08-16 21:38:06 +00:00
11 KiB
11 KiB
In [ ]:
import shelve
import pickle
import zlib
import lzma
import pytz
from datetime import datetime, timedelta
from aiomql import MetaTrader, MetaTester, TimeFrame, TestData, AccountInfo, TimeFrame, CopyTicks, Account, GetData
# from MetaTrader5 import SymbolInfo
import pandas as pdIn [ ]:
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.timeIn [ ]:
end.timestamp()In [ ]:
len(ticks)In [ ]:
type(ticks[0])In [ ]:
ticks.reshape(8, 86160)In [ ]:
import numpy as npIn [ ]:
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.shapeIn [ ]:
ar = list(range(4))
t = np.array(ar)
t.shape = (1, 4)
tIn [ ]:
mt.config.loginIn [ ]:
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 [ ]: