Ejemplo n.º 1
0
    def test_read_data_without_factory_but_HistoricalDataReader(self):
        stock_data_container_list = []
        # Todo mit mehreren testen, auch ohne file --> fileinhalt mit übergeben --> dann kann ichs faken
        # --> file zugriff nicht im webreader drinnen
        apple_stock_data_container = StockDataContainer(
            "Apple Inc.", "AAPL", "")
        intel_container = StockDataContainer("Intel Corporation", "INTC", "")
        stock_data_container_list.append(apple_stock_data_container)
        stock_data_container_list.append(intel_container)

        self.assertEqual(
            len(stock_data_container_list[0].historical_stock_data()), 0)
        strategy_parameter_dict = {
            'Name': 'HistoricalDataReader',
            'weeks_delta': 52,
            'data_source': 'iex'
        }
        # TODO testen der genauen ergebnisse mit einer test datei stocks_dfs --> TestData...
        data_reader = HistoricalDataReader(stock_data_container_list,
                                           stock_data_container_file, False,
                                           strategy_parameter_dict)
        data_reader.read_data()

        # the container must have at least 200 entry days for last and current year
        self.assertEqual(len(stock_data_container_list), 2)
        self.assertGreater(
            len(stock_data_container_list[0].historical_stock_data()), 200)
        self.assertGreater(
            len(stock_data_container_list[1].historical_stock_data()), 200)
        self.assertNotEqual(
            stock_data_container_list[1].historical_stock_data()[
                GlobalVariables.get_stock_data_labels_dict()['High']][0],
            stock_data_container_list[0].historical_stock_data()[
                GlobalVariables.get_stock_data_labels_dict()['High']][0])
def convert_backtrader_to_asta_data(hist_data, news_data_dict, date_time,
                                    stock_data_container_list):
    """
    Converts the backtrader nect-data into asta stock data container format
    :param hist_data: historical dat in backtrader format
    :param news_data_dict: dict with all news data
    :param date_time: current date to look up in news
    :param stock_data_container_list: list to insert result
    :return: - return as ref of the data list
    """
    stock_name = hist_data._name
    dataname = hist_data._dataname
    curr_news = ""
    # add the news text because backtrader does not support news
    # data from pandas do not have a name --> not add news
    if isinstance(dataname, str):
        if dataname in news_data_dict:
            news_data = news_data_dict[dataname]
            if hasattr(news_data, "NewsText") and hasattr(news_data, "Date"):
                for currEntry in range(0, len(news_data.Date)):
                    if str(date_time) in news_data.Date[currEntry]:
                        try:
                            curr_news = str(news_data.NewsText[currEntry])
                        except Exception as e:
                            pass
                        break

    # convert backtrader format to asta-format
    df1 = convert_backtrader_to_dataframe(hist_data)
    # ticker not implemented, but not needed
    stock_data_container = StockDataContainer(stock_name, "", "")
    stock_data_container.set_historical_stock_data(df1)
    news_dec = NewsDataContainerDecorator(stock_data_container, 0, 0,
                                          curr_news)
    stock_data_container_list.append(news_dec)
Ejemplo n.º 3
0
    def test_buy_recommendations__apple_today__not_buy(self):
        orders_test_file = GlobalVariables.get_test_data_files_path(
        ) + "orders_test.csv"
        broker = IBPyInteractiveBrokers(orders_test_file)

        # insert apple as last entry today
        curr_order_id = broker._read_current_order_id()
        broker._save_current_order(curr_order_id, "AAPL")

        container = StockDataContainer("Apple Inc.", "AAPL", "en")
        container.set_stop_buy(12)
        container.set_stop_loss(10)
        container.set_position_size(200)
        container.update_used_strategy_and_recommendation(
            "TestStrategy", "BUY")

        stocks = [container]
        max_num_of_different_stocks_to_buy = 2

        buy_recommendations(broker, stocks, max_num_of_different_stocks_to_buy)
        sleep(0.5)
        error_message_list = broker.get_and_clear_error_message_list()

        # the order id should be the next to the last manual entry,
        # because nothing should be bought
        next_order_id = broker._read_current_order_id()
        self.assertEqual(curr_order_id + 1, next_order_id)
        self.assertEqual(0, len(error_message_list))
    def test_run_strategy__no52high__empty_result(self):
        w52hi_parameter_dict = {
            'check_days': 5,
            'min_cnt': 3,
            'min_vol_dev_fact': 1.2,
            'within52w_high_fact': 0.98
        }
        # volume higher, but stock value under 52w high within 98%
        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [
            ('2016-09-30', 23.35, 23.91, 23.24, 23.8,
             31000),  # --> 23.91 is highest high
            ('2016-10-03', 23.68, 23.69, 23.39, 23.5, 31000),
            ('2016-10-04', 23.52, 23.64, 23.18, 23.28, 31000),
            ('2016-10-05', 23.28, 23.51, 23.27, 23.43, 31000),
            ('2016-10-06', 23.38, 23.56, 23.29, 23.48, 42000),
            ('2016-10-07', 23.58, 23.65, 23.37, 23.48, 43000),
            ('2016-10-10', 23.62, 23.88, 23.55, 23.77, 44000),
            ('2016-10-11', 23.62, 23.74, 23.01, 23.16, 45000),
            ('2016-10-12', 23.16, 23.21, 23.11, 23.18, 46000)
        ]  # --> 23.91 * 0, 98 = 23.431 > 23.21

        df = DataFrame.from_records(data, columns=labels)
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")
        stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [stock_data_container]
        stock_screener = StrategyFactory()
        w52_hi_strat = stock_screener.prepare(
            "W52HighTechnicalStrategy",
            stock_data_container_list=stock_data_container_list,
            analysis_parameters=w52hi_parameter_dict)
        results = w52_hi_strat.run_strategy()
        self.assertEqual(0, len(results))
