nickmccullum / algorithmic-trading-python

The repository for freeCodeCamp's YouTube course, Algorithmic Trading in Python
2.35k stars 2.37k forks source link

TypeError: list indices must be integers or slices, not str #68

Closed FaceFwd closed 3 months ago

FaceFwd commented 3 months ago

Does anyone know how to resolve this issue on "001_equal_weight_S&P_500": Screenshot 2024-03-24 at 21 40 31

I'm a bit stuck. I have tried to set the parameter to the following by adding curly brackets {}:

price = data[{'latestPrice'}]

No luck there either.

Any help would be much appreciated.

Thanks!

FaceFwd commented 3 months ago

HI Everyone,

For all the newbies out there who might come across this issue, I found the solution to my problem. Essentially, I had been treating lists as dictionaries:

Screenshot 2024-03-25 at 00 41 03

If you note at the very beginning and end of the data, there are curly brackets inside of brackets: [{...}]. As I understand it, this denotes the information within these two types of brackets as "list of dictionaries" that must be converted into integer form so python can recognize it. Here is the link that helped me devise the solution:

https://www.learndatasci.com/solutions/python-typeerror-list-indices-must-be-integers-or-slices-not-str/

The solution I was able to derive is the following:

for i in range(len(data)): print(f'{data[i]["latestPrice"]}')

I hope this helps any future wondering souls who come across this issue!

Good luck and have fun everyone!