AWS Machine Learning Blog 2024年11月27日
Create a virtual stock technical analyst using Amazon Bedrock Agents
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何利用Amazon Bedrock Agents构建一个虚拟股票分析师,该分析师能够理解自然语言查询,并根据股票技术指标条件(如SMA、EMA、RSI等)提供相关信息。通过将Amazon Bedrock与Lambda函数集成,该解决方案能够分析用户查询,分解任务,并调用相应的Lambda函数获取数据。Lambda函数会访问存储在Amazon S3中的股票数据,实时计算技术指标,并将结果返回给Amazon Bedrock Agents。最终,该助手可以回答各种股票技术分析问题,例如列出特定指数中的股票、计算股票在特定时间段内的涨跌幅,以及计算股票的技术指标等。

🤔 **利用Amazon Bedrock Agents构建虚拟股票分析师:**该解决方案的核心是使用Amazon Bedrock Agents来理解和执行用户的自然语言查询,从而实现自动化股票技术分析。

💡 **Lambda函数处理技术指标计算:**通过创建多个Lambda函数,分别负责获取股票数据、计算技术指标(如SMA、EMA、RSI)等,并与Amazon Bedrock Agents集成,实现数据处理和分析。

⚙️ **Amazon S3存储股票数据:**利用Amazon S3存储历史股票数据,Lambda函数可以访问这些数据,进行实时计算和分析。

🔄 **工作流程:**用户提出自然语言查询,Amazon Bedrock Agents分析查询,创建行动计划,并调用相应的Lambda函数获取数据。Lambda函数处理数据并返回结果,Agent根据结果继续执行操作,直到得到最终答案。

🧰 **关键资源:**包括Amazon S3存储桶、Lambda函数(获取股票数据、计算技术指标等)、Amazon EventBridge触发器、以及使用Anthropic Claude 3 Sonnet模型的Amazon Bedrock Agent。

Stock technical analysis questions can be as unique as the individual stock analyst themselves. Queries often have multiple technical indicators like Simple Moving Average (SMA), Exponential Moving Average (EMA), Relative Strength Index (RSI), and others. Answering these varied questions would mean writing complex business logic to unpack the query into parts and fetching the necessary data. With the number of indicators available, the possibility of having one or many of them in any combination, and each of those indicators over different time periods, it can get quite complex to build such a business logic into code.

As AI technology continues to evolve, the capabilities of generative AI agents continue to expand, offering even more opportunities for you to gain a competitive edge. At the forefront of this evolution sits Amazon Bedrock, a fully managed service that makes high-performing foundation models (FMs) from Amazon and other leading AI companies available through a single API. With Amazon Bedrock, you can build and scale generative AI applications with security, privacy, and responsible AI. Amazon Bedrock Agents plans and runs multistep tasks using company systems and data sources—from answering customer questions about your product availability to taking their orders. With Amazon Bedrock, you can create an agent in just a few quick steps by first selecting an FM and providing it access to your enterprise systems, knowledge bases, and actions to securely execute your APIs. These actions can be implemented in the cloud using AWS Lambda, or you can use local business logic with return of control. An agent analyzes the user request and automatically calls the necessary APIs and data sources to fulfill the request. Amazon Bedrock Agents offers enhanced security and privacy—no need for you to engineer prompts, manage session context, or manually orchestrate tasks.

In this post, we create a virtual analyst that can answer natural language queries of stocks matching certain technical indicator criteria using Amazon Bedrock Agents. As part of the agent, we configure action groups consisting of Lambda functions, which gives the agent the ability to perform various actions. The Amazon Bedrock agent will transform the user natural language query into relevant Lambda calls, passing the technical indicators and their needed duration. Lambda will access the open source stock data pre-fetched into an Amazon Simple Storage Service (Amazon S3) bucket, calculate the technical indicator in real time, and pass it back to the agent. The agent will take further actions like other Lambda calls or filtering and ordering based on the task.

Solution overview

The technical analysis assistant solution will use Amazon Bedrock Agents to answer natural language technical analysis queries, from simple ones like “Can you give me a list of stocks in the NASDAQ 100 index” to complex ones like “Which stocks in the NASDAQ 100 index has both grown over 10% in last 6 months and also closed above their 20-day SMA?” Agents orchestrate and analyze the task and break it down into the correct logical sequence using the FM’s reasoning abilities. Agents automatically call the necessary Lambda functions to fetch the relevant stock technical analysis data, determining along the way if they can proceed or if they need to gather more information.

The following diagram illustrates the solution architecture.

