1

I'm trying to create a program in Python, in which the user inputs a number of stock tickers and a desired date range; the output would be a graph of daily cumulative returns of the portfolio in that date range assuming the stocks have equal weights.

So far, this is my input block:

import datetime
import pandas
import numpy
import matplotlib.pyplot as plt
from matplotlib import pyplot
import csv
from pandas.io.data import DataReader

isValid = False

userInStart = raw_input("Enter Start Date mm/dd/yyyy: ") """start of date range"""
userInEnd = raw_input ("Enter End Date mm/dd/yyyy: ") """end of date range"""

StockCount = input ('Input the number of stocks in the porfolio: ')
StockArray = list() """an array of input stocks"""
Return = [[]] """an array of daily returns for each stock"""
CumulativeReturn = list() """an array of cumulative returns for each stock"""

The processing block is:

for i in range(0,StockCount): 
   for j in range(1,len(StockArray[i]["Adj Close"])):
       Return[i].append (StockArray[i]["Adj Close"][j] - StockArray[i]["Adj Close"][j-1])

1) If I input 1 stock, by the end, I am left with an array of returns but no dates. How can I relate the Return array with the Dates

2) If I input more than one stock, I am given an index out of range error. What may be causing the problem?

Any help on conceptualizing a solution would also be highly appreciated.

Thanks

user2373506
  • 45
  • 2
  • 4

1 Answers1

0

Without seeing the full listing of your program it is impossible to answer question 2. If you take a closer look at the stacktrace of the 'index out of range' exception, it should tell you the line number at which the exception is fired. Start from there.

As for question 1, You should consider applying OO design technique in your application.

For example, return can be implemented as class like this

class Return(object):
   def __init__(self, date, r):
      self.date = date
      self.r = r 

In this way you associate a date with a return value.

Anthony Kong
  • 37,791
  • 46
  • 172
  • 304