Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Value of holdings at start of month & end of month

I'm trying to establish variables equal to ...

  1. The value of my SPY holdings at the beginning of the month. I've attempted to capture this with below language, but I keep getting an error that says "TypeError: history() takes exactly 4 positional arguments (3 given)." Any ideas how to fix?

       context.portfolio.positions[context.SPY].amount * data.history(context.SPY, 'price', 30)  
    
  2. And the value of my SPY holdings at the end of the month, given (i) the price change in SPY over the course of one month and (ii) assuming the number of shares held in SPY stays constant during the month. Is this correct?

        context.SPY.amount*data.current(context.SPY, 'price')  
    
1 response

You need to add the frequency parameter (ie the 4th parameter) to the 'data.history' method. Also, the days are 'trading days' so to get a calendar's month of data a good approximation is 20 (not 30). See https://www.quantopian.com/help#api-data-history. Also, to get the single price from the series of prices you need to add an index (eg [0]). That should fix the first error.

month_ago_spy_value = context.portfolio.positions[context.SPY].amount * data.history(context.SPY, 'price', 20, '1d') [0] 

The current value of your holdings can be calculated as

current_spy_value = context.portfolio.positions[context.SPY].amount * data.current(context.SPY, 'price')

Note that this will fail with an 'index error' if you don't hold any SPY.