Ejemplo n.º 1
0
    def make_document(doc):
        source = ColumnDataSource()
        source.data = make_cds(df, None).data

        table = make_table_tab(source)
        plots = make_plots_tab(source)
        about = make_about_tab()

        tabs = Tabs(tabs=[plots, table, about])
        doc.add_root(tabs)
        doc.title = "iPlot"
Ejemplo n.º 2
0
#color_bar.click_policy="hide"

p.add_layout(color_bar, 'right')

from bokeh.io import output_file, show

#output_file("hex_tile.html")

#VERIFICATION STUFF
dictionary = {}
for col_name in df.columns:
    # if col_name not in [X,Y]:
    dictionary[col_name] = df[col_name]
datapoints_source = ColumnDataSource()
datapoints_source.data = dictionary

circle_plot = p.circle(
    x='mrc_start_long',
    y='mrc_start_lat',
    #size=cluster_point_size,
    fill_alpha=0.2,
    source=datapoints_source,
    color="firebrick",
    line_alpha=0
    #line_color='black'
)

#VERIFICATION STUFF
#df['x']=df['mrc_start_long']
#df['y']=df['mrc_start_lat']
resample_df = df.resample(sample_peorid).sum()
materals = get_materal_cluster(resample_df)

slider_smoothing = Slider(start=2,
                          end=9,
                          step=1,
                          value=smoothing_window,
                          title="Smoothing Window")

p = Figure(title="Clustered Materals", plot_height=500, plot_width=650)
df_source = ColumnDataSource(
    data=dict(x=resample_df[materals[0]], y=resample_df.index))

p.line(x='x', y='y', source=df_source)

df_source.data = dict(x=resample_df[materals[0]], y=resample_df.index)

grid = girdplot()


def get_materal_cluster(df):
    rolling_df = df.rolling(window=smoothing_window, center=True)
    rolling_df = rolling_df.mean()

    rm = round(smoothing_window / 2)
    rolling_df = c.iloc[rm:-rm]

    clusters = hac.linkage(rolling_df.T, 'complete', 'correlation')
    clusters = hac.fcluster(cluster, .5, criterion='distance')

    clst = pd.value_counts(clusters).index[0]
Ejemplo n.º 4
0
    price_hover = p.select(dict(type=HoverTool))

    # Choose, which glyphs are active by glyph name
    price_hover.names = ["price"]
    # Creating tooltips
    price_hover.tooltips = [("Datetime", "@Date{%Y-%m-%d}"),
                            ("Open", "@Open{$0,0.00}"),
                            ("Close", "@Close{$0,0.00}"),
                            ("Volume", "@Volume{($ 0.00 a)}")]
    price_hover.formatters = {"Date": 'datetime'}

    return p


stock = ColumnDataSource(
    data=dict(Date=[], Open=[], Close=[], High=[], Low=[], index=[]))

myPortfolio = Portfolio(['CSCO', 'DTE.DE', 'NOK'], [1, 1, 2], [1, 1, 1],
                        [1, 1, 1])
sample_df = pd.DataFrame(data=myPortfolio.share_data['CSCO']).dropna()
sample_df.reset_index(inplace=True)

stock.data = stock.from_df(sample_df)
elements = list()

p_stock = plot_stock_price(stock)
elements.append(p_stock)

