./code/stocks/readme.txt
To be honest, I wrote this game while in some argument with my brother. I wrote it to try to prove a point to him, which, if you have siblings, you know is a good motivating factor. I'm not sure if I ever proved my point, or what my point even was, but playing this game ended up being pretty fun, and a few people have enjoyed it. 

It puts you into a comically simplified stock trading situation, where you have the stock of a single company available to you, and you start off with a bank of $10,000 dollars to spend. The price of that stock has an equal change of going up as it does of going down. 

Your goal as a player is to try to make money by "buying low and selling high" while having to pay rent every month!
The final goal is to see how much money you can earn in one year. At the end of the year, the computer will tell you how much money it was possible to have made making the optimal decisions. 

controls: 

b : buy
s : sell
enter: enter (or skip if nothing to enter)
numbers: numbers

note: you need your own python interpreter to run this. This is one of the disadvantages of python compared to javascript, in my opinion, and I plan to rewrite this in javascript so that I can make it more easily available online.
./code/stocks/colors.py
from random import randint

reset       = '\u001b[0m'
dark_blue   = '\u001b[38;2;63;94;143m'
bold        = '\u001b[1m'
clear_screen= '\033[2J'
cyan        = '\u001b[96m'
yellow      = '\u001b[93m'
green       = '\u001b[92m'
red         = '\u001b[91m'
show_cursor = "\033[?25h"
hide_cursor = "\033[?25l"
blink       = "\033[5m"
stop_blink  = "\033[25m"
magenta     = '\x1b[95m'
faint       = '\x1b[2m'

def rgb(r,g,b):
    return f'\u001b[38;2;{r};{g};{b}m'

def bg_rgb(r,g,b):
    return f'\u001b[48;2;{r};{g};{b}m'

def rand_f():
    r = randint(0,255)
    g = randint(0,255)
    b = randint(0,255)
    return f'\u001b[38;2;{r};{g};{b}m'

def rand_b():
    r = randint(0,255)
    g = randint(0,255)
    b = randint(0,255)
    return f'\u001b[48;2;{r};{g};{b}m'
    

./code/stocks/stocks.py
import random, colors, sys

class stock_ticker():
    def __init__(self, initial_price, investment):
        self.price = initial_price
        self.account = investment
        self.num_shares = 0
        self.days = 0
        self.total_days = 0
        self.month_average = 0
        self.month = []
        self.price_history = [float('inf')]

    def month_avg(self):
        self.month.append(self.num_shares*self.price)
        try:
            self.month_average = sum(self.month)/len(self.month)
        except DivisionByZeroError:
            self.month_average = 0

    def die(self):
        if self.account > 0:
            return
        print(colors.clear_screen)
        print(colors.red, 'you ran out of money.\n you died ☠')
        input()
        sys.exit(0)

    def pay_rent(self):
        print('   Pay rent:', colors.red, '$500', colors.reset)
        print('3% dividend:', colors.green,  '${0:.2f}'.format(self.month_average/33.3), colors.reset)
        input()
        print(colors.clear_screen)
        self.account += self.month_average/33.3
        self.account -= 500
        self.month = []

    def ideal_play(self):
        self.price_history.append(-float('inf'))
        month = []
        bank = 10000
        shares = 0
        for i in range(1, len(self.price_history)-1):
            price = self.price_history[i]
            month.append(shares*price)
            if price < self.price_history[i+1]:
                if price < self.price_history[i-1]:
                    shares_to_buy = bank//price
                    bank -= shares_to_buy*price
                    shares = shares_to_buy
            if price > self.price_history[i+1]:
                if price > self.price_history[i-1]:
                    bank += shares * price
                    shares = 0
            if i%30 == 0:
                bank -= 500
                bank += sum(month)/30*0.03
                month = []
        return bank + shares*self.price_history[-2]

    def start(self):
        while 1:
            self.die()
            self.month_avg()
            print(colors.clear_screen)
            #print(self.month_average)
            self.days = (self.days + 1)%30
            self.total_days += 1
            if self.total_days == 364:
                print("final day!")
            if self.total_days == 365:
                print('the year is up')
                print('you ended up with')
                print(colors.cyan + '  ', '${0:,.2f}'.format(self.account + self.num_shares*self.price), colors.green)
                print('with perfect choices,\n it could have been:\n '+ colors.cyan, '${0:,.2f}'.format(self.ideal_play()))
                input(colors.reset)
                return
            if self.days == 0:
                self.pay_rent()
                self.die()
            print(colors.green + 'Day:', colors.cyan, self.total_days, '/ 365', colors.reset)

            print('Rent due in'+colors.red, 30 - self.days, colors.reset + 'days:',colors.red +  '$500', colors.yellow)
            print('3% dividend paid monthly')
            print('   on average holding.')
            print('-'*26, colors.reset)
            change = random.gauss(0,4)

            if change > 0:
                color = colors.green
            else:
                color = colors.red
            self.price += change
            self.price_history.append(self.price)
            if self.price < 0:
                print(colors.red, 'This company went out of business, sorry ☠')
                self.account += self.num_shares*self.price
                print('You ended up with')

                print('{0:.2f}'.format(self.account))
                input('')
                return
            print('Your cash balance:', colors.cyan)
            print()
            print('      ', '${0:,.2f}'.format(self.account))
            print(colors.reset)
            print('Current price per share:', color)
            print()
            print('      ', '${0:.2f}'.format(self.price))
            print(colors.reset)
            print('You own', colors.yellow, self.num_shares, \
                    colors.reset, 'shares, worth:', colors.yellow)
            print()
            print('      ', '${0:,.2f}'.format(self.price*self.num_shares))
            print(colors.green)
            choice = input('Buy/Sell/Skip? (b/s/enter):')
            if 'b' in choice:
                try:
                    number = int(input("how many shares? "))
                except:
                    continue
                self.account -= number*self.price
                self.num_shares += number
            if 's' in choice:
                try:
                    number = int(input("how many shares? "))
                except:
                    continue
                if number > self.num_shares:
                    self.account += self.num_shares*self.price
                    self.num_shares = 0
                else:
                    self.account += number*self.price
                    self.num_shares -= number
            

s = stock_ticker(100, 10000)
s.start()