| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226 |
- from hermes_mcp.decision_engine import assess_wallet_state, make_decision, normalize_strategy_snapshot, score_strategy_fit
- def test_assess_wallet_state_marks_one_sided_wallet_as_depleted_base_side():
- concern = {"account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- account_info = {
- "balances": [
- {"asset_code": "XRP", "available": 1},
- {"asset_code": "USD", "available": 1000},
- ]
- }
- wallet = assess_wallet_state(account_info=account_info, concern=concern, price=2.0)
- assert wallet["inventory_state"] == "depleted_base_side"
- assert wallet["rebalance_needed"] is True
- assert wallet["grid_ready"] is False
- def test_assess_wallet_state_infers_base_and_quote_from_market_symbol_when_missing():
- concern = {"account_id": "a1", "market_symbol": "xrpusd", "base_currency": None, "quote_currency": None}
- account_info = {
- "balances": [
- {"asset_code": "XRP", "available": 64.68158},
- {"asset_code": "USD", "available": 12.64},
- ]
- }
- wallet = assess_wallet_state(account_info=account_info, concern=concern, price=1.318, strategies=[])
- assert wallet["base_currency"] == "XRP"
- assert wallet["quote_currency"] == "USD"
- assert wallet["base_available"] == 64.68158
- assert wallet["quote_available"] == 12.64
- def test_assess_wallet_state_marks_one_sided_wallet_as_depleted_quote_side():
- concern = {"account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- account_info = {
- "balances": [
- {"asset_code": "XRP", "available": 1000},
- {"asset_code": "USD", "available": 1},
- ]
- }
- wallet = assess_wallet_state(account_info=account_info, concern=concern, price=2.0)
- assert wallet["inventory_state"] == "depleted_quote_side"
- assert wallet["rebalance_needed"] is True
- assert wallet["grid_ready"] is False
- def test_score_strategy_fit_penalizes_grid_when_wallet_unbalanced():
- strategy = normalize_strategy_snapshot({
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "state": {},
- "config": {},
- })
- narrative = {"stance": "constructive_bullish", "opportunity_map": {"continuation": 0.7, "mean_reversion": 0.1, "reversal": 0.1, "wait": 0.1}}
- wallet_state = {"inventory_state": "critically_unbalanced", "rebalance_needed": True}
- fit = score_strategy_fit(strategy=strategy, narrative=narrative, wallet_state=wallet_state)
- assert fit["score"] < 0
- assert any("grid" in block or "wallet" in block for block in fit["blocks"])
- def test_score_strategy_fit_rewards_trend_when_breakout_is_confirmed():
- strategy = normalize_strategy_snapshot({
- "id": "trend-1",
- "strategy_type": "trend_follower",
- "mode": "off",
- "account_id": "a1",
- "state": {},
- "config": {},
- })
- base_narrative = {
- "stance": "constructive_bullish",
- "opportunity_map": {"continuation": 0.72, "mean_reversion": 0.08, "reversal": 0.05, "wait": 0.15},
- "grid_breakout_pressure": {"phase": "developing"},
- }
- confirmed_narrative = {
- **base_narrative,
- "grid_breakout_pressure": {"phase": "confirmed", "persistent": True},
- }
- wallet_state = {"inventory_state": "balanced", "rebalance_needed": False}
- base_fit = score_strategy_fit(strategy=strategy, narrative=base_narrative, wallet_state=wallet_state)
- confirmed_fit = score_strategy_fit(strategy=strategy, narrative=confirmed_narrative, wallet_state=wallet_state)
- assert confirmed_fit["score"] > base_fit["score"]
- assert any("confirmed breakout" in reason for reason in confirmed_fit["reasons"])
- def test_score_strategy_fit_prefers_matching_trade_side():
- bullish_narrative = {
- "stance": "constructive_bullish",
- "opportunity_map": {"continuation": 0.75, "mean_reversion": 0.08, "reversal": 0.05, "wait": 0.12},
- "grid_breakout_pressure": {"phase": "confirmed", "persistent": True, "micro_bias": "bullish", "meso_bias": "bullish"},
- }
- bearish_narrative = {
- "stance": "constructive_bearish",
- "opportunity_map": {"continuation": 0.75, "mean_reversion": 0.08, "reversal": 0.05, "wait": 0.12},
- "grid_breakout_pressure": {"phase": "confirmed", "persistent": True, "micro_bias": "bearish", "meso_bias": "bearish"},
- }
- wallet_state = {"inventory_state": "balanced", "rebalance_needed": False}
- buy_strategy = normalize_strategy_snapshot({"id": "trend-long", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {"trade_side": "buy"}})
- sell_strategy = normalize_strategy_snapshot({"id": "trend-short", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {"trade_side": "sell"}})
- buy_fit = score_strategy_fit(strategy=buy_strategy, narrative=bullish_narrative, wallet_state=wallet_state)
- sell_fit = score_strategy_fit(strategy=sell_strategy, narrative=bullish_narrative, wallet_state=wallet_state)
- assert buy_fit["score"] > sell_fit["score"]
- buy_fit_bear = score_strategy_fit(strategy=buy_strategy, narrative=bearish_narrative, wallet_state=wallet_state)
- sell_fit_bear = score_strategy_fit(strategy=sell_strategy, narrative=bearish_narrative, wallet_state=wallet_state)
- assert sell_fit_bear["score"] > buy_fit_bear["score"]
- def test_assess_wallet_state_counts_reserved_orders_in_effective_inventory():
- concern = {"account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- account_info = {
- "balances": [
- {"asset_code": "XRP", "available": 0},
- {"asset_code": "USD", "available": 0},
- ]
- }
- strategies = [
- {
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "market_symbol": "xrpusd",
- "state": {
- "orders": [
- {"side": "sell", "status": "open", "amount": "10", "price": "1.50"},
- {"side": "buy", "status": "open", "amount": "10", "price": "1.40"},
- ]
- },
- }
- ]
- wallet = assess_wallet_state(account_info=account_info, concern=concern, price=1.45, strategies=strategies)
- assert wallet["inventory_state"] == "balanced"
- assert wallet["base_reserved"] == 10.0
- assert wallet["quote_reserved"] == 14.0
- assert wallet["base_effective"] == 10.0
- assert wallet["quote_effective"] == 14.0
- def test_make_decision_keeps_grid_when_imbalance_is_manageable():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.72,
- "opportunity_map": {"continuation": 0.75, "mean_reversion": 0.1, "reversal": 0.05, "wait": 0.1},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.8,
- "quote_ratio": 0.2,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "observe"
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_does_not_replace_grid_with_rebalancer_only_because_grid_mentions_handoff_readiness():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "cautious_bullish",
- "confidence": 0.74,
- "opportunity_map": {"continuation": 0.5, "mean_reversion": 0.25, "reversal": 0.05, "wait": 0.2},
- "scoped_state": {
- "micro": {"impulse": "mixed", "trend_bias": "mixed"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.64,
- "quote_ratio": 0.36,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": False, "side_capacity": {"buy": False, "sell": True}}}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_replaces_grid_when_breakout_pressure_is_persistent():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.78,
- "opportunity_map": {"continuation": 0.72, "mean_reversion": 0.08, "reversal": 0.05, "wait": 0.15},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "critically_unbalanced",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.88,
- "quote_ratio": 0.12,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "replace_with_exposure_protector"
- assert decision.target_strategy == "protect-1"
- def test_make_decision_keeps_grid_when_critically_unbalanced_but_grid_still_has_working_side_capacity():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "cautious_bullish",
- "confidence": 0.7,
- "opportunity_map": {"continuation": 0.55, "mean_reversion": 0.2, "reversal": 0.05, "wait": 0.2},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "upper_half", "reversal_risk": "low"},
- "meso": {"structure": "range", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- "features_by_timeframe": {"1m": {"raw": {"price": 1.4374}}},
- }
- wallet_state = {
- "inventory_state": "critically_unbalanced",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.86,
- "quote_ratio": 0.14,
- }
- strategies = [
- {
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "state": {
- "last_price": 1.4374,
- "open_order_count": 4,
- "orders": [
- {"side": "sell", "status": "open", "price": "1.43956", "amount": "7"},
- {"side": "sell", "status": "open", "price": "1.44500", "amount": "7"},
- ],
- },
- "config": {},
- "report": {"supervision": {"capacity_available": False, "side_capacity": {"buy": False, "sell": True}, "inventory_pressure": "critical", "degraded": False}},
- },
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "observe"
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_keeps_grid_when_trend_has_only_eaten_two_levels():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.78,
- "opportunity_map": {"continuation": 0.72, "mean_reversion": 0.08, "reversal": 0.05, "wait": 0.15},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "upper_half", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- "features_by_timeframe": {"1m": {"raw": {"price": 110.0}}},
- }
- wallet_state = {
- "inventory_state": "balanced",
- "rebalance_needed": False,
- "grid_ready": True,
- "base_ratio": 0.52,
- "quote_ratio": 0.48,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {"center_price": 100.0}, "config": {"grid_step_pct": 0.05}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "warn"
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_replaces_grid_when_third_level_is_sustained():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.82,
- "opportunity_map": {"continuation": 0.82, "mean_reversion": 0.05, "reversal": 0.03, "wait": 0.1},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "near_upper_band", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- "features_by_timeframe": {"1m": {"raw": {"price": 116.0}}},
- }
- wallet_state = {
- "inventory_state": "balanced",
- "rebalance_needed": False,
- "grid_ready": True,
- "base_ratio": 0.52,
- "quote_ratio": 0.48,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {"center_price": 100.0}, "config": {"grid_step_pct": 0.05}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_trend_follower"
- assert decision.target_strategy == "trend-1"
- assert decision.payload["grid_breakout_pressure"]["phase"] == "confirmed"
- def test_make_decision_exits_grid_early_on_fast_bearish_alignment():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "generated_at": "2026-04-18T20:15:00+00:00",
- "stance": "constructive_bearish",
- "confidence": 0.84,
- "opportunity_map": {"continuation": 0.8, "mean_reversion": 0.05, "reversal": 0.05, "wait": 0.1},
- "features_by_timeframe": {
- "1m": {"raw": {"price": 96.0, "atr_percent": 0.22, "rsi": 28.0, "macd_histogram": -0.02, "vwap": 97.2, "ema_fast": 96.8, "ema_slow": 97.6, "sma_long": 98.0, "bands": {"bollinger": {"middle": 97.0, "upper": 98.0, "lower": 95.5}}}},
- },
- "scoped_state": {
- "micro": {"impulse": "down", "trend_bias": "bearish", "location": "near_lower_band", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bearish"},
- "macro": {"bias": "bearish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "balanced",
- "rebalance_needed": False,
- "grid_ready": True,
- "base_ratio": 0.5,
- "quote_ratio": 0.5,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {"center_price": 100.0}, "config": {"grid_step_pct": 0.05}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {"trade_side": "sell"}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- history_window = {
- "window_seconds": 1800,
- "recent_states": [
- {"created_at": "2026-04-18T19:55:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":100.0}}}}'},
- {"created_at": "2026-04-18T20:00:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":99.2}}}}'},
- {"created_at": "2026-04-18T20:05:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":97.8}}}}'},
- {"created_at": "2026-04-18T20:10:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":96.8}}}}'},
- ],
- }
- decision = make_decision(
- concern=concern,
- narrative_payload=narrative,
- wallet_state=wallet_state,
- strategies=strategies,
- history_window=history_window,
- )
- assert decision.mode == "act"
- assert decision.action == "replace_with_trend_follower"
- assert decision.target_strategy == "trend-1"
- assert decision.payload["decision_audit"]["rapid_downside_pressure"] is True
- def test_make_decision_prefers_exposure_protector_when_downside_hits_skewed_wallet():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "generated_at": "2026-04-18T20:15:00+00:00",
- "stance": "constructive_bearish",
- "confidence": 0.84,
- "opportunity_map": {"continuation": 0.8, "mean_reversion": 0.05, "reversal": 0.05, "wait": 0.1},
- "features_by_timeframe": {
- "1m": {"raw": {"price": 96.0, "atr_percent": 0.22, "rsi": 28.0, "macd_histogram": -0.02, "vwap": 97.2, "ema_fast": 96.8, "ema_slow": 97.6, "sma_long": 98.0}},
- },
- "scoped_state": {
- "micro": {"impulse": "down", "trend_bias": "bearish", "location": "near_lower_band", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bearish"},
- "macro": {"bias": "bearish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.8,
- "quote_ratio": 0.2,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {"center_price": 100.0}, "config": {"grid_step_pct": 0.05}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {"trade_side": "sell"}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- history_window = {
- "window_seconds": 1800,
- "recent_states": [
- {"created_at": "2026-04-18T19:55:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":100.0}}}}'},
- {"created_at": "2026-04-18T20:00:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":99.2}}}}'},
- {"created_at": "2026-04-18T20:05:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":97.8}}}}'},
- {"created_at": "2026-04-18T20:10:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":96.8}}}}'},
- ],
- }
- decision = make_decision(
- concern=concern,
- narrative_payload=narrative,
- wallet_state=wallet_state,
- strategies=strategies,
- history_window=history_window,
- )
- assert decision.mode == "act"
- assert decision.action == "replace_with_exposure_protector"
- assert decision.target_strategy == "protect-1"
- def test_make_decision_prefers_exposure_protector_when_upside_hits_skewed_wallet():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "generated_at": "2026-04-18T20:15:00+00:00",
- "stance": "constructive_bullish",
- "confidence": 0.84,
- "opportunity_map": {"continuation": 0.8, "mean_reversion": 0.05, "reversal": 0.05, "wait": 0.1},
- "features_by_timeframe": {
- "1m": {"raw": {"price": 104.0, "atr_percent": 0.22, "rsi": 72.0, "macd_histogram": 0.02, "vwap": 102.8, "ema_fast": 103.2, "ema_slow": 102.4, "sma_long": 102.0}},
- },
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "near_upper_band", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "quote_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.2,
- "quote_ratio": 0.8,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {"center_price": 100.0}, "config": {"grid_step_pct": 0.05}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {"trade_side": "buy"}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- history_window = {
- "window_seconds": 1800,
- "recent_states": [
- {"created_at": "2026-04-18T19:55:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":100.0}}}}'},
- {"created_at": "2026-04-18T20:00:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":100.8}}}}'},
- {"created_at": "2026-04-18T20:05:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":102.2}}}}'},
- {"created_at": "2026-04-18T20:10:00+00:00", "payload_json": '{"features_by_timeframe":{"1m":{"raw":{"price":103.4}}}}'},
- ],
- }
- decision = make_decision(
- concern=concern,
- narrative_payload=narrative,
- wallet_state=wallet_state,
- strategies=strategies,
- history_window=history_window,
- )
- assert decision.mode == "act"
- assert decision.action == "replace_with_exposure_protector"
- assert decision.target_strategy == "protect-1"
- def test_make_decision_targets_the_trade_side_that_matches_direction():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.82,
- "opportunity_map": {"continuation": 0.82, "mean_reversion": 0.05, "reversal": 0.03, "wait": 0.1},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "near_upper_band", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- "features_by_timeframe": {"1m": {"raw": {"price": 116.0}}},
- }
- wallet_state = {"inventory_state": "balanced", "rebalance_needed": False, "grid_ready": True, "base_ratio": 0.52, "quote_ratio": 0.48}
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {"center_price": 100.0}, "config": {"grid_step_pct": 0.05}},
- {"id": "trend-long", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {"trade_side": "buy"}},
- {"id": "trend-short", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {"trade_side": "sell"}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_trend_follower"
- assert decision.target_strategy == "trend-long"
- def test_make_decision_marks_breakout_as_developing_under_partial_alignment():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "cautious_bullish",
- "confidence": 0.76,
- "opportunity_map": {"continuation": 0.62, "mean_reversion": 0.12, "reversal": 0.06, "wait": 0.2},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.74,
- "quote_ratio": 0.26,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": False, "side_capacity": {"buy": True, "sell": False}}}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "keep_grid"
- assert decision.payload["grid_breakout_pressure"]["phase"] == "developing"
- assert decision.reason_summary == "breakout pressure is developing, but grid can still work and should not be abandoned yet"
- def test_make_decision_argus_compression_stays_context_only():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.82,
- "opportunity_map": {"continuation": 0.82, "mean_reversion": 0.05, "reversal": 0.03, "wait": 0.1},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- "argus_context": {
- "regime": "compression",
- "regime_confidence": 0.72,
- "regime_components": {"compression": 0.81},
- },
- "features_by_timeframe": {"1m": {"raw": {"price": 112.0}}},
- }
- wallet_state = {
- "inventory_state": "balanced",
- "rebalance_needed": False,
- "grid_ready": True,
- "base_ratio": 0.52,
- "quote_ratio": 0.48,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {"center_price": 100.0}, "config": {"grid_step_pct": 0.05}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "keep_grid"
- assert decision.payload["grid_breakout_pressure"]["argus_compression_active"] is True
- assert decision.payload["grid_breakout_pressure"]["phase"] == "confirmed"
- assert decision.payload["argus_decision_context"]["compression_active"] is True
- def test_make_decision_promotes_developing_breakout_from_time_window_memory():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "generated_at": "2026-04-18T20:15:00+00:00",
- "stance": "cautious_bullish",
- "confidence": 0.76,
- "opportunity_map": {"continuation": 0.62, "mean_reversion": 0.12, "reversal": 0.06, "wait": 0.2},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.74,
- "quote_ratio": 0.26,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": False, "side_capacity": {"buy": True, "sell": False}}}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- history_window = {
- "window_seconds": 15 * 60,
- "recent_states": [
- {
- "created_at": "2026-04-18T20:06:00+00:00",
- "payload_json": '{"scoped_state":{"micro":{"impulse":"up","trend_bias":"bullish"},"meso":{"structure":"trend_continuation","momentum_bias":"bullish"},"macro":{"bias":"bullish"}},"cross_scope_summary":{"alignment":"partial_alignment","friction":"medium"}}',
- },
- {
- "created_at": "2026-04-18T20:10:30+00:00",
- "payload_json": '{"scoped_state":{"micro":{"impulse":"up","trend_bias":"bullish"},"meso":{"structure":"trend_continuation","momentum_bias":"bullish"},"macro":{"bias":"bullish"}},"cross_scope_summary":{"alignment":"partial_alignment","friction":"medium"}}',
- },
- ],
- }
- decision = make_decision(
- concern=concern,
- narrative_payload=narrative,
- wallet_state=wallet_state,
- strategies=strategies,
- history_window=history_window,
- )
- assert decision.payload["grid_breakout_pressure"]["phase"] == "confirmed"
- assert decision.payload["grid_breakout_pressure"]["time_window_memory"]["promoted_to_confirmed"] is True
- assert decision.payload["grid_breakout_pressure"]["time_window_memory"]["same_direction_seconds"] >= 540
- def test_make_decision_replaces_grid_with_trend_when_breakout_is_persistent_but_inventory_is_only_base_heavy():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.78,
- "opportunity_map": {"continuation": 0.72, "mean_reversion": 0.08, "reversal": 0.05, "wait": 0.15},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.64,
- "quote_ratio": 0.36,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": False, "side_capacity": {"buy": True, "sell": False}}}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_prefers_active_grid_over_observe_trend_as_current_primary():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.72,
- "opportunity_map": {"continuation": 0.75, "mean_reversion": 0.1, "reversal": 0.05, "wait": 0.1},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.81,
- "quote_ratio": 0.19,
- }
- strategies = [
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "observe", "account_id": "a1", "state": {}, "config": {}},
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "keep_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_prefers_trend_over_rebalancer_on_bullish_breakout_with_depleted_base():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.9,
- "opportunity_map": {"continuation": 0.85, "mean_reversion": 0.05, "reversal": 0.02, "wait": 0.08},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "critically_unbalanced",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.0,
- "quote_ratio": 1.0,
- }
- strategies = [
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "replace_with_exposure_protector"
- assert decision.target_strategy == "protect-1"
- def test_make_decision_replaces_grid_when_next_sell_is_close_but_confirmed_trend_handoff_is_ready():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.9,
- "opportunity_map": {"continuation": 0.85, "mean_reversion": 0.05, "reversal": 0.02, "wait": 0.08},
- "features_by_timeframe": {
- "1m": {"raw": {"price": 1.4374, "atr_percent": 0.11}},
- },
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.65,
- "quote_ratio": 0.35,
- }
- strategies = [
- {
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "market_symbol": "xrpusd",
- "state": {
- "last_price": 1.4374,
- "center_price": 1.24,
- "orders": [
- {"side": "sell", "status": "open", "price": "1.43956", "amount": "7"},
- {"side": "buy", "status": "open", "price": "1.42523", "amount": "7"},
- ],
- },
- "config": {"grid_step_pct": 0.05},
- },
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "observe", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.action == "replace_with_trend_follower"
- assert decision.target_strategy == "trend-1"
- assert decision.payload["grid_fill_context"]["near_fill_side"] == "sell"
- def test_make_decision_replaces_grid_when_time_promoted_confirmation_clears_lower_handoff_threshold():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "generated_at": "2026-04-18T20:15:00+00:00",
- "stance": "breakout_watch",
- "confidence": 0.78,
- "opportunity_map": {"continuation": 0.7, "mean_reversion": 0.1, "reversal": 0.04, "wait": 0.16},
- "features_by_timeframe": {
- "1m": {"raw": {"price": 111.0, "atr_percent": 0.35}},
- },
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.66,
- "quote_ratio": 0.34,
- }
- strategies = [
- {
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "market_symbol": "xrpusd",
- "state": {"last_price": 111.0, "center_price": 100.0},
- "config": {"grid_step_pct": 0.05},
- },
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "observe", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- history_window = {
- "window_seconds": 15 * 60,
- "recent_states": [
- {
- "created_at": "2026-04-18T20:06:00+00:00",
- "payload_json": '{"scoped_state":{"micro":{"impulse":"up","trend_bias":"bullish"},"meso":{"structure":"trend_continuation","momentum_bias":"bullish"},"macro":{"bias":"bullish"}},"cross_scope_summary":{"alignment":"partial_alignment","friction":"medium"}}',
- },
- {
- "created_at": "2026-04-18T20:10:30+00:00",
- "payload_json": '{"scoped_state":{"micro":{"impulse":"up","trend_bias":"bullish"},"meso":{"structure":"trend_continuation","momentum_bias":"bullish"},"macro":{"bias":"bullish"}},"cross_scope_summary":{"alignment":"partial_alignment","friction":"medium"}}',
- },
- ],
- }
- decision = make_decision(
- concern=concern,
- narrative_payload=narrative,
- wallet_state=wallet_state,
- strategies=strategies,
- history_window=history_window,
- )
- assert decision.action == "replace_with_trend_follower"
- assert decision.target_strategy == "trend-1"
- assert decision.payload["grid_breakout_pressure"]["time_window_memory"]["promoted_to_confirmed"] is True
- def test_normalize_strategy_snapshot_uses_live_report_contract_and_supervision():
- normalized = normalize_strategy_snapshot({
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "report": {
- "fit": {
- "role": "primary",
- "inventory_behavior": "balanced",
- "safe_when_unbalanced": False,
- "can_run_with": ["exposure_protector"],
- },
- "state": {"last_action": "hold", "open_order_count": 12},
- "supervision": {"inventory_pressure": "base_heavy", "capacity_available": False, "side_capacity": {"buy": True, "sell": False}, "degraded": False},
- },
- })
- assert normalized["contract"]["inventory_behavior"] == "balanced"
- assert normalized["supervision"]["capacity_available"] is False
- assert normalized["open_order_count"] == 12
- def test_make_decision_keeps_trend_during_directional_regime_even_if_wallet_is_skewed():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.7,
- "opportunity_map": {"continuation": 0.8, "mean_reversion": 0.1, "reversal": 0.05, "wait": 0.05},
- }
- wallet_state = {
- "inventory_state": "critically_unbalanced",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.88,
- "quote_ratio": 0.12,
- }
- strategies = [
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "observe"
- assert decision.action == "keep_trend"
- assert decision.target_strategy == "trend-1"
- def test_make_decision_replaces_trend_with_rebalancer_after_trend_cools_and_wallet_needs_repair():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "neutral_rotational",
- "confidence": 0.65,
- "opportunity_map": {"continuation": 0.15, "mean_reversion": 0.25, "reversal": 0.2, "wait": 0.4},
- }
- wallet_state = {
- "inventory_state": "critically_unbalanced",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.88,
- "quote_ratio": 0.12,
- }
- strategies = [
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_exposure_protector"
- assert decision.target_strategy == "protect-1"
- def test_make_decision_replaces_trend_with_rebalancer_on_edge_cooling_even_before_full_rotational_stance():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.74,
- "opportunity_map": {"continuation": 0.58, "mean_reversion": 0.12, "reversal": 0.08, "wait": 0.22},
- "scoped_state": {
- "micro": {"impulse": "mixed", "trend_bias": "mixed", "location": "near_upper_band"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.74,
- "quote_ratio": 0.26,
- }
- strategies = [
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_exposure_protector"
- assert decision.target_strategy == "protect-1"
- def test_make_decision_replaces_trend_with_rebalancer_when_micro_reversal_risk_spikes():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.76,
- "opportunity_map": {"continuation": 0.62, "mean_reversion": 0.1, "reversal": 0.18, "wait": 0.1},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "near_upper_band", "reversal_risk": "high"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.72,
- "quote_ratio": 0.28,
- }
- strategies = [
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_exposure_protector"
- assert decision.target_strategy == "protect-1"
- def test_make_decision_replaces_rebalancer_with_grid_when_balanced_and_rotational():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "neutral_rotational",
- "confidence": 0.68,
- "opportunity_map": {"continuation": 0.15, "mean_reversion": 0.72, "reversal": 0.05, "wait": 0.08},
- }
- wallet_state = {
- "inventory_state": "balanced",
- "rebalance_needed": False,
- "grid_ready": True,
- "base_ratio": 0.49,
- "quote_ratio": 0.51,
- }
- strategies = [
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_replaces_rebalancer_with_grid_when_within_tolerance_even_before_perfect_balance():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "neutral_rotational",
- "confidence": 0.7,
- "opportunity_map": {"continuation": 0.18, "mean_reversion": 0.68, "reversal": 0.05, "wait": 0.09},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": True,
- "base_ratio": 0.71,
- "quote_ratio": 0.29,
- }
- strategies = [
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_replaces_rebalancer_with_grid_when_trend_is_directional_but_not_sustained():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.72,
- "opportunity_map": {"continuation": 0.4, "mean_reversion": 0.4, "reversal": 0.08, "wait": 0.12},
- "scoped_state": {
- "micro": {"impulse": "mixed", "trend_bias": "mixed", "location": "centered", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- }
- wallet_state = {
- "inventory_state": "balanced",
- "rebalance_needed": False,
- "grid_ready": True,
- "base_ratio": 0.51,
- "quote_ratio": 0.49,
- }
- strategies = [
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "off", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": True, "side_capacity": {"buy": True, "sell": True}, "inventory_pressure": "balanced", "degraded": False}}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": True, "trend_strength": 0.58, "inventory_pressure": "balanced", "degraded": False}}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_grid"
- assert decision.target_strategy == "grid-1"
- def test_make_decision_replaces_rebalancer_with_grid_when_micro_easing_restores_harvestability():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bearish",
- "confidence": 0.73,
- "opportunity_map": {"continuation": 0.42, "mean_reversion": 0.38, "reversal": 0.08, "wait": 0.12},
- "scoped_state": {
- "micro": {"impulse": "mixed", "trend_bias": "mixed", "location": "centered", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bearish"},
- "macro": {"bias": "bearish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- "features_by_timeframe": {
- "1m": {
- "raw": {"price": 1.01, "atr_percent": 0.42},
- "volatility": {"bollinger_width_pct": 1.35},
- },
- },
- }
- wallet_state = {
- "inventory_state": "quote_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.29,
- "quote_ratio": 0.71,
- }
- strategies = [
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "off", "account_id": "a1", "state": {}, "config": {"grid_step_pct": 0.005}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_grid"
- assert decision.target_strategy == "grid-1"
- assert decision.payload["decision_audit"]["tactical_easing"] is True
- assert decision.payload["decision_audit"]["grid_harvestable_now"] is True
- assert decision.payload["decision_audit"]["rebalancer_release_ready"] is True
- def test_make_decision_replaces_rebalancer_with_grid_near_local_bottom_when_noise_exceeds_grid_size():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "cautious_bearish",
- "confidence": 0.69,
- "opportunity_map": {"continuation": 0.34, "mean_reversion": 0.44, "reversal": 0.1, "wait": 0.12},
- "scoped_state": {
- "micro": {"impulse": "mixed", "trend_bias": "mixed", "location": "near_lower_band", "reversal_risk": "medium"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bearish"},
- "macro": {"bias": "bearish"},
- },
- "cross_scope_summary": {"alignment": "partial_alignment", "friction": "medium"},
- "features_by_timeframe": {
- "1m": {
- "raw": {"price": 0.992, "atr_percent": 0.48},
- "volatility": {"bollinger_width_pct": 1.6},
- },
- },
- }
- wallet_state = {
- "inventory_state": "quote_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.31,
- "quote_ratio": 0.69,
- }
- strategies = [
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "off", "account_id": "a1", "state": {}, "config": {"grid_step_pct": 0.005}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "act"
- assert decision.action == "replace_with_grid"
- assert decision.target_strategy == "grid-1"
- assert decision.payload["decision_audit"]["pullback_to_grid_ratio"] is not None
- assert decision.payload["decision_audit"]["pullback_to_grid_ratio"] > 2.0
- def test_make_decision_replaces_grid_with_trend_when_pullbacks_are_too_shallow_for_grid_step():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "generated_at": "2026-04-19T19:05:00+00:00",
- "stance": "constructive_bullish",
- "confidence": 0.88,
- "opportunity_map": {"continuation": 0.84, "mean_reversion": 0.05, "reversal": 0.03, "wait": 0.08},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "near_upper_band", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- "features_by_timeframe": {
- "1m": {
- "raw": {"price": 104.2, "atr_percent": 0.11},
- "volatility": {"bollinger_width_pct": 0.24},
- },
- },
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.67,
- "quote_ratio": 0.33,
- }
- strategies = [
- {
- "id": "grid-1",
- "strategy_type": "grid_trader",
- "mode": "active",
- "account_id": "a1",
- "market_symbol": "xrpusd",
- "state": {
- "last_price": 104.2,
- "center_price": 100.0,
- "orders": [
- {"side": "sell", "status": "open", "price": "104.7", "amount": "5"},
- {"side": "buy", "status": "open", "price": "103.7", "amount": "5"},
- ],
- },
- "config": {"grid_step_pct": 0.005},
- },
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "observe", "account_id": "a1", "state": {}, "config": {}},
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "off", "account_id": "a1", "state": {}, "config": {}},
- ]
- history_window = {
- "window_seconds": 15 * 60,
- "recent_states": [
- {
- "created_at": "2026-04-19T18:55:00+00:00",
- "payload_json": '{"scoped_state":{"micro":{"impulse":"up","trend_bias":"bullish"},"meso":{"structure":"trend_continuation","momentum_bias":"bullish"},"macro":{"bias":"bullish"}},"cross_scope_summary":{"alignment":"micro_meso_macro_aligned","friction":"low"}}',
- },
- {
- "created_at": "2026-04-19T19:00:30+00:00",
- "payload_json": '{"scoped_state":{"micro":{"impulse":"up","trend_bias":"bullish"},"meso":{"structure":"trend_continuation","momentum_bias":"bullish"},"macro":{"bias":"bullish"}},"cross_scope_summary":{"alignment":"micro_meso_macro_aligned","friction":"low"}}',
- },
- ],
- }
- decision = make_decision(
- concern=concern,
- narrative_payload=narrative,
- wallet_state=wallet_state,
- strategies=strategies,
- history_window=history_window,
- )
- assert decision.mode == "act"
- assert decision.action == "replace_with_trend_follower"
- assert decision.target_strategy == "trend-1"
- assert decision.payload["decision_audit"]["pullback_to_grid_ratio"] < 1.0
- assert decision.payload["decision_audit"]["trend_following_pressure"] is True
- def test_make_decision_replaces_rebalancer_with_trend_when_breakout_is_still_strong():
- concern = {"id": "c1", "account_id": "a1", "market_symbol": "xrpusd", "base_currency": "XRP", "quote_currency": "USD"}
- narrative = {
- "stance": "constructive_bullish",
- "confidence": 0.84,
- "opportunity_map": {"continuation": 0.82, "mean_reversion": 0.08, "reversal": 0.03, "wait": 0.07},
- "scoped_state": {
- "micro": {"impulse": "up", "trend_bias": "bullish", "location": "near_upper_band", "reversal_risk": "low"},
- "meso": {"structure": "trend_continuation", "momentum_bias": "bullish"},
- "macro": {"bias": "bullish"},
- },
- "cross_scope_summary": {"alignment": "micro_meso_macro_aligned", "friction": "low"},
- }
- wallet_state = {
- "inventory_state": "base_heavy",
- "rebalance_needed": True,
- "grid_ready": False,
- "base_ratio": 0.74,
- "quote_ratio": 0.26,
- }
- strategies = [
- {"id": "protect-1", "strategy_type": "exposure_protector", "mode": "active", "account_id": "a1", "state": {}, "config": {}},
- {"id": "grid-1", "strategy_type": "grid_trader", "mode": "off", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": True, "side_capacity": {"buy": True, "sell": True}, "inventory_pressure": "balanced", "degraded": False}}},
- {"id": "trend-1", "strategy_type": "trend_follower", "mode": "off", "account_id": "a1", "state": {}, "config": {}, "report": {"supervision": {"capacity_available": True, "trend_strength": 0.92, "inventory_pressure": "balanced", "degraded": False}}},
- ]
- decision = make_decision(concern=concern, narrative_payload=narrative, wallet_state=wallet_state, strategies=strategies)
- assert decision.mode == "observe"
- assert decision.action == "keep_rebalancer"
- assert decision.target_strategy == "protect-1"
|