test(protocol): compiler semantica dow + is_weekend

This commit is contained in:
2026-05-11 17:01:02 +02:00
parent 9d1f97cff3
commit 22a934a6cf
+53
View File
@@ -130,3 +130,56 @@ def test_compile_hour_feature_returns_index_hour(ohlcv: pd.DataFrame) -> None:
signal = fn(ohlcv)
# All rows have hour >= 0 > -1, so all entry-long.
assert (signal == Side.LONG).all()
def test_compile_dow_feature_monday_is_zero(ohlcv: pd.DataFrame) -> None:
# 2024-01-01 is Monday -> dow=0; eq(dow, 0) gates LONG on Monday rows only.
src = json.dumps(
{
"rules": [
{
"condition": {
"op": "eq",
"args": [
{"kind": "feature", "name": "dow"},
{"kind": "literal", "value": 0.0},
],
},
"action": "entry-long",
}
]
}
)
ast = parse_strategy(src)
fn = compile_strategy(ast)
signal = fn(ohlcv)
monday_rows = signal[signal.index.dayofweek == 0]
other_rows = signal[signal.index.dayofweek != 0]
assert (monday_rows == Side.LONG).all()
assert (other_rows == Side.FLAT).all()
def test_compile_is_weekend_returns_zero_one(ohlcv: pd.DataFrame) -> None:
src = json.dumps(
{
"rules": [
{
"condition": {
"op": "eq",
"args": [
{"kind": "feature", "name": "is_weekend"},
{"kind": "literal", "value": 1.0},
],
},
"action": "entry-long",
}
]
}
)
ast = parse_strategy(src)
fn = compile_strategy(ast)
signal = fn(ohlcv)
weekend = signal[signal.index.dayofweek >= 5]
weekdays = signal[signal.index.dayofweek < 5]
assert (weekend == Side.LONG).all()
assert (weekdays == Side.FLAT).all()