1
0
mirror of https://gitlab.com/simple-stock-bots/simple-stock-bot.git synced 2025-06-15 14:56:40 +00:00

Resolve "Add Options from MarketData.app"

This commit is contained in:
Anson Biggs 2023-10-13 05:37:48 +00:00
parent 10f8460cae
commit 04abd15fcc
14 changed files with 233 additions and 57 deletions

View File

@ -9,8 +9,8 @@ Enhance your group chats on Telegram and Discord with real-time stock and crypto
## Documentation
Comprehensive documentation is available to help you understand the features and capabilities of Simple Stock Bots:
- [Official Documentation](https://docs.simplestockbot.com/)
- [Command Reference](https://docs.simplestockbot.com/commands/)
- [Official Documentation](https://simplestockbot.com/)
- [Command Reference](https://simplestockbot.com/commands/)
## Support the Project
@ -32,12 +32,12 @@ You can contribute by:
## Hosting
Self-hosting instructions are provided for those interested in running the bot on their own servers:
- [Hosting Guide](https://docs.simplestockbot.com/hosting/)
- [Hosting Guide](https://simplestockbot.com/hosting/)
## Contact
Reach out for bug reports, feature requests, or other inquiries:
- [Contact Page](https://docs.simplestockbot.com/contact/)
- [Contact Page](https://simplestockbot.com/contact/)
---

View File

@ -2,7 +2,9 @@ import datetime as dt
import logging
import os
from typing import Dict
from collections import OrderedDict
import humanize
import pandas as pd
import pytz
import requests as r
@ -286,3 +288,58 @@ class MarketData:
return df
return pd.DataFrame()
def options_reply(self, request: str) -> str:
"""Undocumented API Usage!"""
options_data = self.get(f"options/quotes/{request}")
for key in options_data.keys():
options_data[key] = options_data[key][0]
options_data["underlying"] = "$" + options_data["underlying"]
options_data["updated"] = humanize.naturaltime(dt.datetime.now() - dt.datetime.fromtimestamp(options_data["updated"]))
options_data["expiration"] = humanize.naturaltime(
dt.datetime.now() - dt.datetime.fromtimestamp(options_data["expiration"])
)
options_data["firstTraded"] = humanize.naturaltime(
dt.datetime.now() - dt.datetime.fromtimestamp(options_data["firstTraded"])
)
rename = {
"optionSymbol": "Option Symbol",
"underlying": "Underlying",
"expiration": "Expiration",
"side": "side",
"strike": "strike",
"firstTraded": "First Traded",
"updated": "Last Updated",
"bid": "bid",
"bidSize": "bidSize",
"mid": "mid",
"ask": "ask",
"askSize": "askSize",
"last": "last",
"openInterest": "Open Interest",
"volume": "Volume",
"inTheMoney": "inTheMoney",
"intrinsicValue": "Intrinsic Value",
"extrinsicValue": "Extrinsic Value",
"underlyingPrice": "Underlying Price",
"iv": "Implied Volatility",
"delta": "delta",
"gamma": "gamma",
"theta": "theta",
"vega": "vega",
"rho": "rho",
}
options_cleaned = OrderedDict()
for old, new in rename.items():
if old in options_data:
options_cleaned[new] = options_data[old]
return options_cleaned

View File

@ -1,6 +1,7 @@
import pandas as pd
import logging
import pandas as pd
class Symbol:
"""

View File

@ -1,6 +1,7 @@
requests==2.31.0
pandas==2.1.1
schedule==1.2.1
mplfinance==0.12.10b0
markdownify==0.11.6
cachetools==5.3.1
humanize==4.8.0
markdownify==0.11.6
mplfinance==0.12.10b0
pandas==2.1.1
requests==2.31.0
schedule==1.2.1

View File

@ -5,6 +5,7 @@ import datetime
import logging
import random
import re
from typing import Dict
import pandas as pd
import schedule
@ -14,8 +15,6 @@ from common.cg_Crypto import cg_Crypto
from common.MarketData import MarketData
from common.Symbol import Coin, Stock, Symbol
from typing import Dict
log = logging.getLogger(__name__)
@ -38,7 +37,7 @@ class Router:
t_copy = self.trending_count.copy()
for key in t_copy.keys():
if t_copy[key] < 0.01:
# This just makes sure were not keeping around keys that havent been called in a very long time.
# Prune Keys
dead_keys.append(key)
else:
t_copy[key] = t_copy[key] * decay
@ -48,7 +47,7 @@ class Router:
self.trending_count = t_copy.copy()
log.info("Decayed trending symbols.")
def find_symbols(self, text: str, *, trending_weight: int = 1) -> list[Stock | Symbol]:
def find_symbols(self, text: str, *, trending_weight: int = 1) -> list[Stock | Coin]:
"""Finds stock tickers starting with a dollar sign, and cryptocurrencies with two dollar signs
in a blob of text and returns them in a list.
@ -66,6 +65,8 @@ class Router:
symbols: list[Symbol] = []
stock_matches = set(re.findall(self.STOCK_REGEX, text))
coin_matches = set(re.findall(self.CRYPTO_REGEX, text))
for stock_match in stock_matches:
# Market data lacks tools to check if a symbol is valid.
if stock_info := self.stock.symbol_id(stock_match):
@ -73,11 +74,10 @@ class Router:
else:
log.info(f"{stock_match} is not in list of stocks")
coins = set(re.findall(self.CRYPTO_REGEX, text))
for coin in coins:
sym = self.crypto.symbol_list[self.crypto.symbol_list["symbol"].str.fullmatch(coin.lower(), case=False)]
for coin_match in coin_matches:
sym = self.crypto.symbol_list[self.crypto.symbol_list["symbol"].str.fullmatch(coin_match.lower(), case=False)]
if sym.empty:
log.info(f"{coin} is not in list of coins")
log.info(f"{coin_match} is not in list of coins")
else:
symbols.append(Coin(sym))
if symbols:
@ -396,3 +396,12 @@ class Router:
replies = replies + self.crypto.batch_price(coins)
return replies
def options(self, request: str, symbols: list[Symbol]) -> Dict:
request = request.lower()
if len(symbols) == 1:
symbol = symbols[0]
request = request.replace(symbol.tag.lower(), symbol.symbol.lower())
return self.stock.options_reply(request)
else:
return self.stock.options_reply(request)

View File

@ -8,4 +8,5 @@ pylama==8.4.1
mypy==1.5.1
types-cachetools==5.3.0.6
types-pytz==2023.3.1.1
ruff==0.0.292
ruff==0.0.292
isort==5.12.0

View File

@ -21,7 +21,7 @@ For stock data or hosting your own bot, use my link. This helps keep the bot fre
**Updates**: Join the bot's discord: https://t.me/simplestockbotnews.
**Documentation**: All details about the bot are at [docs](https://docs.simplestockbot.com).
**Documentation**: All details about the bot are at [docs](https://simplestockbot.com).
The bot reads _"Symbols"_. Use `$` for stock tickers and `$$` for cryptocurrencies. For example:
- `/chart $$eth` gives Ethereum's monthly chart.
@ -41,7 +41,7 @@ Type @SimpleStockBot `[search]` anywhere to find and get stock/crypto prices. No
Data from: [marketdata.app](https://dashboard.marketdata.app/marketdata/aff/go/misterbiggs?keyword=discord).
Issues with the bot? Use `/status` or [contact us](https://docs.simplestockbot.com/contact).
Issues with the bot? Use `/status` or [contact us](https://simplestockbot.com/contact).
"""
donate_text = """
@ -55,5 +55,5 @@ Every donation supports server costs and
2. Or, donate at [buymeacoffee](https://www.buymeacoffee.com/Anson).
- It's quick, doesn't need an account, and accepts Paypal or Credit card.
Questions? Visit our [website](https://docs.simplestockbot.com).
Questions? Visit our [website](https://simplestockbot.com).
"""

View File

@ -5,9 +5,9 @@ import os
import mplfinance as mpf
import nextcord
from D_info import D_info
from nextcord.ext import commands
from D_info import D_info
from common.symbol_router import Router
DISCORD_TOKEN = os.environ["DISCORD"]
@ -38,7 +38,7 @@ async def on_ready():
@bot.command()
async def status(ctx: commands):
"""Debug command for diagnosing if the bot is experiencing any issues."""
logging.warning(f"Status command ran by {ctx.message.author}")
logging.info(f"Status command ran by {ctx.message.author}")
message = ""
try:
message = "Contact MisterBiggs#0465 if you need help.\n"
@ -183,20 +183,74 @@ async def trending(ctx: commands):
@bot.event
async def on_message(message):
# Ignore messages from the bot itself
if message.author.id == bot.user.id:
return
if message.content:
if message.content[0] == "/":
await bot.process_commands(message)
return
if "$" in message.content:
symbols = s.find_symbols(message.content)
content_lower = message.content.lower()
if symbols:
for reply in s.price_reply(symbols):
await message.channel.send(reply)
return
# Process commands starting with "/"
if message.content.startswith("/"):
await bot.process_commands(message)
return
symbols = None
if "$" in message.content:
symbols = s.find_symbols(message.content)
if "call" in content_lower or "put" in content_lower:
await handle_options(message, symbols)
return
if symbols:
for reply in s.price_reply(symbols):
await message.channel.send(reply)
return
async def handle_options(message, symbols):
logging.info("Options detected")
try:
options_data = s.options(message.content.lower(), symbols)
# Create the embed directly within the function
embed = nextcord.Embed(title=options_data["Option Symbol"], description=options_data["Underlying"], color=0x3498DB)
# Key details
details = (
f"Expiration: {options_data['Expiration']}\n" f"Side: {options_data['side']}\n" f"Strike: {options_data['strike']}"
)
embed.add_field(name="Details", value=details, inline=False)
# Pricing info
pricing_info = (
f"Bid: {options_data['bid']} (Size: {options_data['bidSize']})\n"
f"Mid: {options_data['mid']}\n"
f"Ask: {options_data['ask']} (Size: {options_data['askSize']})\n"
f"Last: {options_data['last']}"
)
embed.add_field(name="Pricing", value=pricing_info, inline=False)
# Volume and open interest
volume_info = f"Open Interest: {options_data['Open Interest']}\n" f"Volume: {options_data['Volume']}"
embed.add_field(name="Activity", value=volume_info, inline=False)
# Greeks
greeks_info = (
f"IV: {options_data['Implied Volatility']}\n"
f"Delta: {options_data['delta']}\n"
f"Gamma: {options_data['gamma']}\n"
f"Theta: {options_data['theta']}\n"
f"Vega: {options_data['vega']}\n"
f"Rho: {options_data['rho']}"
)
embed.add_field(name="Greeks", value=greeks_info, inline=False)
# Send the created embed
await message.channel.send(embed=embed)
except KeyError as ex:
logging.warning(f"KeyError processing options for message {message.content}: {ex}")
bot.run(DISCORD_TOKEN)

View File

@ -51,11 +51,11 @@ Here are some simple commands to get you started:
Simple Stock Bot is a community-supported project, thriving on the contributions from its users. It's sustained entirely through donations to cover server costs and premium market data subscriptions, ensuring it remains free for everyone.
Feeling generous? You can support the project by [donating](https://docs.simplestockbot.com/donate/), following on [Twitter](https://twitter.com/AnsonBiggs), or contributing on [GitLab](https://gitlab.com/simple-stock-bot).
Feeling generous? You can support the project by [donating](https://simplestockbot.com/donate/), following on [Twitter](https://twitter.com/AnsonBiggs), or contributing on [GitLab](https://gitlab.com/simple-stock-bot).
## Dive Deeper
Craving more insights and features? Explore the [official documentation](https://docs.simplestockbot.com/) to uncover all the capabilities of Simple Stock Bot.
Craving more insights and features? Explore the [official documentation](https://simplestockbot.com/) to uncover all the capabilities of Simple Stock Bot.
Get ready to elevate your financial discussions with Simple Stock Bot! Your group chats will never be the same again.

View File

@ -5,13 +5,12 @@ Symbols are used in headings to denote what platforms and symbol types a command
- Bot Commands :robot:
- Cryptocurrency Support :material-currency-btc:
- Stock Market Support :bank:
- OTC Support :dollar:
## Get the Bots
[:fontawesome-brands-telegram: Telegram](https://t.me/SimpleStockBot){ .md-button } [:fontawesome-brands-discord: Discord](https://discordapp.com/api/oauth2/authorize?client_id=532045200823025666&permissions=36507338752&scope=bot){ .md-button }
## Symbol Detection :material-currency-btc: :bank: :dollar:
## Symbol Detection :material-currency-btc: :bank:
The Simple Stock Bot looks at every message it can see and tries to detect stock and cryptocurrency symbols. Stock market tickers are denoted with a single `$` and cryptocurrency coins are denoted with a double `$$`. So getting the price of Tesla is as simple as `$tsla` and Bitcoin `$$btc`. These symbols can be in any part of a message and there can be multiple of them aswell.
@ -29,6 +28,13 @@ The Simple Stock Bot looks at every message it can see and tries to detect stock
</div>
</div>
## Options Detection :bank:
This command allows you to query real-time data for stock options. By simply inputting the stock symbol, strike price, month, and specifying either a call or a put, you can get the latest options data right at your fingertips. For example, `AAPL $220 December call` will provide the current data for Apple's call option with a $220 strike price expiring in December.
![Image of the telegram bot providing options info.](img/telegram_options.png)
## `/donate [Amount in USD]` :fontawesome-brands-telegram-plane:
The donate command is used to send money to the bot to help keep it free. The premium stock market data and server rentals add up so any amount helps. See the [Donate](donate.md) page for more information.
@ -171,7 +177,7 @@ Bot Status:
</div>
## Inline Features :fontawesome-brands-telegram: :material-currency-btc: :bank: :dollar:
## Inline Features :fontawesome-brands-telegram: :material-currency-btc: :bank:
You can type @SimpleStockBot `[search]` in any chat or direct message to search for the stock bots
full list of stock symbols and return the price of the ticker. Then once you select the ticker

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -21,7 +21,7 @@ Want stock data or to host your own bot? Help keep this bot free by using my
📢 Stay updated on the bot's Telegram: https://t.me/simplestockbotnews.
**Guide**: All about using and setting up the bot is in the [docs](https://docs.simplestockbot.com).
**Guide**: All about using and setting up the bot is in the [docs](https://simplestockbot.com).
The bot recognizes _"Symbols"_. `$` for stocks and `$$` for cryptos. Example:
- `/chart $$eth` gets a month's Ethereum chart.
@ -43,7 +43,7 @@ Pick a ticker, and the bot shares the current price in chat. Note: Prices can la
Data thanks to [marketdata.app](https://dashboard.marketdata.app/marketdata/aff/go/misterbiggs?keyword=telegram).
Bot issues? Use `/status` or [contact us](https://docs.simplestockbot.com/contact).
Bot issues? Use `/status` or [contact us](https://simplestockbot.com/contact).
"""
@ -56,7 +56,7 @@ All funds help maintain servers, with data from
1. Use `/donate [amount in USD]`. E.g., `/donate 2` donates 2 USD.
2. Or, quickly donate at [buymeacoffee](https://www.buymeacoffee.com/Anson). No account needed, accepts Paypal & Credit card.
For questions, visit our [website](https://docs.simplestockbot.com).
For questions, visit our [website](https://simplestockbot.com).
"""

View File

@ -178,17 +178,33 @@ async def symbol_detect(update: Update, context: ContextTypes.DEFAULT_TYPE):
message = update.message.text
chat_id = update.message.chat_id
if "$" in message:
symbols = s.find_symbols(message)
log.info("Looking for Symbols")
symbols = s.find_symbols(message)
else:
return
except AttributeError as ex:
log.info(ex)
return
if symbols:
# Let user know bot is working
# Detect Options
if ("call" in message.lower()) or ("put" in message.lower()):
log.info("Options detected")
await context.bot.send_chat_action(chat_id=chat_id, action=telegram.constants.ChatAction.TYPING)
try:
options_data = s.options(message, symbols)
await update.message.reply_text(
text=generate_options_reply(options_data),
parse_mode=telegram.constants.ParseMode.MARKDOWN,
)
return
except KeyError as ex:
logging.warning(ex)
pass
if symbols:
log.info(f"Symbols found: {symbols}")
await context.bot.send_chat_action(chat_id=chat_id, action=telegram.constants.ChatAction.TYPING)
for reply in s.price_reply(symbols):
await update.message.reply_text(
@ -198,6 +214,47 @@ async def symbol_detect(update: Update, context: ContextTypes.DEFAULT_TYPE):
)
def generate_options_reply(options_data: dict):
# Header with Option Symbol and Underlying
message_text = f"*{options_data['Option Symbol']} ({options_data['Underlying']})*\n\n"
# Key details
details = (
f"*Expiration:* `{options_data['Expiration']}`\n"
f"*Side:* `{options_data['side']}`\n"
f"*Strike:* `{options_data['strike']}`\n"
f"*First Traded:* `{options_data['First Traded']}`\n"
f"*Last Updated:* `{options_data['Last Updated']}`\n\n"
)
message_text += details
# Pricing info
pricing_info = (
f"*Bid:* `{options_data['bid']}` (Size: `{options_data['bidSize']}`)\n"
f"*Mid:* `{options_data['mid']}`\n"
f"*Ask:* `{options_data['ask']}` (Size: `{options_data['askSize']}`)\n"
f"*Last:* `{options_data['last']}`\n\n"
)
message_text += pricing_info
# Volume and open interest
volume_info = f"*Open Interest:* `{options_data['Open Interest']}`\n" f"*Volume:* `{options_data['Volume']}`\n\n"
message_text += volume_info
# Greeks
greeks_info = (
f"*IV:* `{options_data['Implied Volatility']}`\n"
f"*Delta:* `{options_data['delta']}`\n"
f"*Gamma:* `{options_data['gamma']}`\n"
f"*Theta:* `{options_data['theta']}`\n"
f"*Vega:* `{options_data['vega']}`\n"
f"*Rho:* `{options_data['rho']}`\n"
)
message_text += greeks_info
return message_text
async def intra(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""returns a chart of intraday data for a symbol"""
log.info(f"Intra command ran by {update.message.chat.username}")

View File

@ -1,19 +1,9 @@
import keyboard
import time
import keyboard
tests = """$$xno
/info $tsla
/info $$btc
/news $tsla
/news $$btc
/stat $tsla
/stat $$btc
/cap $tsla
/cap $$btc
/dividend $tsla
/dividend $msft
/dividend $$btc
$tsla
/intra $tsla
/intra $$btc
/chart $tsla