Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Opt porfolio question as to how to keep retain hold shares

I finally have my first working (sort of) portfolio, and trying to start off using the opt portfolio rather than some other way..
[i am new to python, but old to programming - which i started back in the 1980s... and am coming along very fast and adapting: YAY!]

First, i cant tell what is going on in the portfolio (yet)
Second, I based my portfolio code on this thread: How to conditionally Long/Short securities in IDE using Optimize API

Basically i created my factors, and returned a pipeline from the base universe like below
Longs has either true or false as does shorts, based on what calculations I created and viewed in notebook
Given that I just want to see it perform i made all the long_alpha +1 and short_alpha -1 to insure the opt portfolio would short the shorts, etc

 Pipeline(  
        columns={  
            'longs' : longs,  
            'long_alpha' : long_alpha,  
            'shorts' : shorts,  
            'short_alpha' : short_alpha,  
        },  
        screen=base_universe & longs | shorts  
    )  

All the above is as intended.

As in the example thread, I then used this code (with the idea i can modify it as i learn):

long_alphas = context.output.query('Longs').Long_Alpha
short_alphas = context.output.query('Shorts').Short_Alpha

all_alphas = pd.concat([long_alphas, short_alphas])

alpha_objective = opt.MaximizeAlpha(all_alphas)
max_exposure_1 = opt.MaxGrossExposure(1.0)
max_position_size = opt.PositionConcentration.with_equal_bounds(-.05, .05)
dollar_neutral = opt.DollarNeutral()
order_optimal_portfolio(objective = alpha_objective, constraints = [max_exposure_1, max_position_size, dollar_neutral])

(i did try to put the above in a code box, but for some reason, no matter how I tried the editor would not cooperate)

I have back tested this concept on a lesser system, and when i back test this here, it does not perform as it should as i know it should
The problem, I think, is that each day the portfolio is renewing, and that is not the behavior that i would like.

What i would like is:
What is already in the portfolio stays in the portfolio once its bought
If a stock is long, and is in the new short list, it should be sold and then shorted
If a stock is short, and is in the new long list, it should be bought, then go long
A way to know whats in the portfolio so that i can tell it to be removed when needed

I am working on what my criteria for removal from the portfolio should be, and will be added later
(as is adjusting the input selection to shrink the selection pool)

I am also working on criteria that would shrink the stocks selected for inclusion

The only code missing (above) from my application is my code that selects true and false for longs and shorts which works fine and was checked in notebooks

I remember reading a thread on the forum that mentions a freeze, but was not able to find it again to refer to it

Thanks in advance

3 responses

A couple of questions...

Question 1. Can the size of existing positions be changed? Presumably, the very first time the algo opens positions, the total weighting of the positions will be about 1.0. So, the next day, you want to keep anything in the portfolio which is already there. Assume the current positions are not in either the new long or short list. Should the algo adjust the shares of existing positions? If you are trying to keep the gross exposure below 1, there won't be any 'room' to open any new positions and the existing share will need to be reduced to 'make room'.

Question 2. Assuming the size of the existing positions can change, how does the optimizer know what to change it to? Since the algo uses the MaximizeAlpha objective, what should be put in for the 'alphas' for existing positions. Maybe save the original alpha?

To get the current holdings along with their portfolio weights use the current_portfolio_weights method. The result is a pandas series with the asset objects as the index and weights as values. So, one could do something like this to include the current positions

long_alphas = context.output.query('Longs').Long_Alpha  
short_alphas = context.output.query('Shorts').Short_Alpha  
current_holdings = context.portfolio.current_portfolio_weights

all_alphas = pd.concat([long_alphas, short_alphas, current_holdings])

The 'alpha' for the current holdings will be the current weight which may not have the same scale as the other alphas? But, this is a start.

One could use the Frozen constraint (https://www.quantopian.com/docs/api-reference/optimize-api-reference#quantopian.optimize.Frozen) but, of course, it will freeze the current holdings 'as is'. Something like this

current_holdings = context.portfolio.current_portfolio_weights.index.to_list()  
freeze_these = opt.Frozen(current_holdings)  
order_optimal_portfolio(objective = alpha_objective, constraints = [max_exposure_1, max_position_size, dollar_neutra, freeze_thesel])

That might give you some ideas? Attach a backtest if you could.

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.

THANKS!! the Frozen thing is what i was looking for!!! Your a great and quick help!!!!!!!!

As soon as i have a bit more in place i will be happy to display the back test. :)
[by then it should be a lot less embarrassing to look at... ]

What i have to do now is add code so that the system doesn't try to flip from long to short. But treat long conditions as long conditions with ends
and treat short conditions as short with their own ends... actually this would be better than what i currently am starting with!!!!
as long as there is a gap between them, even if its one day, the portfolio will then have the ability to remove candidates and pick up others.

So here is the challenge (for me of course), to some how detect when the stock goes sideways, not moving up or moving down
that is the kind of condition that this system doesn't do as well in that anyway...

And to come up with selection criteria that finds more of the stock that is in a condition already that favors the kind of system
in this case, up trending, or down trending bias...

ALSO, thanks to you, i should look at the objectives more.. AND try to figure out what code i can add that will tell it in some way to dump something its holding, thereby opening up a position to another stock in which the opening condition has been met (whether short or long)

Thanks again... this is going to allow me to keep making great progress... (considering that i only started python last week)
I really really appreciate you taking the time!!!

Question 1. Can the size of existing positions be changed?
Most definitely... I will have to figure out what or how to scan for the "richest" target
Should the algo adjust the shares of existing positions? It should, but how do 'we' as programmers get control of that given its optimizing?

Question 2. Assuming the size of the existing positions can change, how does the optimizer know what to change it to?
Maybe a fixed amount rather than a major optimum amount? rather than try to get a maximum of a single ticker, go for maximum diversity?
Maybe save the original alpha? I cant say, as i am still getting a feel for it all and have yet to work with things enough to gain any intuition on it
(you know, the way us kids would get better at video games by playing them more and more and how our brains would pick patterns that gave us clues)

"one could do something like this to include the current positions" - i am going to try this with some way of graphing it
I am not too worried about fitting as the thing is quite simple and is not using lots of factors...

while we were celebrating the new year (my wife is asian), it dawned on me that there are many ways to experiment to try tune it
but a major problem is that whatever you pick, will have already happened already and is not really telling about what happens next

Boy you have me thinking on this... gonna be a busy weekend with family, and of course i will be itching to see what i can come up with
and thats not even figuring out how to implement it within this framework

THANKS!