When running the program XLC is sold, and bought in April 2019. I thought my code would HOLD any repeat sectors, but never SELL, and BUY on the same day.
My guess is that the reBalance routine is in error. It works great for the 1st 3 months (Jan. - March 2019) in my backtest.
Any ideas how I'm miscalculating?
Rahul Chowdhury
Hey Mike,
I changed the logic of Rebalance to stop the same day selling & buying of a repeat symbol. SetHoldings may still buy or sell shares of a repeat symbol to maintain the 1/3 proportion between the top 3 symbols.
def Rebalance(self): if self.IsWarmingUp: return func = lambda x: x[1].Current.Value top3 = {x[0]: x[1].Current.Value for x in sorted(self.data.items(), key=func, reverse=False)[:3]} # Liquidate all invested symbols which are no longer in top 3 for symbol in self.data: if symbol not in top3: if self.Portfolio[symbol].Invested: self.Liquidate(symbol) # Set holdings to top 3 symbols for symbol in top3: self.SetHoldings(symbol, 1/3)
Mike Reichard
Could you explain the following line of code:
func = lambda x: x[1].Current.Value
See that “func” is used as the Key on the “sorted” function, but don’t understand what “lambda” does. Appreciate your insights.
Mike
Alexandre Catarino
Hi Mike Reichard ,
Could you please try to not duplicate threads?
We have been discussing the same algorithm here:
ETF Sector Rotation - Logic Help #7249
Rahul Chowdhury
Hi Mike,
Lambda functions in python are anonymous functions. If I defined func = lambda x : x + 3 , func(4) would return 7.
In this case func is a lambda function which returns x[1].Current.Value for an input x. The way that func is defined implies that x is a list with an indicator at index 1.
We can actually rewrite
func = lambda x: x[1].Current.Value
top3 = {x[0]: x[1].Current.Value for x in sorted(self.data.items(), key=func, reverse=False)[:3]}
in one line, which is how it is usually written.
top3 = {x[0]: x[1].Current.Value for x in sorted(self.data.items(), key=lambda x: x[1].Current.Value, reverse=False)[:3]}
Learn more about sorting in python with lambdas here.
Mike Reichard
Mike Reichard
Mike Reichard
Not sure how to get rid of one of the threads. So, please do so, if it can be done. I’ve been responding via email messages.
Wanted to get your opinion on a “consolidator” issue - would the two sets of code return similar results:
1. Self.data[symbol] = RateOfChangePercent(1)
Consolidator = TradeBarConsolidator(CalendarType.Monthly)
2. Self.data[symbol] = RateOfChangePercent(4)
Consolidator = TradeBarConsolidator(CalendarType.Weekly)
Thanks for your insights.
Mike
Alexandre Catarino
Hi Mike Reichard ,
In order to keep the same conversation in one place, instead of opening a new thread, you can look at your emails for the last one we had a discussion. If you reply to it with a new, but related question, we will continue there. Maybe you deleted the email and cannot find it...
We have yet another thread that deals with the same algorithm design:
S&P500 ETF Sector Rotation Code Design Input #7273
The two approaches are not the same since the Weekly will consolidate Monday-to-Monday data and Monthly 1st-to-1st. However, both approaches are valid, you can test both and pick the one you like the most.
Mike Reichard
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!