|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import unittest |
| 4 | + |
| 5 | +from quant_platform_kit.common.execution_capabilities import ( |
| 6 | + FRACTIONAL_SHARE_EXECUTION_CAPABILITY, |
| 7 | + FRACTIONAL_SHARE_EXECUTION_SKIP_REASON, |
| 8 | + fractional_share_execution_unsupported_reason, |
| 9 | +) |
| 10 | +from quant_platform_kit.common.strategies import ( |
| 11 | + PlatformCapabilityMatrix, |
| 12 | + StrategyCatalog, |
| 13 | + StrategyDefinition, |
| 14 | + US_EQUITY_DOMAIN, |
| 15 | + derive_eligible_profiles_for_platform, |
| 16 | +) |
| 17 | +from quant_platform_kit.common.strategy_contracts import StrategyRuntimeAdapter |
| 18 | + |
| 19 | + |
| 20 | +class ExecutionCapabilitiesTests(unittest.TestCase): |
| 21 | + def test_fractional_share_execution_unsupported_reason(self) -> None: |
| 22 | + catalog = StrategyCatalog( |
| 23 | + definitions={ |
| 24 | + "ibit_smart_dca": StrategyDefinition( |
| 25 | + profile="ibit_smart_dca", |
| 26 | + domain=US_EQUITY_DOMAIN, |
| 27 | + supported_platforms=frozenset({"schwab"}), |
| 28 | + required_inputs=frozenset({"portfolio_snapshot"}), |
| 29 | + target_mode="value", |
| 30 | + compatible_capabilities=frozenset({FRACTIONAL_SHARE_EXECUTION_CAPABILITY}), |
| 31 | + ), |
| 32 | + "tqqq_growth_income": StrategyDefinition( |
| 33 | + profile="tqqq_growth_income", |
| 34 | + domain=US_EQUITY_DOMAIN, |
| 35 | + supported_platforms=frozenset({"schwab"}), |
| 36 | + required_inputs=frozenset({"portfolio_snapshot"}), |
| 37 | + target_mode="value", |
| 38 | + ), |
| 39 | + } |
| 40 | + ) |
| 41 | + whole_share_matrix = PlatformCapabilityMatrix( |
| 42 | + platform_id="schwab", |
| 43 | + supported_domains=frozenset({US_EQUITY_DOMAIN}), |
| 44 | + supported_target_modes=frozenset({"value"}), |
| 45 | + supported_inputs=frozenset({"portfolio_snapshot"}), |
| 46 | + supported_capabilities=frozenset(), |
| 47 | + ) |
| 48 | + fractional_matrix = PlatformCapabilityMatrix( |
| 49 | + platform_id="schwab", |
| 50 | + supported_domains=frozenset({US_EQUITY_DOMAIN}), |
| 51 | + supported_target_modes=frozenset({"value"}), |
| 52 | + supported_inputs=frozenset({"portfolio_snapshot"}), |
| 53 | + supported_capabilities=frozenset({FRACTIONAL_SHARE_EXECUTION_CAPABILITY}), |
| 54 | + ) |
| 55 | + |
| 56 | + self.assertEqual( |
| 57 | + fractional_share_execution_unsupported_reason( |
| 58 | + "ibit_smart_dca", |
| 59 | + strategy_catalog=catalog, |
| 60 | + capability_matrix=whole_share_matrix, |
| 61 | + ), |
| 62 | + FRACTIONAL_SHARE_EXECUTION_SKIP_REASON, |
| 63 | + ) |
| 64 | + self.assertIsNone( |
| 65 | + fractional_share_execution_unsupported_reason( |
| 66 | + "ibit_smart_dca", |
| 67 | + strategy_catalog=catalog, |
| 68 | + capability_matrix=fractional_matrix, |
| 69 | + ) |
| 70 | + ) |
| 71 | + self.assertIsNone( |
| 72 | + fractional_share_execution_unsupported_reason( |
| 73 | + "tqqq_growth_income", |
| 74 | + strategy_catalog=catalog, |
| 75 | + capability_matrix=whole_share_matrix, |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + def test_capability_matrix_excludes_fractional_dca_profiles(self) -> None: |
| 80 | + catalog = StrategyCatalog( |
| 81 | + definitions={ |
| 82 | + "ibit_smart_dca": StrategyDefinition( |
| 83 | + profile="ibit_smart_dca", |
| 84 | + domain=US_EQUITY_DOMAIN, |
| 85 | + supported_platforms=frozenset({"schwab"}), |
| 86 | + required_inputs=frozenset({"portfolio_snapshot"}), |
| 87 | + target_mode="value", |
| 88 | + compatible_capabilities=frozenset({FRACTIONAL_SHARE_EXECUTION_CAPABILITY}), |
| 89 | + ), |
| 90 | + } |
| 91 | + ) |
| 92 | + matrix = PlatformCapabilityMatrix( |
| 93 | + platform_id="schwab", |
| 94 | + supported_domains=frozenset({US_EQUITY_DOMAIN}), |
| 95 | + supported_target_modes=frozenset({"value"}), |
| 96 | + supported_inputs=frozenset({"portfolio_snapshot"}), |
| 97 | + supported_capabilities=frozenset(), |
| 98 | + ) |
| 99 | + adapters = { |
| 100 | + "ibit_smart_dca": StrategyRuntimeAdapter( |
| 101 | + available_inputs=frozenset({"portfolio_snapshot"}), |
| 102 | + available_capabilities=frozenset({FRACTIONAL_SHARE_EXECUTION_CAPABILITY}), |
| 103 | + ), |
| 104 | + } |
| 105 | + |
| 106 | + eligible = derive_eligible_profiles_for_platform( |
| 107 | + catalog, |
| 108 | + capability_matrix=matrix, |
| 109 | + runtime_adapter_loader=lambda profile: adapters[profile], |
| 110 | + ) |
| 111 | + |
| 112 | + self.assertEqual(eligible, frozenset()) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + unittest.main() |
0 commit comments