From c425c7a603a5f2531501b70552537d6f2580e84c Mon Sep 17 00:00:00 2001 From: AquibPy Date: Mon, 24 Jun 2024 22:18:27 +0530 Subject: [PATCH] ADDED: Investment risk analyst agent --- investment_risk_analyst_agent/__init__.py | 0 investment_risk_analyst_agent/agents.py | 65 +++++++++++++++++++++++ investment_risk_analyst_agent/crew.py | 44 +++++++++++++++ investment_risk_analyst_agent/tasks.py | 50 +++++++++++++++++ investment_risk_analyst_agent/tools.py | 9 ++++ 5 files changed, 168 insertions(+) create mode 100644 investment_risk_analyst_agent/__init__.py create mode 100644 investment_risk_analyst_agent/agents.py create mode 100644 investment_risk_analyst_agent/crew.py create mode 100644 investment_risk_analyst_agent/tasks.py create mode 100644 investment_risk_analyst_agent/tools.py diff --git a/investment_risk_analyst_agent/__init__.py b/investment_risk_analyst_agent/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/investment_risk_analyst_agent/agents.py b/investment_risk_analyst_agent/agents.py new file mode 100644 index 0000000..532c873 --- /dev/null +++ b/investment_risk_analyst_agent/agents.py @@ -0,0 +1,65 @@ +from crewai import Agent +from investment_risk_analyst_agent.tools import search_tool,scrape_tool +from dotenv import load_dotenv +load_dotenv() +from langchain_google_genai import ChatGoogleGenerativeAI +import os +import settings + +llm=ChatGoogleGenerativeAI(model=settings.GEMINI_FLASH, + verbose=True, + temperature=0.5, + google_api_key=os.getenv("GOOGLE_API_KEY")) + +data_analyst_agent = Agent( + role="Data Analyst", + goal="Monitor and analyze market data in real-time to identify trends and predict market movements.", + backstory=""" + Specializing in financial markets, this agent employs statistical modeling and machine learning techniques to provide critical insights. + Renowned for its proficiency in data analysis, the Data Analyst Agent serves as a pivotal resource for informing trading decisions. + """, + verbose=True, + allow_delegation=True, + tools=[scrape_tool, search_tool], + llm = llm +) + +trading_strategy_agent = Agent( + role="Trading Strategy Developer", + goal="Develop and test various trading strategies leveraging insights from the Data Analyst Agent.", + backstory=""" + Possessing a deep understanding of financial markets and quantitative analysis, this agent formulates and optimizes trading strategies. + It assesses the performance of diverse approaches to identify the most profitable and risk-averse options. + """, + verbose=True, + allow_delegation=True, + tools=[scrape_tool, search_tool], + llm = llm +) + +execution_agent = Agent( + role="Trade Advisor", + goal="Recommend optimal trade execution strategies based on approved trading plans.", + backstory=""" + Specializing in the analysis of timing, price, and logistical details of potential trades, + this agent evaluates these factors to provide well-founded recommendations. + Its expertise ensures that trades are executed efficiently and in alignment with the overall strategy.""", + verbose=True, + allow_delegation=True, + tools=[scrape_tool, search_tool], + llm = llm +) + +risk_management_agent = Agent( + role="Risk Advisor", + goal="Evaluate and provide insights on the risks associated with potential trading activities.", + backstory=""" + With extensive expertise in risk assessment models and market dynamics, + this agent thoroughly examines the potential risks of proposed trades. + It delivers comprehensive analyses of risk exposure and recommends safeguards to ensure that trading activities align with the firm's risk tolerance. + """, + verbose=True, + allow_delegation=True, + tools=[scrape_tool, search_tool], + llm = llm +) \ No newline at end of file diff --git a/investment_risk_analyst_agent/crew.py b/investment_risk_analyst_agent/crew.py new file mode 100644 index 0000000..ceb2f36 --- /dev/null +++ b/investment_risk_analyst_agent/crew.py @@ -0,0 +1,44 @@ +from crewai import Crew, Process +from dotenv import load_dotenv +load_dotenv() +from langchain_google_genai import ChatGoogleGenerativeAI +# import settings +import os +from investment_risk_analyst_agent.agents import data_analyst_agent,trading_strategy_agent,execution_agent,risk_management_agent +from investment_risk_analyst_agent.tasks import data_analysis_task,strategy_development_task,risk_assessment_task,execution_planning_task + +llm=ChatGoogleGenerativeAI(model="gemini-1.5-flash", + verbose=True, + temperature=0.7, + google_api_key=os.getenv("GOOGLE_API_KEY")) + + +financial_trading_crew = Crew( + agents=[data_analyst_agent, + trading_strategy_agent, + execution_agent, + risk_management_agent], + + tasks=[data_analysis_task, + strategy_development_task, + execution_planning_task, + risk_assessment_task], + + manager_llm=llm, + process=Process.hierarchical, + verbose=True +) + +def run_crew(input_data): + result = financial_trading_crew.kickoff(inputs=input_data) + return result + +if __name__=='__main__': + financial_trading_inputs ={ + 'stock_selection': 'AAPL', + 'initial_capital': '100000', + 'risk_tolerance': 'Medium', + 'trading_strategy_preference': 'Day Trading', + 'news_impact_consideration': True + } + print(run_crew(input_data=financial_trading_inputs)) \ No newline at end of file diff --git a/investment_risk_analyst_agent/tasks.py b/investment_risk_analyst_agent/tasks.py new file mode 100644 index 0000000..c71de9f --- /dev/null +++ b/investment_risk_analyst_agent/tasks.py @@ -0,0 +1,50 @@ +from crewai import Task +from investment_risk_analyst_agent.tools import search_tool,scrape_tool +from investment_risk_analyst_agent.agents import data_analyst_agent,trading_strategy_agent,execution_agent,risk_management_agent + +data_analysis_task = Task( + description=( + "Continuously monitor and analyze market data for the selected stock ({stock_selection}). " + "Employ statistical modeling and machine learning techniques to identify trends and predict market movements." + ), + expected_output=( + "Generate insights and alerts regarding significant market opportunities or threats for {stock_selection}." + ), + agent=data_analyst_agent, + tools=[scrape_tool, search_tool], +) + +strategy_development_task = Task( + description=( + "Develop and refine trading strategies based on insights from the Data Analyst and user-defined risk tolerance ({risk_tolerance}). " + "Incorporate trading preferences ({trading_strategy_preference}) in the strategy development process." + ), + expected_output=( + "A set of potential trading strategies for {stock_selection} that align with the user's risk tolerance." + ), + agent=trading_strategy_agent, + tools=[scrape_tool, search_tool], +) + +execution_planning_task = Task( + description=( + "Analyze approved trading strategies to determine the optimal execution methods for {stock_selection}, " + "considering current market conditions and pricing strategies." + ), + expected_output=( + "Comprehensive execution plans detailing how and when to execute trades for {stock_selection}." + ), + agent=execution_agent, +) + +risk_assessment_task = Task( + description=( + "Evaluate the risks associated with the proposed trading strategies and execution plans for {stock_selection}. " + "Provide a detailed analysis of potential risks and recommend mitigation strategies." + ), + expected_output=( + "A comprehensive risk analysis report detailing potential risks and mitigation recommendations for {stock_selection}." + ), + agent=risk_management_agent, + tools=[scrape_tool, search_tool], +) diff --git a/investment_risk_analyst_agent/tools.py b/investment_risk_analyst_agent/tools.py new file mode 100644 index 0000000..bf1d57d --- /dev/null +++ b/investment_risk_analyst_agent/tools.py @@ -0,0 +1,9 @@ +from crewai_tools import ScrapeWebsiteTool, SerperDevTool +import os +from dotenv import load_dotenv +load_dotenv() + +os.environ['SERPER_API_KEY'] = os.getenv('SERPER_API_KEY') + +search_tool = SerperDevTool() +scrape_tool = ScrapeWebsiteTool() \ No newline at end of file