|
1 | | -from datetime import timedelta |
2 | | -from unittest import TestCase |
3 | | - |
4 | 1 | import pandas as pd |
5 | | -import talib as ta |
6 | | -import numpy as np |
7 | | -import tulipy as ti |
8 | | -from investing_algorithm_framework import CSVOHLCVMarketDataSource |
| 2 | +import polars as pl |
| 3 | +import pandas.testing as pdt |
| 4 | +from polars.testing import assert_frame_equal |
| 5 | + |
| 6 | +from tests.resources import TestBaseline |
| 7 | +from pyindicators import ema |
| 8 | + |
| 9 | + |
| 10 | +class Test(TestBaseline): |
| 11 | + correct_output_csv_filename = \ |
| 12 | + "EMA_200_BTC-EUR_BINANCE_15m_2023-12-01:00:00_2023-12-25:00:00.csv" |
| 13 | + |
| 14 | + def generate_pandas_df(self, polars_source_df): |
| 15 | + polars_source_df = ema( |
| 16 | + data=polars_source_df, |
| 17 | + period=200, |
| 18 | + result_column="EMA_200", |
| 19 | + source_column="Close" |
| 20 | + ) |
| 21 | + return polars_source_df |
| 22 | + |
| 23 | + def generate_polars_df(self, pandas_source_df): |
| 24 | + pandas_source_df = ema( |
| 25 | + data=pandas_source_df, |
| 26 | + period=200, |
| 27 | + result_column="EMA_200", |
| 28 | + source_column="Close" |
| 29 | + ) |
| 30 | + return pandas_source_df |
| 31 | + |
| 32 | + def test_comparison_pandas(self): |
9 | 33 |
|
10 | | -import pyindicators as pyi |
| 34 | + # Load the correct output in a pandas dataframe |
| 35 | + correct_output_pd = pd.read_csv(self.get_correct_output_csv_path()) |
11 | 36 |
|
| 37 | + # Load the source in a pandas dataframe |
| 38 | + source = pd.read_csv(self.get_source_csv_path()) |
12 | 39 |
|
13 | | -class Test(TestCase): |
| 40 | + # Generate the pandas dataframe |
| 41 | + output = self.generate_pandas_df(source) |
| 42 | + output = output[correct_output_pd.columns] |
| 43 | + output["Datetime"] = \ |
| 44 | + pd.to_datetime(output["Datetime"]).dt.tz_localize(None) |
| 45 | + correct_output_pd["Datetime"] = \ |
| 46 | + pd.to_datetime(correct_output_pd["Datetime"]).dt.tz_localize(None) |
14 | 47 |
|
15 | | - def test(self): |
16 | | - data_source = CSVOHLCVMarketDataSource( |
17 | | - csv_file_path="../test_data/OHLCV_BTC-EUR_BINANCE_15m" |
18 | | - "_2023-12-01:00:00_2023-12-25:00:00.csv", |
| 48 | + pdt.assert_frame_equal(correct_output_pd, output) |
| 49 | + |
| 50 | + def test_comparison_polars(self): |
| 51 | + |
| 52 | + # Load the correct output in a polars dataframe |
| 53 | + correct_output_pl = pl.read_csv(self.get_correct_output_csv_path()) |
| 54 | + |
| 55 | + # Load the source in a polars dataframe |
| 56 | + source = pl.read_csv(self.get_source_csv_path()) |
| 57 | + |
| 58 | + # Generate the polars dataframe |
| 59 | + output = self.generate_polars_df(source) |
| 60 | + |
| 61 | + # Convert the datetime columns to datetime |
| 62 | + # Convert the 'Datetime' column in both DataFrames to datetime |
| 63 | + output = output.with_columns( |
| 64 | + pl.col("Datetime").str.strptime(pl.Datetime).alias("Datetime") |
19 | 65 | ) |
20 | | - data_source.end_date = data_source.start_date \ |
21 | | - + timedelta(days=4, hours=4) |
22 | | - |
23 | | - while not data_source.empty(): |
24 | | - data = data_source.get_data(market_credential_service=None) |
25 | | - df = pd.DataFrame( |
26 | | - data, |
27 | | - columns=['Date', 'Open', 'High', 'Low', 'Close', 'Volume'] |
28 | | - ) |
29 | | - pyi_ema = pyi.ema(series=df["Close"], period=200) |
30 | | - ta_ema = ta.EMA(df["Close"], timeperiod=200).astype('float64') |
31 | | - ti_ema = pd.Series(ti.ema(df["Close"].to_numpy(), period=200)) |
32 | | - |
33 | | - # Define a tolerance for comparison |
34 | | - tolerance = 1e-9 |
35 | | - print(ta_ema.iloc[-1], ti_ema.iloc[-1]) |
36 | | - |
37 | | - # Compare the two Series with tolerance |
38 | | - # nan_mask = ~np.isnan(ta_sma) & ~np.isnan(pyi_sma) |
39 | | - # comparison_result = np.abs( |
40 | | - # ta_sma[nan_mask] - pyi_sma[nan_mask]) <= tolerance |
41 | | - # data_source.start_date = \ |
42 | | - # data_source.start_date + timedelta(minutes=15) |
43 | | - # data_source.end_date = data_source.end_date + timedelta(minutes=15) |
44 | | - # self.assertTrue(all(comparison_result)) |
| 66 | + |
| 67 | + correct_output_pl = correct_output_pl.with_columns( |
| 68 | + pl.col("Datetime").str.strptime(pl.Datetime).alias("Datetime") |
| 69 | + ) |
| 70 | + output = output[correct_output_pl.columns] |
| 71 | + output = self.make_polars_column_datetime_naive(output, "Datetime") |
| 72 | + correct_output_pl = self.make_polars_column_datetime_naive( |
| 73 | + correct_output_pl, "Datetime" |
| 74 | + ) |
| 75 | + |
| 76 | + assert_frame_equal(correct_output_pl, output) |
0 commit comments