I'm having trouble with my code and would appreciate any comments related. My problem is that I can't call elements from a list although it seems to me the list is big enough. The error is
Traceback (most recent call last):
File "BrownM.py", line 126, in <module>
optim1(pbias,x,i,nbias,stock1,cash1)
File "BrownM.py", line 88, in optim1
win1[i] = cash1[i] - cash1[i-1]
IndexError: list assignment index out of range
I don't really understand where is the problem. Maybe I'm using ".append" too much?
The code is something like:
import numpy as np
p0 = []
p0.append(0)
p0[0]=1000.00
ts=10
dx = 1.0
x = np.random.uniform(0.0,dx)
pbias= np.random.uniform(1.0, 50.0)
nbias= np.random.uniform(0.0, 1.0)
cash1 = []
cash1.append(1)
cash1[0]=100000.0
cash4 = []
cash4.append(1)
cash4[0]=100000.0
stock1=0
stock4=0
win1 = []
win4 = []
def optim1(pbias,x,i,nbias, stock1,cash1):
if p0[i] <= p0[i-1] + pbias*x :
stock1= stock1 + 1
cash1[i] = cash1[i-1] - p0[i]
elif p0[i] > p0[i-1] + pbias*x :
if stock1 > 0:
stock1 = stock1-1
cash1[i] = cash1[i-1] + p0[i]
win1[i] = cash1[i] - cash1[i-1]
def pesim1(pbias,x,i,nbias, stock4,cash4):
if p0[i] < p0[i-1] - nbias*x :
stock4= stock4 + 1
cash4[i] = cash4[i-1] - p0[i]
elif p0[i] >= p0[i-1] - pbias*x:
if stock4 > 0:
stock4 = stock4-1
cash4[i] = cash4[i-1] + p0[i]
win4[i] = cash4[i] - cash4[i-1]
for i in range(1,ts):
p0.append(i)
p0[i] = p0[i-1]
cash1.append(i)
win1.append(i)
cash4.append(i)
win4.append(i)
optim1(pbias,x,i,nbias,stock1,cash1)
pesim1(pbias,x,i,nbias,stock4,cash4)
So the code shouldn't print anything yet, but when running it throws the error.
Thanks again for any help or comments you can give me
win1 isn't big enough. In your first iteration of for i in range(1,ts)
you're accessing it's 1 position index, win1[1]
, but win1's length is only 1 thus it only has a 0 position index, win1[0]
.