Home Implementing a for loop of share prices
Reply: 1

Implementing a for loop of share prices

J. Smith
J. Smith Published in 2018-01-11 22:43:40Z

I need to work out a value S(n) based on the equation

s(n) = (1 + r(1))(1+r(2))...(1+r(n))

The value of

S(after N days) = (1+r(after 1 day))*(1+r(after 2 days))...(1+r(after N days))

with r coming from:

r = return_daily + sig_daily

but calculated D times (see code below, D is calculated once for each day and is a random value)

I'm just not too sure how to implement the equation including a changing variable(r changes daily).

Thanks for any help

Relevant Code:

#import all modules required
import numpy as np # using different notation for easier writting
import scipy as sp 
import matplotlib.pyplot as plt

#collect variables provided by the user

period = 63 # can be edited to an input() command for variable periods.
Return_annual = np.arange(0.00,0.15,0.05) # creates an array ??? not sure if helpful
sig_annual = np.arange(0.01,0.31,0.01) #use .31 as python doesnt include the upper range value.

#functions for variables of daily return and risk.

Return_daily = (1/252)*R_annual
sig_daily = (1/(np.sqrt(252)))*sig_annual


r_i=Return_daily + sig_daily # must be calculated D times
Cristobal Aguirre
Cristobal Aguirre Reply to 2018-01-11 23:25:14Z

How do you receive your inputs?

you can receive them one by one and append them to a meta list, and then you can use a function where you pass your list of r(n) and output whatever you want.

for example if you want geometrically linked returns:

rors=[0.4, 0.5, 0.8]

#calculate compound return
def calc_ror(ror_list):
    for i in ror_list:
    return final_value

Out[35]: 2.7799999999999994

Another cool trick is using variable input arguments, in that case each argument in the function is a different return.

#calculate compound return
def calc_ror(*args):
    for i in args:
    return final_value

calc_ror(0.4, 0.5, 0.8)
Out[37]: 2.7799999999999994

Or finally using pandas

import pandas as pd

rors=[0.4, 0.5, 0.8]
df = pd.DataFrame(rors, columns=['ror'])
df_cum=pd.DataFrame((1 + df['ror']).cumprod()-1)

2  2.78

I hope one of these alternatives applies to your case

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317771 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO