Exemplo n.º 1
0
 def Initialize(self):
     self.SetStartDate(2003, 1, 1)
     self.SetCash(100000)
     
     # Data resolution
     self.UniverseSettings.Resolution = Resolution.Minute
     
     # Universe selection model
     self.securities = []
     self.CustomUniverseSelectionModel = FactorUniverseSelectionModel(self)
     self.AddUniverse(self.CustomUniverseSelectionModel.SelectCoarse, self.CustomUniverseSelectionModel.SelectFine)
     
     # Alpha model
     self.CustomAlphaModel = ValueAlphaModel()
     
     # Portfolio construction model
     self.CustomPortfolioConstructionModel = OptimisationPortfolioConstructionModel()
     
     # Execution model
     self.CustomExecution = Execution()
     
     # Add SPY for trading days data
     self.AddEquity('SPY', Resolution.Daily)
     
     # Schedule rebalancing
     self.Schedule.On(self.DateRules.EveryDay('SPY'), self.TimeRules.At(13, 0), Action(self.RebalancePortfolio))
     
     # Init charting
     InitCharts(self)
     
     # Schedule charting
     self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday), self.TimeRules.BeforeMarketClose('SPY', 0), Action(self.PlotCharts))
Exemplo n.º 2
0
class TradingBot(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(datetime.now() - timedelta(10))
        self.SetCash(100000)

        # *Data Resolution
        self.UniverseSettings.Resolution = Resolution.Minute

        # *Universe selection model; runs with the above data resolution
        # custom universe selection model class created -- from universe_selection -->UniverseSelectionModel()
        self.securities = []
        self.CustomUniverseSelectionModel = FactorUniverseSelectionModel(self)
        self.AddUniverse(self.CustomUniverseSelectionModel.SelectCoarse,
                         self.CustomUniverseSelectionModel.SelectFine)

        # *Alpha model; A
        self.CustomAlphaModel = ValueAlphaModel()

        # *Portfolio construction model; B
        self.CustomPortfolioConstructionModel = OptimisationPortfolioConstructionModel(
            turnover=0.05, max_wt=0.05, longshort=True)

        #Eexecution model; C
        self.CustomExecution = Execution(liq_tol=0.005)

        # *Add SPY for trading days data; a
        self.AddEquity('SPY', Resolution.Daily)

        # *Scheduling rebalancing; b ; we take the a daily resloution and at 2 oclock we execute a rebalance
        self.Schedule.On(self.DateRules.EveryDay('SPY'),
                         self.TimeRules.At(13, 0),
                         Action(self.RebalancePortfolio))

        # Init charting
        InitCharts(self)

        # Schedule charting
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday),
                         self.TimeRules.BeforeMarketClose('SPY', 0),
                         Action(self.PlotCharts))

    def OnData(self, data):
        pass

    # this controls the A B C ; we chose when we rebalance; we generate our alpha scores ;we pass the alpha scores into our portfolio construction
    #next we execute orders based on our portfolio construction
    def RebalancePortfolio(self):
        alpha_df = self.CustomAlphaModel.GenerateAlphaScores(
            self, self.securities)
        portfolio = self.CustomPortfolioConstructionModel.GenerateOptimalPortfolio(
            self, alpha_df)
        self.CustomExecution.ExecutePortfolio(self, portfolio)

    def PlotCharts(self):
        PlotPerformanceChart(self)
        PlotPosConcentrationChart(self)
        PlotStockCountChart(self)
        PlotExposureChart(self)
