This commit is contained in:
Ichinga Samuel
2024-08-27 16:47:52 +01:00
parent a972a06da8
commit 188cee21af
3 changed files with 333 additions and 3 deletions
+1 -1
View File
@@ -658,7 +658,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
"version": "3.11.4"
}
},
"nbformat": 4,
+327
View File
@@ -0,0 +1,327 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "f1039720-9692-4605-adf9-d37651958e4c",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from pandas import DataFrame, Series"
]
},
{
"cell_type": "code",
"execution_count": 108,
"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)})"
]
},
{
"cell_type": "code",
"execution_count": 109,
"id": "eb69c292-79e3-4105-b10c-6f4f5c22074f",
"metadata": {},
"outputs": [],
"source": [
"rs.set_index(2, drop=False, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "d7976bb8-05cb-4924-a6e2-90ea8af85d9d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <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",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>10</td>\n",
" <td>10</td>\n",
" <td>20</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>20</td>\n",
" <td>11</td>\n",
" <td>22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>30</td>\n",
" <td>12</td>\n",
" <td>24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>40</td>\n",
" <td>13</td>\n",
" <td>26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>50</td>\n",
" <td>14</td>\n",
" <td>28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>30</th>\n",
" <td>60</td>\n",
" <td>15</td>\n",
" <td>30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>32</th>\n",
" <td>70</td>\n",
" <td>16</td>\n",
" <td>32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34</th>\n",
" <td>80</td>\n",
" <td>17</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>36</th>\n",
" <td>90</td>\n",
" <td>18</td>\n",
" <td>36</td>\n",
" </tr>\n",
" <tr>\n",
" <th>38</th>\n",
" <td>100</td>\n",
" <td>19</td>\n",
" <td>38</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"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rs"
]
},
{
"cell_type": "code",
"execution_count": 112,
"id": "3c456100-4931-4fbd-a63d-531eaf735e70",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rs.index.get_loc(rs[rs.index <= 31].index[-1])"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "1a5a99bb-3fc4-458d-95b8-4e031f3e04c1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index([20, 22, 24, 26, 28, 30, 32, 34, 36, 38], dtype='int64', name=2)"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rs.index"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "2d53ccef-72e1-4378-818f-b60682bbd8b7",
"metadata": {},
"outputs": [],
"source": [
"g = rs[rs.index <= 31].iloc[-1]"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "7318351e-e185-4695-ada5-43596b4844e6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(20)"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rs.index[0]"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "55441116-8c00-4fd1-94c8-939320c6dfc4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[25, 26, 27, 28, 29]"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = list(range(30))\n",
"t[25:30]"
]
},
{
"cell_type": "code",
"execution_count": 121,
"id": "6ef28e4c-15a5-4fe5-aa66-10ba464f1256",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 7, 8, 9]"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t[6: 10]"
]
},
{
"cell_type": "code",
"execution_count": 126,
"id": "f15cf18c-54cb-4624-8d18-3998bde2b9ba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"p = 0 or None\n",
"print(p)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47dd4064-dde6-4824-a12b-ecf334bf4ebe",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+5 -2
View File
@@ -58,6 +58,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.index.get_loc(start)
end = start + count
return rates.iloc[start:end].to_numpy()
@@ -65,13 +66,15 @@ class TestData:
def get_rates_from_pos(self, symbol: str, timeframe: TimeFrame, start_pos: int, count: int) -> np.ndarray:
rates = self.rates[symbol][timeframe.name]
end = -start_pos + count
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:
rates = self.rates[symbol][timeframe.name]
start = int(datetime.timestamp(date_from))
start = round_down(start, timeframe.time)
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
return rates.loc[start:end].to_numpy()
def get_ticks_from(self, symbol: str, date_from: datetime | float, count: int, flags: CopyTicks) -> DataFrame: