gaurv007 commited on
Commit
96f136c
·
verified ·
1 Parent(s): 391f000

Upload alpha_factory/deterministic/proven_templates.py

Browse files
alpha_factory/deterministic/proven_templates.py CHANGED
@@ -34,7 +34,7 @@ def _get_bf(field: BrainField) -> str:
34
  return field.id
35
 
36
 
37
- def generate_alpha15_variant(field: BrainField, group_key: str = None) -> str:
38
  """
39
  Alpha 15 archetype: intraday position proxy + value/momentum leg.
40
 
@@ -57,12 +57,12 @@ def generate_alpha15_variant(field: BrainField, group_key: str = None) -> str:
57
  f"rank("
58
  f"zscore(ts_rank((high + low) / 2 - close, 252)) "
59
  f"+ {sign}zscore(ts_rank({field_ref}, 252))"
60
- f"), {group_key}), 5)"
61
  )
62
  return expr
63
 
64
 
65
- def generate_alpha6_variant(field: BrainField, group_key: str = None) -> str:
66
  """
67
  Alpha 6 archetype: microstructure score + value leg.
68
 
@@ -84,12 +84,12 @@ def generate_alpha6_variant(field: BrainField, group_key: str = None) -> str:
84
  f"rank("
85
  f"rank((vwap - close) / close) * rank(volume / ts_mean(volume, 20)) "
86
  f"+ {sign}zscore(ts_rank({field_ref}, 252))"
87
- f"), {group_key}), 5)"
88
  )
89
  return expr
90
 
91
 
92
- def generate_pure_field_rank(field: BrainField, horizon: int = 252, group_key: str = None) -> str:
93
  """
94
  Simplest possible expression — just rank the field cross-sectionally.
95
  Often works for high-quality pre-computed model fields (model77).
@@ -104,12 +104,12 @@ def generate_pure_field_rank(field: BrainField, horizon: int = 252, group_key: s
104
  f"ts_decay_linear("
105
  f"group_neutralize("
106
  f"{sign}rank(ts_rank({field_ref}, {horizon}))"
107
- f", {group_key}), 5)"
108
  )
109
  return expr
110
 
111
 
112
- def generate_delta_momentum(field: BrainField, horizon: int = 21, group_key: str = None) -> str:
113
  """
114
  Delta/change momentum — buy stocks where the field is improving.
115
  Works well for score-type fields (model16 derivatives, earnings revisions).
@@ -124,12 +124,12 @@ def generate_delta_momentum(field: BrainField, horizon: int = 21, group_key: str
124
  f"ts_decay_linear("
125
  f"group_neutralize("
126
  f"{sign}zscore(ts_delta({field_ref}, {horizon}))"
127
- f", {group_key}), 5)"
128
  )
129
  return expr
130
 
131
 
132
- def generate_mean_reversion(field: BrainField, horizon: int = 20, group_key: str = None) -> str:
133
  """
134
  Mean reversion on a field — short stocks where field is high, long where low.
135
  Effective for contrarian indicators (option PCR, social buzz).
@@ -151,16 +151,20 @@ def generate_mean_reversion(field: BrainField, horizon: int = 20, group_key: str
151
  f"ts_decay_linear("
152
  f"group_neutralize("
153
  f"{sign}zscore(ts_rank({field_ref}, {horizon}))"
154
- f", {group_key}), 5)"
155
  )
156
  return expr
157
 
158
 
159
- def generate_batch_from_proven_templates(count: int = 5) -> list[dict]:
160
  """
161
  Generate a batch of alphas using PROVEN templates with novel fields.
162
  This is the PRIMARY generation method — guaranteed valid structure.
163
 
 
 
 
 
164
  Returns list of dicts with expression, field, template, group_key.
