Issue with newest example - trying to use a different function, but need help with proper inputs
See original GitHub issue`import robin_stocks as r import os import datetime import time as t
‘’’ This is an example script that will print out options data every 10 seconds for 1 minute. It also saves the data to a txt file. The txt file is saved in the same directory as this code. ‘’’
#!!! Fill out username and password username = ‘’ password = ‘’ #!!!
login = r.login(username,password)
#!!! fill out the specific option information strike = 150.0 date = “2019-06-21” stock = “V” optionType = “call” #or “put” #!!!
File saving variables
minutesToTrack = 1 #in minutes PrintInterval = 10 #in seconds endTime = t.time() + 60 * minutesToTrack fileName = “options.txt” writeType = “w” #or enter “a” to have it continuously append every time script is run
os.chdir(os.path.dirname(file)) path = os.getcwd() filename = os.path.join(path,fileName) fileStream = open(filename,mode=writeType)
while t.time() < endTime: time = str(datetime.datetime.now()) #Both write and print the data so that you can view it as it runs. fileStream.write(“\n”) fileStream.write(time) print(time) #Get the data instrument_Data = r.get_option_instrument_data(stock,date,strike,optionType) market_Data = r.get_option_market_data(stock,date,strike,optionType)
fileStream.write("\n")
fileStream.write("{} Instrument Data {}".format("="*30,"="*30))
print("{} Instrument Data {}".format("="*30,"="*30))
#Instrument_Data is a dictionary, and the key/value pairs can be accessed with .items()
for key, value in instrument_Data.items():
fileStream.write("\n")
fileStream.write("key: {:<25} value: {}".format(key,value))
print("key: {:<25} value: {}".format(key,value))
fileStream.write("\n")
fileStream.write("{} Market Data {}".format("="*30,"="*30))
print("{} Market Data {}".format("="*30,"="*30))
for key, value in market_Data.items():
fileStream.write("\n")
fileStream.write("key: {:<25} value: {}".format(key,value))
print("key: {:<25} value: {}".format(key,value))
t.sleep(PrintInterval)
#make sure to close the file stream when you are done with it. fileStream.close()`
I am getting an error while trying to use a different function for options data.
I get this error:
Traceback (most recent call last): File “C:\Users\Username\Desktop\Python\Version 2.1.py”, line 63, in <module> for key, value in OptData.item(): AttributeError: ‘list’ object has no attribute ‘item’
I am using the function, '#Get the data OptData = r.find_options_for_stock_by_expiration(symbol, expirationDate, optionType, info)
fileStream.write('\n')
fileStream.write("{} OptData".format("="*30,"="*30))
print("{} OptData {}".format("="*30,"="*30))
#OptData is a dictionary, and the key/value pairs can be accessed with .items()
## for key, value in OptData.items():
for key, value in OptData.items():
item(['chain_symbol'])'
when I try to print it though, I’m missing something in the code and it is giving me this error. Some direction would be greatly appreciated. I tried to just replace what you had input with what I’m trying to get, the line I get the error on is: item([‘chain_symbol’])’
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (3 by maintainers)
Top GitHub Comments
hmmm, unfortunately I wasn’t able to replicate your error. This is the call I made
and it worked for me with no errors. I ran it with ‘both’ at the end too, and it worked.I suggest double checking all your variables.
read through this page to get more info on the difference between lists and dicts and how to access elements. section 5.6 goes over the looping techniques I use.
https://docs.python.org/3/tutorial/datastructures.html
There is also this page on how .items() works
http://www.tutorialspoint.com/python/dictionary_items.htm
but basically, if you want one specific dictionary value for a given key, you do dictionaryName[‘keyName’] and it returns what the value is. If you have a list of dictionaries then you probably want to loop over every element in the list in order to access the value In each dictionary. Alternatively, instead of getting a single value for a specific key name, you can loop over every key/value pair in a dictionary using .items(). It just depends if you want all values or just one, and whether you have direct reference to a dictionary or if that dictionary is stored in a list. Its also possible to have a list of lists or a dictionary that contains dictionaries, but there isn’t a lot of that going on in the code.