0

I am trying to create a function that calculates specific daily returns (i.e. 1,2,5,10 and 30-day returns). I would like to create a different list that has the appropriate name (i.e. one_day_return_list) which contains the returns for that given period.

def calculate_returns(trading_day):

    for i in [1,2,5,10,30]:
        i_day_return = (closing_prices[(trading_day + i)] - closing_prices[(trading_day)]) / closing_prices[(trading_day)
        i_day_return_list.append(i_day_return_return)

where during the first iteration , i_day_return should result in one_day_return and i_day_return_list should result in one_day_return_list. Any clue how I can turn the above pseudo code into an actual code ?

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
DK1
  • 3
  • 2

1 Answers1

1

Typically you would not "name" the elements of the series as you are trying to do, but rather "number" them. This is a good use case for a dict. Like this:

def calculate_returns(trading_day):
    returns = {}
    for i in (1,2,5,10,30):
        returns[i] = random.random()

    return returns

The result of this will be a mapping from time horizon to return.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I am trying to calculate the 1,2,5,10 and 30 day returns (after a specific set-up) of all S&P500 constituents. Thus, I want to calculate each of the period returns for each constituent and then add it to the specific list (i.e. one_day_return_list) Then I can calculate the average returns by summing all returns in the lists and dividing by their lengths. Can this be achieved by either using a list? or a dictionary (and then working with either its keys or values)? – DK1 May 08 '16 at 13:17