-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAST_to_Code.py
More file actions
248 lines (201 loc) · 8.4 KB
/
Copy pathAST_to_Code.py
File metadata and controls
248 lines (201 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import re
def translate_indicator(ind_str):
"""
Translates DSL indicator strings to Python Pandas/TA-Lib code.
Assumes 'df' is a Pandas DataFrame with standard OHLCV columns (lowercase).
"""
if isinstance(ind_str, (int, float)):
return str(ind_str)
ind_str = ind_str.strip()
# Basic columns
if ind_str in ['close', 'open', 'high', 'low', 'volume']:
return f"df['{ind_str}'].iloc[-1]"
# Simple Moving Average: sma(period)
match = re.match(r'sma\((\d+)\)', ind_str, re.IGNORECASE)
if match:
period = match.group(1)
return f"df['close'].rolling({period}).mean().iloc[-1]"
# Exponential Moving Average: ema(period)
match = re.match(r'ema\((\d+)\)', ind_str, re.IGNORECASE)
if match:
period = match.group(1)
return f"df['close'].ewm(span={period}, adjust=False).mean().iloc[-1]"
# RSI: rsi(period)
# Using a simple custom calculation or assuming pandas_ta
match = re.match(r'rsi\((\d+)\)', ind_str, re.IGNORECASE)
if match:
period = match.group(1)
return f"calculate_rsi(df['close'], {period}).iloc[-1]"
# MACD: macd()
if ind_str.lower() == 'macd()':
return f"calculate_macd(df['close']).iloc[-1]"
# Fallback for unknown
return ind_str
def generate_condition_string(rules):
if not rules:
return "False"
code_parts = []
for i, rule in enumerate(rules):
expr = rule['expression']
logic = rule.get('logic_op')
lhs = translate_indicator(expr['indicator'])
op = expr['operator']
rhs = translate_indicator(expr['value'])
# Construct comparison: e.g. (df['close'] > 100)
condition_str = f"({lhs} {op} {rhs})"
code_parts.append(condition_str)
if logic:
py_logic = "and" if logic.upper() == "AND" else "or"
code_parts.append(f" {py_logic} ")
return "".join(code_parts)
def generate_full_code(ast_data, nl_query="", dsl_text=""):
entry_rules = ast_data.get('entry', [])
exit_rules = ast_data.get('exit', [])
entry_code = generate_condition_string(entry_rules)
exit_code = generate_condition_string(exit_rules)
# Escape triple quotes in strings to avoid breaking the template
nl_query_safe = nl_query.replace('"""', '\\"\\"\\"')
dsl_text_safe = dsl_text.replace('"""', '\\"\\"\\"')
ast_safe = str(ast_data).replace('"""', '\\"\\"\\"')
template = f"""import pandas as pd
import numpy as np
# -----------------------------------------------------------------------------
# Metadata
# -----------------------------------------------------------------------------
NL_QUERY = \"\"\"{nl_query_safe}\"\"\"
DSL_TEXT = \"\"\"{dsl_text_safe}\"\"\"
AST_DATA = \"\"\"{ast_safe}\"\"\"
# -----------------------------------------------------------------------------
# Indicator Helper Functions
# -----------------------------------------------------------------------------
def calculate_rsi(series, period):
if len(series) < period:
return pd.Series([50]*len(series)) # Default or NaN
delta = series.diff()
gain = (delta.where(delta > 0, 0)).rolling(pd.to_numeric(period)).mean()
loss = (-delta.where(delta < 0, 0)).rolling(pd.to_numeric(period)).mean()
rs = gain / loss
return 100 - (100 / (1 + rs))
def calculate_macd(series, fast=12, slow=26, signal=9):
exp1 = series.ewm(span=fast, adjust=False).mean()
exp2 = series.ewm(span=slow, adjust=False).mean()
macd = exp1 - exp2
# signal_line = macd.ewm(span=signal, adjust=False).mean()
return macd
# -----------------------------------------------------------------------------
# Strategy Logic
# -----------------------------------------------------------------------------
def entry_rule(df):
try:
return {entry_code}
except Exception:
return False
def exit_rule(df):
try:
return {exit_code}
except Exception:
return False
# -----------------------------------------------------------------------------
# Backtest Engine
# -----------------------------------------------------------------------------
def run_backtest():
# Load Data
try:
df = pd.read_csv('synthetic_OHLCV.csv')
df['date'] = pd.to_datetime(df['date'])
except FileNotFoundError:
print("Error: synthetic_OHLCV.csv not found.")
return
in_position = False
entry_price = 0.0
entry_date = None
trades = []
# Portfolio for Drawdown calc
initial_capital = 10000.0
capital = initial_capital
portfolio_values = []
# Iteration (Warmup period assumed 20 for indicators)
for i in range(20, len(df)):
# Simulate 'Live' by slicing up to current index
window = df.iloc[:i+1]
current_price = window['close'].iloc[-1]
current_date = window['date'].iloc[-1]
# Portfolio Value Tracking
current_val = capital
if in_position:
# If holding, value is capital (cash) + unrealized PnL logic OR just shares value
# Simple interaction: One share per trade? Or all in?
# Let's assume we buy 1 share for simplicity, or fixed amount.
# To match specific return % logic, let's track trade PnL % independently
# or simulate 1 unit to get price diff.
pass
if not in_position:
if entry_rule(window):
in_position = True
entry_price = current_price
entry_date = current_date
else:
if exit_rule(window):
in_position = False
exit_price = current_price
exit_date = current_date
pnl = exit_price - entry_price
return_pct = (pnl / entry_price) * 100
trades.append({{
'Entry Date': entry_date,
'Exit Date': exit_date,
'Entry Price': entry_price,
'Exit Price': exit_price,
'PnL': pnl,
'Return %': return_pct
}})
# Update capital - simplistically adding PnL for Total Return Calc relative to 1 share price?
# Or just sum Returns %.
# Let's accumulate percent return for "Total Return" to match user example logic (Sum of % or Compound?).
# User asked: "Entry price / exit price, Profit/loss, Total return".
# Usually Total Return is compounded or Sum of PnL / Start Capital.
# Use Simple Sum of PnL for now.
capital += pnl
# -----------------------------------------------------------------------------
# Metrics & Reporting
# -----------------------------------------------------------------------------
trades_df = pd.DataFrame(trades)
if not trades_df.empty:
total_pnl = trades_df['PnL'].sum()
total_return_pct = (trades_df['Return %'].sum()) # Simple sum of trade returns
# Max Drawdown Approximation
# Construct equity curve from trade results
equity = [initial_capital]
for pnl in trades_df['PnL']:
equity.append(equity[-1] + pnl)
equity_series = pd.Series(equity)
rolling_max = equity_series.cummax()
drawdown = (equity_series - rolling_max) / rolling_max
max_drawdown_pct = drawdown.min() * 100
trade_count = len(trades)
else:
total_pnl = 0.0
total_return_pct = 0.0
max_drawdown_pct = 0.0
trade_count = 0
# Save Report
trades_df.to_csv('report.csv', index=False)
# Print Final Format
print("Natural Language Input:")
print(f'"{nl_query.strip()}"')
print("\\nGenerated DSL:")
print(DSL_TEXT.strip())
print("\\nParsed AST:", AST_DATA)
print("\\nBacktest Result:")
print(f"Total Return: {{total_return_pct:.1f}}%")
print(f"Max Drawdown: {{max_drawdown_pct:.1f}}%")
print(f"Trades: {{trade_count}}")
print("Entry/Exit Log:")
if not trades_df.empty:
for _, row in trades_df.iterrows():
print(f"- Enter: {{row['Entry Date'].date()}} at {{row['Entry Price']}}")
print(f"- Exit: {{row['Exit Date'].date()}} at {{row['Exit Price']}}")
if __name__ == "__main__":
run_backtest()
"""
return template