Files
aiomql/Untitled.ipynb
T
2024-08-29 17:39:44 +01:00

13 KiB

In [1]:
# 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 [3]:
sym = Symbol(name='Volatility 25 Index')
await sym.init()
Out [3]:
True
In [13]:
await sym.mt5.order_calc_margin(0, 'Volatility 25 Index', 1, sym.tick.ask)
Out [13]:
0.5
In [17]:
sy = await sym.mt5.symbol_info('Volatility 25 Index')
await sym.mt5.symbol_select('Volatility 25 Index', enable=True)
sy.trade_calc_mode
Out [17]:
4
In [12]:
# margin
# worked for forex.
tcs =  1 * sym.trade_contract_size
lv = Account().leverage /0.125
tcs * sym.tick.ask / lv
Out [12]:
0.50097375
In [ ]:
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 [2]:
6 / (0 or 5)
Out [2]:
1.2
In [ ]: