"""按可得日选盈利事实,负利润退出候选,重建两次调仓现金。""" import json from math import isclose facts = [ {'symbol': 'A', 'period': '2025-12-31', 'available': '2026-01-20', 'income': 5}, {'symbol': 'B', 'period': '2025-12-31', 'available': '2026-01-22', 'income': 8}, {'symbol': 'B', 'period': '2025-12-31', 'available': '2026-02-10', 'income': 2}, {'symbol': 'C', 'period': '2025-12-31', 'available': '2026-01-25', 'income': -4}, {'symbol': 'A', 'period': '2026-03-31', 'available': '2026-04-20', 'income': 100}, ] def select(cutoff): scores, lineage = {}, {} for symbol in 'ABC': known = [f for f in facts if f['symbol'] == symbol and f['available'] <= cutoff] if not known: continue fact = max(known, key=lambda f: (f['period'], f['available'])) if fact['income'] > 0: scores[symbol] = fact['income'] / 100 lineage[symbol] = fact['available'] chosen = sorted(scores, key=lambda s: (-scores[s], s))[:1] return {'chosen': chosen, 'scores': scores, 'available': lineage} jan, feb = select('2026-01-30'), select('2026-02-27') assert jan['chosen'] == ['B'] and feb['chosen'] == ['A'] assert jan['scores']['B'] == .08 and feb['scores']['B'] == .02 assert 'C' not in feb['scores'] assert select('2026-01-19')['chosen'] == [] cash = 1000 - 90 * 10 - 1 cash += 90 * 11 - 1 cash -= 100 * 10 + 1 equity = cash + 100 * 10.5 assert isclose(cash, 87) and isclose(equity, 1137) print(json.dumps({'jan': jan, 'feb': feb, 'cash': cash, 'marked_equity': equity, 'fees': 3, 'future_A_income_excluded': True}))