Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] [Stock Market] Add New Cog #554

Open
wants to merge 42 commits into
base: V3/testing
Choose a base branch
from

Conversation

zach1502
Copy link

@zach1502 zach1502 commented May 8, 2022

Description of the changes

  • Added in a new cog.
  • This cog integrates the stock market into Ren using Yahoo Finance's api.
  • It's rough around the edges. 100% needs refractoring, and optimizing. but it works.

Also balanced all the casino games so that the expected value is roughly all the same.

Integrates the Real life stock market into Ren!
Thought adding new ways to gain/lose credits might be fun. Also, has the added benefit of teaching people how to invest and gaining experience losing money.

Unlike Squid.py, effort was put in.

Oh, and comments... yea kinda forgot about those midway so, you got this! Its fairly simple code so should be alright without it.

Couple of issues are still present in no particular order:
- Its slow, that needs to be worked on, especially for the buy and sell functions
- The output could be prettier. 
- Buy & Sell functions are kinda messy. Needs to be cleaned up.
- For the charts, the time stamps are weird if you view a stock when its currently being traded on the market

WIP/To-Do:
- Dividends (think of it as adult allowance for the financial illiterate)
- buy/sell with the full company name instead of tickers
@zach1502 zach1502 changed the title [Stock Market] Add New Cog [WIP] [Stock Market] Add New Cog May 8, 2022
Copy link
Member

@Injabie3 Injabie3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move casino changes into a new PR. It is a separate cog change and should be reviewed separately.

Copy link
Member

@Injabie3 Injabie3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did one quick scan. Please consider breaking your command coroutine definitions into smaller pieces, since they are hard to follow.

cogs/stockmarket/constants.py Outdated Show resolved Hide resolved
cogs/stockmarket/market.py Outdated Show resolved Hide resolved
cogs/casino/data.py Outdated Show resolved Hide resolved
cogs/casino/games.py Outdated Show resolved Hide resolved
cogs/stockmarket/constants.py Outdated Show resolved Hide resolved
cogs/stockmarket/market.py Outdated Show resolved Hide resolved
cogs/stockmarket/market.py Outdated Show resolved Hide resolved
cogs/stockmarket/market.py Outdated Show resolved Hide resolved
cogs/stockmarket/market.py Outdated Show resolved Hide resolved
cogs/stockmarket/market.py Outdated Show resolved Hide resolved
[AfterHours] Added Type Hints
New File(s):
- Created utils.py that contains abstracted functions for market.py

Changes to constants.py
- Constants class removed and all single quotes changed to double quotes
- Added a constant for the maximum message length and the price multiplier
- Removed some quips and adjusted punctuation

Changes to market.py:
- Cognitive complexity is lower
- most functions are now abstracted away and moved into utils.py
@zach1502 zach1502 requested a review from Injabie3 June 25, 2022 21:09
Copy link
Member

@Injabie3 Injabie3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a second pass, please see below.

Comment on lines 6 to 7
DATA_FILE = "cogs\\stockmarket\\portfolios.json"
EMBED_LOCATION = "cogs\\stockmarket\\chart.jpg"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please make these paths platform-agnostic? This would work on Windows but not Linux.

INVALID_TICKER_STR = "Invalid ticker symbol!"
INVALID_AMOUNT_STR = "Invalid amount!"
POOR_STR = "You do not have enough money to buy this stock!"
ACCOUNT_NOT_FOUND_STR = """No account found! Please create an account using `renmarket createacc`.""" # .format(get prefix somehow)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: No need for """.

import time
import datetime
import matplotlib.pyplot as plt
import yfinance as yf # pip install yfinance
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include a info.json and put this as a requirements. You can see other cogs (i.e. YOURLS) for examples.

Comment on lines 87 to 92
@market.command(name="debug")
async def market_debug(self, ctx: commands.Context):
"""Debugging command"""
# print out all the data
all_member_data = await self.config.all_members(guild=None)
print(all_member_data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be protected by some mod/admin equivalent check.

Comment on lines 157 to 158
embed_file = discord.File(EMBED_LOCATION, filename=ticker + ".jpg")
embed.set_image(url="attachment:https://image.jpg")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work if the filename doesn't match the one you're setting with set_image?

Comment on lines 15 to 16
from .constants import *
from .utils import *
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably some other places are using import *, but can we please explicitly import all attributes that you need? Makes tracing where names are coming from easier.

self,
ctx: commands.Context,
ticker: str,
amount_to_sell: int = 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to include default of 0 and let the infra print out the help string if the amount to sell isn't provided?

Comment on lines 100 to 106
user_id: discord.Member = None,
):
"""Sell a user-specified amount of a stock"""
ticker = ticker.upper()

if user_id is None:
user_id = ctx.author
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re:user_id, can we rename this to something that doesn't include ID, since this is not a user ID but rather the Member object itself.

Comment on lines 216 to 217
if user_id is None:
user_id = ctx.author
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as previous sentiment.

Comment on lines 70 to 72
# so messy...
async def build_embed(self, ctx: commands.Context, price: int, ticker: str, ticker_info: dict):
"""Builds the embed"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: does this need to be inside the class? I don't see self being used anywhere in this helper.

Messed up my local git repo so it doesn't contain the 30 or so changes. Still kinda figuring this stuff out.
Copy link
Member

@Injabie3 Injabie3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove the files at the root of the repo in your PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants