from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import TradeBar
import decimal as dclass RollingWindowAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2018, 6, 1) #Set Start Date
self.SetEndDate(2019, 3, 22) #Set End Date
self.SetCash(100000) #Set Strategy Cash
self.SetBrokerageModel(BrokerageName.FxcmBrokerage)
self.SetTimeZone("Europe/Rome")
self.SetWarmUp(100)
self.symbols = ["EURUSD","EURAUD","GBPUSD","AUDUSD"]
self.forex = self.AddForex(self.symbols[0], Resolution.Minute, Market.FXCM)
consolidator_30 = QuoteBarConsolidator(30)
consolidator_30.DataConsolidated += self.On30Data
self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_30)
self.window_30 = RollingWindow[QuoteBar](2) consolidator_60 = QuoteBarConsolidator(60)
consolidator_60.DataConsolidated += self.On60Data
self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_60)
self.window_60 = RollingWindow[QuoteBar](2)
#consolidator_240 = QuoteBarConsolidator(240)
#self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_240)
#b240 = self.RegisterIndicator(self.symbols[0], self.Bolband, consolidator_240)
self.Bolband_30 = self.BB(self.symbols[0], 20, 2, MovingAverageType.Simple, Resolution.Minute)
self.Bolband_30.Updated += self.BolbandUpdated_30
self.RegisterIndicator(self.symbols[0], self.Bolband_30, consolidator_30)
self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_30)
self.BolbandWindow_30 = RollingWindow[IndicatorDataPoint](3)
self.Bolband_60 = self.BB(self.symbols[0], 20, 2, MovingAverageType.Simple, Resolution.Minute)
self.Bolband_60.Updated += self.BolbandUpdated_60
self.RegisterIndicator(self.symbols[0], self.Bolband_60, consolidator_60)
self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_60)
self.BolbandWindow_60 = RollingWindow[IndicatorDataPoint](3)
#self.AddEquity("SPY", Resolution.Minute)
#consolidator = TradeBarConsolidator(30)
#self._sma = SimpleMovingAverage(10)
#self.RegisterIndicator("SPY", self._sma, consolidator)
#self.SubscriptionManager.AddConsolidator("SPY", consolidator)
def BolbandUpdated_30(self, sender, updated):
self.BolbandWindow_30.Add(updated)
UpperBand_30 = self.BolbandWindow_30[0].UpperBand.Current.Value
LowerBand_30 = self.BolbandWindow_30[0].LowerBand.Current.Value
def BolbandUpdated_60(self, sender, updated):
self.BolbandWindow_60.Add(updated)
UpperBand_60 = self.BolbandWindow_60[0].UpperBand.Current.Value
LowerBand_60 = self.BolbandWindow_60[0].LowerBand.Current.Value
def On30Data(self, sender, bar):
self.window_30.Add(bar)
close_price_30 = self.window_30[0].Close
def On60Data(self, sender, updated):
self.window_60.Add(bar)
close_price_60 = self.window_60[0].Close
def OnData(self, data):
#fxOpen = data['EURUSD'].Open
#fxClose = data['EURUSD'].Close
#self.window.Add(data["EURUSD"])
if not (self.window_30.IsReady and self.window_60.IsReady):
return
#holdings = self.Portfolio["EURUSD"].Quantity
#previousPrice = self.window[1].Close
#stop_price_ = self.Securities['EURUSD'].Price * 0.50
#stop_price = self.Securities['EURUSD'].Price * 1.20
def OnOrderEvent(self, orderEvent):
order = self.Transactions.GetOrderById(orderEvent.OrderId)
if order.Status == OrderStatus.Filled:
if order.Type == OrderType.Limit or order.Type == OrderType.StopMarket:
self.Transactions.CancelOpenOrders(order.Symbol)
if order.Status == OrderStatus.Canceled:
self.Log(str(orderEvent))
0