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)
Vladimir
Karsten Ø. Kristensen,
There are many ways to accomplish this task.
I prefer the simplest one - using dictionaries.
Please accept the "MAC and MACD Static Portfolio" from my library.
Karsten Ø. Kristensen
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!