This commit is contained in:
Ichinga Samuel
2024-11-23 10:23:01 +01:00
parent 9d9c02fcfd
commit f8c4c6e9a7
19 changed files with 279 additions and 209 deletions
+5 -3
View File
@@ -1,11 +1,12 @@
from datetime import datetime, UTC
import asyncio
import json
import shutil
from datetime import datetime, UTC
from logging import getLogger
from pathlib import Path
import pytest
from aiomql.core import Config
from aiomql.core.meta_backtester import MetaBackTester
from aiomql.core.backtesting.backtest_engine import BackTestEngine
@@ -57,7 +58,7 @@ async def config(request):
data["mode"] = "backtest"
json.dump(data, fh1, indent=2)
json.dump(data, fh2, indent=2)
config = Config(filename="test.json", root="tests/backtest")
config = Config(config_file="tests/backtest/test.json", root="tests/backtest", filename="test.json")
yield config
await cleanup()
@@ -73,7 +74,8 @@ async def mt():
@pytest.fixture(scope="package")
async def period():
return {"start": datetime(2024, 2, 1, hour=8, tzinfo=UTC), "end": datetime(2024, 2, 7, hour=16, tzinfo=UTC)}
return {"start": datetime(2024, 2, 1, hour=8, tzinfo=UTC),
"end": datetime(2024, 2, 7, hour=16, tzinfo=UTC)}
@pytest.fixture(scope="package")
@@ -27,7 +27,8 @@ async def make_buy_sell_orders():
return {"buy": Order(**buy_req), "sell": Order(**sell_req)}
def test_trade_mode(config, backtest_engine, history, positions, order_sell, order_buy, btc_usd):
def test_trade_mode(config, backtest_engine, history, positions, order_sell, order_buy, btc_usd, capsys):
print(config.filename, config.root)
assert config.mode == "backtest"
assert isinstance(backtest_engine, BackTestEngine)
assert isinstance(history.mt5, MetaBackTester)
+5
View File
@@ -0,0 +1,5 @@
def test_config(config, capsys):
print(config.filename, config.root, config.mode, config.config_file)
assert 6 == 6
assert config.mode == "backtest"
+1 -1
View File
@@ -53,7 +53,7 @@ async def config(request):
data = json.load(fh)
json.dump(data, fh1, indent=2)
json.dump(data, fh2, indent=2)
config = Config(root="tests/live", filename="test.json")
config = Config(root="tests/live", config_file="tests/live/test.json")
yield config
await cleanup()
+3
View File
@@ -0,0 +1,3 @@
bid,volume,actual_profit,order,closed,price,ask,deal,win
0.0,0.0,0,0,False,0.0,0.0,0,False
0.0,0.0,0,0,False,0.0,0.0,0,False
1 bid volume actual_profit order closed price ask deal win
2 0.0 0.0 0 0 False 0.0 0.0 0 False
3 0.0 0.0 0 0 False 0.0 0.0 0 False
+24
View File
@@ -0,0 +1,24 @@
[
{
"deal": 0,
"order": 0,
"volume": 0.0,
"price": 0.0,
"bid": 0.0,
"ask": 0.0,
"actual_profit": 0,
"closed": false,
"win": false
},
{
"deal": 0,
"order": 0,
"volume": 0.0,
"price": 0.0,
"bid": 0.0,
"ask": 0.0,
"actual_profit": 0,
"closed": false,
"win": false
}
]
+3
View File
@@ -0,0 +1,3 @@
bid,name,volume,actual_profit,order,closed,ema,price,ask,deal,rsi,win
0.0,test_trades,0.0,0,0,False,20,0.0,0.0,0,14,False
0.0,test_trades,0.0,0,0,False,20,0.0,0.0,0,14,False
1 bid name volume actual_profit order closed ema price ask deal rsi win
2 0.0 test_trades 0.0 0 0 False 20 0.0 0.0 0 14 False
3 0.0 test_trades 0.0 0 0 False 20 0.0 0.0 0 14 False
+30
View File
@@ -0,0 +1,30 @@
[
{
"name": "test_trades",
"ema": 20,
"rsi": 14,
"deal": 0,
"order": 0,
"volume": 0.0,
"price": 0.0,
"bid": 0.0,
"ask": 0.0,
"actual_profit": 0,
"closed": false,
"win": false
},
{
"name": "test_trades",
"ema": 20,
"rsi": 14,
"deal": 0,
"order": 0,
"volume": 0.0,
"price": 0.0,
"bid": 0.0,
"ask": 0.0,
"actual_profit": 0,
"closed": false,
"win": false
}
]
+1 -1
View File
@@ -25,5 +25,5 @@ class TestConfig:
assert "server" in account_info
def test_load_config(self, config):
config.load_config(file="tests/live/configs/test2.json")
config.load_config(config_file="tests/live/configs/test2.json")
assert config.filename == "test2.json"
+13 -10
View File
@@ -5,7 +5,8 @@ from aiomql.lib.ram import RAM
from aiomql.contrib.traders import SimpleTrader
from aiomql.contrib.symbols import ForexSymbol
from aiomql.core.constants import OrderType
from aiomql.lib.account import Account
from aiomql._utils import round_down
class TestTrader:
@classmethod
@@ -13,11 +14,13 @@ class TestTrader:
ram = RAM(fixed_amount=10)
cls.trader = SimpleTrader(symbol=ForexSymbol(name="BTCUSD"), ram=ram)
cls.simple_trader2 = SimpleTrader(symbol=ForexSymbol(name="EURJPY"), ram=ram)
cls.account = Account()
@pytest.fixture(scope="class", autouse=True)
async def initialize(self):
await self.trader.symbol.initialize()
await self.simple_trader2.symbol.initialize()
await self.account.refresh()
async def test_create_order_no_stops(self):
await self.trader.create_order_no_stops(order_type=OrderType.BUY)
@@ -35,8 +38,8 @@ class TestTrader:
profit = floor(await self.trader.order.calc_profit())
loss = -floor(abs(await self.trader.order.calc_loss()))
assert profit == -loss * self.trader.ram.risk_to_reward
assert profit == self.trader.ram.fixed_amount * self.trader.ram.risk_to_reward
assert loss == -self.trader.ram.fixed_amount
assert abs(profit - self.trader.ram.fixed_amount * self.trader.ram.risk_to_reward) <= 2.5
assert abs(abs(loss) - abs(-self.trader.ram.fixed_amount)) <= 2
assert res is not None
assert res.retcode == 10009
@@ -47,8 +50,8 @@ class TestTrader:
profit = floor(await self.trader.order.calc_profit())
loss = -floor(abs(await self.trader.order.calc_loss()))
assert profit == -loss * self.trader.ram.risk_to_reward
assert profit == self.trader.ram.fixed_amount * self.trader.ram.risk_to_reward
assert loss == -self.trader.ram.fixed_amount
assert abs(profit - self.trader.ram.fixed_amount * self.trader.ram.risk_to_reward) <= 2.5
assert abs(abs(loss) - abs(-self.trader.ram.fixed_amount)) <= 2
assert res is not None
assert res.retcode == 10009
@@ -60,10 +63,10 @@ class TestTrader:
tp = tick.ask + tp
await self.trader.create_order_with_stops(order_type=OrderType.BUY, sl=sl, tp=tp)
res = await self.trader.order.send()
profit = floor(await self.trader.order.calc_profit())
loss = -floor(abs(await self.trader.order.calc_loss()))
assert profit == -loss * self.trader.ram.risk_to_reward
assert profit == self.trader.ram.fixed_amount * self.trader.ram.risk_to_reward
assert loss == -self.trader.ram.fixed_amount
assert res is not None
assert res.retcode == 10009
profit = round(await self.trader.order.calc_profit(), self.account.currency_digits)
loss = -round(abs(await self.trader.order.calc_loss()), self.account.currency_digits)
assert profit == -loss * self.trader.ram.risk_to_reward
assert abs(profit - (self.trader.ram.fixed_amount * self.trader.ram.risk_to_reward)) <= 2.5
assert abs(abs(loss) - self.trader.ram.fixed_amount) <= 2.5