Esempio n. 1
0
 def test_Bollinger(self):
     indicator = BollingerIndicator()
     self.assertEquals(20, indicator.required_days())
     result = indicator.calculate(self.prices)
     self.assertEquals(176, len(result))
     last_row = result.iloc[-1]
     self.assertEquals(0.93401469261655201,
                       last_row[HeaderFactory.Bollinger])
Esempio n. 2
0
def test_real_machine():
    Portfolio.resources = YahooMarketDataSource()
    PortfolioAnalyser.interest_rate = 0.015
    indicators = CombinedIndicator((MomentumIndicator(), BollingerIndicator(),
                                    RsiIndicator(), MACDIndicator()))

    symbol_list = ['RY', 'AZNCF', 'GSK', 'GLD']
    symbol_list = ['EZJ.L']
    symbol_list = ['CS']

    for symbol in symbol_list:
        algo = LinearAlgoTrader(indicators, dump=True)
        manager = MachineStrategyManager(symbol, algo)
        start_date = datetime(2014, 12, 31)
        end_date = datetime(2016, 3, 31)
        manager.train_strategy(start_date, end_date)

        start_date = datetime(2016, 4, 1)
        end_date = datetime(2016, 7, 28)
        df_prices = PortfolioOrders.resources.get_data(["SPY"], start_date,
                                                       end_date, True)
        instructions = manager.process_strategy(start_date,
                                                end_date,
                                                threshold=0.01)

        portfolio = PortfolioOrders(instructions, 10000)
        analysis = PortfolioAnalyser.assess_portfolio(portfolio)

        plot_performance(analysis, df_prices)
 def test_train(self):
     indicators = CombinedIndicator((MomentumIndicator(), BollingerIndicator(), RsiIndicator()))
     algo = LinearAlgoTrader(indicators)
     algo.train(self.train_prices)
     data, rmse, c = algo.predict(self.test_prices)
     self.assertEquals(0.027115168423577626, rmse)
     self.assertEquals(0.25628745701762157, c)
Esempio n. 4
0
    def load_data(self,
                  stock,
                  days,
                  sentiment_location=None,
                  source=QuandlMarketDataSource(),
                  full_articles=True,
                  from_date='2011-04-01',
                  to_date='2015-04-01'):
        articles = None
        if sentiment_location is not None:
            articles = self.load_sentiment(sentiment_location, full_articles)
        price_df = source.get_stock_data(stock)
        # noinspection PyTypeChecker
        indicators = CombinedIndicator(
            (MomentumIndicator(1), MomentumIndicator(5), BollingerIndicator(),
             MACDIndicator(), CommodityChannelIndex(),
             AverageDirectionalIndex(), TripleExponentialMovingAverage(),
             AverageTrueRange(), RsiIndicator(), Williams()))
        ma = [50, 100, 200]

        market = MarketData(stock,
                            price_df,
                            days=days,
                            ma=ma,
                            indicator=indicators,
                            df_additional=articles)
        price_df = market.get_stock_data()
        return market.get_binary_data(price_df,
                                      days=days,
                                      from_date=from_date,
                                      to_date=to_date)
Esempio n. 5
0
def test_ML4T():
    indicators = CombinedIndicator(
        (MomentumIndicator(), BollingerIndicator(), RsiIndicator()))
    algo = LinearAlgoTrader(indicators)
    symbol = 'ML4T-220'
    manager = MachineStrategyManager(symbol, algo)
    start_date = datetime(2007, 12, 31)
    end_date = datetime(2009, 12, 31)
    df_prices = PortfolioOrders.resources.get_data([symbol], start_date,
                                                   end_date, True)
    manager.train_strategy(start_date, end_date)
    result, rmse, c = manager.algo.predict(df_prices)
    ax = result.plot(title="Predicted", label="My ", fontsize=2)
    data = df_prices.Price
    y_data = data.shift(-5) / data - 1.0
    y_data.plot(label="Actual", fontsize=2, ax=ax)
    plt.legend(loc='upper left')
    plt.show()
    def test_construct(self):
        indicators = CombinedIndicator(
            (MomentumIndicator(), BollingerIndicator(), RsiIndicator(),
             MACDIndicator()))
        algo = LinearAlgoTrader(indicators)
        manager = MachineStrategyManager('IBM', algo)
        from_date = datetime(2007, 12, 31)
        to_date = datetime(2009, 12, 31)
        manager.train_strategy(from_date, to_date)

        from_date = datetime(2009, 12, 31)
        to_date = datetime(2010, 12, 31)
        # from_date = datetime(2007, 12, 31)
        # to_date = datetime(2009, 12, 31)
        instructions = manager.process_strategy(from_date,
                                                to_date,
                                                threshold=0.01)

        portfolio = PortfolioOrders(instructions, 10000, to_date)
        analysis = PortfolioAnalyser.assess_portfolio(portfolio)

        self.assertEquals(round(0.3524, 4), round(analysis.cumulative_return,
                                                  4))
Esempio n. 7
0
 def test_Combined(self):
     indicator = CombinedIndicator((RsiIndicator(), BollingerIndicator()))
     self.assertEquals(20, indicator.required_days())
     result = indicator.calculate(self.prices)
     self.assertEquals(176, len(result))
 def test_MC3_2(self):
     indicators = CombinedIndicator(
         (MomentumIndicator(), BollingerIndicator(), RsiIndicator()))
     algo = LinearAlgoTrader(indicators)
     manager = MachineStrategyManager('IBM', algo)
     manager.train_strategy(datetime(2007, 12, 31), datetime(2009, 12, 31))