Exemplo n.º 3
0
class TradingBot(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2019, 1, 1)
        self.SetEndDate(2020, 1, 1)
        self.SetCash(100000)

        # Data resolution
        # By default, assets selected by universe selection are requested with minute resolution data.
        # https://www.quantconnect.com/docs/algorithm-reference/universes
        self.UniverseSettings.Resolution = Resolution.Minute
        self.UniverseSettings.Leverage = 1

        # Universe selection model
        self.securities = []
        self.CustomUniverseSelectionModel = FactorUniverseSelectionModel(self)
        self.AddUniverse(self.CustomUniverseSelectionModel.SelectCoarse,
                         self.CustomUniverseSelectionModel.SelectFine)

        # Alpha model
        self.CustomAlphaModel = ValueAlphaModel()

        # Portfolio construction model
        self.CustomPortfolioConstructionModel = OptimisationPortfolioConstructionModel(
            turnover=0.05, max_wt=0.05, longshort=True)

        # Execution model
        self.CustomExecution = Execution(liq_tol=0.005)

        # Add SPY for trading days data
        self.AddEquity('SPY', Resolution.Daily)

        # Schedule rebalancing
        self.Schedule.On(self.DateRules.EveryDay('SPY'),
                         self.TimeRules.At(13, 0),
                         Action(self.RebalancePortfolio))

        # Init charting
        InitCharts(self)

        # Schedule charting
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday),
                         self.TimeRules.BeforeMarketClose('SPY', 0),
                         Action(self.PlotCharts))

    def OnData(self, data):
        pass

    def RebalancePortfolio(self):
        alpha_df = self.CustomAlphaModel.GenerateAlphaScores(
            self, self.securities)
        portfolio = self.CustomPortfolioConstructionModel.GenerateOptimalPortfolio(
            self, alpha_df)
        self.CustomExecution.ExecutePortfolio(self, portfolio)

    def PlotCharts(self):
        PlotPerformanceChart(self)
        PlotPosConcentrationChart(self)
        PlotStockCountChart(self)
        PlotExposureChart(self)
Exemplo n.º 4
0
    def Initialize(self):
        self.SetStartDate(2017, 1, 1)  # Set Start Date
        self.SetEndDate(2020, 5, 20)
        self.SetCash(100000)  # Set Strategy Cash

        # Weighting style - normalise or alpha_max (alpha maximisation w/ optimisation)
        self.weighting_style = 'normalise'

        # Market neutral
        self.mkt_neutral = True

        # Audio feature to use
        self.audio_feature = 'valence'

        # Get data
        self.data, self.etf_list, self.etf_country = self.DataSetup()

        # Add ETFs
        for etf in self.etf_list:
            self.AddEquity(etf, Resolution.Minute)

        # Portfolio construction model
        self.CustomPortfolioConstructionModel = OptimisationPortfolioConstructionModel(
            turnover=1,
            max_wt=0.2,
            longshort=True,
            mkt_neutral=self.mkt_neutral)

        # Execution model
        self.CustomExecution = Execution(liq_tol=0.005)

        # Schedule rebalancing
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Wednesday),
                         self.TimeRules.BeforeMarketClose('IVV', 210),
                         Action(self.RebalancePortfolio))

        # Init charting
        InitCharts(self)

        # Schedule charting
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Wednesday),
                         self.TimeRules.BeforeMarketClose('IVV', 5),
                         Action(self.PlotCharts))
Exemplo n.º 5
0
class TradingBot(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2003, 1, 1)
        self.SetCash(100000)

        # Data resolution
        self.UniverseSettings.Resolution = Resolution.Minute

        # Universe selection model
        self.securities = []
        self.CustomUniverseSelectionModel = FactorUniverseSelectionModel(self)
        self.AddUniverse(self.CustomUniverseSelectionModel.SelectCoarse,
                         self.CustomUniverseSelectionModel.SelectFine)

        # Alpha model
        self.CustomAlphaModel = ValueAlphaModel()

        # Portfolio construction model
        self.CustomPortfolioConstructionModel = OptimisationPortfolioConstructionModel(
        )

        # Execution model
        self.CustomExecution = Execution()

        # Add SPY for trading days data
        self.AddEquity('SPY', Resolution.Daily)

        # Schedule rebalancing
        self.Schedule.On(self.DateRules.EveryDay('SPY'),
                         self.TimeRules.At(13, 0),
                         Action(self.RebalancePortfolio))

        # Init charting
        InitCharts(self)

        # Schedule charting
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday),
                         self.TimeRules.BeforeMarketClose('SPY', 0),
                         Action(self.PlotCharts))

    def OnData(self, data):
        pass

    def RebalancePortfolio(self):
        alpha_df = self.CustomAlphaModel.GenerateAlphaScores(
            self, self.securities)
        portfolio = self.CustomPortfolioConstructionModel.GenerateOptimalPortfolio(
            self, alpha_df)
        self.CustomExecution.ExecutePortfolio(self, portfolio)

    def PlotCharts(self):
        PlotPerformanceChart(self)
        PlotPosConcentrationChart(self)
