|
| 1 | +import os |
| 2 | +from typing import Dict, Any |
| 3 | +from datetime import datetime, timezone |
| 4 | +import pandas as pd |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from investing_algorithm_framework import PortfolioConfiguration, \ |
| 8 | + MarketCredential, TradingStrategy, DataSource, BacktestDateRange |
| 9 | +from investing_algorithm_framework.domain.exceptions import DataError |
| 10 | +from tests.resources import TestBase |
| 11 | + |
| 12 | +class TestStrategy(TradingStrategy): |
| 13 | + time_unit = "MINUTE" |
| 14 | + interval = 1 |
| 15 | + data_sources = [ |
| 16 | + DataSource( |
| 17 | + data_type="OHLCV", |
| 18 | + identifier="sol_1d_ohlcv_data", |
| 19 | + time_frame="1d", |
| 20 | + window_size=200, |
| 21 | + symbol="SOL/EUR", |
| 22 | + ) |
| 23 | + ] |
| 24 | + |
| 25 | + def generate_sell_signals( |
| 26 | + self, data: Dict[str, Any] |
| 27 | + ) -> Dict[str, pd.Series]: |
| 28 | + return {} |
| 29 | + |
| 30 | + def generate_buy_signals( |
| 31 | + self, data: Dict[str, Any] |
| 32 | + ) -> Dict[str, pd.Series]: |
| 33 | + return {} |
| 34 | + |
| 35 | + |
| 36 | +class TestStrategyIncompleteData(TradingStrategy): |
| 37 | + time_unit = "MINUTE" |
| 38 | + interval = 1 |
| 39 | + data_sources = [ |
| 40 | + DataSource( |
| 41 | + data_type="OHLCV", |
| 42 | + identifier="sol_1d_incomplete_ohlcv_data", |
| 43 | + time_frame="1d", |
| 44 | + window_size=200, |
| 45 | + symbol="SOL/EUR", |
| 46 | + market="BITVAVO", |
| 47 | + data_provider_identifier="BITVAVO" |
| 48 | + ) |
| 49 | + ] |
| 50 | + |
| 51 | + def generate_sell_signals( |
| 52 | + self, data: Dict[str, Any] |
| 53 | + ) -> Dict[str, pd.Series]: |
| 54 | + return {} |
| 55 | + |
| 56 | + def generate_buy_signals( |
| 57 | + self, data: Dict[str, Any] |
| 58 | + ) -> Dict[str, pd.Series]: |
| 59 | + return {} |
| 60 | + |
| 61 | + |
| 62 | +class TestConfig(TestBase): |
| 63 | + portfolio_configurations = [ |
| 64 | + PortfolioConfiguration( |
| 65 | + market="BITVAVO", |
| 66 | + trading_symbol="EUR" |
| 67 | + ) |
| 68 | + ] |
| 69 | + market_credentials = [ |
| 70 | + MarketCredential( |
| 71 | + market="BITVAVO", |
| 72 | + api_key="api_key", |
| 73 | + secret_key="secret_key" |
| 74 | + ) |
| 75 | + ] |
| 76 | + external_balances = { |
| 77 | + "EUR": 1000, |
| 78 | + } |
| 79 | + |
| 80 | + def test_data_completeness(self): |
| 81 | + """Test that check_data_completeness passes when data is complete.""" |
| 82 | + strategy = TestStrategy() |
| 83 | + |
| 84 | + self.app.check_data_completeness( |
| 85 | + strategies=[strategy], |
| 86 | + backtest_date_range=BacktestDateRange( |
| 87 | + start_date=datetime( |
| 88 | + 2021, 8, 3, tzinfo=timezone.utc |
| 89 | + ), |
| 90 | + end_date=datetime( |
| 91 | + 2021, 8, 20, tzinfo=timezone.utc |
| 92 | + ) |
| 93 | + ) |
| 94 | + ) |
| 95 | + |
| 96 | + def test_data_completeness_incomplete_data(self): |
| 97 | + """ |
| 98 | + Test that check_data_completeness raises DataError when data is |
| 99 | + incomplete. This test mocks the data provider to return incomplete |
| 100 | + data with gaps. |
| 101 | + """ |
| 102 | + strategy = TestStrategyIncompleteData() |
| 103 | + |
| 104 | + data_complete, completeness_info = self.app.check_data_completeness( |
| 105 | + strategies=[strategy], |
| 106 | + backtest_date_range=BacktestDateRange( |
| 107 | + start_date=datetime( |
| 108 | + 2021, 6, 15, tzinfo=timezone.utc |
| 109 | + ), |
| 110 | + end_date=datetime( |
| 111 | + 2024, 1, 1, tzinfo=timezone.utc |
| 112 | + ) |
| 113 | + ) |
| 114 | + ) |
| 115 | + |
| 116 | + self.assertFalse(data_complete) |
| 117 | + self.assertIn( |
| 118 | + "sol_1d_incomplete_ohlcv_data", completeness_info |
| 119 | + ) |
| 120 | + print(completeness_info) |
0 commit comments