# Calculating finacial statics/Analyze stock data

Hi, im struggling with exercise ‘calculating financial statics’ in course ‘How to analyze financial data with python’.
Here is my code;

``````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
def get_returns(prices):
returns=[]
#step 3. start at 0, ends with n-1
for i in range(0,len(prices)):
#step 4. position i is start price
start_price = prices[i]
#step 4. i+1 will be the end price
end_price = prices [i]
returns = calculate_log_return(start_price,end_price)
return returns

#step 5. monthly log rates of return
#amazon
amazon_returns = display_as_percentage(get_returns(amazon_prices))

#ebay
ebay_returns = display_as_percentage(get_returns(ebay_prices))

print(amazon_returns)
print(ebay_returns)

#Step 6. print out list of monthly returns.
#amazon
amazon_returns=[display_as_percentage(i) for i in amazon_prices]
print(amazon_returns)

#ebay
ebay_returns=[display_as_percentage(i) for i in ebay_prices]
print(ebay_returns)

#step 8. calculate variance of each stocks monthly returns.
#amazon
amazon_variance=calculate_variance(amazon_prices)
print(amazon_variance)

#ebay
ebay_variance=calculate_variance(ebay_prices)
print(ebay_variance)

#Calculate standard deviation
#amazon
amazon_stddev=calculate_stddev(amazon_prices)
print(display_as_percentage(amazon_stddev))

#ebay
ebay_stddev=calculate_stddev(ebay_prices)
print(display_as_percentage(ebay_stddev))

#step 10. calculate the correlation
am_eb_corr=calculate_correlation(amazon_prices, ebay_prices)
print(am_eb_corr)
``````

I do not get the correct result!
In function get_returns(), when i declare end_price to [i+1] o get error - List index out of range. What does that mean? even hint is telling me to write like that?!

And in the for loop, ‘for i in range(0,len(prices)):’
I should declear it to start at 0?!
and it should end at the second last element? prices-1? i get error when i write like that.
What is wrong?

I would love some help with this!
Thank you! =)

Hi @ajax9143129979.
List index out of range is warning you that you’re attempting to access an index which the list simply doesn’t have. In your case you’ve gone beyond the length of your list.
If you’re using range over the length of your list as a method of indexing then starting at 0 starts at the first element of the list.
`Prices - 1` may well throw an error, but you’re interested in the length of prices. Perhaps adjust your code to consider this.

1 Like

Hi,

Aaah, ofc! Thank you!

BR,
Jenny =)