Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
What it is the easiest way to get daily Profit/Loss for portfolio ?

Hi quants,

Before I start coding is there any easy way to track during the day the daily profit/loss of the portfolio without myself keep track in the context? Just checking if anyone has implemented something like that for their daily trading.

Thanks
EG

8 responses

Hello Erick,

Do you just need the overall daily portfolio P/L, or does it need to be broken down across all of your securities & cash (e.g at noon, how much have I made on AAPL since the start of the trading day)?

Grant

Hey Grant, Only need the total daily P/L of the account

Thanks
EG

I guess I'd look at just tracking it in context. You should be able to use a schedule_function that grabs whatever you need at the open as a baseline, and then just update every minute throughout the day. If you can't get it to work, just let me know, and I'll give it a shot. --Grant

And, how to track Profit and Loss for each position since the day it has been Longed/Shorted?
I dit this. Is there a Quantopian method?

sid_profit = (context.portfolio.positions[sid].last_sale_price - context.portfolio.positions[sid].cost_basis)*np.sign(context.portfolio.positions[sid].amount)  

There's context.portfolio.pnl built-in, and then for individual securities you might be able to extract the relevant elements out of this where I added a few lines:

        who  = 'pnl_' + sym  
        what = int(context.books[sym]['balance'])  
        record( **{ who: what } )  

It is from here, could use some work, and also doesn't handle shorting yet. In my ponderings over modifying it to do so, I figure maybe a starting point would be to think of both buying and shorting in terms of their risk, exchange money for stock or stock for money, negative shares are like money spent, sort of. Anyway, some idea basics, even if not perfect yet.

@Marco No Q-built-in pnl per stock. Here's a method without numpy for pnl long or short: https://www.quantopian.com/posts/pnl

@Blue

Link is wrong?

If I get you right

        # ToDo: Don't know the official actual fill prices.  
        transaction         = filled * b[sym]['price']  # Last known price.  

Is far from being accurate. An order may be filled throughout the day, over a wide range of prices.
Anyone found a solution for that?

Right. I was able to fix the pnl URL just now by changing the title so it should work now.
Some other things related: https://www.quantopian.com/posts/long-and-short-values-counts-and-sids-lists-plus-other-useful-items

Your other note is a about run summary (link in Source). Mentioned here recently. I did some work on it last night. Pretty tough, the kind of thing that normally would be tackled by a team, I'm all alone in these things and would appreciate if someone wants to roll up their sleeves and contribute. You just became the test team, congratulations. :) I kid.