From 88d2f9263e26fdb9d32761a9cfaf285c696a9c7d Mon Sep 17 00:00:00 2001 From: Young Date: Thu, 2 Sep 2021 00:45:04 +0000 Subject: [PATCH] fix sum index data bug --- qlib/backtest/order.py | 2 +- qlib/utils/index_data.py | 2 +- tests/misc/test_index_data.py | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/qlib/backtest/order.py b/qlib/backtest/order.py index e169ffd64..a1b21be0a 100644 --- a/qlib/backtest/order.py +++ b/qlib/backtest/order.py @@ -492,7 +492,7 @@ class BaseTradeDecision: for obj in self.get_decision(): if isinstance(obj, Order): # Zero amount order will be treated as empty - if not np.isclose(obj.amount, 0.0): + if obj.amount > 1e-6: return False else: return True diff --git a/qlib/utils/index_data.py b/qlib/utils/index_data.py index 9bd059add..09ddbf471 100644 --- a/qlib/utils/index_data.py +++ b/qlib/utils/index_data.py @@ -74,7 +74,7 @@ def sum_by_index(data_list: Union["SingleData"], new_index: list, fill_value=0) for id in new_index: item_sum = 0 for data in data_list: - if id in data and data[id] != np.NaN: + if id in data and not np.isnan(data[id]): item_sum += data[id] else: item_sum += fill_value diff --git a/tests/misc/test_index_data.py b/tests/misc/test_index_data.py index 010b32847..3cd819a0f 100644 --- a/tests/misc/test_index_data.py +++ b/tests/misc/test_index_data.py @@ -107,6 +107,8 @@ class IndexDataTest(unittest.TestCase): self.assertTrue(np.isnan((sd1 + sd2).iloc[3])) self.assertTrue(sd1.add(sd2).sum() == 13) + self.assertTrue(idd.sum_by_index([sd1, sd2], sd1.index, fill_value=0.0).sum() == 13) + def test_todo(self): pass # here are some examples which do not affect the current system, but it is weird not to support it