Ejemplo n.º 5
0
    def test_get_ticker_data_with_webreader__get_apple_data(self):
        # Todo mit mehreren testen, auch ohne file --> fileinhalt mit übergeben --> dann kann ichs faken
        # --> file zugriff nicht im webreader drinnen
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")

        stock_data_container_list = [stock_data_container]
        # stock_data_container_list = [stock_data_container, stock_data_container2]

        self.assertEqual(
            len(stock_data_container_list[0].historical_stock_data()), 0)

        # TODO testen der genauen ergebnisse mit einer test datei stocks_dfs --> TestData...
        strategy_parameter_dict = {
            'Name': 'HistoricalDataReader',
            'parameter_dict': {
                'weeks_delta': 52,
                'data_source': 'iex'
            }
        }
        data_reader = HistoricalDataReader(stock_data_container_list,
                                           stock_data_container_file, True,
                                           strategy_parameter_dict)
        df = data_reader._get_ticker_data_with_webreader(
            stock_data_container.stock_ticker(),
            stock_data_container.stock_exchange(),
            data_source,
            weeks_delta=52)

        self.assertGreater(len(df), 200)
Ejemplo n.º 6
0
 def test_NewsDataContainerDecorator_set_stop_loss(self):
     container = StockDataContainer("test1", "t1", "en")
     container.set_stop_loss(10)
     self.assertEqual(10, container.get_stop_loss())
     news_dec = NewsDataContainerDecorator(container, 111, 1, "test news")
     news_dec.set_stop_loss(20)
     self.assertEqual(20, news_dec.get_stop_loss())
Ejemplo n.º 7
0
    def test_NewsDataContainerDecorator_set_historical_stock_data(self):
        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [('2016-09-13', 90, 90, 100.15, 100.26, 4000)]

        df = DataFrame.from_records(data, columns=labels)
        apple_stock_data_container = StockDataContainer(
            "Apple Inc.", "AAPL", "")
        apple_stock_data_container.set_historical_stock_data(df)

        sd = apple_stock_data_container.historical_stock_data()

        for col in range(0, len(data[0])):
            cur_val = sd[sd.keys()[col]][0]
            des_val = data[0][col]
            self.assertEqual(des_val, cur_val)

        news_dec = NewsDataContainerDecorator(apple_stock_data_container, 111,
                                              1, "test news")

        data_2 = [('2017-11-11', 11, 22, 33, 44, 55)]
        df_2 = DataFrame.from_records(data_2, columns=labels)
        news_dec.set_historical_stock_data(df_2)

        sd = news_dec.historical_stock_data()

        for col in range(0, len(data_2[0])):
            cur_val = sd[sd.keys()[col]][0]
            des_val = data_2[0][col]
            self.assertEqual(des_val, cur_val)
    def test_get_implemented_strategies_list(self):
        w52hi_parameter_dict = {
            'check_days': 5,
            'min_cnt': 3,
            'min_vol_dev_fact': 1.2,
            'within52w_high_fact': 0.98
        }

        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [
            ('2016-09-30', 23.35, 23.91, 23.24, 23.8, 31000),
        ]

        df = DataFrame.from_records(data, columns=labels)
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")
        stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [stock_data_container]

        ##################################################
        # 52 w strategy
        stock_screener = StrategyFactory()
        w52_hi_strat = stock_screener.prepare(
            "W52HighTechnicalStrategy",
            stock_data_container_list=stock_data_container_list,
            analysis_parameters=w52hi_parameter_dict)

        self.assertNotEqual(None, w52_hi_strat)
        self.assertTrue(isinstance(w52_hi_strat, Abstract_Strategy))
Ejemplo n.º 9
0
 def test_NewsDataContainerDecorator_set_position_size(self):
     container = StockDataContainer("test1", "t1", "en")
     container.set_position_size(100)
     self.assertEqual(100, container.get_position_size())
     news_dec = NewsDataContainerDecorator(container, 111, 1, "test news")
     news_dec.set_position_size(20)
     self.assertEqual(20, news_dec.get_position_size())
Ejemplo n.º 10
0
    def test_read_data_with_DataReaderFactory__HistoricalDataReader_read_apple__read_intel(
            self):
        stock_data_container_list = []
        apple_stock_data_container = StockDataContainer(
            "Apple Inc.", "AAPL", "")
        intel_container = StockDataContainer("Intel Corporation", "INTC", "")
        stock_data_container_list.append(apple_stock_data_container)
        stock_data_container_list.append(intel_container)

        self.assertEqual(
            len(stock_data_container_list[0].historical_stock_data()), 0)
        strategy_parameter_dict = {
            'Name': 'HistoricalDataReader',
            'weeks_delta': 52,
            'data_source': 'iex'
        }
        data_storage = DataReaderFactory()
        stock_data_reader = data_storage.prepare("HistoricalDataReader",
                                                 stock_data_container_list,
                                                 stock_data_container_file,
                                                 True, strategy_parameter_dict)
        stock_data_reader.read_data()

        self.assertEqual(len(stock_data_container_list), 2)
        self.assertGreater(
            len(stock_data_container_list[0].historical_stock_data()), 200)
        self.assertGreater(
            len(stock_data_container_list[1].historical_stock_data()), 200)
