def ga(words): if len(words) == 2: type_ga = words[0] count = int(words[1]) else: return RSP_INVALID_PARAMETERS % str(words) LOG.info('start NSGA with population count %d' % count) all_stocks = list() db.connect() for s in Stock.objects(): all_stocks.append(s) db.close() print(len(all_stocks)) if PARALLEL: workers = env.get(THREAD) if workers is None: workers = 1 with ProcessPoolExecutor(max_workers=int(workers)) as executor: features = { executor.submit(parallel_solve, all_stocks, type_ga, curr, count): curr for curr in range(count) } for feature in concurrent.futures.as_completed(features): LOG.info('Complete %s ' % feature.result()) else: for curr in range(count): parallel_solve(all_stocks, type_ga, curr, count) return 'Finish GA!'
def parallel_solve(all_stocks, type_ga, curr, count): LOG.info('Start parallel solve in %s' % str(threading.get_ident())) range_stock = len(all_stocks) number = list() stocks = [] for position in range(15): pos = random.randint(0, range_stock) while pos in number: pos = random.randint(0, range_stock) stock = all_stocks[pos - 1] while len(stock.day_history) < 66: pos = random.randint(0, range_stock) stock = all_stocks[pos - 1] stocks.append(stock) # download daily price data for each of the stocks in the portfolio data = get_stock_price(stocks) returns = data.pct_change() # calculate mean daily return and covariance of daily returns mean_daily_returns = returns.mean() cov_matrix = returns.cov() days = len(stocks[0].day_history) iterations = 50000 start = time.time() if type_ga == GA_SIMPLE: results_frame = simple.solve(stocks, iterations, mean_daily_returns, cov_matrix, days) if type_ga == GA_NSGAII: results_frame = NSGAII.solve(stocks, iterations, mean_daily_returns, cov_matrix, days) if type_ga == GA_NSGAIII: results_frame = NSGAII.solve_nsgaiii(stocks, iterations, mean_daily_returns, cov_matrix, days) duration = time.time() - start LOG.info('Duration solved: %s' % duration) sharpe_id_min = results_frame['sharpe'].idxmin() max_sharpe_port = results_frame.iloc[sharpe_id_min] stdev_id_min = results_frame['stdev'].idxmax() min_vol_port = results_frame.iloc[stdev_id_min] LOG.info('Max Sharpe ratio: %s' % str(max_sharpe_port)) LOG.info('Min standard deviation: %s' % str(min_vol_port)) db.connect() solved = Portfolio() max_item = parse_solved_portfolio(max_sharpe_port, stocks) min_item = parse_solved_portfolio(min_vol_port, stocks) solved.max_item = max_item solved.min_item = min_item solved.date = datetime.datetime.today() solved.save() db.close() LOG.info('Save %d portfolio from %d in thread %s' % (curr + 1, count, str(threading.get_ident()))) return 'Duration %s' % str(duration)
def history_all_stocks(): LOG.info("Load all stocks") connect() stocks = s.Stock.objects() all_stocks = stocks.count() for num, stock in enumerate(stocks): load_history(stock.trade_code) LOG.info("Load [%d/%d] %s" % (num, all_stocks, stock.trade_code)) close() return RSP_FINAM_CODE_ALL
def process_result_of_ga(results_frame, stocks, gmean): try: id_sharpe = results_frame['sharpe'].idxmax() except TypeError as exc: id_sharpe = maxId(results_frame, 'sharpe') try: max_sharpe_port = results_frame.iloc[id_sharpe] except TypeError as exc: id_sharpe = maxId(results_frame, 'sharpe') max_sharpe_port = results_frame.iloc[id_sharpe] try: id_stdev = results_frame['stdev'].idxmin() except TypeError as exc: id_stdev = minId(results_frame, 'stdev') if id_stdev == np.NaN: id_stdev = minId(results_frame, 'stdev') try: min_vol_port = results_frame.iloc[id_stdev] except TypeError as exc: id_stdev = minId(results_frame, 'stdev') min_vol_port = results_frame.iloc[id_stdev] LOG.info('Max Sharpe ratio: \n%s' % str(max_sharpe_port)) LOG.info('Min standard deviation: \n%s' % str(min_vol_port)) db.connect() solved = Portfolio() max_item = parse_solved_portfolio(max_sharpe_port, stocks) min_item = parse_solved_portfolio(min_vol_port, stocks) solved.max_item = max_item solved.min_item = min_item solved.date = datetime.datetime.today() solved.total_sum = 0 for stock in max_item.stocks: solved.total_sum += stock.value if gmean is None: solved.gmean = 0.0 else: solved.gmean = gmean solved.save() db.close() return max_item.sharpe_ratio, solved._id
import random import numpy as np import pandas as pd import platypus as pt from bot.my_log import get_logger from bot.mongo import mongo as db from bot.mongo.Stock import Stock LOG = get_logger('platypus') db.connect() all_stocks = list() for s in Stock.objects(): all_stocks.append(s) db.close() class ProblemPortfolio(pt.Problem): def __init__(self, cov_matrix, mean_daily_returns, days): super(ProblemPortfolio, self).__init__(nvars=15, nobjs=2, nconstrs=4) self.cov_matrix = cov_matrix self.mean_daily_returns = mean_daily_returns self.days = days self.types[:] = pt.Real(0.0, 1.0) self.constraints.__setitem__(0, "==1") self.constraints.__setitem__(1, "<=0.06") self.constraints.__setitem__(2, ">=0.02") self.constraints.__setitem__(3, "<=0.18")