Exemplo n.º 6
0
    def Initialize(self):
        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(datetime.now() - timedelta(10))
        self.SetCash(100000)

        # *Data Resolution
        self.UniverseSettings.Resolution = Resolution.Minute

        # *Universe selection model; runs with the above data resolution
        # custom universe selection model class created -- from universe_selection -->UniverseSelectionModel()
        self.securities = []
        self.CustomUniverseSelectionModel = FactorUniverseSelectionModel(self)
        self.AddUniverse(self.CustomUniverseSelectionModel.SelectCoarse,
                         self.CustomUniverseSelectionModel.SelectFine)

        # *Alpha model; A
        self.CustomAlphaModel = ValueAlphaModel()

        # *Portfolio construction model; B
        self.CustomPortfolioConstructionModel = OptimisationPortfolioConstructionModel(
            turnover=0.05, max_wt=0.05, longshort=True)

        #Eexecution model; C
        self.CustomExecution = Execution(liq_tol=0.005)

        # *Add SPY for trading days data; a
        self.AddEquity('SPY', Resolution.Daily)

        # *Scheduling rebalancing; b ; we take the a daily resloution and at 2 oclock we execute a rebalance
        self.Schedule.On(self.DateRules.EveryDay('SPY'),
                         self.TimeRules.At(13, 0),
                         Action(self.RebalancePortfolio))

        # Init charting
        InitCharts(self)

        # Schedule charting
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday),
                         self.TimeRules.BeforeMarketClose('SPY', 0),
                         Action(self.PlotCharts))
Exemplo n.º 7
0
class StockifySentiment(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2017, 1, 1)  # Set Start Date
        self.SetEndDate(2020, 5, 20)
        self.SetCash(100000)  # Set Strategy Cash

        # Weighting style - normalise or alpha_max (alpha maximisation w/ optimisation)
        self.weighting_style = 'normalise'

        # Market neutral
        self.mkt_neutral = True

        # Audio feature to use
        self.audio_feature = 'valence'

        # Get data
        self.data, self.etf_list, self.etf_country = self.DataSetup()

        # Add ETFs
        for etf in self.etf_list:
            self.AddEquity(etf, Resolution.Minute)

        # Portfolio construction model
        self.CustomPortfolioConstructionModel = OptimisationPortfolioConstructionModel(
            turnover=1,
            max_wt=0.2,
            longshort=True,
            mkt_neutral=self.mkt_neutral)

        # Execution model
        self.CustomExecution = Execution(liq_tol=0.005)

        # Schedule rebalancing
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Wednesday),
                         self.TimeRules.BeforeMarketClose('IVV', 210),
                         Action(self.RebalancePortfolio))

        # Init charting
        InitCharts(self)

        # Schedule charting
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Wednesday),
                         self.TimeRules.BeforeMarketClose('IVV', 5),
                         Action(self.PlotCharts))

    def OnData(self, data):
        pass

    def RebalancePortfolio(self):
        df = self.data.loc[self.Time -
                           timedelta(7):self.Time].reset_index().set_index(
                               'symbol')

        if self.weighting_style == 'normalise':
            portfolio = normalise(df['alpha_score'], equal_ls=self.mkt_neutral)
        elif self.weighting_style == 'alpha_max':
            df = df[['alpha_score']]
            portfolio = self.CustomPortfolioConstructionModel.GenerateOptimalPortfolio(
                self, df)
        else:
            raise Exception('Invalid weighting style')

        self.CustomExecution.ExecutePortfolio(self, portfolio)

    def PlotCharts(self):
        PlotPerformanceChart(self)
        PlotExposureChart(self)
        PlotCountryExposureChart(self)

    def DataSetup(self):
        df = pd.read_csv(
            StringIO(
                self.Download(
                    'https://raw.githubusercontent.com/Ollie-Hooper/StockifySentiment/master/data/scores.csv'
                )))
        data = df[['date', 'country', f's_{self.audio_feature}']].copy()
        data['date'] = pd.to_datetime(data['date'])
        data.rename(columns={f's_{self.audio_feature}': 'alpha_score'},
                    inplace=True)
        etf_df = pd.read_csv(
            StringIO(
                self.Download(
                    'https://raw.githubusercontent.com/Ollie-Hooper/StockifySentiment/master/data/etf.csv'
                )))
        data = pd.merge(data, etf_df)
        data = data.sort_values('date')
        data.set_index(['date', 'symbol'], inplace=True)
        data = data[['alpha_score']]
        return data, etf_df['symbol'].to_list(), etf_df.set_index(
            'symbol')['country_name'].to_dict()