Ejemplo n.º 11
0
    def test_run_strategy__no_gap_EMPTY_RESULT(self):
        analysis_parameters = {'min_gap_factor': 1.1}
        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [('2016-09-13', 23.6, 23.73, 23.15, 23.26, 31000),
                ('2016-09-14', 23.33, 23.43, 22.95, 23.11, 31000),
                ('2016-09-15', 23.15, 23.77, 23.14, 23.62, 31000),
                ('2016-09-16', 23.57, 23.68, 23.38, 23.5, 31000),
                ('2016-09-19', 23.6, 23.77, 23.46, 23.51, 31000),
                ('2016-09-20', 23.73, 23.76, 23.26, 23.31, 31000),
                ('2016-09-21', 23.36, 23.78, 23.35, 23.73, 31000),
                ('2016-09-22', 23.36, 23.78, 23.35, 23.73, 31000), ]

        df = DataFrame.from_records(data, columns=labels)
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")
        stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [stock_data_container]

        ##################################################
        stock_screener = StrategyFactory()
        strat = stock_screener.prepare("GapUpHighVolumeStrategy",
                                       stock_data_container_list=stock_data_container_list,
                                       analysis_parameters=analysis_parameters)
        results = strat.run_strategy()
        self.assertEqual(len(results), 0)
Ejemplo n.º 12
0
 def test_StockDataContainer_get_names_and_values__stockname_test1__ticker_t1__exchange_en(
         self):
     container = StockDataContainer("test1", "t1", "en")
     result_container = container.get_names_and_values()
     self.assertEqual(10, len(result_container))
     self.assertEqual({}, result_container["StrategyAndRecommendation"])
     self.assertEqual("t1", result_container["Ticker"])
     self.assertEqual("test1", result_container["Stockname"])
     self.assertEqual("en", result_container["Exchange"])
Ejemplo n.º 13
0
    def test_eg_method(self):
        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [('2016-09-13', 90, 90, 100.15, 100.26, 4000)]

        df = DataFrame.from_records(data, columns=labels)
        apple_stock_data_container = StockDataContainer(
            "Apple Inc.", "AAPL", "")
        apple_stock_data_container.set_historical_stock_data(df)
        rwe_stock_data_container = StockDataContainer("RWE AG ST O.N.", "RWE",
                                                      "")
        testag_stock_data_container = StockDataContainer(
            "Test AG", "TestAG", "")
        rwe_stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [
            apple_stock_data_container, rwe_stock_data_container
        ]

        # if apple_stock_data_container in stock_data_container_list:
        self.assertEqual(
            apple_stock_data_container in stock_data_container_list, True)
        self.assertEqual(rwe_stock_data_container in stock_data_container_list,
                         True)
        self.assertEqual(
            testag_stock_data_container in stock_data_container_list, False)
    def test_StrategyAsta_SMA_and__EMA_or_RoC__result_one_SELL_entry(self):
        strategy_name = "StrategyAsta_SMA_and__EMA_or_RoC"
        parameter_dict = {
            'sma_timeperiod': 5,
            'ema_timeperiod': 5,
            'roc_timeperiod': 5,
            'data_readers': {
                'HistoricalDataReader': {
                    'weeks_delta': 52,
                    'data_source': 'iex',
                    'reload_data': False,
                    'ticker_needed': False
                }
            }
        }

        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [
            ('2016-09-30', 23.35, 23.91, 23.24, 23.8, 31000),
            ('2016-10-03', 23.68, 23.69, 23.39, 23.5, 31000),
            ('2016-10-04', 23.52, 23.64, 23.18, 23.28, 31000),
            ('2016-10-05', 23.28, 23.51, 23.27, 23.43, 31000),
            ('2016-10-06', 23.38, 23.56, 23.29, 23.48, 42000),
            ('2016-10-07', 23.58, 23.65, 23.37, 23.48, 43000),
            ('2016-10-10', 25.62, 25.88, 25.55, 25.77,
             44000),  # raising prices --> sma, ema, roc
            ('2016-10-11', 26.62, 26.74, 26.01, 26.16, 45000),
            ('2016-10-12', 27.16, 26, 27.11, 27.18, 46000),
            ('2016-10-13', 23.35, 23.91, 23.24, 23.8, 31000),
        ]

        df = DataFrame.from_records(data, columns=labels)
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")
        stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [stock_data_container]

        # sma_ema_roc strategy
        stock_screener = StrategyFactory()
        sma_ema_roc_strat = stock_screener.prepare(
            strategy_name,
            stock_data_container_list=stock_data_container_list,
            analysis_parameters=parameter_dict)

        results = sma_ema_roc_strat.run_strategy()
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0].get_stock_name(),
                         stock_data_container.get_stock_name())
        #  get the recommendation for the given strategy (BUY / SELL)
        recommendation = results[0].get_recommendation_strategies(
        )[strategy_name][0]
        self.assertEqual('SELL', recommendation)
    def test_run_strategy_with_two_news_for_one_stock(self):
        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [('2016-09-13', 90, 90, 100.15, 100.26, 4000),
                ('2016-09-14', 90, 90, 22.95, 100.11, 4000),
                ('2016-09-15', 90, 90, 100.14, 100.62, 4000)]

        df = DataFrame.from_records(data, columns=labels)

        apple_stock_data_container = NewsDataContainerDecorator(
            StockDataContainer("Apple Inc.", "AAPL", ""), 0, 0,
            "ANALYSE-FLASH: Credit Suisse nimmt Apple mit 'Underperform' wieder auf"
        )
        apple_stock_data_container2 = NewsDataContainerDecorator(
            StockDataContainer("Apple Inc.", "AAPL", ""), 0, 0,
            "ANALYSE-FLASH: Sparkasse nimmt Apple mit Buy wieder auf")

        apple_stock_data_container.set_historical_stock_data(df)
        apple_stock_data_container2.set_historical_stock_data(df)
        rwe_stock_data_container = NewsDataContainerDecorator(
            StockDataContainer("RWE AG ST O.N.", "RWE", ""), 0, 0,
            "ANALYSE-FLASH: Credit Suisse nimmt RWE mit 'Outperform' wieder auf"
        )
        rwe_stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [
            apple_stock_data_container, rwe_stock_data_container
        ]

        analysis_parameters = {
            'news_threshold': 0.7,
            'german_tagger': filepath + 'nltk_german_classifier_data.pickle'
        }
        stock_screener = StrategyFactory()
        news_strategy = stock_screener.prepare(
            "SimplePatternNewsStrategy",
            stock_data_container_list=stock_data_container_list,
            analysis_parameters=analysis_parameters)

        results = news_strategy.run_strategy()

        self.assertEqual(results[0] in stock_data_container_list, True)
        apple_idx = results.index(apple_stock_data_container)
        rwe_idx = results.index(rwe_stock_data_container)

        t1 = round(results[apple_idx].positive_prob_dist(), 2)
        self.assertEqual(results[apple_idx].get_stock_name(), "Apple Inc.")
        self.assertGreater(0.7, t1)

        t1 = round(results[rwe_idx].positive_prob_dist(), 2)
        self.assertEqual(results[rwe_idx].get_stock_name(), "RWE AG ST O.N.")
        self.assertGreater(t1, 0.7)
