This commit is contained in:
Ichinga Samuel
2024-11-04 00:32:51 +01:00
parent 2c46e18528
commit d65a8db1a2
85 changed files with 5071 additions and 2512 deletions
+50 -23
View File
@@ -1,27 +1,36 @@
from aiomql.contrib import BackTestEngine, ForexSymbol, GetData
from aiomql.core import MetaBackTester
from aiomql.contrib import ForexSymbol
from aiomql.core import MetaBackTester, BackTestEngine, GetData
from aiomql.lib import Order
async def make_buy_sell_orders():
sym = ForexSymbol(name='BTCUSD')
sym = ForexSymbol(name="BTCUSD")
sym_info = await sym.mt5.symbol_info(sym.name)
dsl = (sym_info.trade_stops_level + sym_info.spread) * 2 * sym_info.point
sl = sym_info.ask - dsl
tp = sym_info.ask + dsl
buy_req = {'action': sym.mt5.TRADE_ACTION_DEAL, 'symbol': sym.name, 'volume': sym_info.volume_min,
'type': sym.mt5.ORDER_TYPE_BUY, 'price': sym_info.ask, 'sl': sl, 'tp': tp}
buy_req = {
"action": sym.mt5.TRADE_ACTION_DEAL,
"symbol": sym.name,
"volume": sym_info.volume_min,
"type": sym.mt5.ORDER_TYPE_BUY,
"price": sym_info.ask,
"sl": sl,
"tp": tp,
}
sell_req = buy_req.copy()
sell_req['type'] = sym.mt5.ORDER_TYPE_SELL
sell_req['price'] = sym_info.bid
del sell_req['tp']
del sell_req['sl']
return {'buy': Order(**buy_req), 'sell': Order(**sell_req)}
sell_req["type"] = sym.mt5.ORDER_TYPE_SELL
sell_req["price"] = sym_info.bid
del sell_req["tp"]
del sell_req["sl"]
return {"buy": Order(**buy_req), "sell": Order(**sell_req)}
def test_trade_mode(config, backtest_engine, history, positions, order_sell, order_buy, btc_usd):
assert config.mode == 'backtest'
def test_trade_mode(
config, backtest_engine, history, positions, order_sell, order_buy, btc_usd
):
assert config.mode == "backtest"
assert isinstance(backtest_engine, BackTestEngine)
assert isinstance(history.mt5, MetaBackTester)
assert isinstance(positions.mt5, MetaBackTester)
@@ -69,15 +78,27 @@ async def test_history(backtest_engine, history, order_sell, order_buy, position
async def test_margin(backtest_engine, order_sell, order_buy):
await backtest_engine.setup_account(balance=100)
so_margin = await backtest_engine.order_calc_margin(action=order_sell.action, volume=order_sell.volume,
symbol=order_sell.symbol, price=order_sell.price)
bo_margin = await backtest_engine.order_calc_margin(action=order_buy.action, volume=order_buy.volume,
symbol=order_buy.symbol, price=order_buy.price)
so_margin = await backtest_engine.order_calc_margin(
action=order_sell.action,
volume=order_sell.volume,
symbol=order_sell.symbol,
price=order_sell.price,
)
bo_margin = await backtest_engine.order_calc_margin(
action=order_buy.action,
volume=order_buy.volume,
symbol=order_buy.symbol,
price=order_buy.price,
)
total_margin = so_margin + bo_margin
await backtest_engine.order_send(request=order_sell.request)
await backtest_engine.order_send(request=order_buy.request)
# noinspection PyTestUnpassedFixture
assert backtest_engine.positions.margin == total_margin == backtest_engine._account.margin
assert (
backtest_engine.positions.margin
== total_margin
== backtest_engine._account.margin
)
backtest_engine.reset(clear_data=True)
@@ -87,8 +108,8 @@ async def test_account(backtest_engine, positions):
balance = backtest_engine._account.balance
equity = backtest_engine._account.equity
orders = await make_buy_sell_orders()
buy_order = orders['buy']
sell_order = orders['sell']
buy_order = orders["buy"]
sell_order = orders["sell"]
so = await backtest_engine.order_send(request=sell_order.request)
bo = await backtest_engine.order_send(request=buy_order.request)
backtest_engine.fast_forward(steps=22000)
@@ -103,7 +124,11 @@ async def test_account(backtest_engine, positions):
deal = backtest_engine.deals.history_deals_get(position=bo.order)
bo_profit = deal[-1].profit
assert len(all_pos) == 1
assert backtest_engine.positions.margin == backtest_engine._account.margin == backtest_engine.positions.margins[so.order]
assert (
backtest_engine.positions.margin
== backtest_engine._account.margin
== backtest_engine.positions.margins[so.order]
)
profit = sum([pos.profit for pos in all_pos])
n_balance = backtest_engine._account.balance
n_equity = backtest_engine._account.equity
@@ -128,9 +153,11 @@ async def test_wrapup(positions, buy_order, sell_order, backtest_engine, config)
last_balance = backtest_engine._account.balance
last_equity = backtest_engine._account.equity
last_profit = backtest_engine._account.profit
backtest_engine.wrap_up()
tdata = GetData.load_data(name=config.backtest_dir / f'{backtest_engine.name}.pkl')
new_bte = BackTestEngine(data=tdata, restart=False)
await backtest_engine.wrap_up()
tdata = GetData.load_data(name=config.backtest_dir / f"{backtest_engine.name}.pkl")
new_bte = BackTestEngine(
data=tdata, restart=False, assign_to_config=False, preload=False
)
assert new_bte._account.balance == last_balance
assert new_bte._account.equity == last_equity
assert new_bte._account.profit == last_profit