Skip to content

Commit 026c837

Browse files
First steps to using agents
Experiment of using llama 3.2 agent as the decision maker in the trading algorithm
1 parent 4c97af2 commit 026c837

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Agent.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import ollama
2+
3+
4+
def ollama_bigger(value):
5+
content = f"Is float {value} bigger than 0? Y or N. Do not write code. Just the correct letter."
6+
# print(content)
7+
response = ollama.chat(model="llama3.2", messages=[
8+
{
9+
"role": "user",
10+
"content": content
11+
},
12+
])
13+
return response
14+

MainLoop.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import datetime
44
import datetime as dt
55
from LiabilityClasses import Liability
6+
from Agent import ollama_bigger
67

78
def create_cashflow_dataframe(cf_dict:dict, unique_dates:list) -> pd.DataFrame:
89
"""
@@ -178,18 +179,25 @@ def trade(current_date: dt.date, bank_account:pd.DataFrame, eq_units:pd.DataFram
178179
List with the Dataframe documenting the number of units after trading and the bank_account DataFrame with the
179180
updated bank account balance for the modelling date.
180181
"""
182+
181183
total_market_value = sum(eq_units[current_date]*eq_price[current_date])+sum(bd_units[current_date]*bd_price[current_date]) # Total value of portfolio after growth
182184

183-
if total_market_value <= 0:
185+
response_MV = ollama_bigger(total_market_value)
186+
response_bank = ollama_bigger(bank_account[current_date][0])
187+
188+
# if total_market_value <= 0:
189+
if response_MV["message"]["content"] == "N" or response_MV["message"]["content"] == "N.":
184190
pass
185-
elif bank_account[current_date][0] < 0: # Sell assets
191+
# elif bank_account[current_date][0] < 0: # Sell assets
192+
elif response_bank["message"]["content"] == "N" or response_bank["message"]["content"] == "N.": # Sell assets
186193
percent_to_sell = min(1, -bank_account[current_date][0] / total_market_value) # How much of the portfolio needs to be sold
187194
eq_units[current_date] = eq_units[current_date] * (1 - percent_to_sell)
188195
bd_units[current_date] = bd_units[current_date] * (1 - percent_to_sell)
189196

190197
bank_account[current_date] += total_market_value - sum(eq_units[current_date]*eq_price[current_date])-sum(bd_units[current_date]*bd_price[current_date]) # Add cash to bank account equal to shares sold
191198

192-
elif bank_account[current_date][0] > 0: # Buy assets
199+
# elif bank_account[current_date][0] > 0: # Buy assets
200+
elif response_bank["message"]["content"] =="Y" or response_bank["message"]["content"] =="Y.": # Buy assets
193201
percent_to_buy = bank_account[current_date][0] / total_market_value # What % of the portfolio is the excess cash
194202
eq_units[current_date] = eq_units[current_date] * (1 + percent_to_buy)
195203
bd_units[current_date] = bd_units[current_date] * (1 + percent_to_buy)

0 commit comments

Comments
 (0)