I ran the original code but recieved this error: Runtime Error: QuantConnect.Scheduling.ScheduledEventException: Python.Runtime.PythonException: Exception : The security 'JNPR' 'Equity' has already been added. \

Ill attach the full error below.

from QuantConnect.Data.UniverseSelection import *
import math
import numpy as np
import pandas as pd
import scipy as sp
# import statsmodels.api as sm

class FundamentalFactorAlgorithm(QCAlgorithm):

def Initialize(self):

self.SetStartDate(2004, 01, 01) #Set Start Date
self.SetEndDate(2018, 01, 01) #Set Start Date
self.SetCash(50000) #Set Strategy Cash


self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
self.holding_months = 1
self.num_screener = 100
self.num_stocks = 10
self.formation_days = 200
self.lowmom = False
self.month_count = self.holding_months
self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.At(0, 0), Action(self.monthly_rebalance))
self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.At(10, 0), Action(self.rebalance))
# rebalance the universe selection once a month
self.rebalence_flag = 0
# make sure to run the universe selection at the start of the algorithm even it's not the manth start
self.first_month_trade_flag = 1
self.trade_flag = 0
self.symbols = None

def CoarseSelectionFunction(self, coarse):
if self.rebalence_flag or self.first_month_trade_flag:
# drop stocks which have no fundamental data or have too low prices
selected = [x for x in coarse if (x.HasFundamentalData) and (float(x.Price) > 5)]
# rank the stocks by dollar volume
filtered = sorted(selected, key=lambda x: x.DollarVolume, reverse=True)

return [ x.Symbol for x in filtered[:200]]
else:
return self.symbols


def FineSelectionFunction(self, fine):
if self.rebalence_flag or self.first_month_trade_flag:
hist = self.History([i.Symbol for i in fine], 1, Resolution.Daily)
try:
filtered_fine = [x for x in fine if (x.ValuationRatios.EVToEBITDA > 0)
and (x.EarningReports.BasicAverageShares.ThreeMonths > 0)
and float(x.EarningReports.BasicAverageShares.ThreeMonths) * hist.loc[str(x.Symbol)]['close'][0] > 2e9]
except:
filtered_fine = [x for x in fine if (x.ValuationRatios.EVToEBITDA > 0)
and (x.EarningReports.BasicAverageShares.ThreeMonths > 0)]

top = sorted(filtered_fine, key = lambda x: x.ValuationRatios.EVToEBITDA, reverse=True)[:self.num_screener]
self.symbols = [x.Symbol for x in top]

self.rebalence_flag = 0
self.first_month_trade_flag = 0
self.trade_flag = 1
return self.symbols
else:
return self.symbols

def OnData(self, data):
pass

def monthly_rebalance(self):
self.rebalence_flag = 1

def rebalance(self):
spy_hist = self.History([self.spy], 120, Resolution.Daily).loc[str(self.spy)]['close']
if self.Securities[self.spy].Price < spy_hist.mean():
for symbol in self.Portfolio.Keys:
if symbol.Value != "TLT":
self.Liquidate()
self.AddEquity("TLT")
self.SetHoldings("TLT", 1)
return

if self.symbols is None: return
chosen_df = self.calc_return(self.symbols)
chosen_df = chosen_df.iloc[:self.num_stocks]

self.existing_pos = 0
add_symbols = []
for symbol in self.Portfolio.Keys:
if symbol.Value == 'SPY': continue
if (symbol.Value not in chosen_df.index):
self.SetHoldings(symbol, 0)
elif (symbol.Value in chosen_df.index):
self.existing_pos += 1

weight = 0.99/len(chosen_df)
for symbol in chosen_df.index:
self.AddEquity(symbol)
self.SetHoldings(symbol, weight)

def calc_return(self, stocks):
hist = self.History(stocks, self.formation_days, Resolution.Daily)
current = self.History(stocks, 1, Resolution.Minute)

self.price = {}
ret = {}

for symbol in stocks:
if str(symbol) in hist.index.levels[0] and str(symbol) in current.index.levels[0]:
self.price[symbol.Value] = list(hist.loc[str(symbol)]['close'])
self.price[symbol.Value].append(current.loc[str(symbol)]['close'][0])

for symbol in self.price.keys():
ret[symbol] = (self.price[symbol][-1] - self.price[symbol][0]) / self.price[symbol][0]
df_ret = pd.DataFrame.from_dict(ret, orient='index')
df_ret.columns = ['return']
sort_return = df_ret.sort_values(by = ['return'], ascending = self.lowmom)

return sort_return

Error code: 

Runtime Error: QuantConnect.Scheduling.ScheduledEventException: Python.Runtime.PythonException: Exception : The security 'JNPR' 'Equity' has already been added. at QuantConnect.Algorithm.QCAlgorithm.AddSecurity[T] (QuantConnect.SecurityType securityType, System.String ticker, QuantConnect.Resolution resolution, System.String market, System.Boolean fillDataForward, System.Decimal leverage, System.Boolean extendedMarketHours) [0x00083] in <a8b13edcb24c4fa4acb30df03c15ca94>:0 at QuantConnect.Algorithm.QCAlgorithm.AddEquity (System.String ticker, QuantConnect.Resolution resolution, System.String market, System.Boolean fillDataForward, System.Decimal leverage, System.Boolean extendedMarketHours) [0x00001] in <a8b13edcb24c4fa4acb30df03c15ca94>:0 at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <2e7c1c96edae44d496118948ca617c11>:0 at Python.Runtime.Dispatcher.Dispatch (System.Collections.ArrayList args) [0x00018] in <3e87c364169b46d2b45f0865ae967c08>:0 at __System_ActionDispatcher.Invoke () [0x00006] in <57842ce96c5c4ff8916c34f6c06bab21>:0 at QuantConnect.Scheduling.ScheduleManager+<>c__DisplayClass16_0.<On>b__0 (System.String name, System.DateTime time) [0x00000] in <32eb38a3b7cb4298b4d49e1623171594>:0 at QuantConnect.Scheduling.ScheduledEvent.OnEventFired (System.DateTime triggerTime) [0x00036] in <32eb38a3b7cb4298b4d49e1623171594>:0 at QuantConnect.Scheduling.ScheduledEvent.OnEventFired (System.DateTime triggerTime) [0x00086] in <32eb38a3b7cb4298b4d49e1623171594>:0 at QuantConnect.Scheduling.ScheduledEvent.Scan (System.DateTime utcTime) [0x0010f] in <32eb38a3b7cb4298b4d49e1623171594>:0 at QuantConnect.Lean.Engine.RealTime.BacktestingRealTimeHandler.SetTime (System.DateTime time) [0x00019] in <0c007253613f4c96834d5d4cb3c3b185>:0 at QuantConnect.Lean.Engine.AlgorithmManager.Run(QuantConnect.Packets.AlgorithmNodePacket job, QuantConnect.Interfaces.IAlgorithm algorithm, QuantConnect.Lean.Engine.DataFeeds.IDataFeed feed, QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler transactions, QuantConnect.Lean.Engine.Results.IResultHandler results, QuantConnect.Lean.Engine.RealTime.IRealTimeHandler realtime, QuantConnect.Lean.Engine.Server.ILeanManager leanManager, QuantConnect.Lean.Engine.Alpha.IAlphaHandler alphas, System.Threading.CancellationToken token) [0x007d0] in <0c007253613f4c96834d5d4cb3c3b185>:0 at QuantConnect.Lean.Engine.Engine+<>c__DisplayClass8_1.<Run>b__3 () [0x000ac] in <0c007253613f4c96834d5d4cb3c3b185>:0

Author