from Execution.ImmediateExecutionModel import ImmediateExecutionModel
class ParticleNadionReplicator(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 12, 19) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.Settings.FreePortfolioValuePercentage = 0.10
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
self.SetExecution(ImmediateExecutionModel())
self.SetPortfolioConstruction(InsightLeveragePortfolioConstructionModel())
self.SetRiskManagement(MaximumUnrealizedProfitPercentPerSecurity(0.03))
#CFD Pairs
tickers =['AU200AUD','BCOUSD','CORNUSD','DE30EUR','EU50EUR','DE10YBEUR','CH20CHF','FR40EUR','HK33HKD',
'JP225USD','NAS100USD','NATGASUSD','NL25EUR','SOYBNUSD','SPX500USD','SUGARUSD','SG30SGD','UK100GBP',
'UK10YBGBP','US2000USD','US30USD','USB02YUSD','USB05YUSD','USB10YUSD','USB30YUSD','WHEATUSD','WTICOUSD',
'XAGAUD','XAGCAD','XAGCHF','XAGEUR','XAGGBP','XAGHKD','XAGJPY','XAGNZD','XAGSGD',
'XAGUSD','XAUAUD','XAUCAD','XAUCHF','XAUEUR','XAUGBP','XAUHKD','XAUJPY','XAUNZD',
'XAUSGD','XAUUSD','XAUXAG','XCUUSD','XPDUSD','XPTUSD']
#timezones: https://github.com/QuantConnect/Lean/blob/master/Data/market-hours/market-hours-database.json
timezones =['Australia/Sydney','America/New_York','America/Chicago','Europe/Berlin','Europe/Berlin','Europe/Berlin',
'Europe/Zurich','Europe/Paris','Asia/Hong_Kong','America/Chicago','America/Chicago','America/New_York',
'Europe/Amsterdam','America/Chicago','America/Chicago','America/New_York','Asia/Singapore','Europe/London',
'Europe/London','America/New_York','America/Chicago','America/Chicago','America/Chicago','America/Chicago',
'America/Chicago','America/Chicago','America/New_York','UTC','UTC','UTC','UTC','UTC','UTC','UTC','UTC','UTC',
'UTC','UTC','UTC','UTC','UTC','UTC','UTC','UTC','UTC','UTC','UTC','UTC','America/New_York','UTC','UTC']
cfds = {key: {"Symbol" : tickers[key], "Timezone" : timezones[key]} for key in range(len(tickers))}
UtcCfds = [27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 49, 50]
NyCfds = [1, 11, 15, 19, 26, 48]
ChicagoCfds = [2, 9, 10, 13, 14, 20, 21, 22, 23, 24, 25]
BerlinCfds = [3, 4, 5]
LondonCfds = [17, 18]
##########################################
#select = [1] #select here the CFDs to trade
select = LondonCfds
##########################################
self.SetTimeZone(cfds[select[0]]["Timezone"])
self.UniverseSettings.Resolution = Resolution.Minute
self.symbols = [ Symbol.Create(cfds[x]["Symbol"], SecurityType.Cfd, Market.Oanda) for x in select]
self.SetUniverseSelection( ManualUniverseSelectionModel(self.symbols) )
self.SetSecurityInitializer(self.CustomSecurityInitializer)
self.alpha = MyAlphaModel()#leverage
self.AddAlpha(self.alpha)
self.SetWarmUp(TimeSpan.FromDays(7))
def CustomSecurityInitializer(self, security):
security.SetLeverage(1)
security.SetDataNormalizationMode(DataNormalizationMode.Raw)
class MyAlphaModel(AlphaModel):
def __init__(self):
self.equities = []
def OnSecuritiesChanged(self, algorithm, changes):
for security in changes.AddedSecurities:
if str(type(security)) == "<class 'QuantConnect.Securities.Cfd.Cfd'>":
self.equities.append(security.Symbol)
for security in changes.RemovedSecurities:
symbol = security.Symbol
if symbol in self.equities:
self.equities.remove(symbol)
def Update(self, algorithm, data):
insights = []
if algorithm.IsWarmingUp or algorithm.Portfolio.Invested:
return insights
for symbol in self.equities:
if algorithm.IsMarketOpen(symbol):
insights.append(Insight.Price(symbol,timedelta(days=180), InsightDirection.Up, None, None, None, 1))
return insights
class InsightLeveragePortfolioConstructionModel(InsightWeightingPortfolioConstructionModel):
def CreateTargets(self, algorithm, insights):
targets = super().CreateTargets(algorithm, insights)
return [PortfolioTarget(x.Symbol, x.Quantity*algorithm.Securities[x.Symbol].Leverage) for x in targets]