Hello Quants,

This is my most ambitious algorithm to date. Unfortunately, I believe I may have bitten off more than I can chew. In essence I have noticed certain algorithms that do well when a market is trending up, others when it is trending down, and I have pick up a few specific items that are generally useful to any algorithm. The one I have posted represents a collection of code taken from these other algorithms that when utilized together should be fairly robust. I prefer to predict stock performance using fundamental analysis, thus it is reflected in the algorithm. These modifications reflect my own personal preferences and hopefully will lead to an algo that could trade my own money. However, this algorithm produces a few errors I have not been able to correct. Firstly, the “#” have removed code on lines 102, 103 (identical code on 120, 121) that I would like to use. And secondly, I would like to remove “long_filter(context, data)” and replace it with the “long_trending(context, data)” on line 239 (I assume fixes for line 239 can be applied appropriately for line 240 because they apply to similar code in separate functions). I would be very appreciative if someone could take a gander at this and apply some fixes. Suggestions always welcome. I also would not mind working with others who have an interest in fundamental analysis.

2
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56323e7c43fb9c10ff8408c3
There was a runtime error.
6 responses

Hi Darth,

I took a look at your code and I see both of the errors that you are reporting. The first (when lines 102 and 103 are uncommented), is related to the fact that your universe appears to be empty in combination with the fact that you are using the history() function to try to get the history of a specific stock. It's important to note that the history() function gets the volume history (in your case) for all stocks in your universe. If you would then like to look at the history of one stock, you can access it by doing something like this:

history(30, '1d', 'volume')[stock]

The second problem you are encountering seems to be related to the context.sector_mappings data structure. In your for loop on line 126, it seems that you are trying to write sectors for specific sids back to the structure that contains the sector code -> sector name mapping. Python doesn't let you add to a data structure that it is looping through so this is why you are seeing an error. In order to help you solve this one, do you think you could describe to me what the intended purpose of this code bit is?

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.

Hello Jamie,

Thank you for your very swift response!

I’ll go through my logic from start to finish just in case it gives you an idea for a workaround.

Def long_basket/def short_basket: the purpose of this function is to select ~20 stocks from all those available and group them based on fundamental criteria. One group has specific parameters that are desirable for a long position, the other for a short position. In this scenario, they are sorted by PE ratio accordingly.

Def long_filter/def short_filter: the purpose of this function is to take the 20 stocks from the long_basket and remove those that do not meet a minimum average volume scalable by account size, remove stocks that are scheduled to delist, remove the SPY etf, remove flagged stocks and remove leveraged ETFs.

Def long_trending/def short_trending: the purpose of this function is to take the remaining stocks from the long_trending, add the sector mappings so we know what sector it is in, and remove those that where their corresponding ETF is below the 200day moving average. For example if an energy stock is in the long_trending group but XLE is below it’s 200 day MA, then it would be removed. If a stock is in the short_trending group and its corresponding sector ETF is above it’s 200 day moving average, then it is removed from the group. Lastly, the groups are paired down to a max number of stocks based on the number of trending ETFs. For example in the compute_conditional_weights, if it is determined that 2 ETFs have a current daily price above their 200 DMA of the 9 ETFs in sector_mappingsetf then this corresponds with choosing 25% of the context.num_stocks (in this case 5) to go long with and 75% (in this case 15) stocks to go short with. The compute_conditional_weights settings can let me go as long or as short as I want once I find out the optimum balance.

Def rebalance: This section would long 5 stocks and short 15 stocks if context.num_stocks was = 20. Long 4 stocks and short 12 stocks if context.num_stocks was = 16, etc.

Def has_orders: This prevents ordering when orders are pending.

If you have any other questions, please don’t hesitate to ask. Thank you for taking a look at the algo.

I suggest you take a look at this example from the help docs. The example looks at sector codes from a fundamentals query as well as shows how to update your universe with stocks returned from the query. I think this should help you with the problems I mentioned in my previous post!

Hello Jamie,