from System import *
from QuantConnect import *
from QuantConnect.Data.Consolidators import *
from QuantConnect.Data.Market import *
from QuantConnect.Orders import OrderStatus
from QuantConnect.Algorithm import QCAlgorithm
from QuantConnect.Indicators import *
import numpy as np
from datetime import timedelta, datetime
import decimal as d
class MultipleSymbolConsolidationAlgorithm(QCAlgorithm):
def Initialize(self):
BarPeriod = TimeSpan.FromMinutes(1)
RollingWindowSize = 14
SimpleMovingAveragePeriodfast = 14 ##USD CAD AUD GBP EUR CHF NZD
self.Data = {}
ForexSymbols =["EURUSD"]#, "EURCHF", "EURAUD", "EURGBP", "EURCAD", "EURNZD", "USDCHF", "USDCAD", "GBPCAD", "GBPAUD", "GBPCHF", "GBPUSD", "GBPNZD", "AUDCAD", "CADCHF", "NZDCAD", "AUDUSD", "AUDCHF", "AUDNZD", "NZDCHF", "NZDUSD"]
self.SetStartDate(2018, 2, 5)
self.SetEndDate(2018, 2, 10)
self.SetCash(10000) #Initialize
self.SetWarmUp(14)
self.SetTimeZone("Europe/Rome")
StochasticPeriod = 14
BollingerPeriod = 20
KPeriod = 3
DPeriod = 3
############################################################################################################################################################
for symbol in ForexSymbols:
forex = self.AddForex(symbol)
self.Data[symbol] = SymbolData(forex.Symbol, BarPeriod, RollingWindowSize)
self.Securities[symbol].SetLeverage(50)
for symbol, symbolData in self.Data.items():
symbolData.SMAfast = SimpleMovingAverage(self.CreateIndicatorName(symbol, "SMA" + str(SimpleMovingAveragePeriodfast), Resolution.Minute), SimpleMovingAveragePeriodfast)
symbolData.STO = Stochastic(self.CreateIndicatorName(symbol, "STO" + str(StochasticPeriod), Resolution.Minute), StochasticPeriod, KPeriod, DPeriod)
symbolData.BB = BollingerBand(self.CreateIndicatorName(symbol, "BB" + str(BollingerPeriod), Resolution.Hour), BollingerPeriod)
consolidator = QuoteBarConsolidator(BarPeriod)
consolidator.DataConsolidated += self.OnDataConsolidated
self.SubscriptionManager.AddConsolidator(symbolData.Symbol, consolidator)
##########################################################################################################################################
def OnDataConsolidated(self, sender, bar): #ConsolidateData
self.Data[bar.Symbol.Value].SMAfast.Update(bar.Time, bar.Close)
self.Data[bar.Symbol.Value].STO.Update(bar)
self.Data[bar.Symbol.Value].BB.Update(bar.Time, bar.Close)
self.Data[bar.Symbol.Value].Bars.Add(bar)
#########################################################################################################################################
def OnData(self, data):
for symbol in self.Data.keys():
symbolData = self.Data[symbol]
if symbolData.IsReady() and symbolData.WasJustUpdated(self.Time):
symbolData.smaWin.Add(symbolData.SMAfast.Current.Value)
symbolData.Pricewin.Add(self.Securities[symbol].Price)
if symbolData.Pricewin.Count == 5:
###################################################################################################################################
PipLoss = 10
Equity = self.Portfolio.TotalPortfolioValue*50
pips = self.Securities[symbol].SymbolProperties.MinimumPriceVariation * 10
ConversionRate = self.Portfolio.Securities[symbol].QuoteCurrency.ConversionRate
LotSize = ((Equity)/(PipLoss*ConversionRate)) ################### 1 = .01*100 = .01*100000/1000. If risk/trade is changed from 1% change that in Lot size equation
NowPrice = self.Securities[symbol].Price
########################################################################################################################################
sma_list = [i for i in symbolData.smaWin]
SMA = symbolData.SMAfast.Current.Value
STO =symbolData.STO.Current.Value
hour = self.Time.hour
minute = self.Time.minute
self.Log("{0}, {1}, {2}, {3}".format(str(hour), str(minute), str(STO), str(NowPrice)))
SMA = symbolData.SMAfast.Current.Value
smaslope = (sma_list[0] - sma_list[1])
###############################################################################################################################
class SymbolData(object):
def __init__(self, symbol, barPeriod, windowSize):
self.Symbol = symbol
self.BarPeriod = barPeriod
self.Bars = RollingWindow[IBaseDataBar](windowSize)
self.SMAfast = None
self.STO = None
self.BB = None
self.smaWin = RollingWindow[float](5)
self.Pricewin = RollingWindow[float](5)
def IsReady(self):
return self.Bars.IsReady and self.SMAfast.IsReady
def WasJustUpdated(self, current):
return self.Bars.Count > 0 and self.Bars[0].Time == current - self.BarPeriod