forked from virattt/ai-hedge-fund
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.bat
More file actions
355 lines (317 loc) · 9.61 KB
/
Copy pathrun.bat
File metadata and controls
355 lines (317 loc) · 9.61 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
@echo off
setlocal enabledelayedexpansion
:: Default values
set TICKER=AAPL,MSFT,NVDA
set USE_OLLAMA=
set START_DATE=
set END_DATE=
set INITIAL_AMOUNT=100000.0
set MARGIN_REQUIREMENT=0.0
set SHOW_REASONING=
set COMMAND=
set MODEL_NAME=
:: Help function
:show_help
echo AI Hedge Fund Docker Runner
echo.
echo Usage: run.bat [OPTIONS] COMMAND
echo.
echo Options:
echo --ticker SYMBOLS Comma-separated list of ticker symbols (e.g., AAPL,MSFT,NVDA)
echo --start-date DATE Start date in YYYY-MM-DD format
echo --end-date DATE End date in YYYY-MM-DD format
echo --initial-cash AMT Initial cash position (default: 100000.0)
echo --margin-requirement RATIO Margin requirement ratio (default: 0.0)
echo --ollama Use Ollama for local LLM inference
echo --show-reasoning Show reasoning from each agent
echo.
echo Commands:
echo main Run the main hedge fund application
echo backtest Run the backtester
echo build Build the Docker image
echo compose Run using Docker Compose with integrated Ollama
echo ollama Start only the Ollama container for model management
echo pull MODEL Pull a specific model into the Ollama container
echo help Show this help message
echo.
echo Examples:
echo run.bat --ticker AAPL,MSFT,NVDA main
echo run.bat --ticker AAPL,MSFT,NVDA --ollama main
echo run.bat --ticker AAPL,MSFT,NVDA --start-date 2024-01-01 --end-date 2024-03-01 backtest
echo run.bat compose # Run with Docker Compose (includes Ollama)
echo run.bat ollama # Start only the Ollama container
echo run.bat pull llama3 # Pull the llama3 model to Ollama
echo.
goto :eof
:: Parse arguments
:parse_args
if "%~1"=="" goto :check_command
if "%~1"=="--ticker" (
set TICKER=%~2
shift
shift
goto :parse_args
)
if "%~1"=="--start-date" (
set START_DATE=--start-date %~2
shift
shift
goto :parse_args
)
if "%~1"=="--end-date" (
set END_DATE=--end-date %~2
shift
shift
goto :parse_args
)
if "%~1"=="--initial-cash" (
set INITIAL_AMOUNT=%~2
shift
shift
goto :parse_args
)
if "%~1"=="--margin-requirement" (
set MARGIN_REQUIREMENT=%~2
shift
shift
goto :parse_args
)
if "%~1"=="--ollama" (
set USE_OLLAMA=--ollama
shift
goto :parse_args
)
if "%~1"=="--show-reasoning" (
set SHOW_REASONING=--show-reasoning
shift
goto :parse_args
)
if "%~1"=="main" (
set COMMAND=main
shift
goto :parse_args
)
if "%~1"=="backtest" (
set COMMAND=backtest
shift
goto :parse_args
)
if "%~1"=="build" (
set COMMAND=build
shift
goto :parse_args
)
if "%~1"=="compose" (
set COMMAND=compose
shift
goto :parse_args
)
if "%~1"=="ollama" (
set COMMAND=ollama
shift
goto :parse_args
)
if "%~1"=="pull" (
set COMMAND=pull
set MODEL_NAME=%~2
shift
shift
goto :parse_args
)
if "%~1"=="help" (
call :show_help
exit /b 0
)
if "%~1"=="--help" (
call :show_help
exit /b 0
)
echo Unknown option: %~1
call :show_help
exit /b 1
:check_command
if "!COMMAND!"=="" (
echo Error: No command specified.
call :show_help
exit /b 1
)
:: Show help if 'help' command is provided
if "!COMMAND!"=="help" (
call :show_help
exit /b 0
)
:: Check for Docker Compose existence
docker compose version >nul 2>&1
if !ERRORLEVEL! EQU 0 (
set COMPOSE_CMD=docker compose
) else (
docker-compose --version >nul 2>&1
if !ERRORLEVEL! EQU 0 (
set COMPOSE_CMD=docker-compose
) else (
echo Error: Docker Compose is not installed.
exit /b 1
)
)
:: Build the Docker image if 'build' command is provided
if "!COMMAND!"=="build" (
docker build -t ai-hedge-fund .
exit /b 0
)
:: Start Ollama container if 'ollama' command is provided
if "!COMMAND!"=="ollama" (
echo Starting Ollama container...
!COMPOSE_CMD! up -d ollama
:: Check if Ollama is running
echo Waiting for Ollama to start...
for /l %%i in (1, 1, 30) do (
!COMPOSE_CMD! exec ollama curl -s http://localhost:11434/api/version >nul 2>&1
if !ERRORLEVEL! EQU 0 (
echo Ollama is now running.
:: Show available models
echo Available models:
!COMPOSE_CMD! exec ollama ollama list
echo.
echo Manage your models using:
echo run.bat pull ^<model-name^> # Download a model
echo run.bat ollama # Start Ollama and show models
exit /b 0
)
timeout /t 1 /nobreak >nul
echo.
)
echo Failed to start Ollama within the expected time. You may need to check the container logs.
exit /b 1
)
:: Pull a model if 'pull' command is provided
if "!COMMAND!"=="pull" (
if "!MODEL_NAME!"=="" (
echo Error: No model name specified.
echo Usage: run.bat pull ^<model-name^>
echo Example: run.bat pull llama3
exit /b 1
)
:: Start Ollama if it's not already running
!COMPOSE_CMD! up -d ollama
:: Wait for Ollama to start
echo Ensuring Ollama is running...
for /l %%i in (1, 1, 30) do (
!COMPOSE_CMD! exec ollama curl -s http://localhost:11434/api/version >nul 2>&1
if !ERRORLEVEL! EQU 0 (
echo Ollama is running.
goto :pull_model
)
timeout /t 1 /nobreak >nul
echo.
)
:pull_model
:: Pull the model
echo Pulling model: !MODEL_NAME!
echo This may take some time depending on the model size and your internet connection.
echo You can press Ctrl+C to cancel at any time (the model will continue downloading in the background).
!COMPOSE_CMD! exec ollama ollama pull "!MODEL_NAME!"
:: Check if the model was successfully pulled
!COMPOSE_CMD! exec ollama ollama list | findstr /i "!MODEL_NAME!" >nul
if !ERRORLEVEL! EQU 0 (
echo Model !MODEL_NAME! was successfully downloaded.
) else (
echo Warning: Model !MODEL_NAME! may not have been properly downloaded.
echo Check the Ollama container status with: run.bat ollama
)
exit /b 0
)
:: Run with Docker Compose if 'compose' command is provided
if "!COMMAND!"=="compose" (
echo Running with Docker Compose (includes Ollama)...
!COMPOSE_CMD! up --build
exit /b 0
)
:: Check if .env file exists, if not create from .env.example
if not exist .env (
if exist .env.example (
echo No .env file found. Creating from .env.example...
copy .env.example .env
echo Please edit .env file to add your API keys.
) else (
echo Error: No .env or .env.example file found.
exit /b 1
)
)
:: Set script path and parameters based on command
if "!COMMAND!"=="main" (
set SCRIPT_PATH=src/main.py
if "!COMMAND!"=="main" (
set INITIAL_PARAM=--initial-cash !INITIAL_AMOUNT!
)
) else if "!COMMAND!"=="backtest" (
set SCRIPT_PATH=src/backtester.py
if "!COMMAND!"=="backtest" (
set INITIAL_PARAM=--initial-capital !INITIAL_AMOUNT!
)
)
:: If using Ollama, make sure the service is started
if not "!USE_OLLAMA!"=="" (
echo Setting up Ollama container for local LLM inference...
:: Start Ollama container if not already running
!COMPOSE_CMD! up -d ollama
:: Wait for Ollama to start
echo Waiting for Ollama to start...
for /l %%i in (1, 1, 30) do (
!COMPOSE_CMD! exec ollama curl -s http://localhost:11434/api/version >nul 2>&1
if !ERRORLEVEL! EQU 0 (
echo Ollama is running.
:: Show available models
echo Available models:
!COMPOSE_CMD! exec ollama ollama list
goto :continue_ollama
)
timeout /t 1 /nobreak >nul
echo.
)
:continue_ollama
:: Build the AI Hedge Fund image if needed
docker images -q ai-hedge-fund 2>nul | findstr /r /c:"^..*$" >nul
if !ERRORLEVEL! NEQ 0 (
echo Building AI Hedge Fund image...
docker build -t ai-hedge-fund .
)
:: Create command override for Docker Compose
set COMMAND_OVERRIDE=
if not "!START_DATE!"=="" (
set COMMAND_OVERRIDE=!COMMAND_OVERRIDE! !START_DATE!
)
if not "!END_DATE!"=="" (
set COMMAND_OVERRIDE=!COMMAND_OVERRIDE! !END_DATE!
)
if not "!INITIAL_PARAM!"=="" (
set COMMAND_OVERRIDE=!COMMAND_OVERRIDE! !INITIAL_PARAM!
)
if not "!MARGIN_REQUIREMENT!"=="" (
set COMMAND_OVERRIDE=!COMMAND_OVERRIDE! --margin-requirement !MARGIN_REQUIREMENT!
)
:: Run the command with Docker Compose
echo Running AI Hedge Fund with Ollama using Docker Compose...
:: Use the appropriate service based on command and reasoning flag
if "!COMMAND!"=="main" (
if not "!SHOW_REASONING!"=="" (
!COMPOSE_CMD! run --rm hedge-fund-reasoning python src/main.py --ticker !TICKER! !COMMAND_OVERRIDE! !SHOW_REASONING! --ollama
) else (
!COMPOSE_CMD! run --rm hedge-fund-ollama python src/main.py --ticker !TICKER! !COMMAND_OVERRIDE! --ollama
)
) else if "!COMMAND!"=="backtest" (
!COMPOSE_CMD! run --rm backtester-ollama python src/backtester.py --ticker !TICKER! !COMMAND_OVERRIDE! !SHOW_REASONING! --ollama
)
exit /b 0
)
:: Standard Docker run (without Ollama)
:: Build the command
set CMD=docker run -it --rm -v %cd%\.env:/app/.env
:: Add the command
set CMD=!CMD! ai-hedge-fund python !SCRIPT_PATH! --ticker !TICKER! !START_DATE! !END_DATE! !INITIAL_PARAM! --margin-requirement !MARGIN_REQUIREMENT! !SHOW_REASONING!
:: Run the command
echo Running: !CMD!
!CMD!
:: Exit
exit /b 0
:: Start script execution
call :parse_args %*