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.