"""行业时点标签、全市场/行业内固定名额及两套独立费用账。""" import json rows = [('A', 9, 'Tech'), ('B', 8, 'Tech'), ('C', 7, 'Tech'), ('D', 3, 'Utility'), ('E', 2, 'Utility'), ('F', 1, 'Utility')] labels = [('D', '2026-01-01', 'Utility'), ('D', '2026-02-01', 'Tech')] def label_at(symbol, cutoff): return max((r for r in labels if r[0] == symbol and r[1] <= cutoff), key=lambda r: r[1])[2] def choose(data, within): if not within: return [s for s, _, _ in sorted(data, key=lambda r: (-r[1], r[0]))[:2]] chosen = [] for industry in ['Tech', 'Utility']: members = [r for r in data if r[2] == industry] if len(members) >= 2: chosen.append(sorted(members, key=lambda r: (-r[1], r[0]))[0][0]) return chosen def account(target): holdings, cash, fees, orders = {'A', 'E'}, 200, 0, [] for side, names in [('sell', holdings - set(target)), ('buy', set(target) - holdings)]: for symbol in sorted(names): cash += (400 - 1) if side == 'sell' else -(400 + 1) fees += 1 orders.append([symbol, side, 40, 10, 1]) marks = {'A': 10.2, 'B': 10.1, 'D': 10.5} return {'orders': orders, 'fees': fees, 'cash': cash, 'equity': cash + sum(40 * marks[s] for s in target)} global_names, within_names = choose(rows, False), choose(rows, True) assert global_names == ['A', 'B'] and within_names == ['A', 'D'] assert label_at('D', '2026-01-30') == 'Utility' assert label_at('D', '2026-02-27') == 'Tech' small = choose(rows[:4], True) assert small == ['A'] assert account(global_names)['equity'] == 1010 assert account(within_names)['equity'] == 1026 assert account(small)['equity'] == 1007 print(json.dumps({'global': global_names, 'within': within_names, 'global_account': account(global_names), 'within_account': account(within_names), 'small_industry': {'selected': small, 'account': account(small)}}))