This commit is contained in:
Ichinga Samuel
2024-08-28 16:49:10 +01:00
parent 188cee21af
commit 24eec96fae
2 changed files with 147 additions and 58 deletions
+93 -47
View File
@@ -14,28 +14,67 @@
},
{
"cell_type": "code",
"execution_count": 108,
"execution_count": 19,
"id": "8d39819f-2cac-437f-b5fc-633ca7443f8a",
"metadata": {},
"outputs": [],
"source": [
"rs = DataFrame({0: range(10, 101, 10), 1: range(10, 20), 2: range(20, 40, 2)})"
"rs = DataFrame({0: range(10, 101, 10), 1: range(10, 20), 2: range(20, 40, 2), \"symbols\": [chr(i) for i in [65]*5 + [68]*5]})"
]
},
{
"cell_type": "code",
"execution_count": 109,
"execution_count": 32,
"id": "d7976bb8-05cb-4924-a6e2-90ea8af85d9d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[75, 75, 75, 75, 75, 75, 75, 75, 75, 75]"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[75]*10"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "eb69c292-79e3-4105-b10c-6f4f5c22074f",
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "KeyError",
"evalue": "None",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[71], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m ind \u001b[38;5;241m=\u001b[39m rs[rs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msymbols\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39miloc[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39mindex\u001b[38;5;241m.\u001b[39mname\n\u001b[1;32m----> 2\u001b[0m \u001b[43mrs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloc\u001b[49m\u001b[43m[\u001b[49m\u001b[43mind\u001b[49m\u001b[43m]\u001b[49m\n",
"File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexing.py:1191\u001b[0m, in \u001b[0;36m_LocationIndexer.__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 1189\u001b[0m maybe_callable \u001b[38;5;241m=\u001b[39m com\u001b[38;5;241m.\u001b[39mapply_if_callable(key, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj)\n\u001b[0;32m 1190\u001b[0m maybe_callable \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_deprecated_callable_usage(key, maybe_callable)\n\u001b[1;32m-> 1191\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_getitem_axis\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmaybe_callable\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexing.py:1431\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m 1429\u001b[0m \u001b[38;5;66;03m# fall thru to straight lookup\u001b[39;00m\n\u001b[0;32m 1430\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_key(key, axis)\n\u001b[1;32m-> 1431\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_label\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexing.py:1381\u001b[0m, in \u001b[0;36m_LocIndexer._get_label\u001b[1;34m(self, label, axis)\u001b[0m\n\u001b[0;32m 1379\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_get_label\u001b[39m(\u001b[38;5;28mself\u001b[39m, label, axis: AxisInt):\n\u001b[0;32m 1380\u001b[0m \u001b[38;5;66;03m# GH#5567 this will fail if the label is not present in the axis.\u001b[39;00m\n\u001b[1;32m-> 1381\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mobj\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mxs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mlabel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\generic.py:4301\u001b[0m, in \u001b[0;36mNDFrame.xs\u001b[1;34m(self, key, axis, level, drop_level)\u001b[0m\n\u001b[0;32m 4299\u001b[0m new_index \u001b[38;5;241m=\u001b[39m index[loc]\n\u001b[0;32m 4300\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m-> 4301\u001b[0m loc \u001b[38;5;241m=\u001b[39m \u001b[43mindex\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 4303\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(loc, np\u001b[38;5;241m.\u001b[39mndarray):\n\u001b[0;32m 4304\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m loc\u001b[38;5;241m.\u001b[39mdtype \u001b[38;5;241m==\u001b[39m np\u001b[38;5;241m.\u001b[39mbool_:\n",
"File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexes\\range.py:417\u001b[0m, in \u001b[0;36mRangeIndex.get_loc\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 415\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[0;32m 416\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(key, Hashable):\n\u001b[1;32m--> 417\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key)\n\u001b[0;32m 418\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_indexing_error(key)\n\u001b[0;32m 419\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key)\n",
"\u001b[1;31mKeyError\u001b[0m: None"
]
}
],
"source": [
"rs.set_index(2, drop=False, inplace=True)"
"ind = rs[rs['symbols'] != 'A'].iloc[0].index.name\n",
"rs.loc[ind]"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "d7976bb8-05cb-4924-a6e2-90ea8af85d9d",
"execution_count": 44,
"id": "cce9fa9e-d841-4f45-8813-313f17e7b12f",
"metadata": {},
"outputs": [
{
@@ -62,95 +101,99 @@
" <th>0</th>\n",
" <th>1</th>\n",
" <th>2</th>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th>symbols</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>20</th>\n",
" <th>0</th>\n",
" <td>10</td>\n",
" <td>10</td>\n",
" <td>20</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <th>1</th>\n",
" <td>20</td>\n",
" <td>11</td>\n",
" <td>22</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <th>2</th>\n",
" <td>30</td>\n",
" <td>12</td>\n",
" <td>24</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <th>3</th>\n",
" <td>40</td>\n",
" <td>13</td>\n",
" <td>26</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <th>4</th>\n",
" <td>50</td>\n",
" <td>14</td>\n",
" <td>28</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>30</th>\n",
" <th>5</th>\n",
" <td>60</td>\n",
" <td>15</td>\n",
" <td>30</td>\n",
" <td>D</td>\n",
" </tr>\n",
" <tr>\n",
" <th>32</th>\n",
" <th>6</th>\n",
" <td>70</td>\n",
" <td>16</td>\n",
" <td>32</td>\n",
" <td>D</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34</th>\n",
" <th>7</th>\n",
" <td>80</td>\n",
" <td>17</td>\n",
" <td>34</td>\n",
" <td>D</td>\n",
" </tr>\n",
" <tr>\n",
" <th>36</th>\n",
" <th>8</th>\n",
" <td>90</td>\n",
" <td>18</td>\n",
" <td>36</td>\n",
" <td>D</td>\n",
" </tr>\n",
" <tr>\n",
" <th>38</th>\n",
" <th>9</th>\n",
" <td>100</td>\n",
" <td>19</td>\n",
" <td>38</td>\n",
" <td>D</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0 1 2\n",
"2 \n",
"20 10 10 20\n",
"22 20 11 22\n",
"24 30 12 24\n",
"26 40 13 26\n",
"28 50 14 28\n",
"30 60 15 30\n",
"32 70 16 32\n",
"34 80 17 34\n",
"36 90 18 36\n",
"38 100 19 38"
" 0 1 2 symbols\n",
"0 10 10 20 A\n",
"1 20 11 22 A\n",
"2 30 12 24 A\n",
"3 40 13 26 A\n",
"4 50 14 28 A\n",
"5 60 15 30 D\n",
"6 70 16 32 D\n",
"7 80 17 34 D\n",
"8 90 18 36 D\n",
"9 100 19 38 D"
]
},
"execution_count": 110,
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
@@ -161,23 +204,26 @@
},
{
"cell_type": "code",
"execution_count": 112,
"execution_count": 31,
"id": "3c456100-4931-4fbd-a63d-531eaf735e70",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
"ename": "InvalidIndexError",
"evalue": "RangeIndex(start=0, stop=10, step=1)",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mInvalidIndexError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[31], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mrs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrs\u001b[49m\u001b[43m[\u001b[49m\u001b[43mrs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m<\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m31\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexes\\range.py:418\u001b[0m, in \u001b[0;36mRangeIndex.get_loc\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 416\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(key, Hashable):\n\u001b[0;32m 417\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key)\n\u001b[1;32m--> 418\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_check_indexing_error\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 419\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key)\n",
"File \u001b[1;32m~\\OneDrive - UBA\\Documents\\Personal\\VS\\aiomql\\venv\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:6059\u001b[0m, in \u001b[0;36mIndex._check_indexing_error\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 6055\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_check_indexing_error\u001b[39m(\u001b[38;5;28mself\u001b[39m, key):\n\u001b[0;32m 6056\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_scalar(key):\n\u001b[0;32m 6057\u001b[0m \u001b[38;5;66;03m# if key is not a scalar, directly raise an error (the code below\u001b[39;00m\n\u001b[0;32m 6058\u001b[0m \u001b[38;5;66;03m# would convert to numpy arrays and raise later any way) - GH29926\u001b[39;00m\n\u001b[1;32m-> 6059\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m InvalidIndexError(key)\n",
"\u001b[1;31mInvalidIndexError\u001b[0m: RangeIndex(start=0, stop=10, step=1)"
]
}
],
"source": [
"rs.index.get_loc(rs[rs.index <= 31].index[-1])"
"rs.index.get_loc(rs[rs.index <= 31].index)"
]
},
{
@@ -203,7 +249,7 @@
},
{
"cell_type": "code",
"execution_count": 79,
"execution_count": 28,
"id": "2d53ccef-72e1-4378-818f-b60682bbd8b7",
"metadata": {},
"outputs": [],
+54 -11
View File
@@ -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 =