Ejemplo n.º 16
0
    def test_NewsDataContainerDecorator_get_names_and_values__target_price_111__stockname_test1(
            self):
        container = StockDataContainer("test1", "t1", "en")
        result_container = container.get_names_and_values()
        self.assertEqual(10, len(result_container))

        news_dec = NewsDataContainerDecorator(container, 111, 0.9, "test news")
        result_news_dec = news_dec.get_names_and_values()
        self.assertEqual(13, len(result_news_dec))
        self.assertEqual({}, result_news_dec["StrategyAndRecommendation"])
        self.assertEqual(111, result_news_dec["Target Price"])
        self.assertEqual("test1", result_news_dec["Stockname"])
        self.assertEqual(0.9, result_news_dec["Pos. Probability Distribution"])
Ejemplo n.º 17
0
    def test_insert_into_treeview(self):
        container = StockDataContainer("test1", "t1", "en")
        result_container = container.get_names_and_values()
        self.Labelframe1 = LabelFrame()
        scrolled_treeview1 = ScrolledTreeView(self.Labelframe1)
        existing_columns = [
            'Rank', 'StrategyAndRecommendation', 'Stockname', "Ticker",
            "Exchange"
        ]
        GuiUtils.insert_into_treeview(scrolled_treeview1, existing_columns,
                                      result_container)

        result_values = []
        for child in scrolled_treeview1.get_children():
            result_values.extend(scrolled_treeview1.item(child)["values"])

        self.assertEqual([0, '{}', 'test1', 't1', 'en', 0, 0, 0, 0, ''],
                         result_values)

        # ---------
        container = StockDataContainer("test1", "t1", "en")
        result_container = container.get_names_and_values()
        self.Labelframe1 = LabelFrame()
        scrolled_treeview1 = ScrolledTreeView(self.Labelframe1)
        existing_columns = [
            'Rank', 'StrategyAndRecommendation', 'Stockname', "Ticker"
        ]
        GuiUtils.insert_into_treeview(scrolled_treeview1, existing_columns,
                                      result_container)

        result_values = []
        for child in scrolled_treeview1.get_children():
            result_values.extend(scrolled_treeview1.item(child)["values"])

        self.assertEqual([0, '{}', 'test1', 't1', 'en', 0, 0, 0, 0, ''],
                         result_values)

        # --------
        container = StockDataContainer("test1", "t1", "en")
        result_container = container.get_names_and_values()
        self.Labelframe1 = LabelFrame()
        scrolled_treeview1 = ScrolledTreeView(self.Labelframe1)
        existing_columns = [
            'Rank', 'StrategyAndRecommendation', 'Stockname', "Ticker",
            "Exchange", "TEST"
        ]
        GuiUtils.insert_into_treeview(scrolled_treeview1, existing_columns,
                                      result_container)

        result_values = []
        for child in scrolled_treeview1.get_children():
            result_values.extend(scrolled_treeview1.item(child)["values"])

        self.assertEqual([0, '{}', 'test1', 't1', 'en', '-', 0, 0, 0, 0, ''],
                         result_values)
    def test_lookup_stock_abr_in_all_names(self):
        stock_data_container_list = [
            StockDataContainer("RWE AG ST O.N.", "RWE", "de"),
            StockDataContainer("RHEINMETALL AG", "RHM", "de"),
            StockDataContainer("BEIERSDORF AG O.N.", "BEI", "de"),
            StockDataContainer("ADIDAS AG NA O.N.", "ADS", "de"),
            StockDataContainer("Apple Inc.", "AAPL", "en"),
            StockDataContainer("BET-AT-HOME.COM AG O.N.", "ACX", "de"),
            StockDataContainer("Roche Holding AG", "RHHBY", ""),
            StockDataContainer("LOrealfuture", "LORFK8.EX", "")
        ]

        analysis = GermanTaggerAnalyseNews(
            stock_data_container_list, 0.7,
            test_data_filepath + 'nltk_german_classifier_data.pickle')

        result = analysis.lookup_stock_abr_in_all_names("Rheinmetall")
        self.assertEqual(result, "RHEINMETALL AG")

        result = analysis.lookup_stock_abr_in_all_names("Beiersdorf")
        self.assertEqual(result, "BEIERSDORF AG O.N.")

        result = analysis.lookup_stock_abr_in_all_names("Adidas")
        self.assertEqual(result, "ADIDAS AG NA O.N.")

        self.assertRaises(AttributeError,
                          analysis.lookup_stock_abr_in_all_names, "XCERET")
