Hi,
I have been trying to test an algorithm based on Fundamental and Techincal factors. The algorithm is structured something like this:
Fundamental Factors: used for ranking stocks
Technical Factors: used for deciding when to take a position
Till now I have been using Techincal Factors as a Screen in my pipeline and I forced my algorithm to go 'Long Only' by using LongOnly constraint in Optimize API. For example:
combined_factor = e.zscore() + r.zscore()
return Pipeline(
columns={
'combined_factor': combined_factor},
screen= QTradableStocksUS() & combined_factor.notnull() & sector.notnull()
& screen2 & screen3 & screen4
)
where:
screen2= USEquityPricing.close.latest >SMA_50
screen3= SMA_50 > SMA_150
screen4= SMA_150 > SMA_200
Now I want to logic for going SHORT. But for that, I want to pick the bottom quantile as per factor ranking and use different Technical Indicators. Example of conditions to go short:
bottom_scores= combined_factor.bottom(200)
screen2= USEquityPricing.close.latest <SMA_50
screen3= SMA_50 < SMA_150
screen4= SMA_150 < SMA_200
return Pipeline(
columns={
'combined_factor': combined_factor},
screen= QTradableStocksUS() & combined_factor.notnull() & sector.notnull()
& bottom_scores & screen2 & screen3 & screen4
)
I have not been able to find a solution as to what I can do to implement both Long and Short logic while using the Optimize API.
To put it differently- I want to apply conditions so that my algo knows which stocks to go long and which ones to go short and Optimize the portfolio.
Thanks in advance!