def btn_update__click(dom): symbol = dom['select_stock']['value'] if symbol == 'AAPL': df = AAPL elif symbol == 'GOOG': df = GOOG else: return dom bounds = [dom[x]['value'] if dom[x]['value'] else None for x in ['date_start', 'date_end']] ts = df['Close'][bounds[0]:bounds[1]] if ts.any(): try: ts.plot() for win in [int(dom[x]['value']) for x in ['slider_window_1', 'slider_window_2']]: pd.rolling_mean(ts, win).plot() plt.title("Weekly closing prices for {}".format(symbol)) # get_svg is added by ashiba.plot dom['img_plot'].set_image(plt.get_svg(), 'svg') finally: plt.close() return dom
def btn_plot__click(dom): ra = calc(dom) plotNumber(ra,dom['label']['value']) #plt.plot([1,2,3],[1,2,3]) #plt.savefig('image.jpg') dom['img_plot'].set_image(plt.get_svg(), 'svg') return dom
def _plot_control__change(dom): year = int(dom['slider_year']['value']) sex = None if dom['radio_male']['checked']: sex = 1 elif dom['radio_female']['checked']: sex = 2 if year == 1890 or sex is None: return subset = df[df.year == year][df.sex == sex][df.age >= 15] # Does this need to be a new instantiation? dom['my_table'] = ashiba.dom.DataTable(data=subset) pivot = subset.pivot('age', 'marst', 'people') if 3 not in pivot: pivot[3] = [0] * len(pivot) pivot.plot(kind='bar', stacked=True, sort_columns=True) gender = {1: 'male', 2: 'female'}[sex] plt.title('Marriage records for year {}, {}'.format(year, gender)) statuses = ( 'Unknown', 'Married', 'Married, Spouse Absent', 'Separated', 'Divorced', 'Widowed', 'Single', ) plt.legend(statuses) dom['img_plot'].set_image(plt.get_svg(), 'svg') plt.close()
def btn_update__click(dom): symbol = dom['select_stock']['value'] if symbol == 'AAPL': df = AAPL elif symbol == 'GOOG': df = GOOG else: return dom bounds = [ dom[x]['value'] if dom[x]['value'] else None for x in ['date_start', 'date_end'] ] ts = df['Close'][bounds[0]:bounds[1]] if ts.any(): try: ts.plot() for win in [ int(dom[x]['value']) for x in ['slider_window_1', 'slider_window_2'] ]: pd.rolling_mean(ts, win).plot() plt.title("Weekly closing prices for {}".format(symbol)) # get_svg is added by ashiba.plot dom['img_plot'].set_image(plt.get_svg(), 'svg') finally: plt.close() return dom
def mybutton__click(dom): user = dom['usersearch']['value'] repo = dom['reposearch']['value'] ts = weekly_commit_timeline(user, repo) ts2 = daily_commit_timeline(user, repo) ts.plot() plt.title("Weekly commits for %s".format('Github') % repo) dom['img_plot'].set_image(plt.get_svg(), 'svg') ts2.plot() plt.title("Daily commits for %s".format('Github') % repo) dom['img_plot2'].set_image(plt.get_svg(), 'svg') plt.close() return dom
def mybutton__click(dom): user = dom['usersearch']['value'] repo = dom['reposearch']['value'] ts = weekly_commit_timeline(user, repo) ts2 = daily_commit_timeline(user, repo) ts.plot() plt.title("Weekly commits for %s".format('Github') %repo) dom['img_plot'].set_image(plt.get_svg(), 'svg') ts2.plot() plt.title("Daily commits for %s".format('Github') % repo) dom['img_plot2'].set_image(plt.get_svg(), 'svg') plt.close() return dom
def _plot_control__change(dom): year = int(dom["slider_year"]["value"]) sex = None if dom["radio_male"]["checked"]: sex = 1 elif dom["radio_female"]["checked"]: sex = 2 if year == 1890 or sex is None: return subset = df[df.year == year][df.sex == sex][df.age >= 15] # Does this need to be a new instantiation? dom["my_table"] = ashiba.dom.DataTable(data=subset) pivot = subset.pivot("age", "marst", "people") if 3 not in pivot: pivot[3] = [0] * len(pivot) pivot.plot(kind="bar", stacked=True, sort_columns=True) gender = {1: "male", 2: "female"}[sex] plt.title("Marriage records for year {}, {}".format(year, gender)) statuses = ("Unknown", "Married", "Married, Spouse Absent", "Separated", "Divorced", "Widowed", "Single") plt.legend(statuses) dom["img_plot"].set_image(plt.get_svg(), "svg") plt.close()
def calculate__click(dom): # Here we grab the equity info from the dom and perform a few conversions stock1 = stock2 = stock3 = stock4 = None stock1 = dom['stock1']['value'] stock2 = dom['stock2']['value'] stock3 = dom['stock3']['value'] stock4 = dom['stock4']['value'] ls_symbols = [stock1, stock2, stock3, stock4] symbols = filter(lambda a: len(a) > 0, ls_symbols) stock1_ratio = dom['stock1_ratio']['value'] stock2_ratio = dom['stock2_ratio']['value'] stock3_ratio = dom['stock3_ratio']['value'] stock4_ratio = dom['stock4_ratio']['value'] ratios = [stock1_ratio, stock2_ratio, stock3_ratio, stock4_ratio] ls_alloc = [float(i) for i in ratios] allocations = filter(lambda a: a > 0, ls_alloc) #get the trading days in the time period bounds = [dom[x]['value'] if dom[x]['value'] else None for x in ['date_start', 'date_end']] if bounds[0] != None: start = bounds[0].split('-') dt_start = dt.datetime(int(start[0]),int(start[1]), int(start[2])) else: dt_start = dt.datetime(2006, 1, 1) if bounds[1] != None: end = bounds[1].split('-') dt_end = dt.datetime(int(end[0]), int(end[1]), int(end[2])) else: dt_end = dt.datetime(2012, 5, 20) #timestamps of trading days timestamps = get_NYSE_days(dt_start, dt_end) #close prices normalized_close_prices = close_prices(ls_symbols, timestamps) portfolio_total, market_per, final_stddev, final_daily_ret, final_cum_ret, max_sharpe = portfolio_stats(symbols, allocations, normalized_close_prices, timestamps) text = '''Symbols : %s \n\n Std. Deviation : %f Daily Returns : %f Cum. Returns : %f Sharpe Ratio : %f ''' % (symbols, final_stddev, final_daily_ret, final_cum_ret, max_sharpe) dom['my_tabs'].add_tab('[%s]' % ', '.join(map(str, symbols)), text) plt.clf() plt.plot(timestamps, normalized_close_prices) plt.legend(symbols) plt.ylabel('Normalized Close') plt.xlabel('Date') dom['close_prices'].set_image(plt.get_svg(), 'svg') plt.clf() plt.plot(timestamps, portfolio_total, label='Portfolio') plt.plot(timestamps, market_per, label='SPY') plt.legend() plt.ylabel('Returns') plt.xlabel('Date') dom['daily_returns'].set_image(plt.get_svg(), 'svg') return dom