def stocks_from_file(): companies = list() with open(SELECTED_STOCKS, mode='r', encoding='UTF-8') as file: for line in file: trade_code = line.split(';')[1] companies.append(db.stock_by_trade_code(trade_code)) return companies
def process_stock(a, count, num, sort_action, upload_files): if a[4] == property.STOCKS: if count is not None and count == num: return trade_code = a[7] LOG.info('Process stock %s in thred %s' % (trade_code, threading.get_ident())) try: stock = db.stock_by_trade_code(trade_code) except db.NotFoundStock as e: stock = s.Stock() stock_line(stock, line=a) ## TODO: need to add loading history of the stock # LOG.info("Will updated finance document company " + str(a)) # load_files(stock.trade_code, stock.url) # stock.files_name = get_list(property.TYPE2_PATH + "/" + stock.trade_code + property.ARCHIVES + '/') stock.short_name = get_short_name(stock.trade_code) stock.finame_em = finam_code(stock.trade_code) # update_stock_from_file(stock.emitent_full_name, False) stock.lot = get_lot(stock.trade_code) sort_action.append(stock) # if upload_files: stock.save() LOG.info("Save stock %s" % str(stock._id)) return num
def get_stock_from_portfolio(portfolios): stocks = list() sharpes = list() for portfolio in portfolios: portfolio_stock = list() sharpes.append(portfolio.max_item.sharpe_ratio) for stock in portfolio.max_item.stocks: portfolio_stock.append(db.stock_by_trade_code(stock.trade_code)) stocks.append(portfolio_stock) return stocks, sharpes
def process_stock_tivest(num, ta): trade_code = ta.ticker LOG.info('Process stock %s in thread %s' % (trade_code, threading.get_ident())) try: stock = db.stock_by_trade_code(trade_code) except db.NotFoundStock as e: stock = s.Stock() stock.update_from_tinvest(ta) stock.save() LOG.info("Save stock %s" % str(stock._id)) return num
def load_history(trade_code): LOG.info('[%s]' % trade_code) stock = db.stock_by_trade_code(trade_code) stock.month_history = list() stock.week_history = list() stock.day_history = list() stock.hour_history = list() for period in property.FINAM_PERIODS: process_by_period(stock, period) stock.save() return trade_code
def update_stock_from_file(name, download, is_priviledged=False): try: LOG.info('Update from file') if name == "" or name is None: raise db.NotFoundStock stock = db.stock_by_trade_code(name, is_priviledged) action = read_to_list(property.DATA) stock_file = None for a in action: stock_file = stock_from_line(name, a, download) if stock_file is not None: break stock.update_file(stock_file) stock.save() except db.NotFoundStock: LOG.error('Not found stock. Update all.') load_stocks(upload_files=download)
def load_history_tinvest(trade_code): LOG.info('[%s]' % trade_code) try: stock = db.stock_by_trade_code(trade_code) except db.NotFoundStock as e: return "" stock.month_history = list() stock.week_history = list() stock.day_history = list() stock.hour_history = list() for period in property.TINVESE_PERIODS: process_by_period_tinvest(stock, period) time.sleep(0.5) stock.save() return trade_code
def predict(porftolio: pf.ItemPortfolio, money: int, from_date=(datetime.today() - timedelta(days=21)), to_date=datetime.today() - timedelta(days=1)): income = list() correct_summ = list() table = texttable.Texttable() table.add_row([ 'Code', 'SHORT', 'LOT', 'COUNT', 'COUNT_LOT', 'BASE_COUNT', 'PRICE', 'PART', 'FULL', 'INCOME' ]) for stock in porftolio.stocks: try: part = float(stock.value) trade_code = str(stock.trade_code) stock_orm = db.stock_by_trade_code(trade_code) lot = int(stock_orm.lot) from_price = load_price(stock_orm, from_date) count, before_correct_count, full = correct_on_lot( lot, part, money, from_price) correct_full_pirce = count * from_price correct_summ.append(correct_full_pirce) to_price = load_price(stock_orm, to_date) income.append(float(count * to_price)) row = [ stock_orm.trade_code, stock_orm.short_name, stock_orm.lot, count, full, before_correct_count, from_price, part, count * from_price, to_price ] table.add_row(row) except AttributeError: LOG.error('For stock %s not found attribute' % stock.trade_code) result = sum(income) print(table.draw()) LOG.info('Portfolio base price %s (%s) from %s to %s with price %s' % (money, sum(correct_summ), from_date, to_date, result)) return result, sum(correct_summ)
sharpes_sm = list() sharpes_nsga = list() sharpes_nsgaiii = list() wgmean_sm = list() wgmean_nsga = list() wgmean_nsgaiii = list() time_sm = list() time_nsga = list() time_nsgaiii = list() trade_codes = list() for stock in ordered.max_item.stocks: trade_codes.append(stock.trade_code) stocks = list() for trade_code in trade_codes: stocks.append(db.stock_by_trade_code(trade_code)) for num, iter in enumerate(iterations): fig = plt.figure('compare-' + str(iter)) data = solver.get_stock_price(stocks) returns = data.pct_change() days = len(stocks[0].day_history) LOG.info('History length %d' % int(days)) mean_daily_returns = returns.mean() cov_matrix = returns.cov() avr_gmean, gmeans = solver.average_gmean(returns) LOG.info('Geometric mean: %.8f, Daily meand: %.8f, Gmeans: %.8f' % (avr_gmean, np.average(mean_daily_returns), gmeans))
def all_stock(stocks): companies = list() for trade_code in stocks: companies.append(db.stock_by_trade_code(trade_code)) return companies