Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
log.info() problem

Hi,

I need a little help with the log.info function. I enclosed a sample algorithm which contains the problem. It simply buys 100 Apple shares once. But in the Logs I can see just this line:

"2008-01-04handle_data:11 INFO AAPL_pos: 0"

If I didn't set the condition false than it would buy more shares and keep giving logs. But always with 1 period delay. So when I have 100 shares it shows 0, when I have 200 it shows 100 etc...
I assumed that if I write the code in the attached order then:
1.) the order function buys 100 shares
2.)AAPl_pos variable sets to 100
3.)log.info shows that I have 100 shares

What is the proper use of the function if I'd like to see my share amounts without a delay?

def initialize(context):  
    context.test=True  
    context.aapl=sid(24)  
def handle_data(context, data):  
    if context.test:  
        order(context.aapl,100)  
        AAPL_pos=context.portfolio.positions[context.aapl].amount  
        log.info('AAPL_pos: %s' % (AAPL_pos))  
        context.test=False  
7 responses

Hello David,

The Quantopian event-driven model is that your algo has knowledge of prices etc. at time t and that the results of any orders etc. only become visible at time t+1. (Things get worse with a large order for a thinly traded equity in that you could get several partial fills at t+1, t+2 etc.)

def initialize(context):  
    context.invested=False  
    context.aapl=sid(24)

def handle_data(context, data):  
    if context.invested==False:  
        order(context.aapl,100)  
        context.invested=True  
    else:  
        AAPL_pos=context.portfolio.positions[context.aapl].amount  
        log.info('AAPL_pos: %s' % (AAPL_pos))  

P.

OK, it's clear.
Thanks very much Peter.

Hi David,

Peter is correct, our simulation does not perform instantaneous order filling. Instead, orders are filled in the next "bar" processed by the backtester. You can see the open orders with get_open_orders(sid=context.aapl)
Open orders will change as soon as you order or cancel orders (see help docs). Depending on the size of your order and the volume available in the subsequent bars, your order's fill may be simulated in one or many transactions.

thanks,
fawce

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks for the help John.
An additional Q:
I tried the get_open_orders function. If I need just the 'amount' value from the return of the function how can I get it?
I googled how to use dicts in phyton and I tried this way:

AAPL_pos=get_open_orders(context.aapl)  
log.info('AAPL_pos: %s' % (AAPL_pos['amount']))  

But the return of the function is a list which contains a dict which called 'Event'. So I tried this way:

AAPL_pos=get_open_orders(context.aapl)  
log.info('AAPL_pos: %s' % (AAPL_pos[0]['amount']))

None of them is good.
I am very new in phyton so I am a little bit confused how to get simply just the 'amount' value and how to use a dict in a list.
The return of the function looks like this:

[Event({'status': 0, 'created': datetime.datetime(2008, 1, 4, 0, 0, tzinfo=), 'limit_reached': False, 'stop': None, 'stop_reached': False, 'amount': 100, 'limit': None, 'sid': Security(24, symbol=u'AAPL', security_name=u'APPLE INC', start_date=datetime.datetime(1993, 1, 4, 5, 0, tzinfo=), end_date=datetime.datetime(2013, 9, 5, 5, 0, tzinfo=), first_traded=None), 'dt': datetime.datetime(2008, 1, 4, 0, 0, tzinfo=), 'id': '58002ceef0ad426e894173cbe82c9fe4', 'filled': 0})]

David, Gus wrote some code to help track open orders. Does this one help?

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

David,

I thought a few code snippets might help. Every order in the system has a unique ID, which is returned by the order function. You can then access the order using that ID. The order is a subclass of Event, which means you can use dot notation to access properties like amount. Here is a snippet that stores the order_id and uses it on subsequent calls to log the order amount and the amount filled:

# place a single order on the first bar only.  
    if context.invested==False:  
        context.order_id = order(context.aapl, 1000000)  
        context.invested=True  

    # retrieve the order placed in the first bar  
    aapl_order = get_order(context.order_id)  
    if aapl_order:  
        # log the order amount and the amount that is filled  
        message = "Order for {amount} has {filled} shares filled."  
        message = message.format(amount=aapl_order.amount, filled=aapl_order.filled)  
        log.info(message)  

There is also a convenience method to get open orders - the idea was to help you keep track of pending orders. In practice, this is most useful if you are using limit orders, where the orders may remain open for several bars. Here is a snippet that logs all the open orders across all securities:

# retrieve all the open orders and log the total open amount  
    # for each order  
    open_orders = get_open_orders()  
    # open_orders is a dictionary keyed by sid, with values that are lists of orders.  
    if open_orders:  
        # iterate over the dictionary  
        for security, orders in open_orders.iteritems():  
            # iterate over the orders  
            for oo in orders:  
                message = "Open order for {amount} shares in {stock}"  
                message = message.format(amount=oo.amount, stock=security)  
                log.info(message)  

In the example you shared, you are only trading in one security. You can request the list of open orders for a single security. Here is a snippet that just iterates over open orders in one stock:

# retrieve all the open orders and log the total open amount  
    # for each order  
    open_aapl_orders = get_open_orders(context.aapl)  
    # open_aapl_orders is a list of order objects.  
    # iterate over the orders in aapl  
    for oo in open_aapl_orders:  
        message = "Open order for {amount} shares in {stock}"  
        message = message.format(amount=oo.amount, stock=security)  
        log.info(message)  

The interesting properties of the order object are:

  • created, a datetime in UTC timezone
  • stop, a float for the optional stop price
  • limit, a float for the optional limit price
  • amount, an int for the total shares ordered
  • sid, a security object
  • filled, an int for the total shares bought/sold for this order

Thanks for bringing up these questions!

thanks,
fawce

John,
I am glad that I found this blog. I have been having this issue for some time and am guessing that your information above is key to fixing this issue. If you notice in my backtest, around Jan 15th, I am beginning to short position. Could you help me to understand if this is essentially caused by the same question David pointed out? Also if you could offer me a tip so that I could fix this, I would greatly appreciate it. Note that I have 2 securities instead of one. Thank you
Ujae