|
1 |
| -import numpy as np |
2 | 1 | import pandas as pd
|
3 | 2 | import pytest
|
4 | 3 |
|
5 | 4 | from freqtrade.constants import DEFAULT_TRADES_COLUMNS
|
6 | 5 | from freqtrade.data.converter import populate_dataframe_with_trades
|
7 | 6 | from freqtrade.data.converter.orderflow import (
|
8 | 7 | ORDERFLOW_ADDED_COLUMNS,
|
| 8 | + stacked_imbalance, |
9 | 9 | timeframe_to_DateOffset,
|
10 | 10 | trades_to_volumeprofile_with_total_delta_bid_ask,
|
11 | 11 | )
|
@@ -185,24 +185,24 @@ def test_public_trades_mock_populate_dataframe_with_trades__check_orderflow(
|
185 | 185 | assert results["max_delta"] == 17.298
|
186 | 186 |
|
187 | 187 | # Assert that stacked imbalances are NaN (not applicable in this test)
|
188 |
| - assert np.isnan(results["stacked_imbalances_bid"]) |
189 |
| - assert np.isnan(results["stacked_imbalances_ask"]) |
| 188 | + assert results["stacked_imbalances_bid"] == [] |
| 189 | + assert results["stacked_imbalances_ask"] == [] |
190 | 190 |
|
191 | 191 | # Repeat assertions for the third from last row
|
192 | 192 | results = df.iloc[-2]
|
193 | 193 | assert pytest.approx(results["delta"]) == -20.862
|
194 | 194 | assert pytest.approx(results["min_delta"]) == -54.559999
|
195 | 195 | assert 82.842 == results["max_delta"]
|
196 |
| - assert 234.99 == results["stacked_imbalances_bid"] |
197 |
| - assert 234.96 == results["stacked_imbalances_ask"] |
| 196 | + assert results["stacked_imbalances_bid"] == [234.97] |
| 197 | + assert results["stacked_imbalances_ask"] == [234.94] |
198 | 198 |
|
199 | 199 | # Repeat assertions for the last row
|
200 | 200 | results = df.iloc[-1]
|
201 | 201 | assert pytest.approx(results["delta"]) == -49.302
|
202 | 202 | assert results["min_delta"] == -70.222
|
203 | 203 | assert pytest.approx(results["max_delta"]) == 11.213
|
204 |
| - assert np.isnan(results["stacked_imbalances_bid"]) |
205 |
| - assert np.isnan(results["stacked_imbalances_ask"]) |
| 204 | + assert results["stacked_imbalances_bid"] == [] |
| 205 | + assert results["stacked_imbalances_ask"] == [] |
206 | 206 |
|
207 | 207 |
|
208 | 208 | def test_public_trades_trades_mock_populate_dataframe_with_trades__check_trades(
|
@@ -358,7 +358,8 @@ def test_public_trades_binned_big_sample_list(public_trades_list):
|
358 | 358 | assert 197.512 == df["bid_amount"].iloc[0] # total bid amount
|
359 | 359 | assert 88.98 == df["ask_amount"].iloc[0] # total ask amount
|
360 | 360 | assert 26 == df["ask"].iloc[0] # ask price
|
361 |
| - assert -108.532 == pytest.approx(df["delta"].iloc[0]) # delta (bid amount - ask amount) |
| 361 | + # delta (bid amount - ask amount) |
| 362 | + assert -108.532 == pytest.approx(df["delta"].iloc[0]) |
362 | 363 |
|
363 | 364 | assert 3 == df["bid"].iloc[-1] # bid price
|
364 | 365 | assert 50.659 == df["bid_amount"].iloc[-1] # total bid amount
|
@@ -567,6 +568,40 @@ def test_analyze_with_orderflow(
|
567 | 568 | assert isinstance(lastval_of2, dict)
|
568 | 569 |
|
569 | 570 |
|
| 571 | +def test_stacked_imbalances_multiple_prices(): |
| 572 | + """Test that stacked imbalances correctly returns multiple price levels when present""" |
| 573 | + # Test with empty result |
| 574 | + df_no_stacks = pd.DataFrame( |
| 575 | + { |
| 576 | + "bid_imbalance": [False, False, True, False], |
| 577 | + "ask_imbalance": [False, True, False, False], |
| 578 | + }, |
| 579 | + index=[234.95, 234.96, 234.97, 234.98], |
| 580 | + ) |
| 581 | + no_stacks = stacked_imbalance(df_no_stacks, "bid", stacked_imbalance_range=2) |
| 582 | + assert no_stacks == [] |
| 583 | + |
| 584 | + # Create a sample DataFrame with known imbalances |
| 585 | + df = pd.DataFrame( |
| 586 | + { |
| 587 | + "bid_imbalance": [True, True, True, False, False, True, True, False, True], |
| 588 | + "ask_imbalance": [False, False, True, True, True, False, False, True, True], |
| 589 | + }, |
| 590 | + index=[234.95, 234.96, 234.97, 234.98, 234.99, 235.00, 235.01, 235.02, 235.03], |
| 591 | + ) |
| 592 | + # Test bid imbalances (should return prices in ascending order) |
| 593 | + bid_prices = stacked_imbalance(df, "bid", stacked_imbalance_range=2) |
| 594 | + assert bid_prices == [234.95, 234.96, 235.00] |
| 595 | + |
| 596 | + # Test ask imbalances (should return prices in descending order) |
| 597 | + ask_prices = stacked_imbalance(df, "ask", stacked_imbalance_range=2) |
| 598 | + assert ask_prices == [234.97, 234.98, 235.02] |
| 599 | + |
| 600 | + # Test with higher stacked_imbalance_range |
| 601 | + bid_prices_higher = stacked_imbalance(df, "bid", stacked_imbalance_range=3) |
| 602 | + assert bid_prices_higher == [234.95] |
| 603 | + |
| 604 | + |
570 | 605 | def test_timeframe_to_DateOffset():
|
571 | 606 | assert timeframe_to_DateOffset("1s") == pd.DateOffset(seconds=1)
|
572 | 607 | assert timeframe_to_DateOffset("1m") == pd.DateOffset(minutes=1)
|
|
0 commit comments