Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Sales size for Ranking a Stock Universe (Adaptation)

Sales size for Ranking a Stock Universe

Taken from:

https://www.quantopian.com/posts/sales-size-for-ranking-a-stock-universe-template-fundamental-algo

Import libraries

Numpy
Quantopian Algorithmin: Provides functions that facilitate order scheduling and execution, and allow us to initialize and manage parameters in our algorithms.
Quantopian Optimize: Optimize is the recommended method for placing orders on Quantopian. It uses advanced optimization techniques to move your portfolio from its current state to your desired state, managing constraints and objectives that you can pass in.
Quantopian Pipeline: Is a powerful tool for cross-sectional analysis of asset data. It allows us to define a set of calculations on multiple data inputs and analyze a large amount of assets at a time.
Function Initialize

Is called exactly once when our algorithm starts running and requires context as input. Any parameter initialization and one-time startup logic should go here.

algo.attach_pipeline: Attach the pipeline for the risk model factors that we want to neutralize in the optimization step. The 'risk_factors' string is used to retrieve the output of the pipeline in before_trading_start below. In this case our 'risk_factors' is 'alpha_factor_template'
algo.schedule_function:
Rebalance: It allows us to execute personalized functions in specific dates and times. For example, we can program a function to rebalance your portfolio in the open market every 45 minutes
Record Vars: It allows us to store all variables at the end of day
Function Make Pipeline

factset.Fundamentals: FactSet fundamental data contains both continuous values and values describing a certain time period. An example of a continuous value is assets, where each observation in the column corresponds to the most recently known value of a company's assets as of a certain date. In this case our configuration is ltm (last twelve months)
Risk

Sector Exposures: Accepted. Exposure to various economic sectors.
Style Exposures: Accepted. Exposure to various investing styles. Momentum, Size, Value, Short Term Reversal and Volatility
Leverage: Accepted. 1.00x. The leverage constraint allows a small outlier tolerance. Leverage can go down to 0.7 at the 0th percentile, and must be at least 0.8 at the 2nd percentile. On the upper limit, leverage can go up to 1.2 at the 100th percentile, and should be no more than 1.1 at the 98th percentile.
Turnover: Failed. 1.05%. The turnover constraint allows a small outlier tolerance. It can go down to 3% at the 0th percentile, and must be at least 5% at the 2nd percentile. On the upper limit, turnover can go up to 80% at the 100th percentile, and must be no more than 65% at the 98th percentile.
Beta To SPY: Accepted. 0.05. The beta to SPY constraint allows a mall outlier tolerance. The absolute value of the trailing 6-month beta can go up to 0.4 at the 100th percentile, and must be under 0.3 at the 98th percentile.
Position Concentration: Accepted. 0.18%. The position concentration constraint allows a small outlier tolerance. The maximum single-position concentration can go up to 10% at the 100th percentile, and must be under 5% at the 98th percentile.
Net Dollar Exposure: Accepted. -0.19%. The net dollar exposure constraint allows a small outlier tolerance. Net dollar exposure can go up to 20% at the 100th percentile, and must be under 10% at the 98th percentile.
Performance

Total Returns: 8.08%. The total percentage return of the portfolio from the start to the end of the backtest.
Specific Returns: 1.55%. The difference between the portfolio's total returns and common returns.
Common Returns: 6.48%. Returns that are attributable to common risk factors.
Sharpe: 0.65. A measure of risk-adjusted performance, which divides the portfolio's excess return over the risk-free rate by the portfolio's standard deviation.
Max Drawdown: -6.05%. The largest peak-to-trough drop in the portfolio's history.
Volatility: 0.03. The standard deviation of the portfolio’s returns.
References

https://www.quantopian.com/tutorials/getting-started#lesson1

https://www.factset.com/