The workflow consists of the following steps:

    The solution spins up a Python-based Lambda function that fetches the daily stock data for the last one year using the yfinance package. The Lambda function is triggered to run every day using an Amazon EventBridge The functions puts the last 1-year stock data into an S3 bucket. A user asks a natural language query, like “Can you give me a list of stocks in NASDAQ 100 index” or “Which stocks have closed over both 20 SMA and 50 EMA in the FTSE 100 index?” This query is passed to the Amazon Bedrock agent powered by Anthropic’s Claude 3 Sonnet. The agent deconstructs the user query, creates an action plan, and executes it step-by-step to fetch various data needed to answer the question. To fetch the needed data, the agent has three action groups, each powered by a Lambda function that can use the raw data stored in the S3 bucket to calculate technical indicators and other stock-related information. Based on the response from the action group and the agent’s plan of action, the agent will continue to make calls or take other actions like filtering or summarizing until it arrives at the answer to the question. The action groups are as follows:
      get-index – Get the stock symbol of constituents for a given index. The example currently has constituents configured for Nasdaq 100, FTSE 100, and Nifty 50 indexes. get-stock-change – For a given stock or list of stocks, calculate the change percentage over a given period based on the pre-fetched raw data in Amazon S3. This solution currently is configured to have data of the past 1 year. get-technical-analysis – For a given stock or list of stocks, calculate the given technical indicator for a given time period. It also fetches the last closing price of the stock based on the pre-fetched raw data in Amazon S3. This solution currently is configured to handle SMA, EMA, and RSI technical indicators for up to 1 year.

Prerequisites

To set up this solution, you need a basic knowledge of AWS and the relevant AWS services. Additionally, request model access on Amazon Bedrock for Anthropic’s Claude 3 Sonnet.

Deploy the solution

Complete the following steps to deploy the solution using AWS CloudFormation:

    Launch the CloudFormation stack in the us-east-1 AWS Region:

    For Stack name, enter a stack name of your choice. Leave the rest as defaults. Choose Next. Choose Next Select the acknowledgement check box and choose Submit.

    Wait for the stack creation to complete. Verify all resources are created on the stack details page.

The CloudFormation stack creates the solution described in the solution overview and the following key resources:

# Example call to Yahoo finance to get stock historyimport yfinance as yfstock_ticker = yf.Ticker(<Stock Symbol>)stock_history = stock_ticker.history(period= <Time duration to fetch history e.g. 1y>))
# Sample code to calculate Simple Moving Average,  a technical indicatorfrom ta.trend import SMAIndicatorindicator_ta = SMAIndicator(<PAndas series with Close price>, window=<SMA window number of days>)stock_SMA = indicator_ta.sma_indicator()
{"openapi": "3.0.0","info": {"title": "Get Index list of stocks api","version": "1.0.0","description": "API to fetch the list of stocks in a given index"},"paths": {"/get-index": {"get": {"summary": "Get list of stock symbols in index","description": "Based on provided index, return list of stock symbols in the index","operationId": "getIndex","parameters": [{"name": "indexName","in": "path","description": "Index Name","required": true,"schema": {"type": "string"}}],"responses": {"200": {"description": "Get Index stock list","content": {"application/json": {"schema": {"type": "array",

Test the solution

To test the solution, complete the following steps:

    On the Amazon Bedrock console, choose Agents in the navigation pane. Choose the agent created by the CloudFormation stack.

    Under Test, choose the alias with the name Version 1 and expand the Test

Now you can enter questions to interact with the agent.

    Let’s start with the query “Can you give me a list of stocks in Nasdaq.”

You can see the answer in the following screenshot. Expand the Trace Step section in the right pane to see the agent’s rationale and the call to the Lambda function.

    Now let’s ask a question that is likely to use all three action groups and their Lambda functions: “Can you give list of stocks that has both grown over 10% in last 6 months and also closed above their 20-day SMA. Use stocks from the Nasdaq index.”

You will get the response shown in the following screenshot, and in the trace steps, you will see the various Lambda functions being invoked at different steps as the agent reasons through the steps to get to the answer to the question.

You can further test with additional prompts, such as:

Programmatically invoke the agent

When you’re satisfied with the performance of your agent, you can build an application to programmatically invoke the agent using the InvokeAgent API. The agent ID and the agent alias ID needed for invoking the agent alias programmatically can be found on the Outputs tab of the CloudFormation stack, titled AgentId and AgentAliasId, respectively. To learn more about the programmatic invocation, refer the following Python example and JavaScript example.

Clean up

To avoid charges in your AWS account, clean up the solution’s provisioned resources:

    On the Amazon S3 console, empty the S3 bucket created as part of the CloudFormation stack. The bucket name should start with your stack name that you entered while creating the CloudFormation stack. You can also check the name of the bucket on the CloudFormation stack’s Resources On the AWS CloudFormation console, select the stack you created for this solution and choose Delete.

Conclusion

In this post, we showed how you can use Amazon Bedrock Agents to carry out complex tasks that need multiple step orchestration through just natural language instructions. With agents, you can automate tasks for your customers and answer questions for them. We encourage you to explore the Amazon Bedrock Agents User Guide to understand its capabilities further and use it for your use cases.


About the authors

Bharath Sridharan is a Senior Technical Account Manager at AWS and works with strategic customers of AWS in pro-actively optimizing their workloads on AWS. Bharath additionally specializes on Machine learning services of AWS with a focus on Generative AI.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Amazon Bedrock 股票技术分析 自然语言处理 Lambda Amazon S3
相关文章