Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to get prior morningstar value? (Custom Factor)

Hello everyone, I am trying to build a custom factor that if possible will always return that last morning start different to latest returned by the .latest attribute . Trying to explore some factors around delta changes. Any help would be appreciated

For example if I were to pull in net income and the MS value over the last 5 days was

1,000,000
1,000,000
1,000,000
950,000
950,000

The previous custom factor would return 950,000 where as the .lastest would give 1,000,000. Once the .latest became something other then 1,000,000 the previous custom factor would then pull the 1,000,000

6 responses

Hello,

use numpy.unique()

Hello Mathieu,

I appreciate the quick response. Sorry I should have provided a little more clarity. What you provide will work for a single asset as I noted in my first response but what I really need is to be able to apply this over X number of asset. From the API doc when working with Custom factors

*inputs are M x N numpy arrays, where M is the window_length and N is the number of securities (usually around ~8000 unless a mask is provided). *inputs are trailing data windows. Note that there will be one M x N array for each BoundColumn provided in the factor's inputs list. The data type of each array will be the dtype of the corresponding BoundColumn.

Below is the exact array (minus the dates I added) retuned by the notebook of a random fundamental I chose but staged to the exact scenario that I am trying to get to where each column corresponds to an asset over a 2 day period

1/2/2015 - [[ 60000. 133. 84400. ..., nan nan nan]

[ 60000. 133. 84400. ..., nan nan nan]
[ 60000. 133. 97000. ..., nan nan nan]
...,
[ 60000. 133. 97000. ..., nan nan nan]
[ 60000. 133. 97000. ..., nan nan nan]
[ 60000. 133. 97000. ..., nan nan nan]]

1/1/2015 - [[ 60000. 133. 84400. ..., nan nan nan]
[ 60000. 133. 97000. ..., nan nan nan]
[ 60000. 133. 97000. ..., nan nan nan]
...,
[ 60000. 133. 97000. ..., nan nan nan]

[ 60000. 133. 97000. ..., nan nan nan]
[ 60000. 133. 97000. ..., nan nan nan]]

if I were to slice this using x[0] equlivalnt to using the .latest attribute I would get below for the two day period

[ 60000. 133. 84400. ..., nan nan nan]
[ 60000. 133. 84400. ..., nan nan nan]

retrieving the day prior using x[1] give s

[ 60000. 133. 84400. ..., nan nan nan]
[ 60000. 133. 97000. ..., nan nan nan]

on 1/1/2015 you can see that the delta for the third asset went from 97000 down to 84400 indicating a 12% decline. The next day the prior day value slicing with the same action as above would pull a latest value of 84400 and a prior values of 84400 indicating no change. I am trying to get the last unique value prior to the latest for each asset returned by the Custom Factor. Which in this case would be the 97000. I would like the keep the pulling this value preserving the delta change until either my window length expired or a new value comes in an resets the delta

Hope this make more sense now

Take a look at this post https://www.quantopian.com/posts/change-in-fundamental-data-in-research . May help with what you are trying to do?

Hey Dan, nice one. Correct me if I'm wrong, but there is still an issue however, numpy unique sort the value anyhow, that's why you need to re-sort values using

z = [x[idx] for idx in sorted(idx)]  

@Mathieu Meynier

Good catch. My mistake in that original post. The numpy 'unique' method sorts the unique values (and associated indexes) before returning them. One needs to re-sort by 'index' to find the last unique value. The method you posted works. Here's another way.

values, index = np.unique(x, return_index=True)  
z = x[sorted(index)]

z will be an array of values sorted in the original order.

I'll go back and edit the post in that thread above. Again, thanks for catching that.

Much appreciated guys, this does look to return data more in line with what I am trying to see.