Ejemplo n.º 19
0
    def test_StockDataContainer__set_and_get_stock_current_prize(self):
        container = StockDataContainer("test1", "t1", "en", [], 10)
        self.assertEqual(container.stock_current_prize(), 10)

        container2 = StockDataContainer("test1", "t1", "en")
        container2.set_stock_current_prize(20)
        self.assertEqual(container2.stock_current_prize(), 20)
Ejemplo n.º 20
0
    def test_StockDataContainer__set_and_get_historical_stock_data(self):
        container = StockDataContainer("test1", "t1", "en")
        self.assertEqual([], container.historical_stock_data())

        cols = list(GlobalVariables.get_stock_data_labels_dict().values())
        data = [('2016-09-13', 90, 80, 100, 110, 4000)]

        df = DataFrame.from_records(data, columns=cols)
        apple_stock_data_container = StockDataContainer(
            "Apple Inc.", "AAPL", "")
        apple_stock_data_container.set_historical_stock_data(df)
        sd = apple_stock_data_container.historical_stock_data()

        self.assertEqual(
            sd[GlobalVariables.get_stock_data_labels_dict()['Date']][0],
            '2016-09-13')
        self.assertEqual(
            sd[GlobalVariables.get_stock_data_labels_dict()['Open']][0], 90)
        self.assertEqual(
            sd[GlobalVariables.get_stock_data_labels_dict()['High']][0], 80)
        self.assertEqual(
            sd[GlobalVariables.get_stock_data_labels_dict()['Low']][0], 100)
        self.assertEqual(
            sd[GlobalVariables.get_stock_data_labels_dict()['Close']][0], 110)
        self.assertEqual(
            sd[GlobalVariables.get_stock_data_labels_dict()['Volume']][0],
            4000)
    def test__volume_not_above__empty_result(self):
        w52hi_parameter_dict = {
            'check_days': 5,
            'min_cnt': 3,
            'min_vol_dev_fact': 1.2,
            'within52w_high_fact': 0.98
        }

        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [('2016-09-30', 23.35, 23.91, 23.24, 23.8, 31000),
                ('2016-10-03', 23.68, 23.69, 23.39, 23.5, 31000),
                ('2016-10-04', 23.52, 23.64, 23.18, 23.28, 31000),
                ('2016-10-05', 23.28, 23.51, 23.27, 23.43, 31000),
                ('2016-10-06', 23.38, 23.56, 23.29, 23.48, 32000),
                ('2016-10-07', 23.58, 23.65, 23.37, 23.48, 33000),
                ('2016-10-10', 23.62, 23.88, 23.55, 23.77, 34000),
                ('2016-10-11', 23.62, 23.74, 23.01, 23.16, 35000),
                ('2016-10-12', 23.16, 23.8, 23.11, 23.18, 36000)]

        # file = test_filepath + 'test_strat_52WHi_HiVolume_VolumeNotAbove.csv'
        df = DataFrame.from_records(data, columns=labels)
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")
        stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [stock_data_container]
        stock_screener = StrategyFactory()
        w52_hi_strat = stock_screener.prepare(
            "W52HighTechnicalStrategy",
            stock_data_container_list=stock_data_container_list,
            analysis_parameters=w52hi_parameter_dict)
        results = w52_hi_strat.run_strategy()
        self.assertEqual(0, len(results))

        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [('2016-09-30', 23.35, 23.91, 23.24, 23.8, 31000),
                ('2016-10-03', 23.68, 23.69, 23.39, 23.5, 31000),
                ('2016-10-04', 23.52, 23.64, 23.18, 23.28, 31000),
                ('2016-10-05', 23.28, 23.51, 23.27, 23.43, 31000),
                ('2016-10-06', 23.38, 23.56, 23.29, 23.48, 42000),
                ('2016-10-07', 23.58, 23.65, 23.37, 23.48, 43000),
                ('2016-10-10', 23.62, 23.88, 23.55, 23.77, 44000),
                ('2016-10-11', 23.62, 23.74, 23.01, 23.16, 45000),
                ('2016-10-12', 23.16, 23.0, 23.11, 23.18, 46000)]
        # file = test_filepath + 'test_strat_52WHi_HiVolume_Not52WHigh.csv'
        df = DataFrame.from_records(data, columns=labels)
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")
        stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [stock_data_container]
        stock_screener = StrategyFactory()
        w52_hi_strat = stock_screener.prepare(
            "W52HighTechnicalStrategy",
            stock_data_container_list=stock_data_container_list,
            analysis_parameters=w52hi_parameter_dict)
        results = w52_hi_strat.run_strategy()
        self.assertEqual(0, len(results))
