Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Problem. #import Apple, Google, Microsoft stock prices data import os dirpath = os.getcwd() # print(current directory is : + dirpath) filepath = dirpath+'/Users/ignatiosdraklellis/Desktop/AAPL.csv'

Python Problem.

#import Apple, Google, Microsoft stock prices data

import os

dirpath = os.getcwd() # print("current directory is : " + dirpath)

filepath = dirpath+'/Users/ignatiosdraklellis/Desktop/AAPL.csv' # lastdate is 9/12/19, firstdate is 9/12/14,

#using os.path.join will take care of difference between

#mac/pc/platform issues how folder paths are used, backslash/forward-slash/etc

filepath = os.path.join( os.getcwd(), 'AAPL.csv')

aapl = ('AAPL','Apple Inc','9/12/14','9/12/19',filepath)

# Great! Now we can get the competitors easily

filepath = os.path.join( os.getcwd(), 'MSFT.csv')

msft = ('MSFT','Microsoft Inc','9/12/14','9/12/19',filepath)

filepath = os.path.join( os.getcwd(), 'GOOG.csv')

goog = ('GOOG','Alphabet Inc','9/12/14','9/12/19',filepath)

#%%

# Next step, create a class for a basket of stocks, called electronic trading fund ETF

#

class ETF:

"""

ETF class of a collection of Stocks in a similar/related sector

"""

def __init__(self, name, sector, firstdate, lastdate) :

self.name = name

self.sector = sector

self.firstdate = firstdate

self.lastdate = lastdate

# below can be started with empty lists, then update and compute the rest later

self.stocks = {} # a dictionary in the format { 'AAPL': aaplStockObject, 'MSFT': msftStockObject, 'GOOG': googStockObject }

self.index_eod = [] # The EFT is an index fund, which has an eod price as well, calculated from the basket of stocks.

self.index_delta1 = [] # So we also need to calculate the daily changes delta1

self.index_delta2 = []

def add_stock(self, stock):

"""

add a stock (Stock class) to the dict/list self.stocks

:param stock: a Stock class instance

"""

# check if already exist stock list

if stock.symbol in self.stocks.keys():

print("new stock symbol already exist in stock list (dict): ", stock.symbol)

return self # exit from the function

# continue below if not exist/duplicate, add it to the dictionary

self.stocks[stock.symbol] = stock

# to-be-implemented: some rules to overwrite firstdate and lastdate if the new stock has dates different from current records

# to-be-implemented: updates the daily_index_eod values

return self

def del_stock(self, stocksymbol):

"""

remove a stock (Stock class) from the list self.stocks

"""

# ###### QUESTION 7 ###### QUESTION 7 ###### QUESTION 7 ###### QUESTION 7 ######

#

# Which stock (out of the three) perform best in the last

# (i) 50 days

# (ii) 200 days (about 1 year)

# (iii) 600 days (about 3 years)

# NEW_NEW_NEW (use the nday_change_percent method that you defined)

# ###### END QUESTION 7 ###### END QUESTION 7 ###### END QUESTION 7 ###### END QUESTION 7 ######

return self

def compute_day_index(self):

"""

with daily price update from the stock stocks, need to update the etf index value as well

"""

# (nothing to do here. Just a placeholder for future projects)

# to-be-implemented:

# update index_eod

# update index_delta1

# update index_delta2

return self

def compute_day_index_list(self):

"""

with new stock added or removed, it will be needed to update the eod_index values and the derivatives.

"""

# (nothing to do here. Just a placeholder for future projects)

# to-be-implemented:

# update index_eod

# update index_delta1

# update index_delta2

return self

largeCapTech = ETF('QQQ','Large Cap Tech','9/12/14','9/12/19')

largeCapTech.add_stock(goog)

print(len(largeCapTech.stocks))

# ###### QUESTION 6 ###### QUESTION 6 ###### QUESTION 6 ###### QUESTION 6 ######

#

# continue to add msft and aapl to the largeCapTech ETF

# check the length at the end to make sure it is 3

#

largeCapTech.add_stock(aapl)

largeCapTech.add_stock(msft)

print(len(largeCapTech.stocks))

# ###### END QUESTION 6 ###### END QUESTION 6 ###### END QUESTION 6 ###### END QUESTION 6 ######

#%%

# ###### QUESTION 7 ###### QUESTION 7 ###### QUESTION 7 ###### QUESTION 7 ######

#

# Which stock (out of the three) perform best in the last

# (i) 50 days

largeCapTech.stocks['AAPL'].nday_change_percent(50)

# (ii) 200 days (about 1 year)

largeCapTech.stocks['AAPL'].nday_change_percent(200)

# (iii) 600 days (about 3 years)

largeCapTech.stocks['AAPL'].nday_change_percent(600) #best

# NEW_NEW_NEW (use the nday_change_percent method that you defined)

#

# ###### END QUESTION 7 ###### END QUESTION 7 ###### END QUESTION 7 ###### END QUESTION 7 ######

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions