Beispiel #1
0
 def update_fundamentals(self):
     """Updates all fundamentals of stocks
     """
     # select stock if first google symbol
     stocks = list(
         select((pit.stock, sym.name) for pit in PriceItem
                for sym in pit.symbols if (Tag.GOG in sym.item.tags.name)
                and sym.id == min(pit.symbols.id)))
     # filter specific stocks if not all
     if ALL_SYMBOLS not in self.symbols:
         stocks_filtered = []
         for stock in stocks:
             for sym in self.symbols:
                 for st_sym in stock[0].price_item.symbols:
                     if sym == st_sym.name:
                         stocks_filtered.append(stock)
         stocks = stocks_filtered
     # create list of google symbols
     gog_syms = [sto[1] for sto in stocks]
     fundamentals = Fundamentals(base_url=Fundamentals.BASE_URL)
     tickers = fundamentals.get_ticker_ids(gog_syms)
     for ticker in tickers:
         self.logger.info('Download fundamentals for {}'.format(
             tickers[ticker]))
         stock = [sto[0] for sto in stocks if sto[1] == ticker]
         if len(stock) != 1:
             self.logger.warning(
                 'Can not download fundamentals for {}'.format(ticker))
             continue
         stock = stock[0]
         ica = fundamentals.get_income_analysis(tickers[ticker])
         ifc = fundamentals.get_income_facts(tickers[ticker])
         rec = fundamentals.get_recommendation(tickers[ticker])
         ble = fundamentals.get_balance(tickers[ticker])
         ico = fundamentals.get_income(tickers[ticker])
         csh = fundamentals.get_cash_flow(tickers[ticker])
         for val in [(ica, Tag.ICA), (ifc, Tag.ICF), (rec, Tag.REC),
                     (ble, Tag.BLE), (ico, Tag.ICO), (csh, Tag.CSH)]:
             # hash stock name, tag and data
             m = hashlib.sha256()
             m.update(val[1].encode('UTF-8'))
             m.update(stock.name.encode('UTF-8'))
             data_str = json.dumps(val[0])
             m.update(data_str.encode('UTF-8'))
             shahash = m.hexdigest()
             # only add data if not exist
             if Data.get(hash=shahash) is None:
                 tag = Tag.get(name=val[1])
                 obj = Data(data=val[0],
                            hash=shahash,
                            data_item=DataItem(item=Item(tags=[tag])))
                 stock.data_items.add(obj.data_item)
     commit()
Beispiel #2
0
 def __add_stock_to_index(self, index, stock_info):
     stock_in_db = Stock.get(name=stock_info['name'])
     if stock_in_db:
         self.logger.info('Add stock {}:{} to index.'.format(
             index.name, stock_in_db.name))
         index.stocks.add(stock_in_db)
     else:
         self.logger.info('Add stock {}:{} to db'.format(
             index.name, stock_info[Type.SYM]))
         # create stock
         stock = Stock(name=stock_info['name'],
                       price_item=PriceItem(item=Item()))
         # add symbols
         yao = Tag.get(name=Tag.YAO)
         gog = Tag.get(name=Tag.GOG)
         usd = Tag.get(name=Tag.USD)
         eur = Tag.get(name=Tag.EUR)
         for symbol in stock_info['symbols']:
             if Tag.GOG in symbol and symbol[Tag.GOG] != '-':
                 self.__create_symbol(stock, Tag.GOG, gog, symbol, eur, usd)
             if Tag.YAO in symbol and symbol[Tag.YAO] != '-':
                 self.__create_symbol(stock, Tag.YAO, yao, symbol, eur, usd)
         index.stocks.add(stock)
         # connect stock with industry and country
         # country
         name = stock_info['country']
         country = Tag.select(
             lambda t: t.name == name and t.type.name == Type.REG).first()
         country.items.add(stock.price_item.item)
         # industry
         indus = stock_info['industries']
         industries = Tag.select(
             lambda t: t.name in indus and t.type.name == Type.IND)
         for industry in industries:
             industry.items.add(stock.price_item.item)