curdoc().add_root(column(elements))
curdoc().title = 'CSCO Data'
Ejemplo n.º 5
0
def modify_doc(doc):
    """ Contains the application, including all callbacks
        TODO: could the callbacks be outsourced?
    :param doc:
    :type doc:
    """
    logger.debug('modify_doc has been called')
    def get_data_frames(ie,):
        """ Called one time initially, and then every time the experiment number is changed by the slider
        :param ie: experiment number
        :type ie: int
        :returns: dataframe from stella datafile and dataframe with tau and phi and fitted values
        :rtype: list of 2 pandas dataframes
        """
        logger.debug('get_dataframe with ie={}'.format(ie))
        fid = polymer.getfid(ie) #read FID or series of FIDs for selected experiment
        try:
            tau = polymer.get_tau_axis(ie) #numpy array containing the taus for experiment ie
            try:
                startpoint=fid_slider.range[0] #lower integration bound
                endpoint = fid_slider.range[1] #upper integration bound
            except NameError:
                # fid_slider not initialized for first plot. Use default values:
                startpoint=int(0.05*polymer.getparvalue(ie,'BS'))
                endpoint = int(0.1*polymer.getparvalue(ie,'BS'))
                logger.debug('fid_slider not initialized for first plot. Use default values {} and {}.'.format(startpoint, endpoint))
                
            polymer.addparameter(ie,'fid_range',(startpoint,endpoint)) #add integration range to parameters to make it accesible
            phi = get_mag_amplitude(fid, startpoint, endpoint,
                                    polymer.getparvalue(ie,'NBLK'),
                                    polymer.getparvalue(ie,'BS')) # list containing averaged fid amplitudes (which is proportional to a magnetization phi)
            df = pd.DataFrame(data=np.c_[tau, phi], columns=['tau', 'phi']) # DataFrames are nice
            df['phi_normalized'] = (df['phi'] - df['phi'].iloc[0] ) / (df['phi'].iloc[-1] - df['phi'].iloc[1] ) #Normalize magnetization,
            #Note: in the normalized magnetization the magnetization build-up curves and magnetization decay curves look alike
            #Note: this makes it easier for fitting as everything looks like 1 * exp(-R/time) in first order
            polymer.addparameter(ie,'df_magnetization',df) # make the magnetization dataframe accesible as parameter
            fit_option = 2 #mono exponential, 3 parameter fit
            p0=[1.0, polymer.getparvalue(ie,'T1MX')**-1*2, 0] #choose startparameters for fitting an exponential decay
            df, popt = magnetization_fit(df, p0, fit_option) # use leastsq to find optimal parameters
            polymer.addparameter(ie,'popt(mono_exp)',popt) # add fitting parameters for later access
            logger.info('fitfunction(t) = {} * exp(- {} * t) + {}'.format(*popt)) # print the fitting parameters to console (for convenience)
        except KeyError:
            logger.warning('no relaxation experiment found')
            tau=np.zeros(1)
            phi=np.zeros(1)
            df = pd.DataFrame(data=np.c_[tau, phi], columns=['tau', 'phi'])
            df['phi_normalized'] = np.zeros(1)
            df['fit_phi'] = np.zeros(1)
        return fid, df

    def calculate_mag_dec(attr, old, new, start_ie=None):
        ''' Is being call from the callback for the experiment chooser
            loads selected experiment visualize in plot p1 and p2 
            gets experiment number from the slider
            writes source_fid.data from the fid from the polymer object
            writes source_mag_dec.data from the dataframe
            '''
        ie = experiment_slider.value   #get expermient number from the slider
        logger.debug('calculate mag_dec for ie={}'.format(ie))
        fid, df = get_data_frames(ie)
        source_fid.data=ColumnDataSource.from_df(fid) #convert fid to bokeh format
        source_mag_dec.data = ColumnDataSource.from_df(df)

    def plot_par():
        ''' Creates plot for the parameters 
            Called with every update from the callback'''
        logger.debug('creating plot for the parameters')

        # read data due to selection of select_x/y
        xs = par_df[select_xaxis.value ].values
        ys = par_df[select_yaxis.value].values
        # read titles due to name of select_x/y
        x_title = select_xaxis.value.title()
        y_title = select_yaxis.value.title()

        # remark: many attributes in a bokeh plot cannot be modified after initialization
        #         for example p4.x_axis_type='datetime' does not work. keywords are a
        #         workaround to pass all optional arguments initially
        # set optional keyword arguments, kw, for figure()
        kw = dict() #initialize
        if select_xaxis.value in discrete:
            kw['x_range'] = sorted(set(xs))
        if select_yaxis.value in discrete:
            kw['y_range'] = sorted(set(ys))
        if select_yaxis.value in time:
            kw['y_axis_type'] = 'datetime'
        if select_xaxis.value in time:
            kw['x_axis_type'] = 'datetime'
        kw['title']="%s vs %s" % (x_title, y_title)
        # create figure using optional keywords kw
        p4 = figure(plot_height=300, plot_width=600, tools='pan,box_zoom,reset',
                    **kw)
        # set axis label
        p4.xaxis.axis_label = x_title
        p4.yaxis.axis_label = y_title

        # strings at x axis ticks need a lot of space. solution: rotate label orientation
        if select_xaxis.value in discrete:
            p4.xaxis.major_label_orientation = pd.np.pi / 4 # rotates labels...

        # standard size of symbols
        sz = 9
        # custom size of symbols due to select_size
        if select_size.value != 'None':
            groups = pd.qcut(pd.to_numeric(par_df[select_size.value].values), len(SIZES))
            sz = [SIZES[xx] for xx in groups.codes]

        # standard color
        c = "#31AADE"        
        # custom color due to select_color
        if select_color.value != 'None':
            groups = pd.qcut(pd.to_numeric(par_df[select_color.value]).values, len(COLORS))
            c = [COLORS[xx] for xx in groups.codes]

        # create the plot using circles
        p4.circle(x=xs, y=ys, color=c, size=sz, line_color="white", alpha=0.6, hover_color='white', hover_alpha=0.5)
        return p4 #return the plot
    
    def callback_update_plot_1(attr, old, new):
        ''' Callback for update of figure 1 in parameters tab '''
        tabs.tabs[1].child.children[1] = plot_par()
        print(tabs.tabs[1].child.children[1])
        logger.debug('Parameter plot updated')
#        p4 = plot_par()

    def callback_update_p3():
        logger.debug('update plot 3')
        p3 = fit_mag_decay_all(polymer,par_df)
        return p3

    def callback_update_experiment(attr, old, new):
        """ Callback for the experiment chooser
        """
        ie = experiment_slider.value
        logger.debug('Callback experiment update, ie={}'.format(ie))
        fid_slider.end = polymer.getparvalue(ie,'BS')
        try:
            fid_slider.range=polymer.getparvalue(ie,'fid_range')
        except:
            startpoint = int(0.05 * polymer.getparvalue(ie,'BS'))
            endpoint = int(0.1 * polymer.getparvalue(ie,'BS'))
            fid_slider.range=(startpoint,endpoint)
        calculate_mag_dec(attr,old,new)
        
    def callback_load_more_data(attr,old,new):
        ''' callback for loading of data '''
        # TODO: implement
        logger.debug('callback for loading of data ')
        logger.error('Not implemented!')
        path=pathbox.value.strip()
        file=filebox.value.strip()
        if file=="*.sdf":
            logger.info('callback for loading data. filename: {}'.format(file))
            allsdf=filter(lambda x: x.endswith('.sdf'),os.listdir(path))
            for f in allsdf:
                sdf_list.append(sdf.StelarDataFile(f,path))
        else:
            sdf_list.append(sdf.StelarDataFile(file,path))
        
        filenames=[x.file() for x in sdf_list]
        filenames_df=pd.DataFrame(data=filenames,columns=['file'])
        table_source.data=ColumnDataSource.from_df(filenames_df)

    def callback_export_data(attr,old,new):
        logger.debug('callback_export_data has been called ')
        logger.error('Not implemented!')
        pass
    
    def callback_write_table_to_file(attr,old,new): ##FIXME
        logger.debug('callback_write_table_to_file has been called ')
        logger.error('Not implemented!')
        pass