Ejemplo n.º 22
0
    def read_table_columns_from_webpage(websource_address, find_name,
                                        class_name, table_class,
                                        ticker_column_to_read,
                                        name_column_to_read, stock_exchange):
        """
        read the sp500 tickers and saves it to given file
        :param stock_exchange:
        :param name_column_to_read:
        :param find_name:
        :param ticker_column_to_read: 0 for sp500, 2 for cdax
        :param table_class: like 'wikitable sortable' or 'wikitable sortable zebra'
        :param websource_address: like wikepedia: 'http://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
        :return: stock data container list
        """
        resp = requests.get(websource_address)
        soup = bs.BeautifulSoup(resp.text, 'lxml')
        table = soup.find(find_name, {class_name: table_class})
        stock_data_container_list = []

        if table is None or len(table) <= 0:
            raise ConnectionError("Error establishing a database connection")

        for row in table.findAll('tr')[1:]:
            ticker = row.findAll('td')[ticker_column_to_read].text
            name = row.findAll('td')[name_column_to_read].text
            ticker = ticker.replace("\n", "")
            name = name.replace("\n", "")
            stock_data_container_list.append(
                StockDataContainer(name, ticker, stock_exchange))

        Utils.Logger_Instance.logger.info("Tickers from " + websource_address +
                                          " read.")
        return stock_data_container_list
Ejemplo n.º 23
0
    def test_FixedSizeRiskModel_with_factory_and_Decorator(self):

        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [('2016-09-30', 23.35, 23.91, 23.24, 23.8, 31000),
                ('2016-10-03', 23.68, 23.69, 23.39, 23.5, 31000),
                ('2016-10-04', 23.52, 23.64, 23.18, 23.28, 31000),
                ('2016-10-05', 23.28, 23.51, 23.27, 23.43, 31000),
                ('2016-10-06', 23.38, 23.56, 23.29, 23.48, 42000),
                ('2016-10-07', 23.58, 23.65, 23.37, 23.48, 43000),
                ('2016-10-10', 23.62, 23.88, 23.55, 23.77, 44000),
                ('2016-10-11', 23.62, 23.74, 23.01, 23.16, 45000),
                ('2016-10-12', 23.16, 26, 23.11, 23.18, 46000)]

        df = DataFrame.from_records(data, columns=labels)
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")
        stock_data_container.set_historical_stock_data(df)
        news_dec = NewsDataContainerDecorator(stock_data_container, 111, 0.9,
                                              "test news")
        stock_data_container_list = [news_dec]

        rm_factory = RiskModelFactory()
        parameter_dict = {
            'OrderTarget': 'order_target_value',
            'TargetValue': 2500
        }
        fsr = rm_factory.prepare(
            "FixedSizeRiskModel",
            stock_data_container_list=stock_data_container_list,
            parameter_dict=parameter_dict)
        fsr.determine_risk()

        self.assertEqual(
            np.math.isclose(26.130,
                            stock_data_container_list[0].get_stop_buy(),
                            abs_tol=0.01), True)
        self.assertEqual(
            np.math.isclose(25.35,
                            stock_data_container_list[0].get_stop_loss(),
                            abs_tol=0.01), True)
        self.assertEqual(
            np.math.isclose(95,
                            stock_data_container_list[0].get_position_size(),
                            abs_tol=0.01), True)
        self.assertEqual("FixedSizeRiskModel",
                         stock_data_container_list[0].get_risk_model())
    def test_RiskModelFactory_and_risk_determinitation(self):
        fixes_pos_size = 2500
        # attention these are test data files
        other_params = GlobalVariables.get_other_parameters_with_default_parameters(
        )

        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)

        calc_val = 23.5  # last high value
        data = [('2016-10-07', 23.58, 23.65, 23.37, 23.48, 43000),
                ('2016-10-10', 23.62, 23.88, 23.55, 24.0, 44000),
                ('2016-10-11', 23.62, 30.0, 23.01, 23.16, 45000),
                ('2016-10-12', 23.16, calc_val, 23.11, 23.5, 46000)]

        df = DataFrame.from_records(data, columns=labels)
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")
        stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [stock_data_container]

        risk_models = other_params['RiskModels']
        for rm_name in risk_models.keys():
            rm_parameters = risk_models[rm_name]
            rm_factory = RiskModelFactory()
            fsr = rm_factory.prepare(
                rm_name,
                stock_data_container_list=stock_data_container_list,
                parameter_dict=rm_parameters)
            fsr.determine_risk()

        self.assertEqual("FixedSizeRiskModel",
                         stock_data_container_list[0].get_risk_model())

        # real calculation with real 52 w high value
        sb = round(calc_val * 1.005, 2)
        self.assertEqual(
            sb, stock_data_container_list[0].get_stop_buy())  # =23.5*1.005
        sl = round(sb * 0.97, 2)
        self.assertEqual(
            sl,
            stock_data_container_list[0].get_stop_loss())  # =23.5*1.005*0.97
        self.assertEqual(
            np.math.isclose(stock_data_container_list[0].get_position_size(),
                            int(fixes_pos_size / sb),
                            abs_tol=0.001), True)
