|
| 1 | +from langgraph.graph import END, START, StateGraph |
| 2 | + |
| 3 | +from brain.agents import ( |
| 4 | + invoke_executor, |
| 5 | + invoke_fine_tuner, |
| 6 | + invoke_planner, |
| 7 | + invoke_seeder, |
| 8 | + invoke_tester, |
| 9 | +) |
| 10 | +from brain.alpha_storage import Storage |
| 11 | +from brain.api import BrainAPI |
| 12 | +from brain.graph_state import GraphState |
| 13 | +from brain.score import get_score |
| 14 | + |
| 15 | +MAX_EXPLORATION_COUNT = 3 |
| 16 | + |
| 17 | + |
| 18 | +def plateau_condition(state: GraphState) -> bool: |
| 19 | + """Check if the exploration has plateaued.""" |
| 20 | + storage = state["storage"] |
| 21 | + best_alpha = storage.best_alpha |
| 22 | + old_best_alpha = state.get("old_best_alpha") |
| 23 | + |
| 24 | + if best_alpha is None or old_best_alpha is None: |
| 25 | + return False |
| 26 | + |
| 27 | + # Check if the score has not improved significantly |
| 28 | + score_diff = ( |
| 29 | + best_alpha.fitness - old_best_alpha.fitness + best_alpha.sharpe - old_best_alpha.sharpe |
| 30 | + ) |
| 31 | + return score_diff < 0.01 |
| 32 | + |
| 33 | + |
| 34 | +def seed_finder_node(state: GraphState) -> GraphState: |
| 35 | + """Find a seed alpha to start the exploration.""" |
| 36 | + # Iterate databse till we find some decent alpha, or some other seed idea |
| 37 | + alpha_idea, config = invoke_seeder(state) |
| 38 | + print(f"Seed alpha: {alpha_idea}") |
| 39 | + return { |
| 40 | + "alpha_idea": alpha_idea, |
| 41 | + "default_config": config, |
| 42 | + "node": "plan", |
| 43 | + "state": "explore", |
| 44 | + "explore_count": 0, |
| 45 | + "static_finetune": True, |
| 46 | + "storage": Storage(score_func=get_score, max_size=50), |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +def planner_node(state: GraphState) -> GraphState: |
| 51 | + """Plan the next steps based on the current alpha and state.""" |
| 52 | + state["explore_count"] += 1 |
| 53 | + state["old_best_alpha"] = state["storage"].best_alpha |
| 54 | + |
| 55 | + plan = invoke_planner(state) |
| 56 | + if not plan: |
| 57 | + return {**state, "node": "seed"} |
| 58 | + |
| 59 | + if state["storage"].best_alpha is None: |
| 60 | + plan.insert(0, "Execute initial alpha idea to obtain baseline") |
| 61 | + |
| 62 | + plan.append( |
| 63 | + "Think about previous changes and how they affected the alpha." |
| 64 | + " Propose new alphas based on the most successful changes." |
| 65 | + ) |
| 66 | + |
| 67 | + return {**state, "node": "execute", "plan": plan} |
| 68 | + |
| 69 | + |
| 70 | +def executor_node(state: GraphState) -> GraphState: |
| 71 | + state = invoke_executor(state) |
| 72 | + # TODO: Pass some summary of results from the executor to planner |
| 73 | + return {**state, "node": "explore_test", "state": "explore"} |
| 74 | + |
| 75 | + |
| 76 | +def fine_tuner_node(state: GraphState) -> GraphState: |
| 77 | + invoke_fine_tuner(state) |
| 78 | + return {**state, "node": "submit_test", "state": "fine_tune", "static_finetune": False} |
| 79 | + |
| 80 | + |
| 81 | +def explore_test_node(state: GraphState) -> GraphState: |
| 82 | + """Decide what happens next after exploring a new alpha idea.""" |
| 83 | + best_alpha = state["storage"].best_alpha |
| 84 | + old_best_alpha = state.get("old_best_alpha") |
| 85 | + |
| 86 | + if ( |
| 87 | + best_alpha is not None |
| 88 | + and (old_best_alpha is None or old_best_alpha.alpha_id != best_alpha.alpha_id) |
| 89 | + and len(best_alpha.failing_tests) == 0 |
| 90 | + ): |
| 91 | + score = best_alpha.update_score() |
| 92 | + trade_count = best_alpha.long_count + best_alpha.short_count |
| 93 | + print("Best alpha score:", score, "Trade count:", trade_count) |
| 94 | + if score > -50 and trade_count > 400 and invoke_tester(state): |
| 95 | + return { |
| 96 | + **state, |
| 97 | + "node": "fine_tuner", |
| 98 | + "state": "fine_tune", |
| 99 | + "static_finetune": True, |
| 100 | + "explore_count": 0, |
| 101 | + } |
| 102 | + |
| 103 | + # TODO: Test plateau condition, compare previous best, with current best alpha |
| 104 | + if state["explore_count"] < MAX_EXPLORATION_COUNT and plateau_condition(state): |
| 105 | + return {**state, "node": "plan", "state": "explore"} |
| 106 | + |
| 107 | + return {**state, "node": "seed", "state": "explore"} |
| 108 | + |
| 109 | + |
| 110 | +def submit_test_node(state: GraphState) -> GraphState: |
| 111 | + """Decide what happens next after fine-tuning a new alpha idea.""" |
| 112 | + best_alpha = state["storage"].best_alpha |
| 113 | + old_best_alpha = state.get("old_best_alpha") |
| 114 | + |
| 115 | + if ( |
| 116 | + best_alpha is not None |
| 117 | + and (old_best_alpha is None or old_best_alpha.alpha_id != best_alpha.alpha_id) |
| 118 | + and len(best_alpha.failing_tests) == 0 |
| 119 | + ): |
| 120 | + score = best_alpha.update_score() |
| 121 | + trade_count = best_alpha.long_count + best_alpha.short_count |
| 122 | + print("Best alpha score:", score, "Trade count:", trade_count) |
| 123 | + if score > 200 and trade_count > 400 and invoke_tester(state): |
| 124 | + # TODO: Mark alpha as "submitted" or "ready for production" |
| 125 | + print("Submitting alpha! Score:", score) |
| 126 | + BrainAPI.submit_alpha(best_alpha.alpha_id) |
| 127 | + return {**state, "node": "seed", "state": "explore", "explore_count": 0} |
| 128 | + |
| 129 | + # TODO: Test plateau condition, compare previous best, with current best alpha |
| 130 | + if state["explore_count"] < MAX_EXPLORATION_COUNT and plateau_condition(state): |
| 131 | + return {**state, "node": "plan", "state": "fine_tune"} |
| 132 | + |
| 133 | + return {**state, "node": "seed", "state": "explore"} |
| 134 | + |
| 135 | + |
| 136 | +builder = StateGraph(GraphState) |
| 137 | + |
| 138 | +builder.add_node("seed_finder", seed_finder_node) |
| 139 | +builder.add_node("planner", planner_node) |
| 140 | +builder.add_node("executor", executor_node) |
| 141 | +builder.add_node("fine_tuner", fine_tuner_node) |
| 142 | +builder.add_node("explore_test", explore_test_node) |
| 143 | +builder.add_node("submit_test", submit_test_node) |
| 144 | +# builder.add_node("stagnation_chk", stagnation_node) |
| 145 | + |
| 146 | + |
| 147 | +# Static flow |
| 148 | +builder.add_edge(START, "seed_finder") |
| 149 | +builder.add_edge("executor", "explore_test") |
| 150 | +builder.add_edge("fine_tuner", "submit_test") |
| 151 | + |
| 152 | +# Conditional branching (no more …then= kwarg in 0.4.8) |
| 153 | +for node in ["seed_finder", "planner", "explore_test", "submit_test"]: |
| 154 | + builder.add_conditional_edges( |
| 155 | + node, |
| 156 | + lambda state: state["node"], |
| 157 | + path_map={ |
| 158 | + "plan": "planner", |
| 159 | + "execute": "executor", |
| 160 | + "seed": "seed_finder", |
| 161 | + "fine_tuner": "fine_tuner", |
| 162 | + "explore_test": "explore_test", |
| 163 | + "submit_test": "submit_test", |
| 164 | + "stop": END, |
| 165 | + }, |
| 166 | + ) |
| 167 | + |
| 168 | +graph = builder.compile() # returns a CompiledStateGraph |
0 commit comments