#        path=export_text.value.strip()
#        exportdata=export_source.data
#        CustomJS(args=dict(source=export_source),
#                 code=open(join(dirname(__file__), "export_csv.js")).read())

    def callback_update_parameters():
        ''' callback for button
            function to call when button is clicked
            for updates parameters of polymer, since they can change during evaluation '''
        logger.debug('callback for button (update parameter).')
        par_df, columns, discrete, continuous, time, quantileable = polymer.scan_parameters()
        select_xaxis.options=columns
        select_yaxis.options=columns
        select_size.options=['None']+quantileable
        select_color.options=['None']+quantileable

    logger.info('Starting the script')
    ### This is the start of the script ###
    ### The callbacks are above ###

    #load data:
    # TODO: how to handle multiple datafiles?
    # New Tab for each datafile?
    # dropdown selection to choose datafile
    # complete new start of process? (probably not prefered)

    polymer = load_data('glyzerin_d3_300K.sdf')
    nr_experiments = polymer.get_number_of_experiments()
    start_ie = 1     # initially set ie = 1
    par_df, columns, discrete, continuous, time, quantileable = polymer.scan_parameters(20)
    # for the initial call get the dataframes without callback
    # they are being updated in following callbacks
    fid, df = get_data_frames(start_ie)
    source_fid = ColumnDataSource(data=ColumnDataSource.from_df(fid))
    source_mag_dec = ColumnDataSource(data=ColumnDataSource.from_df(df))
    # initialy creates the plots p1 and p2
    p1, p2 = create_plot_1_and_2(source_fid, source_mag_dec)
    
    ### initiates widgets, which will call the callback on change ###
    # initiate slider to choose experiment
    experiment_slider = Slider(start=1, end=nr_experiments, value=1, step=1,callback_policy='mouseup', width=800) #select experiment by value
    # initiate slider for the range in which fid shall be calculated
    # select the intervall from which magneitization is calculated from fid
    fid_slider = RangeSlider(start=1,end=polymer.getparvalue(start_ie,'BS'),
                             range=polymer.getparvalue(start_ie,'fid_range'),
                             step=1,callback_policy='mouseup', width=400)

    # fit magnetization decay for all experiments
    p3 = fit_mag_decay_all(polymer, par_df)
    # refit mag dec with updated ranges after button push
    button_refit = Button(label='Update',button_type="success")
    button_refit.on_click(callback_update_p3)

    # initialize empty source for experiment slider
    source = ColumnDataSource(data=dict(value=[]))
    # 'data' is the attribute. it's a field in source, which is a ColumnDataSource
    # initiate callback_update_experiment which is the callback
    source.on_change('data',callback_update_experiment) #source for experiment_slider
    experiment_slider.callback = CustomJS(args=dict(source=source),code="""
        source.data = { value: [cb_obj.value] }
    """)#unfortunately this customjs is needed to throttle the callback in current version of bokeh

    # initialize empty source for fid slider, same as above
    source2 = ColumnDataSource(data=dict(range=[], ie=[]))
    source2.on_change('data',calculate_mag_dec)
    fid_slider.callback=CustomJS(args=dict(source=source2),code="""
        source.data = { range: cb_obj.range }
    """)#unfortunately this customjs is needed to throttle the callback in current version of bokeh

    # same for the update button
    button_scan = Button(label='Scan Parameters',button_type="success")
    button_scan.on_click(callback_update_parameters)
    
    # here comes for callbacks for x, y, size, color
    select_xaxis = Select(title='X-Axis', value='ZONE', options=columns)
    select_xaxis.on_change('value', callback_update_plot_1)

    select_yaxis = Select(title='Y-Axis', value='TIME', options=columns)
    select_yaxis.on_change('value', callback_update_plot_1)

    select_size = Select(title='Size', value='None', options=['None'] + quantileable)
    select_size.on_change('value', callback_update_plot_1)

    select_color = Select(title='Color', value='None', options=['None'] + quantileable)
    select_color.on_change('value', callback_update_plot_1)

    controls_p4 = widgetbox([button_scan, select_xaxis,select_yaxis,select_color,select_size], width=150)
    #p4 = plot_par()
    layout_p4 = row(controls_p4,plot_par())
    logger.debug('layout for parameter plot created')

    ####
    #### TODO: write file input
    #### TODO: select files to import
    #### TODO: discard imported files
    ####

    # load more data:
    table_source=ColumnDataSource(data=dict())
    sdf_list=[polymer]
    # TODO: This is current plan, to save the different dataframes in a list, right?
    filenames=[x.file() for x in sdf_list]
    files_df=pd.DataFrame(data=filenames,columns=['file'])
    table_source.data=ColumnDataSource.from_df(files_df)
    t_columns = [
        TableColumn(field='file', title='Path / Filename'),
        #TableColumn(field='file', title='Filename'),
        ]
    table=DataTable(source=table_source,columns=t_columns)
    pathbox=TextInput(title="Path",value=os.path.curdir)
    filebox=TextInput(title="Filename",value="*.sdf")
    pathbox.on_change('value',callback_load_more_data)
    filebox.on_change('value',callback_load_more_data)
    layout_input=column(pathbox,filebox,table)

    # Data Out: export data from figures
    #         & export parameters

    export_source=ColumnDataSource(data=dict())
    export_columns=[]
    output_table=DataTable(source=export_source,columns=export_columns)
    export_slider = Slider(start=1, end=4, value=3, step=1,callback_policy='mouseup', width=200) #do we need mouseup on this?
    export_slider.on_change('value',callback_export_data)
    export_text = TextInput(title="Path",value=os.path.curdir)
    export_button = Button(label='Export to csv',button_type="success") # FIXME Callback  doesn't work yet
    export_button.on_click(callback_write_table_to_file)
 
    layout_output=row(column(export_slider,export_text,export_button),output_table)
    print('after layout_output')
    

    # set the layout of the tabs
    layout_p1 = column(experiment_slider, p1,
                       row(
                           column(fid_slider,p2),
                           column(button_refit, p3)
                           ),
                       )
    tab_relaxation = Panel(child = layout_p1, title = 'Relaxation')
    tab_parameters = Panel(child = layout_p4, title = 'Parameters')
    tab_input = Panel(child = layout_input, title = 'Data In')
    tab_output = Panel(child = layout_output, title = 'Data Out')

    # initialize tabs object with 3 tabs
    tabs = Tabs(tabs = [tab_relaxation, tab_parameters,
                        tab_input, tab_output])
    print('tabs')
    doc.add_root(tabs)
    doc.add_root(source) # i need to add source to detect changes
    doc.add_root(source2)
    print('tab tab')
