Answered You can hire a professional tutor to get the answer.

QUESTION

Write a function named "quantities_owned" that takes one parameter that is a string representing a filename that contains all the trades made by your...

Write a function named "quantities_owned" that takes one parameter that is a string representing a filename that contains all the trades made by your company throughout the year and returns a dictionary containing the quantity of each stock owned by the company by ticker symbol. Each row of CSV file will contain a single trade in the format "buy_or_sell,quantity,ticker,date" where buy_or_sell is either the string "buy" or "sell", quantity is an integer representing the number of shares traded, ticker is a string representing the ticker symbol being traded, and date is the date of the trade in the format YYYY-MM-DD. This function will return a dictionary with ticker symbols as keys, and the number of shares of each ticker symbol owned as values as integers. In part 3 this dictionary will be used to lookup the number of shares owned for each stock/ticker symbol

def quantities_owned(filename):

  tickers={}

  lines=[]

  with open(filename,'r') as input_file:

    lines=[line.strip() for line in input_file.readlines()]

  for line in lines:

    tokens = line.split(',')

    transaction=tokens[0]

    ticker_name = tokens[2]

    quantity = int(tokens[1])

    if transaction=='sell':

      quantity=-quantity

    if tickers.get(ticker_name)==None:

      tickers[ticker_name]=quantity

    else:

      tickers[ticker_name] = quantity+tickers.get(ticker_name)

  return tickers

Write a function named "read_prices" that takes one parameter that is a list of ticker symbols that your company owns in their portfolio. You are to read a CSV file for each of these tickers which contains the price of each stock throughout the year and return these prices in a single dictionary. The returned dictionary will contain ticker symbols as keys and dictionaries as values where the inner dictionaries will have dates as keys (as strings in the format "YYYY-MM-DD") and prices as values as floats. All said this dictionary will contain the price for any stock on any date over the past year. You may assume there will be files named "<ticker>.csv" for each ticker symbol in the input list. For example, if "TXN" is in the ticker list then a file named "TXN.csv" will be in the same directory as your code during testing and each row of this CSV file will be in the format "date,price,volume" where date is in the format YYYY-MM-DD, the price is a float, and volume is an integer representing the number of shares that were traded on that day by all traders (you will not need the trading volume for this prelab). 

import os

def read_prices(tickers):

 price_dict = {}

 for ticker in tickers:

 price_dict[ticker] = {}

 with open("./" + ticker + ".csv") as f:

  for line in f:

  values = line.strip().split(',')

  price_dict[ticker][values[0]] = values[1]

 return price_dict  

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question