question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jmfernandescommented, Jan 28, 2019

hmmm, unfortunately I wasn’t able to replicate your error. This is the call I made

options_data = r.find_options_for_stock_by_expiration('spy', '2019-02-01', 'put')

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.

1reaction
jmfernandescommented, Jan 22, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do I get the error message, 'Too Many Input/Output ...
Solution: make sure the arguments listed in the function signature in your class file matches the arguments in the function you defined in...
Read more >
How To Quickly Fix The Python Input Function! - RebellionRider
A programmer's guide to Python Input Function. Learn how to take String, Integer & Float input in Python 3. Let's learn Python 3...
Read more >
Function Notation | Lesson (article) - Khan Academy
Evaluating functions algebraically and using tables. When we encounter an algebraic function, we can find the value of the function at specific inputs....
Read more >
Python Functions: How to Call & Write Functions - DataCamp
In this tutorial, you'll learn all about Python functions. Follow steps to learn how to write and call functions in Python. Find code...
Read more >
Handling common JavaScript problems - MDN Web Docs
If you want this to work correctly, you can define a function to add the handler separately, calling it on each iteration and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found