Ejemplo n.º 25
0
    def test_(self):
        parameter_dict = {
            'sma_timeperiod': 5,
            'ema_timeperiod': 5,
            'roc_timeperiod': 5,
            'data_readers': {
                'HistoricalDataReader': {
                    'weeks_delta': 52,
                    'data_source': 'iex',
                    'reload_data': False,
                    'ticker_needed': True
                }
            }
        }

        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [('2016-09-30', 23.35, 23.91, 23.24, 23.8, 31000),
                ('2016-10-03', 23.68, 23.69, 23.39, 23.5, 31000),
                ('2016-10-04', 23.52, 23.64, 23.18, 23.28, 31000),
                ('2016-10-05', 23.28, 23.51, 23.27, 23.43, 31000),
                ('2016-10-06', 23.38, 23.56, 23.29, 23.48, 42000),
                ('2016-10-07', 23.58, 23.65, 23.37, 23.48, 43000),
                ('2016-10-10', 23.62, 23.88, 23.55, 23.77, 44000),
                ('2016-10-11', 23.62, 23.74, 23.01, 23.16, 45000),
                ('2016-10-12', 23.16, 26, 23.11, 23.18, 46000)]

        df = DataFrame.from_records(data, columns=labels)
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")
        stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [stock_data_container]

        ##################################################
        # 52 w strategy
        stock_screener = StrategyFactory()
        w52_hi_strat = stock_screener.prepare(
            "StrategyAsta_SMA_and__EMA_or_RoC",
            stock_data_container_list=stock_data_container_list,
            analysis_parameters=parameter_dict)

        results = w52_hi_strat.run_strategy()
        self.assertGreater(len(results), 0)
        self.assertEqual(results[0].get_stock_name(),
                         stock_data_container.get_stock_name())
    def test_run_strategy__strat_fulfilled__stock_in_result(self):

        w52hi_parameter_dict = {
            'check_days': 5,
            'min_cnt': 3,
            'min_vol_dev_fact': 1.2,
            'within52w_high_fact': 0.98
        }

        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [('2016-09-30', 23.35, 23.91, 23.24, 23.8, 31000),
                ('2016-10-03', 23.68, 23.69, 23.39, 23.5, 31000),
                ('2016-10-04', 23.52, 23.64, 23.18, 23.28, 31000),
                ('2016-10-05', 23.28, 23.51, 23.27, 23.43, 31000),
                ('2016-10-06', 23.38, 23.56, 23.29, 23.48, 42000),
                ('2016-10-07', 23.58, 23.65, 23.37, 23.48, 43000),
                ('2016-10-10', 23.62, 23.88, 23.55, 23.77, 44000),
                ('2016-10-11', 23.62, 23.74, 23.01, 23.16, 45000),
                ('2016-10-12', 23.16, 26, 23.11, 23.18, 46000)]

        start_time = datetime.now()

        df = DataFrame.from_records(data, columns=labels)
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")
        stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [stock_data_container]

        ##################################################
        # 52 w strategy
        stock_screener = StrategyFactory()
        w52_hi_strat = stock_screener.prepare(
            "W52HighTechnicalStrategy",
            stock_data_container_list=stock_data_container_list,
            analysis_parameters=w52hi_parameter_dict)

        results = w52_hi_strat.run_strategy()

        print("Time to run strat:" + (str(datetime.now() - start_time)))

        self.assertEqual(1, len(results))
        self.assertEqual(results[0].get_stock_name(),
                         stock_data_container.get_stock_name())
    def test_analyse_single_news_CDAX_companies(self):
        rhm = NewsDataContainerDecorator(
            StockDataContainer("RHEINMETALL AG", "RHM", "de"), 0, 0,
            "05.03.2018, ANALYSE-FLASH: NordLB hebt Rheinmetall auf 'Kaufen' - Ziel 125 Euro"
        )
        bei = NewsDataContainerDecorator(
            StockDataContainer("BEIERSDORF AG O.N.", "BEI", "de"), 0, 0,
            "ANALYSE-FLASH: Independent Research senkt Ziel für Beiersdorf auf 118 Euro"
        )
        ads = NewsDataContainerDecorator(
            StockDataContainer("ADIDAS AG NA O.N.", "ADS", "de"), 0, 0,
            "ANALYSE-FLASH: Credit Suisse nimmt Adidas mit 'Underperform' wieder auf"
        )
        # acx = NewsDataContainerDecorator(StockDataContainer("BET-AT-HOME.COM AG O.N.", "ACX", "de"), 0, 0,
        # , 0
        # rhhby = NewsDataContainerDecorator(StockDataContainer("Roche Holding AG", "RHHBY", "")
        # lor = NewsDataContainerDecorator(StockDataContainer("LOrealfuture", "LORFK8.EX", "")

        stock_data_container_list = [rhm, bei, ads]  # TODO, acx, rhhby, lor]

        analysis = GermanTaggerAnalyseNews(
            stock_data_container_list, 0.7,
            GlobalVariables.get_data_files_path() +
            'nltk_german_classifier_data.pickle')

        # CDAX companies
        result = analysis.analyse_single_news(ads)
        t1 = round(result.positive_prob_dist(), 2)
        self.assertGreater(0.7, t1)
        self.assertEqual(result.get_stock_name(), "ADIDAS AG NA O.N.")
        self.assertEqual(result.stock_ticker(), "ADS")

        result = analysis.analyse_single_news(bei)
        t1 = round(result.positive_prob_dist(), 2)
        self.assertGreater(0.7, t1)
        self.assertEqual(result.get_stock_name(), "BEIERSDORF AG O.N.")
        self.assertEqual("BEI", result.stock_ticker())

        result = analysis.analyse_single_news(rhm)
        t1 = round(result.positive_prob_dist(), 2)
        self.assertGreater(t1, 0.7)
        self.assertEqual(result.get_stock_name(), "RHEINMETALL AG")
        self.assertEqual("RHM", result.stock_ticker())
