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)