Zerbaib / CleanDiscordBot

The bot for all server.
MIT License
6 stars 2 forks source link

📈 Trade cog #10

Open Zerbaib opened 1 year ago

venanceoff commented 1 year ago

import disnake from disnake.ext import commands import yfinance as yf import matplotlib.pyplot as plt import io from difflib import get_close_matches from utils import error # Make sure you have this module or adjust the import accordingly

class TradeViewCog(commands.Cog): def init(self, bot): self.bot = bot

@commands.slash_command(name="tradeview", description="Get stock price information from Yahoo Finance.")
async def tradeview(self, ctx, choice: str):
    try:
        await ctx.response.defer()

        stock = yf.Ticker(choice)
        history = stock.history(period="1mo", interval="1d")

        if history.empty:
            # Find closest matches
            stock_symbols = fetch_stock_symbols()  # You need to implement fetch_stock_symbols function
            close_matches = get_close_matches(choice, stock_symbols)
            if close_matches:
                suggestion_list = "\n".join(close_matches[:3])  # Truncate the list if needed
                embed = disnake.Embed(
                    title="Stock Symbol Not Found",
                    description=f"Did you mean: {choice}?\n\nSuggestions:\n{suggestion_list}",
                    color=disnake.Color.red()
                )
                await ctx.send(embed=embed)
            else:
                embed = disnake.Embed(
                    title="No Data Found",
                    description=f"No data found for {choice}.",
                    color=disnake.Color.red()
                )
                await ctx.send(embed=embed)
            return

        current_price = history['Close'][-1]
        last_month_prices = history['Close']
        dates = history.index.date

        # Create a simple price chart
        plt.figure(figsize=(10, 5))
        plt.plot(dates, last_month_prices, marker='o')
        plt.xlabel('Date')
        plt.ylabel('Price')
        plt.title(f"Stock Price for {choice}")
        plt.grid(True)

        # Convert the plot to an image
        image_stream = io.BytesIO()
        plt.savefig(image_stream, format='png')
        plt.close()
        image_stream.seek(0)

        embed = disnake.Embed(
            title=f"Stock Price for {choice}",
            description=f"Current Price: {current_price:.2f} USD",
            color=disnake.Color.blurple()
        )
        embed.set_image(url="attachment://chart.png")
        embed.set_footer(text="Data from Yahoo Finance")

        msg = await ctx.send(file=disnake.File(fp=image_stream, filename="chart.png"), embed=embed)
        await ctx.message.delete(delay=5)
        await msg.delete(delay=60)

    except Exception as e:
        print(f"An error occurred: {e}")  # Print or log the error message
        embed = error.error_embed(e)
        await ctx.send(embed=embed)

def setup(bot): bot.add_cog(TradeViewCog(bot))

Zerbaib commented 1 year ago

whats change ? In github you need to explain

venanceoff commented 1 year ago

I have la flemme et je suis pas a profesionnal to the english men

Zerbaib commented 1 year ago

My error is: image image And i need this fonction to check and fetch the simbol like: BTC, AAPL, EUR, USD, ETH, and other automaticaly

Zerbaib commented 1 year ago

I have la flemme et je suis pas a profesionnal to the english men

In the world of the code you need to speak english