From 188cee21afb2d76da4f0f75506ea121f444548f6 Mon Sep 17 00:00:00 2001 From: Ichinga Samuel Date: Tue, 27 Aug 2024 16:47:52 +0100 Subject: [PATCH] testdata --- Untitled.ipynb | 2 +- Untitled1.ipynb | 327 ++++++++++++++++++++++++ src/aiomql/core/backtester/test_data.py | 7 +- 3 files changed, 333 insertions(+), 3 deletions(-) create mode 100644 Untitled1.ipynb diff --git a/Untitled.ipynb b/Untitled.ipynb index c73189d..496fa33 100644 --- a/Untitled.ipynb +++ b/Untitled.ipynb @@ -658,7 +658,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.6" + "version": "3.11.4" } }, "nbformat": 4, diff --git a/Untitled1.ipynb b/Untitled1.ipynb new file mode 100644 index 0000000..038ee5c --- /dev/null +++ b/Untitled1.ipynb @@ -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": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
012
2
20101020
22201122
24301224
26401326
28501428
30601530
32701632
34801734
36901836
381001938
\n", + "
" + ], + "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 +} diff --git a/src/aiomql/core/backtester/test_data.py b/src/aiomql/core/backtester/test_data.py index 27f3a37..e7da4c2 100644 --- a/src/aiomql/core/backtester/test_data.py +++ b/src/aiomql/core/backtester/test_data.py @@ -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: