def main(): print_header("START", CONFIG, level=0) data = load(CONFIG) print(data.head()) data.to_csv("./outputs/data.csv", index=False) describe(data, CONFIG) test(data, CONFIG) forecast(data, CONFIG) predict(data, CONFIG) report(data, CONFIG) print_header("DONE", CONFIG, level=0)
from forecaster import forecast from os import environ id = environ['TARGET_CFD_ID'] cc = environ['TARGET_COUNTRY'] cfd_opt = { 'service_id': id, 'timezone': environ['TIMEZONE'], 'time_from': environ['TIME_FROM'], 'time_to': environ['TIME_TO'] } pred_direction, pred_prob = forecast(id, cc, cfd_opt) print('I think %s will go %s tomorrow with %s probability' % (id, pred_direction, pred_prob))
c = conn.cursor() c.execute("select distinct rec_id from event_table order by rec_id desc") rec_id = c.fetchone()[0] candidates_range = list(range(-3, 4)) for i in range(1, rec_id + 1): c.execute( "select start_time from event_table where rec_id = {0} and date(start_time) <='{1}-03-31'" .format(i, int(args[1]) - 1)) events = c.fetchall() events = [ dt.strptime(events[i][0], "%Y-%m-%d %H:%M:%S") for i in range(len(events)) ] if len(events) <= 1: continue #print(events) first = events[0] last = events[-1] print("rec_id =", i) rec_range = (first, last) while True: forecasted = forecaster.forecast(rec_range, candidates_range, events) events.append(forecasted) if forecasted < dt(int(args[1]), 4, 1, 0, 0, 0): continue if forecasted >= dt(int(args[1]) + 1, 4, 1, 0, 0, 0): break print(forecasted) #print(events[0][0]) conn.close()
def run_today(start_date=dt.datetime(2015, 1, 1), end_date=dt.datetime(2017, 1, 1), n_days=21, data_size=12, myport=['AAPL', 'GOOG'], allocations=[0.5, 0.5], train_size=0.7, max_k=50, max_trade_size=0.1, gen_plot=False, verbose=False, savelogs=False): """ :param start_date: Beginning of time period :param end_date: End of time period :param n_days: Number of days into the future to predict the daily returns of a fund :param data_size: The number of months of data to use in the machine learning model. :param myport: The funds available in your portfolio :param allocations: The percentage of your portfolio invested in the funds :param train_size: The percentage of data used for training the ML model, remained used for testing. :param max_k: Maximum number of neighbors used in kNN :param max_trade_size: The maximum percentage of your portfolio permitted to be traded in any one transaction. :param gen_plot: Boolean to see if you want to plot results :param verbose: Boolean to print out information during execution of application. :return: """ start_date = calc_start_date( end_date, data_size) #end_date - dt.timedelta(weeks=int(data_size * 52/12)) #print('start:', start_date, 'end:', end_date) if verbose: print('-' * 20 + '\nFORECAST\n' + '-' * 20) forecast = fc.forecast(start_date, end_date, symbols=myport, train_size=train_size, n_days=n_days, max_k=max_k, gen_plot=gen_plot, verbose=verbose, savelogs=savelogs) if verbose: print('\n' + '-' * 20 + '\nOPTIMIZE\n' + '-' * 20) target_allocations = opt.optimize_return(forecast, myport, allocations, gen_plot=gen_plot, verbose=verbose, savelogs=savelogs) if verbose: print('\n' + '-' * 20 + '\nORDERS\n' + '-' * 20) trade_date = forecast.index.max() orders = td.create_orders(myport, allocations, target_allocations, trade_date=trade_date, max_trade_size=max_trade_size, verbose=verbose, savelogs=savelogs) if verbose: print(orders) new_allocations = allocations.copy() for i in range(orders.shape[0]): # fix this code so that the correct allocations are updated! index = myport.index(orders.loc[i, 'Symbol']) #symbol = orders.loc[i, 'Symbol'] if orders.loc[i, 'Action'] == 'SELL': new_allocations[index] -= orders.loc[i, 'Quantity'] else: new_allocations[index] += orders.loc[i, 'Quantity'] adr_current, vol_current, sr_current, pv_current = util.compute_returns( forecast, allocations=allocations) adr_target, vol_target, sr_target, pv_target = util.compute_returns( forecast, allocations=target_allocations) adr_new, vol_new, sr_new, pv_new = util.compute_returns( forecast, allocations=new_allocations) if verbose: print("Portfolios:", "Current", "Target", "New") print("Daily return: %.5f %.5f %.5f" % (adr_current, adr_target, adr_new)) print("Daily Risk: %.5f %.5f %.5f" % (vol_current, vol_target, vol_new)) print("Sharpe Ratio: %.5f %.5f %.5f" % (sr_current, sr_target, sr_new)) print("Return vs Risk: %.5f %.5f %.5f" % (adr_current / vol_current, adr_target / vol_target, adr_new / vol_new)) print("\nALLOCATIONS\n" + "-" * 40) print("Symbol", "Current", "Target", 'New') for i, symbol in enumerate(myport): print("%s %.3f %.3f %.3f" % (symbol, allocations[i], target_allocations[i], new_allocations[i])) # Compare daily portfolio value with SPY using a normalized plot if gen_plot: fig, ax = plt.subplots() ax.scatter(vol_current, adr_current, c='green', s=15, alpha=0.5) # Current portfolio ax.scatter(vol_target, adr_target, c='red', s=15, alpha=0.5) # ef ax.scatter(vol_new, adr_new, c='black', s=25, alpha=0.75) # ef ax.set_xlabel('St. Dev. Daily Returns') ax.set_ylabel('Mean Daily Returns') #ax.set_xlim(min(vol)/1.5, max(vol)*1.5) #ax.set_ylim(min(adr)/1.5, max(adr)*1.5) ax.grid() ax.grid(linestyle=':') fig.tight_layout() plt.show() # add code to plot here df_temp = pd.concat([pv_current, pv_target, pv_new], keys=['Current', 'Target', 'New'], axis=1) df_temp = df_temp / df_temp.ix[0, :] util.plot_data(df_temp, 'Forecasted Daily portfolio value and SPY', 'Date-21', 'Normalized Price') if False: # meh was going to plot portfolio values for the last year but trying something else now prior_prices = util.load_data(myport, start_date, end_date) prior_prices.fillna(method='ffill', inplace=True) prior_prices.fillna(method='bfill', inplace=True) #prices_SPY = prior_prices['SPY'] # SPY prices, for benchmark comparison prior_prices = prior_prices[myport] # prices of portfolio symbols forecast_prices = forecast * prior_prices time_span = pd.date_range(forecast.index.min(), end_date + dt.timedelta(days=n_days * 2)) forecast_prices = forecast_prices.reindex(time_span) forecast_prices = forecast_prices.shift(periods=n_days * 2) forecast_prices = forecast_prices.dropna() forecast_prices = pd.concat([prior_prices, forecast_prices], axis=0) adr_current, vol_current, sr_current, pv_current = util.compute_returns( forecast_prices, allocations=allocations) adr_target, vol_target, sr_target, pv_target = util.compute_returns( forecast_prices, allocations=target_allocations) adr_new, vol_new, sr_new, pv_new = util.compute_returns( forecast_prices, allocations=new_allocations) df_temp = pd.concat([pv_current, pv_target, pv_new], keys=['Current', 'Target', 'New'], axis=1) df_temp = df_temp / df_temp.ix[0, :] util.plot_data(df_temp, 'Daily portfolio value and SPY', 'Date', 'Normalized Price') return new_allocations, trade_date