Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Need help with the data.current function

When using the data.current functio, i dont get a value, instead i get equity[aapl] price something then i cant use the whole thing for a logic test.
for example the following is my code
def initialize(context):
# AAPL
context.security_AAPL = [sid(24)]

def handle_data(context, data):
AAPL = data.current(context.security_AAPL, 'price')
print AAPL
print '==========================='
if AAPL == 124.79:
print 'GREAT'
else:
print 'BAD'
in the end it tells me that i have a value error, is there some way i can return just a number when using the data.current function?
This is my first time programming and my english is not good. Sorry.

3 responses

The data.current method returns a scaler (float value) when passed a single equity object. It returns a series of scalers when passed a list of equity objects (see the documentation https://www.quantopian.com/help#api-data-current)

You probably knew this but just wanted to make that clear.

Your problem is the line

context.security_AAPL = [sid(24)]  

The brackets turn the value into a list with a single security object. Therefore when data.current is passed 'context.security_AAPL', even though it's a single security, it's still interpreted as a list. Therefore the method returns a series.

Remove the brackets like below and it will work.

context.security_AAPL = sid(24)

I've attached an algorithm demonstrating this. Look at the logs to see the output.

Good luck.

Thank you so much

Does the data.current(equity,'price') return the close price? And what is the difference between price and close?