Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
I am not sure why I am unable to call the last trade price.

I am new to Quantopian and fairly new to Python. My experience if very minimal. I am having a problem with the following code.

import pandas as pd

Put any initialization logic here. The context object will be passed to

the other methods in your algorithm.

def initialize(context):
context.security = sid(24)

Will be called on every trade event for the securities you specify.

def handle_data(context, data):
exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')

if exchange_time.hour == 9 and exchange_time.minute == 35:  
    context.openPrice = data(context.security).open_price  

if exchange_time.hour == 10 and exchange_time.minute == 5:  
    context.lastPrice = data(context.security).price  

    if context.lastPrice > context.openPrice:  
        order_target_percent(context.security, 1)  
    else:  
        order_target_percent(context.security, -1)  
else:  
    return  
return  

if exchange_time.hour == 15 and exchange_time.minute == 55:  
     order_target(context.security, 0)  
3 responses

You can try this algo out. Using schedule_function is much more convenient than trying to play date match games on a timestamp.

Alternatively you could review this thread which performed at time of day correlation entry:

How-to-buy-stock-at-a-specific-time

But this results in the same problem. If call HandleEntry or HandleExit when the stock doesn't have a price point, then it still results in the same error.

Do you have a date range and a security in mind in order to test?