Ejemplo n.º 6
0
def dashboard(request):
    current_user = request.user
    user = Profile.objects.get(user = current_user)
    if request.user.is_authenticated:
        all_stocks = []
        all_portfolios_of_current_users = Portfolio.objects.filter(user = user)
        for indv in all_portfolios_of_current_users:
            all_stocks.append(indv.stock)
    else:
        return redirect(landingpage)
    if not all_stocks:
        all_stocks = ["MMM", "ABT", "ABBV", "ABMD", "ACN", "ATVI", "AMZN", "FB", "GOOGL", "GOOG", "JNJ" ]

    if request.method == 'POST':
        symbol = request.POST.get('symbol')
        # Get Stock DataFrame
        # msft = yf.Ticker("MSFT")
        msft = yf.Ticker(symbol)
        hist = msft.history(period='max')

        # Define constants
        W_PLOT = 1000
        H_PLOT = 400
        TOOLS = 'pan,wheel_zoom,hover,reset'

        VBAR_WIDTH = 0.2
        RED = Category20[7][6]
        GREEN = Category20[5][4]

        BLUE = Category20[3][0]
        BLUE_LIGHT = Category20[3][1]

        ORANGE = Category20[3][2]
        PURPLE = Category20[9][8]
        BROWN = Category20[11][10]

        def get_symbol_df(symbol=None):
            df = pd.DataFrame(hist)[-50:]
            df.reset_index(inplace=True)
            df["Date"] = pd.to_datetime(df["Date"])
            return df

        def plot_stock_price(stock):
        
            p = figure(plot_width=W_PLOT, plot_height=H_PLOT, tools=TOOLS,
                    title="Stock price", toolbar_location='above')

            inc = stock.data['Close'] > stock.data['Open']
            dec = stock.data['Open'] > stock.data['Close']
            view_inc = CDSView(source=stock, filters=[BooleanFilter(inc)])
            view_dec = CDSView(source=stock, filters=[BooleanFilter(dec)])

            # map dataframe indices to date strings and use as label overrides
            p.xaxis.major_label_overrides = {i+int(stock.data['index'][0]): date.strftime('%b %d') for i, date in enumerate(pd.to_datetime(stock.data["Date"]))}
            p.xaxis.bounds = (stock.data['index'][0], stock.data['index'][-1])


            p.segment(x0='index', x1='index', y0='Low', y1='High', color=RED, source=stock, view=view_inc)
            p.segment(x0='index', x1='index', y0='Low', y1='High', color=GREEN, source=stock, view=view_dec)

            p.vbar(x='index', width=VBAR_WIDTH, top='Open', bottom='Close', fill_color=BLUE, line_color=BLUE,source=stock,view=view_inc, name="price")
            p.vbar(x='index', width=VBAR_WIDTH, top='Open', bottom='Close', fill_color=RED, line_color=RED,source=stock,view=view_dec, name="price")

            p.legend.location = "top_left"
            p.legend.border_line_alpha = 0
            p.legend.background_fill_alpha = 0
            p.legend.click_policy = "mute"

            p.yaxis.formatter = NumeralTickFormatter(format='$ 0,0[.]000')
            p.x_range.range_padding = 0.05
            p.xaxis.ticker.desired_num_ticks = 40
            p.xaxis.major_label_orientation = 3.14/4
            
            # Select specific tool for the plot
            price_hover = p.select(dict(type=HoverTool))

            # Choose, which glyphs are active by glyph name
            price_hover.names = ["price"]
            # Creating tooltips
            price_hover.tooltips = [("Datetime", "@Date{%Y-%m-%d}"),
                                    ("Open", "@Open{$0,0.00}"),
                                    ("Close", "@Close{$0,0.00}"),("Volume", "@Volume{($ 0.00 a)}")]
            price_hover.formatters={"Date": 'datetime'}

            return p

        
        stock = ColumnDataSource(data=dict(Date=[], Open=[], Close=[], High=[], Low=[],index=[]))
        df = get_symbol_df(symbol)
        stock.data = stock.from_df(df)
        elements = list()

        # update_plot()
        p_stock = plot_stock_price(stock)

        elements.append(p_stock)

        curdoc().add_root(column(elements))
        curdoc().title = 'Bokeh stocks historical prices'

        #show(p_stock)

        script, div = components(p_stock)
        kwargs = {'script': script, 'div': div}
        
        stock_list = all_stocks
        er = EventRegistry(apiKey = '3a7a023b-6280-4476-bec0-5c9ff41770bd')
        q = QueryArticlesIter(
            keywords = QueryItems.OR(stock_list),
            dataType = ["news"],
            lang = 'eng'
        )
        news_list = q.execQuery(er, sortBy = ["rel","date","sourceImportance"], maxItems = 5, )

        url = "https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/get-detail"

        querystring = {"region":"US","lang":"en","symbol":symbol}

        headers = {
            'x-rapidapi-host': "apidojo-yahoo-finance-v1.p.rapidapi.com",
            'x-rapidapi-key': "ff4cff81d5msh596a92309f2f43ap13412cjsn002bc2a74b4d"
            }

        response = requests.request("GET", url, headers=headers, params=querystring)
        json_dict  = json.loads(response.text)


        for key in json_dict.keys():
            if key == "summaryDetail":
                try:
                    stock_close = json_dict[key]['previousClose']['raw']
                except KeyError:
                    stock_close = ''
                    
                try:
                    stock_open = json_dict[key]['open']['raw']
                except KeyError:
                    stock_open = ''
                try:
                    stock_bid = json_dict[key]['bid']['raw']
                except KeyError:
                    stock_bid = ''
                try:
                    stock_ask = json_dict[key]['ask']['raw']
                except KeyError:
                    stock_ask = ''
                try:
                    stock_volume = json_dict[key]['volume']['fmt']
                except KeyError:
                    stock_volume = ''
                try:
                    stock_averageVolume = json_dict[key]['averageVolume']['fmt']
                except KeyError:
                    stock_averageVolume = ''
                try:
                    stock_marketCap = json_dict[key]['marketCap']['fmt']    
                except KeyError:  
                    stock_marketCap = ''
            if key == "defaultKeyStatistics":
                try:
                    stock_weekChange = json_dict[key]['52WeekChange']['fmt']
                except KeyError:
                    stock_weekChange = ''
                try:
                    stock_beta = json_dict[key]['beta']['fmt']
                except KeyError:
                    stock_beta = ''
                try:
                    stock_EPS = json_dict[key]['trailingEps']['raw']
                except KeyError:
                    stock_EPS = ''
                try:
                    stock_PE = json_dict[key]['forwardPE']['fmt']
                except KeyError:
                    stock_PE = ''

        return render(request, 'dashboard.html', {**kwargs, 'news_list' : news_list,'stock_close':stock_close,'stock_open':stock_open,'stock_bid':stock_bid,'stock_ask':stock_ask,'stock_volume':stock_volume,'stock_averageVolume':stock_averageVolume,'stock_marketCap':stock_marketCap,'stock_weekChange':stock_weekChange,'stock_beta':stock_beta,'stock_EPS':stock_EPS,'stock_PE':stock_PE,'stock_list':stock_list,'symbol':symbol}) 
    else:
        
        symbol = all_stocks[0]
        stock_list = all_stocks

        msft = yf.Ticker(symbol)
        hist = msft.history(period='max')

        # Define constants
        W_PLOT = 1000
        H_PLOT = 400
        TOOLS = 'pan,wheel_zoom,hover,reset'

        VBAR_WIDTH = 0.2
        RED = Category20[7][6]
        GREEN = Category20[5][4]

        BLUE = Category20[3][0]
        BLUE_LIGHT = Category20[3][1]

        ORANGE = Category20[3][2]
        PURPLE = Category20[9][8]
        BROWN = Category20[11][10]

        def get_symbol_df(symbol=None):
            df = pd.DataFrame(hist)[-50:]
            df.reset_index(inplace=True)
            df["Date"] = pd.to_datetime(df["Date"])
            return df

        def plot_stock_price(stock):
        
            p = figure(plot_width=W_PLOT, plot_height=H_PLOT, tools=TOOLS,
                    title="Stock price", toolbar_location='above')

            inc = stock.data['Close'] > stock.data['Open']
            dec = stock.data['Open'] > stock.data['Close']
            view_inc = CDSView(source=stock, filters=[BooleanFilter(inc)])
            view_dec = CDSView(source=stock, filters=[BooleanFilter(dec)])

            # map dataframe indices to date strings and use as label overrides
            p.xaxis.major_label_overrides = {i+int(stock.data['index'][0]): date.strftime('%b %d') for i, date in enumerate(pd.to_datetime(stock.data["Date"]))}
            p.xaxis.bounds = (stock.data['index'][0], stock.data['index'][-1])


            p.segment(x0='index', x1='index', y0='Low', y1='High', color=RED, source=stock, view=view_inc)
            p.segment(x0='index', x1='index', y0='Low', y1='High', color=GREEN, source=stock, view=view_dec)

            p.vbar(x='index', width=VBAR_WIDTH, top='Open', bottom='Close', fill_color=BLUE, line_color=BLUE,source=stock,view=view_inc, name="price")
            p.vbar(x='index', width=VBAR_WIDTH, top='Open', bottom='Close', fill_color=RED, line_color=RED,source=stock,view=view_dec, name="price")

            p.legend.location = "top_left"
            p.legend.border_line_alpha = 0
            p.legend.background_fill_alpha = 0
            p.legend.click_policy = "mute"

            p.yaxis.formatter = NumeralTickFormatter(format='$ 0,0[.]000')
            p.x_range.range_padding = 0.05
            p.xaxis.ticker.desired_num_ticks = 40
            p.xaxis.major_label_orientation = 3.14/4
            
            # Select specific tool for the plot
            price_hover = p.select(dict(type=HoverTool))

            # Choose, which glyphs are active by glyph name
            price_hover.names = ["price"]
            # Creating tooltips
            price_hover.tooltips = [("Datetime", "@Date{%Y-%m-%d}"),
                                    ("Open", "@Open{$0,0.00}"),
                                    ("Close", "@Close{$0,0.00}"),("Volume", "@Volume{($ 0.00 a)}")]
            price_hover.formatters={"Date": 'datetime'}

            return p

        
        stock = ColumnDataSource(data=dict(Date=[], Open=[], Close=[], High=[], Low=[],index=[]))
        df = get_symbol_df(symbol)
        stock.data = stock.from_df(df)
        elements = list()

        # update_plot()
        p_stock = plot_stock_price(stock)

        elements.append(p_stock)

        curdoc().add_root(column(elements))
        curdoc().title = 'Bokeh stocks historical prices'

        #show(p_stock)

        script, div = components(p_stock)
        kwargs = {'script': script, 'div': div}

        
        er = EventRegistry(apiKey = '3a7a023b-6280-4476-bec0-5c9ff41770bd')
        q = QueryArticlesIter(
            keywords = QueryItems.OR(stock_list),
            dataType = ["news"],
            lang = 'eng'
        )
        news_list = q.execQuery(er, sortBy = ["rel","date","sourceImportance"], maxItems = 5, )

        url = "https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/get-detail"

        querystring = {"region":"US","lang":"en","symbol":symbol}

        headers = {
            'x-rapidapi-host': "apidojo-yahoo-finance-v1.p.rapidapi.com",
            'x-rapidapi-key': "ff4cff81d5msh596a92309f2f43ap13412cjsn002bc2a74b4d"
            }

        response = requests.request("GET", url, headers=headers, params=querystring)
        json_dict  = json.loads(response.text)


        for key in json_dict.keys():
            if key == "summaryDetail":
                try:
                    stock_close = json_dict[key]['previousClose']['raw']
                except KeyError:
                    stock_close = ''
                    
                try:
                    stock_open = json_dict[key]['open']['raw']
                except KeyError:
                    stock_open = ''
                try:
                    stock_bid = json_dict[key]['bid']['raw']
                except KeyError:
                    stock_bid = ''
                try:
                    stock_ask = json_dict[key]['ask']['raw']
                except KeyError:
                    stock_ask = ''
                try:
                    stock_volume = json_dict[key]['volume']['fmt']
                except KeyError:
                    stock_volume = ''
                try:
                    stock_averageVolume = json_dict[key]['averageVolume']['fmt']
                except KeyError:
                    stock_averageVolume = ''
                try:
                    stock_marketCap = json_dict[key]['marketCap']['fmt']    
                except KeyError:  
                    stock_marketCap = ''
            if key == "defaultKeyStatistics":
                try:
                    stock_weekChange = json_dict[key]['52WeekChange']['fmt']
                except KeyError:
                    stock_weekChange = ''
                try:
                    stock_beta = json_dict[key]['beta']['fmt']
                except KeyError:
                    stock_beta = ''
                try:
                    stock_EPS = json_dict[key]['trailingEps']['raw']
                except KeyError:
                    stock_EPS = ''
                try:
                    stock_PE = json_dict[key]['forwardPE']['fmt']
                except KeyError:
                    stock_PE = ''

        return render(request, 'dashboard.html', {**kwargs, 'news_list' : news_list,'stock_close':stock_close,'stock_open':stock_open,'stock_bid':stock_bid,'stock_ask':stock_ask,'stock_volume':stock_volume,'stock_averageVolume':stock_averageVolume,'stock_marketCap':stock_marketCap,'stock_weekChange':stock_weekChange,'stock_beta':stock_beta,'stock_EPS':stock_EPS,'stock_PE':stock_PE,'stock_list':stock_list,'symbol':symbol}) 
    elif symbol == 'EBAY':
        lengthOfTime = '1y'
        break
    else:
        print("Oops you typed it in wrong, try again... \n")