Beispiel #3
0
 def add_indices_and_stocks(self, indices_list):
     for index_name in indices_list:
         index = Index(name=index_name, price_item=PriceItem(item=Item()))
         # add index symbol
         yah_sym = self.ticker_symbols.index_to_yahoo_symbol(index_name)
         if yah_sym is None:
             self.logger.warning(
                 'Can not translate {} into yahoo symbol'.format(
                     index_name))
             continue
         idx_item = Item()
         idx_item.tags.add(Tag.get(name=Tag.IDX))
         index.price_item.symbols.create(name=yah_sym, item=idx_item)
         stocks = self.ticker_symbols.get_stocks_by_index(index.name)
         for stock_info in stocks:
             self.__add_stock_to_index(index, stock_info)
         commit()
Beispiel #4
0
 def test_build(self):
     """
     Tests filter builder
     :return:
     """
     logger = logging.getLogger('test')
     # create simple filter
     my_filter = StockIsHot(
         {
             'name': 'StockIsHot6Month',
             'bars': True,
             'index_bars': False,
             'args': {
                 'threshold_buy': 0.8,
                 'threshold_sell': 0.5,
                 'intervals': [7, 30],
                 'lookback': 6,
             },
         },
         logger,
     )
     # check missing arguments
     builder = BuildFilters({}, logger)
     self.assertEqual(1, builder.build())
     # check simple build
     builder = BuildFilters(
         {
             'filters': [my_filter],
             'symbols': ['IFX.F'],
             'now_date': datetime(2019, 10, 5, 18, 00),
         },
         logger,
     )
     self.assertEqual(0, builder.build())
     with db_session:
         tag_ctx = Tag.select(
             lambda t: t.name == 'StockIsHot6Month').count()
         self.assertEqual(1, tag_ctx)
    def __build(self, my_filter, stock, symbol):
        my_filter.now_date = self.now_date
        if my_filter.look_back_date():
            my_filter.set_stock(stock)
            # set bar prices
            if my_filter.need_bars:
                bars = Price.select(
                    lambda p: p.symbol.name == symbol.name
                    and p.date >= my_filter.look_back_date()
                    and p.date <= self.now_date
                )
                my_filter.set_bars(bars)
            # set index
            if my_filter.need_index_bars:
                # get index symbol of stock
                index_sym = select(
                    i.price_item.symbols.name for i in Index
                    if i in stock.indexs
                ).first()

                bars = Price.select(
                    lambda p: p.symbol.name == index_sym
                    and p.date >= my_filter.look_back_date()
                    and p.date <= self.now_date
                )
                my_filter.set_index_bars(bars)

            strategy_status = my_filter.analyse()
            strategy_value = my_filter.get_calculation()
            fil_typ = Type.get(name=Type.FIL)

            fil_tag = Tag.select(lambda t: t.name == my_filter.name and
                                 t.type.name == Type.FIL) or \
                Tag(name=my_filter.name, type=fil_typ)

            my_res = Result(
                value=strategy_value,
                status=strategy_status,
                date=self.now_date
            )

            # add arguments to result
            arg_typ = Type.get(name=Type.ARG)
            for arg in my_filter.args:
                arg_tag = Tag.select(lambda t: t.name == arg and
                                     t.type.name == Type.ARG) or \
                                     Tag(name=arg, type=arg_typ)
                item = Item()
                item.add_tags([arg_tag])
                arg_str = json.dumps(my_filter.args[arg])
                Argument(item=item, arg=arg_str, result=my_res)
            # create signal item
            sig_item = Item()
            sig_item.add_tags([fil_tag])
            my_sig = Signal(item=sig_item, result=my_res)
            # add stock to signal
            my_sig.price_items.add(symbol.price_item)
            if strategy_status == BaseFilter.BUY:
                self.logger.debug("Buy %s", symbol)
            elif strategy_status == BaseFilter.HOLD:
                self.logger.debug("Hold %s", symbol)
            else:
                self.logger.debug("Do not buy Stock %s ", symbol)