fix: don't sort stacked imbalances, return empty list if no found...
... also removes helper functions `stacked_imbalance_bid` & `stacked_imbalance_ask`
This commit is contained in:
@@ -164,12 +164,12 @@ def populate_dataframe_with_trades(
|
|||||||
dataframe.at[index, "imbalances"] = imbalances.to_dict(orient="index")
|
dataframe.at[index, "imbalances"] = imbalances.to_dict(orient="index")
|
||||||
|
|
||||||
stacked_imbalance_range = config_orderflow["stacked_imbalance_range"]
|
stacked_imbalance_range = config_orderflow["stacked_imbalance_range"]
|
||||||
dataframe.at[index, "stacked_imbalances_bid"] = stacked_imbalance_bid(
|
dataframe.at[index, "stacked_imbalances_bid"] = stacked_imbalance(
|
||||||
imbalances, stacked_imbalance_range=stacked_imbalance_range
|
imbalances, label="bid", stacked_imbalance_range=stacked_imbalance_range
|
||||||
)
|
)
|
||||||
|
|
||||||
dataframe.at[index, "stacked_imbalances_ask"] = stacked_imbalance_ask(
|
dataframe.at[index, "stacked_imbalances_ask"] = stacked_imbalance(
|
||||||
imbalances, stacked_imbalance_range=stacked_imbalance_range
|
imbalances, label="ask", stacked_imbalance_range=stacked_imbalance_range
|
||||||
)
|
)
|
||||||
|
|
||||||
bid = np.where(
|
bid = np.where(
|
||||||
@@ -256,9 +256,7 @@ def trades_orderflow_to_imbalances(df: pd.DataFrame, imbalance_ratio: int, imbal
|
|||||||
return dataframe
|
return dataframe
|
||||||
|
|
||||||
|
|
||||||
def stacked_imbalance(
|
def stacked_imbalance(df: pd.DataFrame, label: str, stacked_imbalance_range: int):
|
||||||
df: pd.DataFrame, label: str, stacked_imbalance_range: int, should_reverse: bool
|
|
||||||
):
|
|
||||||
"""
|
"""
|
||||||
y * (y.groupby((y != y.shift()).cumsum()).cumcount() + 1)
|
y * (y.groupby((y != y.shift()).cumsum()).cumcount() + 1)
|
||||||
https://stackoverflow.com/questions/27626542/counting-consecutive-positive-values-in-python-pandas-array
|
https://stackoverflow.com/questions/27626542/counting-consecutive-positive-values-in-python-pandas-array
|
||||||
@@ -268,27 +266,14 @@ def stacked_imbalance(
|
|||||||
# Group consecutive True values and get their counts
|
# Group consecutive True values and get their counts
|
||||||
groups = (int_series != int_series.shift()).cumsum()
|
groups = (int_series != int_series.shift()).cumsum()
|
||||||
counts = int_series.groupby(groups).cumsum()
|
counts = int_series.groupby(groups).cumsum()
|
||||||
|
|
||||||
# Find indices where count meets or exceeds the range requirement
|
# Find indices where count meets or exceeds the range requirement
|
||||||
valid_indices = counts[counts >= stacked_imbalance_range].index
|
valid_indices = counts[counts >= stacked_imbalance_range].index
|
||||||
|
|
||||||
stacked_imbalance_prices = []
|
stacked_imbalance_prices = []
|
||||||
if not valid_indices.empty:
|
if not valid_indices.empty:
|
||||||
# Get all prices from valid indices from beginning of the range
|
# Get all prices from valid indices from beginning of the range
|
||||||
valid_prices = [imbalance.index.values[idx-(stacked_imbalance_range-1)] for idx in valid_indices]
|
stacked_imbalance_prices = [
|
||||||
# Sort prices according to direction
|
imbalance.index.values[idx - (stacked_imbalance_range - 1)] for idx in valid_indices
|
||||||
stacked_imbalance_prices = (
|
]
|
||||||
sorted(valid_prices)
|
return stacked_imbalance_prices if stacked_imbalance_prices else []
|
||||||
if not should_reverse
|
|
||||||
else sorted(valid_prices, reverse=True)
|
|
||||||
)
|
|
||||||
|
|
||||||
return stacked_imbalance_prices if stacked_imbalance_prices else [np.nan]
|
|
||||||
|
|
||||||
|
|
||||||
def stacked_imbalance_ask(df: pd.DataFrame, stacked_imbalance_range: int):
|
|
||||||
return stacked_imbalance(df, "ask", stacked_imbalance_range, should_reverse=True)
|
|
||||||
|
|
||||||
|
|
||||||
def stacked_imbalance_bid(df: pd.DataFrame, stacked_imbalance_range: int):
|
|
||||||
return stacked_imbalance(df, "bid", stacked_imbalance_range, should_reverse=False)
|
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ from freqtrade.constants import DEFAULT_TRADES_COLUMNS
|
|||||||
from freqtrade.data.converter import populate_dataframe_with_trades
|
from freqtrade.data.converter import populate_dataframe_with_trades
|
||||||
from freqtrade.data.converter.orderflow import (
|
from freqtrade.data.converter.orderflow import (
|
||||||
ORDERFLOW_ADDED_COLUMNS,
|
ORDERFLOW_ADDED_COLUMNS,
|
||||||
|
stacked_imbalance,
|
||||||
timeframe_to_DateOffset,
|
timeframe_to_DateOffset,
|
||||||
trades_to_volumeprofile_with_total_delta_bid_ask,
|
trades_to_volumeprofile_with_total_delta_bid_ask,
|
||||||
stacked_imbalance,
|
|
||||||
)
|
)
|
||||||
from freqtrade.data.converter.trade_converter import trades_list_to_df
|
from freqtrade.data.converter.trade_converter import trades_list_to_df
|
||||||
from freqtrade.data.dataprovider import DataProvider
|
from freqtrade.data.dataprovider import DataProvider
|
||||||
@@ -185,8 +185,8 @@ def test_public_trades_mock_populate_dataframe_with_trades__check_orderflow(
|
|||||||
assert results["max_delta"] == 17.298
|
assert results["max_delta"] == 17.298
|
||||||
|
|
||||||
# Assert that stacked imbalances are NaN (not applicable in this test)
|
# Assert that stacked imbalances are NaN (not applicable in this test)
|
||||||
assert results["stacked_imbalances_bid"] == [np.nan]
|
assert results["stacked_imbalances_bid"] == []
|
||||||
assert results["stacked_imbalances_ask"] == [np.nan]
|
assert results["stacked_imbalances_ask"] == []
|
||||||
|
|
||||||
# Repeat assertions for the third from last row
|
# Repeat assertions for the third from last row
|
||||||
results = df.iloc[-2]
|
results = df.iloc[-2]
|
||||||
@@ -201,8 +201,8 @@ def test_public_trades_mock_populate_dataframe_with_trades__check_orderflow(
|
|||||||
assert pytest.approx(results["delta"]) == -49.302
|
assert pytest.approx(results["delta"]) == -49.302
|
||||||
assert results["min_delta"] == -70.222
|
assert results["min_delta"] == -70.222
|
||||||
assert pytest.approx(results["max_delta"]) == 11.213
|
assert pytest.approx(results["max_delta"]) == 11.213
|
||||||
assert results["stacked_imbalances_bid"] == [np.nan]
|
assert results["stacked_imbalances_bid"] == []
|
||||||
assert results["stacked_imbalances_ask"] == [np.nan]
|
assert results["stacked_imbalances_ask"] == []
|
||||||
|
|
||||||
|
|
||||||
def test_public_trades_trades_mock_populate_dataframe_with_trades__check_trades(
|
def test_public_trades_trades_mock_populate_dataframe_with_trades__check_trades(
|
||||||
@@ -575,34 +575,33 @@ def test_stacked_imbalances_multiple_prices():
|
|||||||
# Test with empty result
|
# Test with empty result
|
||||||
df_no_stacks = pd.DataFrame(
|
df_no_stacks = pd.DataFrame(
|
||||||
{
|
{
|
||||||
'bid_imbalance': [False, False, True, False],
|
"bid_imbalance": [False, False, True, False],
|
||||||
'ask_imbalance': [False, True, False, False]
|
"ask_imbalance": [False, True, False, False],
|
||||||
},
|
},
|
||||||
index=[234.95, 234.96, 234.97, 234.98]
|
index=[234.95, 234.96, 234.97, 234.98],
|
||||||
)
|
)
|
||||||
no_stacks = stacked_imbalance(df_no_stacks, "bid", stacked_imbalance_range=2, should_reverse=False)
|
no_stacks = stacked_imbalance(df_no_stacks, "bid", stacked_imbalance_range=2)
|
||||||
assert no_stacks == [np.nan]
|
assert no_stacks == []
|
||||||
|
|
||||||
# Create a sample DataFrame with known imbalances
|
# Create a sample DataFrame with known imbalances
|
||||||
df = pd.DataFrame(
|
df = pd.DataFrame(
|
||||||
{
|
{
|
||||||
'bid_imbalance': [True, True, True, False, False, True, True, False, True],
|
"bid_imbalance": [True, True, True, False, False, True, True, False, True],
|
||||||
'ask_imbalance': [False, False, True, True, True, False, False, True, True]
|
"ask_imbalance": [False, False, True, True, True, False, False, True, True],
|
||||||
},
|
},
|
||||||
index=[234.95, 234.96, 234.97, 234.98, 234.99, 235.00, 235.01, 235.02, 235.03]
|
index=[234.95, 234.96, 234.97, 234.98, 234.99, 235.00, 235.01, 235.02, 235.03],
|
||||||
)
|
)
|
||||||
# Test bid imbalances (should return prices in ascending order)
|
# Test bid imbalances (should return prices in ascending order)
|
||||||
bid_prices = stacked_imbalance(df, "bid", stacked_imbalance_range=2, should_reverse=False)
|
bid_prices = stacked_imbalance(df, "bid", stacked_imbalance_range=2)
|
||||||
assert bid_prices == [234.95, 234.96, 235.00]
|
assert bid_prices == [234.95, 234.96, 235.00]
|
||||||
|
|
||||||
# Test ask imbalances (should return prices in descending order)
|
# Test ask imbalances (should return prices in descending order)
|
||||||
ask_prices = stacked_imbalance(df, "ask", stacked_imbalance_range=2, should_reverse=True)
|
ask_prices = stacked_imbalance(df, "ask", stacked_imbalance_range=2)
|
||||||
assert ask_prices == [235.02, 234.98, 234.97]
|
assert ask_prices == [234.97, 234.98, 235.02]
|
||||||
|
|
||||||
# Test with higher stacked_imbalance_range
|
# Test with higher stacked_imbalance_range
|
||||||
bid_prices_higher = stacked_imbalance(df, "bid", stacked_imbalance_range=3, should_reverse=False)
|
bid_prices_higher = stacked_imbalance(df, "bid", stacked_imbalance_range=3)
|
||||||
assert bid_prices_higher == [234.95]
|
assert bid_prices_higher == [234.95]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_timeframe_to_DateOffset():
|
def test_timeframe_to_DateOffset():
|
||||||
|
|||||||
Reference in New Issue
Block a user