TaskWeaver Stock Price Plugin

parmarjatin4911@gmail.com - Jan 28 - - Dev Community

**

TaskWeaver Stock Price Plugin**

stock_price.py

import yfinance as yf
from taskweaver.plugin import Plugin, register_plugin

Define the detailed prompt for the plugin

stock_price_prompt = r"""
This plugin is designed to fetch real-time stock market data. It performs the following tasks:

  • Retrieves the latest stock price information based on the provided stock symbol.
  • Extracts key financial details such as the current market price.
  • Finally, it returns this information in a structured dictionary format, as shown below: { "symbol": , "current_price": } """

@register_plugin
class StockPricePlugin(Plugin):
def call(self, stock_symbol: str):
# Fetch stock data using yfinance
stock = yf.Ticker(stock_symbol)

    # Check if stock exists and has info
    if not stock.info:
        return {"error": f"No data found for symbol: {stock_symbol}"}

    # Extract the current stock price
    current_price = stock.history(period="1d")['Close'].iloc[-1]

    # Check if current price is available
    if current_price is None:
        return {"error": f"Current price not available for symbol: {stock_symbol}"}

    # Return a dictionary containing the stock symbol and its current price
    return {
        "symbol": stock_symbol,
        "current_price": current_price
    }
Enter fullscreen mode Exit fullscreen mode

stock_price.yaml

name: stock_price
enabled: true
required: false
description: >-
The StockPricePlugin connects to the Yahoo Finance API using yfinance to fetch
real-time stock market data. It retrieves and returns the latest stock price
information based on the provided stock symbol, including the current market price.

parameters:

  • name: stock_symbol type: str required: true description: The stock symbol for which the current market price is to be retrieved.

returns:

  • name: stock_data type: dict description: >- A dictionary containing the stock symbol and its current market price. The format is: { "symbol": , "current_price": }
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player