def Initialize(self):
self.SetStartDate(2020, 1, 1)
self.SetCash(2500)
self.UniverseSettings.Resolution = Resolution.Daily
self.UniverseSettings.Leverage = 2.0
self.AddUniverse(self.SelectionFilter)
self.rsi = {}
self.symbol_data = {}
def SelectionFilter(self, coarse):
sorted_vol = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
filtered = [x.Symbol for x in sorted_vol if x.Price > 50]
return filtered[:1]
def OnSecuritiesChanged(self, changes):
self.Log(f"OnSecuritiesChanged({self.Time})::{changes}")
for security in changes.RemovedSecurities:
symbol = security.Symbol
if symbol in self.symbol_data:
self.Liquidate(symbol)
self.Debug(f"Liquidated {symbol} at {self.Securities[symbol].Price}")
del self.symbol_data[symbol]
del self.rsi[symbol]
for security in changes.AddedSecurities:
symbol = security.Symbol
self.symbol_data[symbol] = security
self.rsi[symbol] = self.RSI(symbol, 14, Resolution.Daily)
def OnData(self, data):
for symbol, rsi in self.rsi.items():
if symbol in data and data[symbol] is not None:
current_price = data[symbol].Price
rsi_value = rsi.Current.Value
if not self.Securities[symbol].Invested and (rsi_value < 30 or (rsi_value > 50 and rsi_value < 70)):
self.SetHoldings(symbol, 1)
self.Debug(f"Bought {symbol} at {current_price} with RSI {rsi_value}")
elif self.Securities[symbol].Invested and (rsi_value > 70 or (rsi_value > 30 and rsi_value < 50)):
self.Liquidate(symbol)
self.Debug(f"Liquidated {symbol} at {current_price} with RSI {rsi_value}")
I wrote this simple strategy, filtering securities by volume, then buying them, when rsi has a certain value and selling them when a new security takes its place or if rsi reaches a certain value. The problem appeared when I tried to set leverage for my trades, as I was getting the exact same results in the backtest, as if I was trading with no leverage
Mia Alissi
In QuantConnect, setting the leverage in the
UniverseSettings
does not automatically apply that leverage to the trades. The leverage set inUniverseSettings
is the maximum leverage that can be used, but the actual leverage used in trades needs to be set individually for each security.In your code, you have set
self.UniverseSettings.Leverage = 2.0
, but you have not set the leverage for the individual securities that you are trading. To set the leverage for a security, you can use theSetLeverage
method on the security object. Here is how you can do it:Please note that the leverage setting may not have the exact effect you expect on the returns of your strategy. The effect of leverage on returns is not linear, and increasing leverage will also increase the risk and potential losses of the strategy. It's also important to note that the maintenance margin is calculated based on your positions at different notional value tiers, which means it's always calculated in the same way, regardless of what leverage you select (source).
Also, you can monitor your algorithm's utilization of leverage over time using the Leverage chart in the backtest report (source).
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.
Harzarul
Facing the same issue.. tried Mia solution, still get the same result with and without leverage.. look forward if there's any update on the issue..
Robert Prevratil
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!