Skip to content

Instantly share code, notes, and snippets.

@phanhaihiep
Created August 6, 2020 20:35
Show Gist options
  • Save phanhaihiep/68f9ffd9a91c872e53251a4e427d6803 to your computer and use it in GitHub Desktop.
Save phanhaihiep/68f9ffd9a91c872e53251a4e427d6803 to your computer and use it in GitHub Desktop.
from utils import *
def display_as_percentage(val):
return '{:.1f}%'.format(val * 100)
amazon_prices = [1699.8, 1777.44, 2012.71, 2003.0, 1598.01, 1690.17, 1501.97, 1718.73, 1639.83, 1780.75, 1926.52, 1775.07, 1893.63]
ebay_prices = [35.98, 33.2, 34.35, 32.77, 28.81, 29.62, 27.86, 33.39, 37.01, 37.0, 38.6, 35.93, 39.5]
# Write code here
my_value = 0.075
print('The value formatted as a percentage is', display_as_percentage(my_value))
# calculate the monthly returns of the 2 stocks
def get_returns(prices):
returns=[]
for i in range(len(prices)-1):
start_price = prices[i]
end_price = prices[i+1]
log_return = calculate_log_return(start_price, end_price)
returns.append(log_return)
return returns
amazon_returns = get_returns(amazon_prices)
ebay_returns = get_returns(ebay_prices)
print("the monthly returns of Amazon is " + display_as_percentage(amazon_returns[0]) )
print("the monthly returns of Ebay is " + display_as_percentage(ebay_returns[0]) )
# calculate the annual returns of the 2 stocks
annual_rate_month_amazon = sum(amazon_returns)
print("the annual returns of Amazon is " + display_as_percentage(annual_rate_month_amazon))
annual_rate_month_ebay = sum(ebay_returns)
print("the annual returns of Ebay is " + display_as_percentage(annual_rate_month_ebay))
amazon_variance=calculate_variance(amazon_prices)
print("the variance of Amazon monthly returns " + str(round(amazon_variance,2)))
ebay_variance=calculate_variance(ebay_prices)
print("the variance of Ebay monthly returns " + str(round(ebay_variance,2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment