Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Logs not getting generated (or getting lost)

In some of my algorithms, logs don't get generated even though I am sure the log code gets executed.

I have created a simple example where it happens.
The algo is pretty simple, (in daily mode), I just buy one day and sell the next day and so on

My log code looks like this

        stock_order = get_order(context.order_id)  
        if stock_order:  
            message = ',buy,price={price},amount={amount}'  
            message = message.format(amount=stock_order.amount, price=data[context.stock].price)  
            log.info(message)  
            record(BUY=data[context.stock].price)  
        return

If I run it from 11/05/2003 to 11/28/2003, even though buy and sell happens, I don't get the corresponding logs. If I run it over longer periods, logs do get generated, so I am wondering if the log buffer is getting lost or something,

One reason it might not be generating logs is if the stock_order doesn't get filled. If that is the case, then the record() shouldn't get generated either, but it does

If the order gets filled after a delay (due to the function being asynchronous), should add a sleep() before seeing if stock_order is valid? I remember seeing it in some algo, but most sample algos don't seem to have it.

Thanks!
Ajay

PS: Ignore the INTC v/s AAPL flap in the code.

1 response

Hi Ajay,

You're not receiving any logs because of your small error in lines 17 and 28, where you say

context.order_id = order(context.stock, context.portfolio.cash)  

I believe that your intent is to use your entire portfolio cash to purchase a single stock? In that case you should use the order_target_percent method:
context.order_id = order_target_percent(context.stock, +1)

The order_target_percent() method is more robust than the order() method. If you really want to use the order method then you can do something like this:

order_size = int((context.portfolio.cash)/(data[context.stock].price))  
order(context.stock, +order_size)  

However, this is not a great alternative (I thought it would be worth showing another example). It will buy the maximum number of shares only once with your cash. Then the portfolio will have almost no cash and X amount of stock. It will not buy or sell any additional positions.

Try using the order_target_percent in your algo, that should work!

Cheers,
Alisa

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.