165
  """
166
  # Priority: goldmine (AC=0) first, then AC=1, then AC≤5
@@ -191,7 +195,7 @@ def generate_batch_from_proven_templates(count: int = 5) -> list[dict]:
191
  template_name, template_fn = templates[i % len(templates)]
192
  group_key = get_group_for_expression(prefer_novel=True)
193
 
194
- expr = template_fn(field, group_key)
195
 
196
  results.append({
197
  "expression": expr,
@@ -202,7 +206,7 @@ def generate_batch_from_proven_templates(count: int = 5) -> list[dict]:
202
  "theme": "proven_template",
203
  "archetype": template_name,
204
  "sign": field.sign.value,
205
- "decay": 5,
206
  "neutralization": group_key,
207
  })
208
 
 
34
  return field.id
35
 
36
 
37
+ def generate_alpha15_variant(field: BrainField, group_key: str = None, decay: int = 5) -> str:
38
  """
39
  Alpha 15 archetype: intraday position proxy + value/momentum leg.
40
 
 
57
  f"rank("
58
  f"zscore(ts_rank((high + low) / 2 - close, 252)) "
59
  f"+ {sign}zscore(ts_rank({field_ref}, 252))"
60
+ f"), {group_key}), {decay})"
61
  )
62
  return expr
63
 
64
 
65
+ def generate_alpha6_variant(field: BrainField, group_key: str = None, decay: int = 5) -> str:
66
  """
67
  Alpha 6 archetype: microstructure score + value leg.
68
 
 
84
  f"rank("
85
  f"rank((vwap - close) / close) * rank(volume / ts_mean(volume, 20)) "
86
  f"+ {sign}zscore(ts_rank({field_ref}, 252))"
87
+ f"), {group_key}), {decay})"
88
  )
89
  return expr
90
 
91
 
92
+ def generate_pure_field_rank(field: BrainField, horizon: int = 252, group_key: str = None, decay: int = 5) -> str:
93
  """
94
  Simplest possible expression — just rank the field cross-sectionally.
95
  Often works for high-quality pre-computed model fields (model77).
 
104
  f"ts_decay_linear("
105
  f"group_neutralize("
106
  f"{sign}rank(ts_rank({field_ref}, {horizon}))"
107
+ f", {group_key}), {decay})"
108
  )
109
  return expr
110
 
111
 
112
+ def generate_delta_momentum(field: BrainField, horizon: int = 21, group_key: str = None, decay: int = 5) -> str:
113
  """
114
  Delta/change momentum — buy stocks where the field is improving.
115
  Works well for score-type fields (model16 derivatives, earnings revisions).
 
124
  f"ts_decay_linear("
125
  f"group_neutralize("
126
  f"{sign}zscore(ts_delta({field_ref}, {horizon}))"
127
+ f", {group_key}), {decay})"
128
  )
129
  return expr
130
 
131
 
132
+ def generate_mean_reversion(field: BrainField, horizon: int = 20, group_key: str = None, decay: int = 5) -> str:
133
  """
134
  Mean reversion on a field — short stocks where field is high, long where low.
135
  Effective for contrarian indicators (option PCR, social buzz).
 
151
  f"ts_decay_linear("
152
  f"group_neutralize("
153
  f"{sign}zscore(ts_rank({field_ref}, {horizon}))"
154
+ f", {group_key}), {decay})"
155
  )
156
  return expr
157
 
158
 
159
+ def generate_batch_from_proven_templates(count: int = 5, decay: int = 5) -> list[dict]:
160
  """
161
  Generate a batch of alphas using PROVEN templates with novel fields.
162
  This is the PRIMARY generation method — guaranteed valid structure.
163
 
164
+ Args:
165
+ count: Number of alphas to generate.
166
+ decay: Decay parameter for ts_decay_linear (default 5).
167
+
168
  Returns list of dicts with expression, field, template, group_key.
169
  """
170
  # Priority: goldmine (AC=0) first, then AC=1, then AC≤5
 
195
  template_name, template_fn = templates[i % len(templates)]
196
  group_key = get_group_for_expression(prefer_novel=True)
197
 
198
+ expr = template_fn(field, group_key, decay)
199
 
200
  results.append({
201
  "expression": expr,
 
206
  "theme": "proven_template",
207
  "archetype": template_name,
208
  "sign": field.sign.value,
209
+ "decay": decay,
210
  "neutralization": group_key,
211
  })
212