Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f4e62ba57 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -82,9 +82,6 @@ class TradingApp:
|
||||
Fetch 'n' bars of historical data for the given symbol and timeframe.
|
||||
"""
|
||||
rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, n)
|
||||
if rates is None:
|
||||
log_and_print(f"Could not retrieve data for {symbol}", is_error=True)
|
||||
return None
|
||||
rates_frame = pd.DataFrame(rates)
|
||||
rates_frame['time'] = pd.to_datetime(rates_frame['time'], unit='s')
|
||||
rates_frame.set_index('time', inplace=True)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import pytest
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from features.labeling_schemes import calculate_future_returns
|
||||
|
||||
def test_calculate_future_returns_happy_path():
|
||||
# Setup data
|
||||
data = {"close": [100.0, 105.0, 102.0, 110.0, 115.0]}
|
||||
df = pd.DataFrame(data)
|
||||
|
||||
# Test horizon 1
|
||||
result_h1 = calculate_future_returns(df.copy(), horizon=1)
|
||||
|
||||
# Expected:
|
||||
# idx 0: (105 - 100) / 100 = 0.05
|
||||
# idx 1: (102 - 105) / 105 = -0.028571
|
||||
# idx 2: (110 - 102) / 102 = 0.078431
|
||||
# idx 3: (115 - 110) / 110 = 0.045455
|
||||
# idx 4: NaN
|
||||
assert len(result_h1) == 4
|
||||
np.testing.assert_allclose(result_h1["future_returns"].iloc[0], 0.05, atol=1e-5)
|
||||
np.testing.assert_allclose(result_h1["future_returns"].iloc[1], -0.028571, atol=1e-5)
|
||||
|
||||
# Test horizon 2
|
||||
result_h2 = calculate_future_returns(df.copy(), horizon=2)
|
||||
# idx 0: (102 - 100) / 100 = 0.02
|
||||
# idx 1: (110 - 105) / 105 = 0.047619
|
||||
# idx 2: (115 - 102) / 102 = 0.127451
|
||||
# idx 3: NaN
|
||||
# idx 4: NaN
|
||||
assert len(result_h2) == 3
|
||||
np.testing.assert_allclose(result_h2["future_returns"].iloc[0], 0.02, atol=1e-5)
|
||||
np.testing.assert_allclose(result_h2["future_returns"].iloc[1], 0.047619, atol=1e-5)
|
||||
np.testing.assert_allclose(result_h2["future_returns"].iloc[2], 0.127451, atol=1e-5)
|
||||
|
||||
def test_calculate_future_returns_tiny_df():
|
||||
# Rationale: Testing with a tiny DataFrame length < horizon to see if it correctly returns empty or handles it gracefully.
|
||||
data = {"close": [100.0, 105.0]}
|
||||
df = pd.DataFrame(data)
|
||||
|
||||
# Test horizon 5 where df length is 2
|
||||
result = calculate_future_returns(df.copy(), horizon=5)
|
||||
|
||||
# Should return empty DataFrame gracefully
|
||||
assert result.empty
|
||||
assert "future_returns" in result.columns
|
||||
|
||||
def test_calculate_future_returns_missing_close_column():
|
||||
data = {"open": [100.0, 105.0, 102.0]}
|
||||
df = pd.DataFrame(data)
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
calculate_future_returns(df.copy(), horizon=1)
|
||||
@@ -1,24 +0,0 @@
|
||||
import sys
|
||||
from unittest.mock import MagicMock
|
||||
# Mock out MetaTrader5 before importing our module
|
||||
sys.modules['MetaTrader5'] = MagicMock()
|
||||
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
import pandas as pd
|
||||
from live_trading.multi_bar import TradingApp, log_and_print
|
||||
|
||||
class TestTradingApp(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.app = TradingApp(symbol="EURUSD", lot_size=0.01, magic_number=123456)
|
||||
|
||||
@patch("live_trading.multi_bar.mt5.copy_rates_from_pos")
|
||||
@patch("live_trading.multi_bar.log_and_print")
|
||||
def test_get_data_returns_none(self, mock_log, mock_copy_rates):
|
||||
mock_copy_rates.return_value = None
|
||||
|
||||
# Test what happens when mt5.copy_rates_from_pos returns None
|
||||
result = self.app.get_data("EURUSD", 100, 16408)
|
||||
|
||||
self.assertIsNone(result)
|
||||
mock_log.assert_called_once_with("Could not retrieve data for EURUSD", is_error=True)
|
||||
Reference in New Issue
Block a user