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