Hi,

This algo trade 1 stock when crossover appears on SMA20, SMA5 and MACD is > 0 (positive). Any idea how I can add e.g. 10 or more stocks to same algo and trade with same indicators and trade e.g. 10% of portfolio on each stock ?

In this example only 10% of portfolio is allocated to 1 stock. 

Best regards

Karsten

class SimpleStockTrade_KOK(QCAlgorithm):
   
    def Initialize(self):
        self.SetStartDate(2015, 1, 1)  
        #self.SetEndDate(2021, 6, 1)
        self.SetCash(10000)  # Set Strategy Cash
        self.Settings.FreePortfolioValuePercentage = 0.05 # Oprethold altid 5% cash
        
        self.resolution = Resolution.Daily
        self.symbol = self.AddEquity("TSLA", self.resolution).Symbol #How can I trade e.g. 10 different stocks in same algo ?
        
        # Moving average setup
        self.fastResolution = int(5) 
        self.slowResolution = int(20) 
        self.fastSMA = self.SMA(self.symbol, self.fastResolution, self.resolution)
        self.slowSMA = self.SMA(self.symbol, self.slowResolution, self.resolution)
                
        # MACD setup
        self.slowMacd = int(26)
        self.fastMacd = int(12) 
        self.signalPeriod = int(9) 
        self.macdTYpe = int(1) 
        self.macdResolution = self.resolution # Samme som aktien 
        self.macd = self.MACD(self.symbol, self.fastMacd, self.slowMacd, self.signalPeriod, MovingAverageType.Exponential, self.macdResolution)
        self.signalDeltaPercent = 0
        
        self.SetWarmUp(60)
       
    def OnData(self, data):
        
        if not self.fastSMA.IsReady:
            return
        if not self.slowSMA.IsReady:
            return
        if not self.macd.IsReady:
            return
       
        holdings = self.Portfolio[self.symbol].Quantity
        
        self.signalDeltaPercent = (self.macd.Current.Value - self.macd.Signal.Current.Value)/self.macd.Fast.Current.Value
        
        # SMA trade setup
        if holdings <= 0:
            if self.slowSMA < self.fastSMA and self.signalDeltaPercent > 0:
                quantity = self.CalculateOrderQuantity(self.symbol, 0.1) # And trade e.g. 10% of portfolio on each stock 
                self.MarketOrder(self.symbol, quantity)
           
        # Test for crossover and liquidate if
        if holdings > 0 : 
            if self.slowSMA > self.fastSMA:
                self.Liquidate(self.symbol)