Considering an SMA crossover strategy how can I make my algo loop through different forex pairs, e.g. I entered a trade on EURUSD but my conditions are also met on EURJPY and I want my algo to take the trade.
Currently my code only takes trades one by one and completly randomly not following the SMA criteria.
Alexandre Catarino
Hi SIG_94 ,
Could you please share the algorithm you have implemented so far?
It would greatly help the community to make relevant suggestions.
SIG_94
Hello Alexandre, thank you for the reply. Below should appear my code.
What I would like to do is to run this strategy over multiple pairs, at the moment only works when selecting the security manually.
When looping through the items of the list I get the error: FastSmaUpdated is not reocgnized.
import decimal as d from datetime import timedelta class MovingAverageCrossAlgorithm(QCAlgorithm): def Initialize(self): '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.''' self.SetStartDate(2020, 1, 1) #Set Start Date self.SetEndDate(2020, 12, 31) #Set End Date self.SetCash(100000) #Set Strategy Cash self.RollingWindowSize = 2 self.currencies = ["EURUSD","NZDUSD","USDJPY"] self.Data = {} for symbol in self.currencies: self.currency = self.AddForex(symbol , Resolution.Hour) self.Data[symbol] = self.currency self.fast = self.SMA(symbol, 50, Resolution.Hour) self.slow = self.SMA(symbol, 200, Resolution.Hour) self.fastSMAWindow = RollingWindow[IndicatorDataPoint](2) #setting the Rolling Window for the fast SMA indicator, takes two values self.fast.Updated += self.FastSmaUpdated #Updating those two values #self.slowSMAWindow = RollingWindow[IndicatorDataPoint](3) #setting the Rolling Window for the slow SMA indicator, takes two values #self.slow.Updated += self.SlowSmaUpdated #Updating those two values self.SetWarmUp(200) self.quant = 100000 def FastSmaUpdated(self,sender,updated): #Event holder to update the fast SMA Rolling Window values if self.fast.IsReady: self.fastSMAWindow.Add(updated) #def SlowSmaUpdated(self, sender, updated): #Event holder to update the slow SMA Rolling Window values #if self.slow.IsReady: #self.slowSMAWindow.Add(updated) def OnData(self, data): if self.IsWarmingUp: return #holdings = self.Portfolio[symbol].Quantity for symbol in self.Data.keys(): self.FastPastValue = self.fastSMAWindow[1].Value #Previous fast SMA value #self.SlowPastValue = self.slowSMAWindow[0].Value #Previous slow SMA value if not self.Portfolio.Invested: price = data[symbol].Close if self.fast.Current.Value > self.slow.Current.Value and self.FastPastValue < self.slow.Current.Value: self.MarketOrder(symbol, self.quant) self.StopMarketOrder(symbol, -self.quant, (price * 0.99)) self.LimitOrder(symbol, -self.quant, (price * 1.02)) # if self.Portfolio[c].IsLong: # if self.fast.Current.Value < self.slow.Current.Value: # self.Liquidate(c) def OnOrderEvent(self, orderEvent): order = self.Transactions.GetOrderById(orderEvent.OrderId) if order.Status == OrderStatus.Filled: if order.Type == OrderType.Limit or order.Type == OrderType.Limit: self.Transactions.CancelOpenOrders(order.Symbol) if order.Status == OrderStatus.Canceled: self.Log(str(orderEvent))
Alexandre Catarino
Hi SIG_94 ,
The algorithm needs to create the indicators and rolling windows for each security and save a reference to those objects. We recommend using a "SymbolData" class to include all those elements and create a dictionary keyed by Symbol for SymbolData objects:
self.Data = {} for ticker in ["EURUSD","NZDUSD","USDJPY"]: symbol = self.AddForex(ticker, Resolution.Hour).Symbol self.Data[symbol] = SymbolData( self.SMA(symbol, 50, Resolution.Hour), self.SMA(symbol, 200, Resolution.Hour))
SIG_94
Hello Alexandre,
Thank you very mych for your code, that helped me.
Cheers!
SIG_94
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!