<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""
Functions for Assignment A3

This file contains the functions for the assignment.  You should replace the stubs
with your own implementations.

YOUR NAME(S) AND NETID(S) HERE
DATE COMPLETED HERE
"""
import math
import a3assets
import a3helpers

#--------------------------------------Part 1 ---------------------------------------------
def open_portfolio(to_invest,fee):
    """
    Returns: a Portfolio with cash equal to the amount invested minus
    the enrollment fee; if the enrollment fee is larger than the investment,
    the function returns None instead.

    Parameter to_invest: the amount of money to be invested in the account
    Precondition: to_invest is a non-negative float

    Parameter fee: the enrollment fee for opening a new portfolio
    Precondition: fee is a non-negative float
    """
    pass


#-------------------------------------- Part 2 --------------------------------------
def buy_bitcoin(portfolio,amount):
    """
    Returns: a bool; True if the investment was successful, False otherwise.
    Attempts to purchase `amount` Bitcoins.

    If the cost of the transaction (the value of the coins + the portfolio's
    commission fee) is greater than the amount of cash it has on hand,
    or 'amount' is equal to zero, this transaction fails. 
    Otherwise, the portfolio receives `amount` Bitcoins
    and has its cash on hand is decreased by the cost of the transaction.

    Parameter portfolio: the portfolio attempting to buy Bitcoin
    Precondition: portfolio is a Portfolio object

    Parameter amount: the number of Bitcoins to be purchased
    Precondition: amount is a positive int
    """
    pass


def sell_bitcoin(portfolio,amount):
    """
    Returns: a bool; True if the transaction was successful, False Otherwise.
    Attempts to sell `amount` Bitcoins (if the account has less than that,
    then as many as the account has).

    The profit earned from this transaction is the value of the coins
    minus the portfolio's commission fee. If selling would cause a negative
    cash balance, or 'amount' is equal to zero, this transaction fails. 
    Otherwise, the transaction is successful, the profit is added to 
    the portfolio's cash balanceand the portfolio's Bitcoin balance is decreased. 
    This transaction is not subject to taxes.

    Parameter portfolio: the portfolio attempting to sell Bitcoin
    Precondition: portfolio is a Portfolio object

    Parameter amount: the amount of Bitcoins to be sold
    Precondition: amount is a positive int
    """
    pass


#--------------------------------------Part 3 ---------------------------------------------
def take_loan(portfolio,amount,length):
    """
    Returns: a Loan object, or None if the loan is unsuccessful.

    If the portfolio's loan rate is greater than .2, the portfolio is
    considered too risky; the loan is denied and None is returned.

    Otherwise, the portfolio receives additional cash equal to the amount of
    money in the loan. A Loan object is created with a balance equal to
    the amount of money loaned plus interest according to the portfolio's loan
    rate. After this the portfolio's loan rate is
    increased by .01 to account for increased risk.

    Taxes do not have to be paid on loans and there is no commission
    fee on this transaction.

    Parameter portfolio: the portfolio requesting the loan
    Precondition: portfolio is a Portfolio object

    Parameter amount: the amount of money requested in the loan
    Precondition: amount is a non-negative float

    Parameter length: the length in months of the Loan
    Precondition: length is a positive int
    """
    pass


def pay_loan(portfolio,loan):
    """
    Returns: a bool; True if the payment was successful, False otherwise.
    Attempts to pay off the loan by the required monthly amount.

    If there is not enough money to pay off the monthly amount, the balance of
    the loan increases by its late_fee and the portfolio's balance remains the
    same. Otherwise, the loan amount decreases by the amount paid (with money
    from the portfolio) and the loan length is decreased by one. After this,
    if the length of a loan is 0, it is fully paid off and the portfolio's loan_rate is
    decreased by 0.01.

    The required monthly payment is the loan's balance / the loan's remaining length.

    Parameter portfolio: the portfolio paying off its loan
    Precondition: portfolio is a Portfolio object

    Parameter loan: the loan to be paid off
    Precondition: loan is a Loan object
    """
    pass


#-------------------------------------- Part 4 --------------------------------------
def calculate_profit_after_taxes(profit):
    """
    Returns: a float representing net profit after taxes are applied

    If the profit is negative, then there are no taxes on the profit.
    The tax rates utilized for the tax calculations are specified in
    the assignment write-up.

    Parameter profit: the amount of profit earned prior to paying taxes
    Precondition: profit is a float
    """
    pass


def buy_stock(portfolio,stock,amount_shares,short):
    """
    Returns: a Stock object, or None if the stock purchase is unsuccessful.
    Attempts to purchase `amount_shares` shares of `stock`.

    The cost of the transaction is calculated as:
    the stock's value * the number of shares + the portfolio's commission_fee

    If the cost of the transaction (the value of the coins + the portfolio's
    commission fee) is greater than the amount of cash it has on hand,
    or 'amount_shares' is equal to zero, the transaction fails. 
    Otherwise, the portfolio has its cash on hand
    decreased by the cost of the transaction and the Stock with `amount_shares`
    shares is returned.

    Parameter portfolio: the portfolio attempting to purchase a stock
    Precondition: portfolio is a Portfolio object

    Parameter stock: the stock trading symbol of the company
    Precondition: stock is a string

    Parameter amount_shares: the amount of shares to be purchased
    Precondition: amount_shares is a positive int

    Parameter short: whether or not stock is "shorted" (see later description)
    Precondition: short is a bool
    """
    pass


def pay_dividends(portfolio,stock,company,payment):
    """
    Returns: a bool; True if the transaction was successful, False otherwise.
    Attempts to pay dividends to `company`s investors.

    If the stock owned by the portfolio does not match the company paying dividends,
    this operation is unsuccessful. Otherwise, the amount of profit gained
    after paying income taxes on it is deposited into the user's portfolio.

    Parameter portfolio: the portfolio being evaluated for dividends
    Precondition: portfolio is a Portfolio object

    Parameter stock: the portfolio's owned Stock
    Precondition: stock is a Stock object

    Parameter company: the trading symbol of the company that is paying dividends
    Precondition: company is a string

    Parameter payment: the amount of money paid out for each share owned
    Precondition: payment is a non-negative float
    """
    pass


def sell_stock(portfolio,amount_shares,stock):
    """
    Returns: a bool; True if the transaction was successful, False otherwise.
    Attempts to sell `amount_shares` of `stock` (if the account has
    less than that, then as many as the account has).
    
    This function does many steps.

    First, it calculates the net income. 
    Net Income will be calculated as such, 
    If the stock was shorted, the Net Income is:
    Net-Income =`amount_shares`*(the original buy price - the current selling price) 
    Otherwise, the Net Income is:
    Net-Income =`amount_shares`*(the current selling price - the original buy price)
    
    Second, the Net-Income should be taxed.
 
    Third, a certain amount of money should be transferred back to the owner.
    We transfer back the (Post-Tax) Net-Income back into our balance and also transfer 
    back an amount equal to (`amount_shares`*the original buy price) minus a commission fee

    Fourth, we decrease the amount of shares in the stock object. 

    A transaction may only succeed if completing the transaction would not
    make the portfolio's cash on hand negative. This transaction also fails 
    if 'amount_shares' is  equal to zero. If a transaction fails, 
    there is no transfer of funds or shares. 

    Parameter portfolio: the portfolio attempting to sell stocks
    Precondition: portfolio is a Portfolio object

    amount_shares:  how many shares of the stock to sell
    Precondition: amount_shares is a positive int

    stock: the stock to be sold
    Precondition: stock is a Stock object
    """
    pass</pre></body></html>