Ejemplo n.º 28
0
    def _method_to_execute(self, elm):
        """
        read news from traderfox home page with dpa-afx-compact news
        :param date_time_format: news datetime format
        :param date_file: file for last check date
        :param url: traderfox news page url
        :return: news as list
        """
        from Utils.FileUtils import FileUtils
        all_news = []

        date_time = (str(elm.footer.span.get_text()))  # date and Time
        date_time = date_time.rsplit(' Uhr')[
            0]  # TODO: split because of datetime format

        article_text = (str(elm.h2.get_text(strip=True))
                        )  # h2 --> article head line
        news_text = date_time.replace(',', '.') + ", " + article_text.replace(
            ',', '.')
        # TODO REMOVE THAT
        # THIS IS JUST FOR BACKTESTING NEWS DATA COLLECTION
        FileUtils.append_textline_to_file(
            news_text,
            GlobalVariables.get_data_files_path() + "NewsForBacktesting.txt",
            True)
        all_news.append(news_text)

        prep_news = self.text_analysis.optimize_text_for_german_tagger(
            news_text)
        name_ticker_exchange_target_prize = \
            self.text_analysis.identify_stock_name_and_stock_ticker_and_target_price_from_news_nltk_german_classifier(
                news_text)

        if name_ticker_exchange_target_prize is not None and name_ticker_exchange_target_prize.get_stock_name(
        ) != "":
            container = StockDataContainer(
                name_ticker_exchange_target_prize.get_stock_name(),
                name_ticker_exchange_target_prize.stock_ticker(),
                name_ticker_exchange_target_prize.stock_exchange())

            if container in self.stock_data_container_list:
                idx = self.stock_data_container_list.index(container)
                container_2 = self.stock_data_container_list[idx]
                if isinstance(container_2, StockDataContainer):
                    container = container_2
                    self.stock_data_container_list.remove(container_2)

            news_dec = NewsDataContainerDecorator(
                container,
                name_ticker_exchange_target_prize.stock_target_price(), 0,
                prep_news)

            self.stock_data_container_list.append(news_dec)
    def test_RiskModelFactory__with_invalid_risk_model(self):
        labels = []
        for key, value in GlobalVariables.get_stock_data_labels_dict().items():
            labels.append(value)
        data = [('2016-10-07', 23.58, 23.65, 23.37, 23.48, 43000),
                ('2016-10-10', 23.62, 23.88, 23.55, 24.0, 44000),
                ('2016-10-11', 23.62, 30.0, 23.01, 23.16, 45000),
                ('2016-10-12', 23.16, 23.0, 23.11, 23.5, 46000)]

        df = DataFrame.from_records(data, columns=labels)
        stock_data_container = StockDataContainer("Apple Inc.", "AAPL", "")
        stock_data_container.set_historical_stock_data(df)
        stock_data_container_list = [stock_data_container]

        rm_parameters = {}
        rm_factory = RiskModelFactory()
        self.assertRaises(NotImplementedError,
                          rm_factory.prepare,
                          "NotImplementedRiskModel",
                          stock_data_container_list=stock_data_container_list,
                          parameter_dict=rm_parameters)
    def test_read_5_stocks_time(self):
        stock_data_container_list = []
        apple_stock_data_container = StockDataContainer(
            "Apple Inc.", "AAPL", "")
        fb_cont = StockDataContainer("Facebook, Inc.", "FB", "")
        gis_cont = StockDataContainer("General Mills, Inc.", "GIS", "")
        ge_cont = StockDataContainer("General Electric Company.", "GE", "")
        xom_cont = StockDataContainer("Exxon Mobile Corporation", "XOM", "")

        stock_data_container_list.append(apple_stock_data_container)
        stock_data_container_list.append(fb_cont)
        stock_data_container_list.append(gis_cont)
        stock_data_container_list.append(ge_cont)
        stock_data_container_list.append(xom_cont)

        start_time = datetime.now()

        data_storage = DataReaderFactory()
        strategy_parameter_dict = {
            'Name': 'HistoricalDataReader',
            'weeks_delta': 52,
            'data_source': 'iex'
        }
        stock_data_reader = data_storage.prepare(
            "HistoricalDataReader",
            stock_data_container_list=stock_data_container_list,
            reload_stockdata=True,
            parameter_dict=strategy_parameter_dict)
        stock_data_reader.read_data()

        end_time = datetime.now()
        time_diff = end_time - start_time

        self.assertEqual(len(stock_data_container_list), 5)
        self.assertGreater(
            len(stock_data_container_list[0].historical_stock_data()), 200)
        self.assertGreater(
            len(stock_data_container_list[1].historical_stock_data()), 200)

        print("Time to get the stocks:" + (str(time_diff)))