df = get_symbol_df(symbol, lengthOfTime)
stock = ColumnDataSource(data=dict(date=[],
                                   Open=[],
                                   Close=[],
                                   high=[],
                                   low=[],
                                   volume=[],
                                   volHeight=[],
                                   index=[]))
stock.data = stock.from_df(df)

p_stock = plot_stock_price(stock, symbol, lengthOfTime)

while True:
    fifty = input("An orange 50 period moving average? Y/N: ").upper()
    if fifty == 'Y':
        plot_fifty_ma(df, p_stock)
        break
    elif fifty == 'N':
        break
    else:
        print("Oops you typed it in wrong, try again... \n")

while True:
    bollinger = input(
Ejemplo n.º 8
0
def plot():
    if request.method == 'POST':
        symbol = request.form['symbol']
        # Get Stock DataFrame
        # msft = yf.Ticker("MSFT")
        msft = yf.Ticker(symbol)
        hist = msft.history(period='max')

        # Define constants
        W_PLOT = 1000
        H_PLOT = 400
        TOOLS = 'pan,wheel_zoom,hover,reset'

        VBAR_WIDTH = 0.2
        RED = Category20[7][6]
        GREEN = Category20[5][4]

        BLUE = Category20[3][0]
        BLUE_LIGHT = Category20[3][1]

        ORANGE = Category20[3][2]
        PURPLE = Category20[9][8]
        BROWN = Category20[11][10]

        def get_symbol_df(symbol=None):
            df = pd.DataFrame(hist)[-50:]
            df.reset_index(inplace=True)
            df["Date"] = pd.to_datetime(df["Date"])
            return df

        def plot_stock_price(stock):

            p = figure(plot_width=W_PLOT,
                       plot_height=H_PLOT,
                       tools=TOOLS,
                       title="Stock price",
                       toolbar_location='above')

            inc = stock.data['Close'] > stock.data['Open']
            dec = stock.data['Open'] > stock.data['Close']
            view_inc = CDSView(source=stock, filters=[BooleanFilter(inc)])
            view_dec = CDSView(source=stock, filters=[BooleanFilter(dec)])

            # map dataframe indices to date strings and use as label overrides
            p.xaxis.major_label_overrides = {
                i + int(stock.data['index'][0]): date.strftime('%b %d')
                for i, date in enumerate(pd.to_datetime(stock.data["Date"]))
            }
            p.xaxis.bounds = (stock.data['index'][0], stock.data['index'][-1])

            p.segment(x0='index',
                      x1='index',
                      y0='Low',
                      y1='High',
                      color=RED,
                      source=stock,
                      view=view_inc)
            p.segment(x0='index',
                      x1='index',
                      y0='Low',
                      y1='High',
                      color=GREEN,
                      source=stock,
                      view=view_dec)

            p.vbar(x='index',
                   width=VBAR_WIDTH,
                   top='Open',
                   bottom='Close',
                   fill_color=BLUE,
                   line_color=BLUE,
                   source=stock,
                   view=view_inc,
                   name="price")
            p.vbar(x='index',
                   width=VBAR_WIDTH,
                   top='Open',
                   bottom='Close',
                   fill_color=RED,
                   line_color=RED,
                   source=stock,
                   view=view_dec,
                   name="price")

            p.legend.location = "top_left"
            p.legend.border_line_alpha = 0
            p.legend.background_fill_alpha = 0
            p.legend.click_policy = "mute"

            p.yaxis.formatter = NumeralTickFormatter(format='$ 0,0[.]000')
            p.x_range.range_padding = 0.05
            p.xaxis.ticker.desired_num_ticks = 40
            p.xaxis.major_label_orientation = 3.14 / 4

            # Select specific tool for the plot
            price_hover = p.select(dict(type=HoverTool))

            # Choose, which glyphs are active by glyph name
            price_hover.names = ["price"]
            # Creating tooltips
            price_hover.tooltips = [("Datetime", "@Date{%Y-%m-%d}"),
                                    ("Open", "@Open{$0,0.00}"),
                                    ("Close", "@Close{$0,0.00}"),
                                    ("Volume", "@Volume{($ 0.00 a)}")]
            price_hover.formatters = {"Date": 'datetime'}

            return p

        stock = ColumnDataSource(
            data=dict(Date=[], Open=[], Close=[], High=[], Low=[], index=[]))
        #stock = AjaxDataSource(data=dict(Date=[], Open=[], Close=[], High=[], Low=[],index=[]),data_url='http://127.0.0.1:5000/plot',polling_interval=1000,mode='append')
        #symbol = 'msft'
        df = get_symbol_df(symbol)
        stock.data = stock.from_df(df)
        elements = list()

        # update_plot()
        p_stock = plot_stock_price(stock)

        elements.append(p_stock)

        curdoc().add_root(column(elements))
        curdoc().title = 'Bokeh stocks historical prices'

        #show(p_stock)

        script, div = components(p_stock)
        kwargs = {'script': script, 'div': div}
        #kwargs['title'] = 'bokeh-with-flask'
        return render_template('index.html', **kwargs)

        #return redirect(url_for('index', **kwargs))
        #return kwargs
        #return json.dumps(json_item(p_stock,"myplot"))

    #return redirect(url_for('index'))
    return "OK"
Ejemplo n.º 9
0
                  height=400,
                  width=400)

