You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
5.3 KiB
99 lines
5.3 KiB
import json |
|
from openai import OpenAI |
|
from tenacity import retry, wait_random_exponential, stop_after_attempt |
|
import sys |
|
|
|
GPT_MODEL = "gpt-4-turbo" |
|
client = OpenAI(api_key="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") |
|
|
|
|
|
@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3)) |
|
def chat_completion_request(messages, tools=None, tool_choice=None, model=GPT_MODEL): |
|
try: |
|
response = client.chat.completions.create( |
|
model=model, |
|
messages=messages, |
|
tools=tools, |
|
tool_choice=tool_choice, |
|
) |
|
return response |
|
except Exception as e: |
|
print("Unable to generate ChatCompletion response") |
|
print(f"Exception: {e}") |
|
sys.exit(1) # Add this line |
|
|
|
def get_backtest(symbols, sliding_window="six months", frequency="semi-annually", function="sharpe ratio"): |
|
symbol = " , ".join(symbols) |
|
return f"Backtest result for {symbol} Annualized return: 10%, Annualized Sharpe ratio: 1.5, Annualized volatility: 20%, Maximum drawdown: 5%, Alpha: 0.1, Beta: 0.8, VaR10: 5%, R2: 0.9" |
|
def backest_main(query): |
|
tools = [ |
|
{ |
|
"type": "function", |
|
"function": { |
|
"name": "get_backtest", |
|
"description": "Get the portfolio backtesting result by combined with a list of symbol, sliding window, optimize frequency and optimize function", |
|
"parameters": { |
|
"type": "object", |
|
"properties": { |
|
"symbol": { |
|
"type": "array", |
|
"description": "An array of multiple portfolio symbol to be backtested if the symbol is Taiwan Stock exchage the code ex: TSMC to 2330.TW , if there is multiple symbol return a python list format", |
|
"items": { |
|
"type": "string", |
|
"description": "The symbol of the stock", |
|
}, |
|
}, |
|
"sliding window": { |
|
"type": "string", |
|
"enum": ["one month", "three months", "six months", "one year"], |
|
"description": "The sliding window size to be backtested in one month, three months , six months, one year", |
|
}, |
|
"frequency": { |
|
"type": "string", |
|
"enum": ["monthly", "quarterly", "semi-annually", "annually"], |
|
"description": "The optimize frequency to be backtested in monthly, quarterly, semi-annually, annually", |
|
}, |
|
"function": { |
|
"type": "string", |
|
"enum": ["sharpe ratio", "sortino ratio", "volatility", "utility function"], |
|
"description": "The optimize function to be backtested in sharpe ratio, sortino ratio,volatility, utility function", |
|
}, |
|
}, |
|
"required": ["symbol", "sliding window", "frequency", "function"], |
|
}, |
|
} |
|
} |
|
] |
|
|
|
messages = [] |
|
sec_message = [] |
|
messages.append({"role": "system", "content": "You are a software developer who is writing a function to get the portfolio backtesting result by using different multiple symbol, sliding window, optimize frequency and optimize function , " |
|
"only the symbol is required, the sliding window, frequency and function are optional. The sliding window size can be default by six months. The optimize frequency can be default by semi-annually. The optimize function can be default by sharpe ratio."}) |
|
messages.append({"role": "user", "content": query}) |
|
|
|
chat_response = chat_completion_request(messages,tools=tools) |
|
|
|
assistant_messages = chat_response.choices[0].message.tool_calls |
|
messages.append(assistant_messages) |
|
available_functions = { |
|
"get_backtest": get_backtest, |
|
} # only one function in this example, but you can have multiple |
|
for tool_call in assistant_messages: |
|
function_name = tool_call.function.name |
|
function_to_call = available_functions[function_name] |
|
function_args = json.loads(tool_call.function.arguments) |
|
function_response = function_to_call( |
|
symbols=function_args.get("symbol"), |
|
sliding_window=function_args.get("sliding window"), |
|
frequency=function_args.get("frequency"), |
|
function=function_args.get("function"), |
|
) |
|
|
|
sec_message.append({"role": "system","content": "You are a professional financial analyst. The user will provide you with some results from their backtesting of an investment portfolio using the Efficient Frontier calculation. These results include annualized return, annualized Sharpe ratio, annualized volatility, maximum drawdown, Alpha, Beta, VaR10, and R2. Please provide professional advice in Traditional Chinese based on these reports. "}) |
|
sec_message.append({"role": "user","content": function_response}) |
|
result_messages = chat_completion_request(sec_message) |
|
return result_messages.choices[0].message.content |
|
|
|
query = "我想要使用台積電和蘋果股票來進行最大夏普比率的回測" |
|
article = backest_main(query) |
|
print(article) |