Continue from last blog, lots of people have tried to deploy multiple agents, tasks woven in CrewAI to see if AI can perform stock research, analyze, trading and risk management all at once.
from crewai import Agent, Task, Crew
from crewai_tools import ScrapeWebsiteTool, SerperDevTool
search_tool = SerperDevTool()
scrape_tool = ScrapeWebsiteTool()
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 “
“uses statistical modeling and machine learning “
“to provide crucial insights. With a knack for data, “
“the Data Analyst Agent is the cornerstone for “
“informing trading decisions.”,
verbose=True,
allow_delegation=True,
tools = [scrape_tool, search_tool]
)
# Task for Data Analyst Agent: Analyze Market Data
data_analysis_task = Task(
description=(
“Continuously monitor and analyze market data for “
“the selected stock ({stock_selection}). “
“Use statistical modeling and machine learning to “
“identify trends and predict market movements.”
),
expected_output=(
“Insights and alerts about significant market “
“opportunities or threats for {stock_selection}.”
),
agent=data_analyst_agent,
)
from crewai import Crew, Process
from langchain_openai import ChatOpenAI
# Define the crew with agents and tasks
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=ChatOpenAI(model=”gpt-3.5-turbo”,
temperature=0.7),
process=Process.hierarchical,
verbose=True
)
# Example data for kicking off the process
financial_trading_inputs = {
‘stock_selection’: ‘AAPL’,
‘initial_capital’: ‘100000’,
‘risk_tolerance’: ‘Medium’,
‘trading_strategy_preference’: ‘Day Trading’,
‘news_impact_consideration’: True
}
### this execution will take some time to run
The details of how this robot stock analyst/agent at work:
[DEBUG]: Working Agent: Crew Manager
[INFO]: Starting Task: Continuously monitor and analyze market data for the selected stock (AAPL). Use statistical modeling and machine learning to identify trends and predict market movements.
> Entering new CrewAgentExecutor chain...
I need to delegate the task of continuously monitoring and analyzing market data for AAPL to the Data Analyst in order to identify trends and predict market movements.
Action: Delegate work to co-worker
Action Input: {"coworker": "Data Analyst", "task": "Continuously monitor and analyze market data for AAPL using statistical modeling and machine learning", "context": "The goal is to identify trends and predict market movements for AAPL"}
> Entering new CrewAgentExecutor chain...
I should start by reading the latest news and updates on AAPL to understand the current market sentiment and any potential factors affecting its stock price.
Action: Read website content
Action Input: {"website_url": "https://www.marketwatch.com/investing/stock/aapl"}
marketwatch.comPlease enable JS and disable any ad blocker
Final Answer: marketwatch.comPlease enable JS and disable any ad blocker
> Finished chain.
Then there is THOUGHT
Thought:
I need to ask the Data Analyst if they require any specific data sources or tools to successfully monitor and analyze market data for AAPL.
Action: Ask question to co-worker
Action Input: {"coworker": "Data Analyst", "question": "Do you require any specific data sources or tools to successfully monitor and analyze market data for AAPL?", "context": "The goal is to identify trends and predict market movements for AAPL"}
> Entering new CrewAgentExecutor chain...
I need to gather specific data sources and tools to successfully monitor and analyze market data for AAPL.
Action: Read website content
Action Input: {"website_url": "https://finance.yahoo.com/quote/AAPL"}
...
result = financial_trading_crew.kickoff(inputs=financial_trading_inputs)
Final output looks like:
After evaluating the risks associated with the proposed trading strategies and execution plans for AAPL, it is crucial to consider the potential challenges and develop mitigation strategies to minimize risks and maximize gains.
One of the risks identified is the use of a short position in put options with a strike price below the current market price of AAPL shares. This strategy can lead to significant losses due to the potential for unlimited liability and the need for sufficient funds to cover potential losses. To mitigate this risk, it is recommended to carefully assess risk tolerance, set stop-loss orders, and diversify the portfolio to spread out risks.
Another risk to consider is the implementation of stock option trading strategies such as Straddle, Buy-Write, Risk Reversal, and Call Spread. While these strategies can provide insights into potential gains and losses, they also come with risks such as market volatility, unexpected price movements, and liquidity issues. To mitigate these risks, it is advised to conduct thorough research, stay informed about market conditions, and consider using risk management tools such as options spreads and collars.
Day trading strategies for Apple stocks involve analyzing pre-market prices, preparing charts, and conducting technical analysis of AAPL stock charts, market data, and options flow. While these strategies can be lucrative, they also carry risks such as market fluctuations, timing issues, and emotional trading decisions. To mitigate these risks, it is recommended to set clear trading goals, follow a disciplined trading plan, and use risk management techniques such as position sizing and leverage control.
In conclusion, by identifying potential risks associated with the proposed trading strategies and execution plans for AAPL and implementing appropriate mitigation strategies, traders can navigate the market with confidence and increase the likelihood of successful trades. It is essential to stay informed, adapt to changing market conditions, and continuously evaluate and adjust trading strategies to achieve long-term success in trading AAPL.
The output is mostly gibberish from too much useless web content. however, the ability to follow a logical process is absolutely useful and powerful. on the other hand, tools provided by crewAI are:
- Built-in CrewAI Tools:
- DirectoryReadTool: For reading contents of directories
- FileReadTool: For reading file contents
- SerperDevTool: For web searches using the Serper API
- WebsiteSearchTool: For searching and extracting information from websites
- Custom Tool Creation:
- Developers can create custom tools by subclassing BaseTool or using the @tool decorator
- These custom tools can be tailored to specific agent needs and tasks
- LangChain Integration:
- CrewAI seamlessly integrates with LangChain’s toolkit
- This includes tools for search-based queries and other functionalities
- YouTube Search Tool:
- Allows agents to search and extract information from specific YouTube channels
- Collaboration Tools:
- While not explicitly detailed, CrewAI emphasizes tools that enable agent collaboration and task delegation
- Error Handling and Caching:
- All tools in CrewAI come with built-in error handling capabilities
- Tools support caching mechanisms to improve efficiency and reduce redundant operations
- Dynamic Tool Calling:
- Agents can dynamically call and use tools as needed during task execution
- Tool Delegation:
- Agents can delegate tasks or seek help from other agents using appropriate tools
- PDF Search Tools:
- While not explicitly mentioned in the provided context, there’s a reference to PDF search capabilities
- Web Searching Tools:
- Various tools for conducting web searches and extracting information from online sources
The task and agent settings in CrewAI seem verbose and repetitive, but for a few reasons:
It prioritizes explicit and detailed configuration to ensure clarity and precision in defining agent roles, goals, and tasks. This approach helps prevent ambiguity and allows for fine-grained control.
CrewAI is compared to other frameworks like AutoGen and ChatDev, suggesting it’s being considered alongside established tools for AI agent collaboration.
If I apply such agents/tasks in automating recon/rebal process using directory read, file read tools.