OD['thr'] = OD[Z].apply(lambda x: 1 if x > threhsold_slider.value else 0)
OD['thr'] = OD['thr'].astype('str')
OD['x'] = OD[X]
OD['y'] = OD[Y]

seg_dictionary = {}

for col_name in OD.columns:
    # if col_name not in [X,Y]:
    seg_dictionary[col_name] = OD[col_name]

datapoints_source = ColumnDataSource()
datapoints_source.data = seg_dictionary

th = list(set(OD['thr']))
mapper = factor_cmap('thr', 'Category10_3', th)

st = Title()
st.text = "Segmentation Plot"
seg_plot.title = st

seg_plot.scatter("x",
                 "y",
                 source=datapoints_source,
                 legend="thr",
                 alpha=0.5,
                 size=12,
                 color=mapper)
Ejemplo n.º 10
0
    'No_of_paid_rides (Rides with (Fare - Coupon value) > 0)'
]

display_columns = df.columns

samp = 1
dfs = df.sample(int(samp * len(df) / 100), random_state=42)

dictionary = dict(x=dfs[X], y=dfs[Y], thr=dfs['threshold'])

for col_name in display_columns:
    # if col_name not in [X,Y]:
    dictionary[col_name] = dfs[col_name]

datapoints_source = ColumnDataSource()
datapoints_source.data = dictionary

dpal = ColumnDataSource()
dpal.data = dict(th=list(set(df['Rental_Counts']))
                 #time=datapoints_df['start_datetime']
                 )

from bokeh.models.annotations import Title

import numpy as np


def my_slider_handler():
    #slider.value=new

    X = Xselect.value