mirror of
https://github.com/Ichinga-Samuel/aiomql.git
synced 2026-08-16 13:28:08 +00:00
testdata
This commit is contained in:
@@ -4,7 +4,8 @@ import pytz
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from pandas import DataFrame
|
||||
from MetaTrader5 import Tick, SymbolInfo, AccountInfo
|
||||
from MetaTrader5 import Tick, SymbolInfo, AccountInfo, TradeOrder, TradePosition, TradeDeal
|
||||
import MetaTrader5
|
||||
|
||||
from ..constants import TimeFrame, CopyTicks
|
||||
from .get_data import Data, GetData
|
||||
@@ -14,6 +15,9 @@ tz = pytz.timezone('Etc/UTC')
|
||||
|
||||
|
||||
class TestData:
|
||||
history_orders: DataFrame
|
||||
history_deals: DataFrame
|
||||
|
||||
def __init__(self, data: Data):
|
||||
self._data = data
|
||||
self.account = data['account']
|
||||
@@ -24,7 +28,12 @@ class TestData:
|
||||
self.interval = data['interval']
|
||||
self.cursor = 0
|
||||
self.iter = iter(self.interval)
|
||||
|
||||
self.orders: dict[str, dict[int, TradeOrder]] = {}
|
||||
self.open_orders: dict[int, TradeOrder] = {}
|
||||
self.positions: dict[str, dict[int, TradePosition]] = {}
|
||||
self.open_positions = dict[int, TradePosition] = {}
|
||||
self.history_deals = dict[str, dict[int, TradeDeal]] = {}
|
||||
|
||||
def __next__(self):
|
||||
self.cursor = next(self.iter)
|
||||
return self.cursor
|
||||
@@ -58,7 +67,7 @@ class TestData:
|
||||
rates = self.rates[symbol][timeframe.name]
|
||||
start = int(datetime.timestamp(date_from)) if isinstance(date_from, datetime) else int(date_from)
|
||||
start = round_down(start, timeframe.time)
|
||||
start = rates[rates.index <= start].index
|
||||
start = rates[rates.index <= start].iloc[-1].name
|
||||
start = rates.index.get_loc(start)
|
||||
end = start + count
|
||||
return rates.iloc[start:end].to_numpy()
|
||||
@@ -69,17 +78,51 @@ class TestData:
|
||||
end = end or None
|
||||
return rates.iloc[-start_pos:end].to_numpy()
|
||||
|
||||
def get_rates_range(self, symbol: str, timeframe: TimeFrame, date_from: datetime, date_to: datetime) -> np.ndarray:
|
||||
def get_rates_range(self, symbol: str, timeframe: TimeFrame, date_from: datetime | float, date_to: datetime | float) -> np.ndarray:
|
||||
rates = self.rates[symbol][timeframe.name]
|
||||
start = round_down(int(datetime.timestamp(date_from)), timeframe.time)
|
||||
start = rates[rates.index <= start].iloc[-1].index
|
||||
end = round_up(int(datetime.timestamp(date_to)), timeframe.time)
|
||||
end = rates[rates.index >= end].index
|
||||
start = int(datetime.timestamp(date_from)) if isinstance(date_from, datetime) else int(date_from)
|
||||
start = round_down(start, timeframe.time)
|
||||
start = rates[rates.index <= start].iloc[-1].name
|
||||
end = int(datetime.timestamp(date_to)) if isinstance(date_to, datetime) else int(date_to)
|
||||
end = round_up(end, timeframe.time)
|
||||
end = rates[rates.index >= end].iloc[-1].name
|
||||
return rates.loc[start:end].to_numpy()
|
||||
|
||||
def get_ticks_from(self, symbol: str, date_from: datetime | float, count: int, flags: CopyTicks) -> DataFrame:
|
||||
def get_ticks_from(self, symbol: str, date_from: datetime | float, count: int, flags: CopyTicks) -> np.ndarray:
|
||||
ticks = self.ticks[symbol]
|
||||
start = int(datetime.timestamp(date_from)) if isinstance(date_from, datetime) else int(date_from)
|
||||
start = round_down(start, 1)
|
||||
start = ticks[ticks.index <= start].iloc[-1].name
|
||||
start = ticks.index.get_loc(start)
|
||||
end = start + count
|
||||
return ticks.loc[start:end]
|
||||
return ticks.iloc[start:end]
|
||||
|
||||
def get_ticks_range(self, symbol: str, date_from: datetime | float, date_to: datetime | float, flags) -> np.ndarray:
|
||||
ticks = self.ticks[symbol]
|
||||
start = int(datetime.timestamp(date_from)) if isinstance(date_from, datetime) else int(date_from)
|
||||
start = ticks[ticks.index <= start].iloc[-1].index
|
||||
end = int(datetime.timestamp(date_to)) if isinstance(date_to, datetime) else int(date_to)
|
||||
end = ticks[ticks.index >= end].iloc[-1].index
|
||||
return ticks.loc[start:end].to_numpy()
|
||||
|
||||
def get_orders_total(self) -> int:
|
||||
return len(self.live_orders)
|
||||
|
||||
def get_orders(self, symbol: str = '', group: str = '', ticket: int = None) -> tuple[TradeOrder, ...]:
|
||||
if ticket:
|
||||
return self.live_orders.get(ticket, ())
|
||||
|
||||
elif symbol:
|
||||
return tuple(order for order in self.orders.get(symbol, ()) if order.ticket in self.live_orders)
|
||||
|
||||
elif group:
|
||||
return tuple(self.live_orders.values())
|
||||
|
||||
else:
|
||||
return tuple(self.live_orders.values())
|
||||
|
||||
def history_orders_total(self, date_from: datetime | float, date_to: datetime | float):
|
||||
start =
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user