Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Using a set of state variables in a user Defined CustomFactor

I am trying to develop a PNF CustomFactor that returns a BUY=1, or SELL =0 state. This translates to being able to know the previous state and having a previous price value for comparison for each stock item in the universe. Has anyone either (1)addressed this type of issue or (2) can anyone correct my thinking/problem view?

6 responses

I think only Scott can really answer if this is possible, provided you don't do the whole thing with matrix numpy operations and then just compare your [-1] signal vs your [-2] (which would be my personal recommendation anyway).

Thanks!! I will try the matrix approach to solve this particular issue. I would be interested in a solution to the general issue of having a state associated with each member of the asset universe, and then being able to use that state and current data to modify the state and make decisions based on that state.
Again, thanks for your reply.
Jim

Well you could always store the results in context and compare each time in before_trading_start...

I played with that for a little while ... just sort of enamored with the pipeline functionality. Again, thanks,
Jim

Hi Jim,

Can you elaborate a bit more on what you want your PNF factor to do? I couldn't find a definition for that term after a bit of googling.

Regarding your original question, there isn't a well-supported way to maintain/mutate state on a CustomFactor right now. If it's computationally-feasible to do so, the most straightforward way to do what you're asking within the Pipeline API would be to just recompute the previous day's value each day.

Depending on the particular transformations you're performing on the underlying data, I'd be a bit nervous about comparing Pipeline results from day N to results from day N - 1, because the underlying price/volume values are adjusted for splits/dividends/mergers from day to day, which means comparing today's computed pipeline values to yesterday's isn't always an apples-to-apples comparison.

I'd be curious to hear more about your specific use-case. I've thought a bit about how we might add a sane state-maintenance API to CustomFactors, but hearing specific use-cases helps a lot to ensure that design actually helps solve people's real problems.

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.

Scott:
Hello. I must have invented my own acronym! When I refer to PNF, I am referring to Point and Figure Charting.
It looks at a stocks price and determines whether the stock is in a BUY or SELL mode. The way it does it is by comparing the price to a moving/ratcheting comparison value. If the stock is in a BUY mode the ratcheting value is increasing based upon the most recent high. Simplistically, I might use a value that is 97% of the most recent high. If my stock price is greater than my most recent high, I change the ratchet/comparison value to 97% of that value. If my stock price is not greater than the most recent high, but is greater than the ratchet/comparison value, I do nothing.
If my stock price is less than the ratchet/comparison value, I :
1. change the stock state to SELL
2. Set a local minimum value to the stock price
3. set the ratchet/comparison value to 1.03 times the current minimum value
Then while in the SELL state, I do the following with each new price:
1. Is it less than the current minimum? If yes calculate a new ratchet/comparison value
2. Is it greater than the current ratchet/comparison value? If yes, the change state to BUY and initialize that state
3. if both 1 and 2 are false, do nothing.
To solve this problem from the pipeline functionality point view, I need to remember three things for each stock:(1) The state (2) The most recent high or low and (3) The state transition function
In thinking about it, it would be kind of neat to have a stack functionality where I could push a set of values to a stack and then pop them off when I needed them
The reason I was looking so hard at the pipeline functionality is that it takes care of resolving that the data is there for all elements in the universe.
I banged this out pretty quickly, so I hope it makes some sense.