Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Log formatting

Hello,

Please consider allowing some formatting in the log.info message and multiple items, like this:

log.info(stock, value, price, context.portfolio.cash)

and

log.info('Stk: %2d Value: %3d Price: %4d' % (stock, value, price))

Thanks,

JM

4 responses

I've been using the Python string format method:

log.info( 'Stk: {:.2f} Value: {:.3f} Price: {:.4f}'.format(stock, value, price))  

Hello,

Great, thanks.

Can I suggest adding some examples to the documentation, so that newbies like myself pick up on that quickly?

Here are some examples:

In the initialize function

log.info(' SID-- Price-- Shares Value------- Order')  

In handle_data fuction
log.info('{:>5} {:>7.3f} {:>6} {:> 12.3f} SELL'.format(stock, price, shares, value)) or
log.info('{:>5} {:>7.3f} {:>6} {:> 12.3f} BUY'.format(stock, price, shares, value))

Best regards,

JM

Seems like an easy job for Python. Here is a quick make_message() idea for you, but I'm hoping somebody else has a better one to share, with maybe some kwargs like delim, float_format...

For those looking to format currency values for the log, I found this worked well:

     log.info('PNL: %.2f' % (context.portfolio.pnl))