Hello,
Hopefully someone can help me with this. I'm new to quantconnect and just coded my first algorithm. For some reason though, my backtest is not producing any results and I am getting zeros for all the data. It's a simple ema cross strategy that I wanted to explore. I will paste the code below:
from AlgorithmImports import *
class EmotionalBlueDog(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 11, 1)
self.SetEndDate(2023, 11, 1)
self.SetCash(100000)
self.AddEquity("SPY", Resolution.Minute)
self.AddEquity("AAPL", Resolution.Minute)
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
self.UniverseSettings.Resolution = Resolution.Minute
def OnData(self, data: Slice):
if not self.Portfolio.Invested:
for symbol in data.Keys:
if self.IsWarmingUp:
return
if data.ContainsKey(symbol) and data[symbol] is not None and data[symbol].Price:
short_ema = self.EMA(symbol, 20, Resolution.Minute)
long_ema = self.EMA(symbol, 50, Resolution.Minute)
if short_ema > long_ema:
risk_per_trade = 0.01
entry_price = data[symbol].Price
stop_loss_price = entry_price - (entry_price * risk_per_trade)
quantity = (self.Portfolio.TotalPortfolioValue * risk_per_trade) / (entry_price - stop_loss_price)
self.SetHoldings(symbol, quantity)
take_profit_price = entry_price + 2 * (entry_price * risk_per_trade)
self.LimitOrder(symbol, -quantity, take_profit_price)
elif short_ema < long_ema:
risk_per_trade = 0.01
entry_price = data[symbol].Price
stop_loss_price = entry_price + (entry_price *risk_per_trade)
quantity = (self.Portfolio.TotalPortfolioValue * risk_per_trade) / (stop_loss_price - entry_price)
self.SetHoldings(symbol, -quantity)
take_profit_price = entry_price - 2 * (stop_loss_price - entry_price)
self.LimitOrder(symbol, quantity, take_profit_price)
Mia Alissi
Hello Matt,
Your code seems to be fine at first glance. However, there are a few things you might want to check:
Ensure that the data for the symbols "SPY" and "AAPL" is available for the date range you have set in your algorithm. If the data is not available, your algorithm might not be able to execute any trades.
Check if your EMA conditions are being met. If the conditions for your EMA crossover are not being met, no trades will be executed. You can add some logging to your code to see if the conditions are being met. For example, you can add
self.Debug(f"Short EMA: {short_ema}, Long EMA: {long_ema}")
after you calculate the EMAs.Make sure that the quantity of shares you are trying to buy or sell is not zero. If the quantity is zero, no trades will be executed. You can also add some logging to check this. For example, you can add
self.Debug(f"Quantity: {quantity}")
after you calculate the quantity.Lastly, ensure that you have enough cash in your portfolio to execute the trades. If you do not have enough cash, the trades will not be executed.
If you are still having issues, you might want to post your question in the QuantConnect Forum. The community there is very helpful and you might get some additional insights.
Here is how you can add logging to your code:
```python from AlgorithmImports import *
class EmotionalBlueDog(QCAlgorithm):
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.
Matt Foster
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!