def __create_symbol(self, stock, my_tag, my_tag_item, symbol, eur, usd, rub): if my_tag in symbol and symbol[my_tag] != '-': cur = None if (symbol[my_tag].startswith('FRA:') or symbol[my_tag].endswith('.F') or symbol[my_tag].startswith('BME:') or symbol[my_tag].endswith('.MC')): cur = eur elif (symbol[my_tag].startswith('NYSE:') or symbol[my_tag].startswith('OTCMKTS:') or symbol[my_tag].startswith('NASDAQ:') or ('.' not in symbol[my_tag] and ':' not in symbol[my_tag])): cur = usd elif symbol[my_tag].startswith('MCX:') or symbol[my_tag].endswith( '.ME'): cur = rub item = Item() if cur: item.add_tags([my_tag_item, cur]) else: self.logger.warning( 'Currency detection for Symbol {} failed.'.format( symbol[my_tag])) if Symbol.get(name=symbol[my_tag]): self.logger.warning('Symbol {} is related to more than one' ' stock.'.format(symbol[my_tag])) else: stock.price_item.symbols.create(item=item, name=symbol[my_tag])
def build(self): """ Starts the database installation :return: """ # add indices and stocks to db if not self.indices_list: return 0 if (not all(cur in [Tag.EUR, Tag.USD, Tag.RUB] for cur in self.currencies) and self.prices): self.logger.warning('Currency {} is not supported.'.format( self.currencies)) return -1 self.add_indices_and_stocks(self.indices_list) # skip download prices if not self.prices: return 0 # add historical data for currency in self.currencies: # stocks symbols = Symbol.select( lambda t: (Tag.YAO in t.item.tags.name and currency in t.item. tags.name) or Tag.IDX in t.item.tags.name) end = datetime.datetime.now() start = end - timedelta(days=self.history * 365) self.download_historicals( symbols, start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'), ) return 0
def __create_symbol(self, stock, my_tag, my_tag_item, symbol, eur, usd): if my_tag in symbol and symbol[my_tag] != '-': cur = eur if symbol[my_tag].startswith('FRA') or \ symbol[my_tag].endswith('.F') else usd item = Item() item.add_tags([my_tag_item, cur]) if Symbol.get(name=symbol[my_tag]): self.logger.warning('Symbol {} is related to more than one' ' stock.'.format(symbol[my_tag])) else: stock.price_item.symbols.create(item=item, name=symbol[my_tag])
def test_2_dbbase(self): config = { 'db_args': { 'provider': 'sqlite', 'filename': 'database_create.sqlite', 'create_db': False } } logger = logging.getLogger('test') dbbase = DBBase(config, logger) ind = Index.get(name='test123') if ind: ind.delete() sym = Symbol.get(name='test123') if sym: sym.delete() self.assertRaises(NotImplementedError, dbbase.build) self.assertFalse(dbbase.download_historicals(None, None, None)) # override pytickersymbols def get_stocks_by_index(name): stock = { 'name': 'adidas AG', 'symbol': 'ADS', 'country': 'Germany', 'indices': ['DAX', 'test123'], 'industries': [], 'symbols': [] } return [stock] def index_to_yahoo_symbol(name): return 'test123' dbbase.ticker_symbols.get_stocks_by_index = get_stocks_by_index dbbase.ticker_symbols.index_to_yahoo_symbol = index_to_yahoo_symbol dbbase.add_indices_and_stocks(['test123']) ads = Stock.select(lambda s: 'test123' in s.indexs.name).first() self.assertNotEqual(ads, None) Index.get(name='test123').delete() Symbol.get(name='test123').delete()
def update_prices(self): """Update all prices of stocks """ # get symbols prices = list(select((max(p.date), p.symbol) for p in Price)) if ALL_SYMBOLS not in self.symbols: price_filtered = [] for symbol in set(self.symbols): if len(prices) == 0: # download initial data price_filtered.append( [ datetime.datetime.now() - timedelta(days=365 * self.history), Symbol.get(name=symbol), ] ) else: for price in prices: if symbol == price[1].name: price_filtered.append(price) prices = price_filtered # create update dict update = {} for price in prices: max_date = (price[0] + timedelta(days=1)).strftime('%Y-%m-%d') if max_date in update: update[max_date].append(price[1]) else: update[max_date] = [price[1]] # update for key in update: start = datetime.datetime.strptime(key, '%Y-%m-%d') end = datetime.datetime.now() if start.date() >= end.date(): continue self.download_historicals( update[key], start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'), ) commit()
def build(self): """ Starts the build process for given filters :return: nothing """ if self.symbols is None or self.filters is None: return 1 rc = 0 # select all symbols with price connection if 'ALL' in self.symbols: # without index symbols symbols = list( select(p.symbol.name for p in Price if Tag.IDX not in p.symbol.item.tags.name) ) self.symbols = symbols for symbol_str in self.symbols: self.logger.info('Create filters for {}.'.format(symbol_str)) # get stock of symbol stock = Stock.select( (lambda s: symbol_str in s.price_item.symbols.name) ).first() symbol = Symbol.get(name=symbol_str) for my_filter in self.filters: try: self.logger.info( 'Execute filter {}.'.format(my_filter.name) ) self.__build(my_filter, stock, symbol) except (TypeError, RuntimeError, KeyError, ZeroDivisionError, IndexError, ValueError): self.logger.exception( 'Filter {} causes exceptions.'.format(my_filter.name) ) rc += 1 commit() return rc