Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import pi
- from bokeh.plotting import figure, show, output_file
- from datetime import datetime
- import pandas_datareader.data as web
- import pandas as pd
- start_year = 2016
- start_month = 01
- start_day = 01
- end_year = 2016
- end_month = 07
- end_day = 29
- df_apple = web.DataReader("aapl", 'yahoo', datetime(start_year, start_month, start_day), datetime(end_year, end_month, end_day)) # Get Apple stock History info
- df_google = web.DataReader("goog", 'yahoo', datetime(start_year, start_month, start_day), datetime(end_year, end_month, end_day)) # Get Apple stock History info
- df_index_csv = df_apple + df_google # Add the stock history together to get Carlos's Index
- df_index_csv.index = pd.DatetimeIndex(df_index_csv.index) # create time index
- idx = pd.date_range(str(start_month) + '/' + str(start_day) + '/' + str(start_year), str(end_month) + '/' + str(end_day) + '/' + str(end_year)) # create time index span
- df_index_csv = df_index_csv.reindex(idx, fill_value=0) # Fill in indieces without values with 0
- test = df_index_csv.shape[0] # get length of Rows
- df_index_csv.loc[:,'Rows'] = pd.Series(range(test), index=df_index_csv.index) # Create new Column at end with row number
- df_index_csv.to_csv("test.csv") # Print all data to a CSV file
- mids = (df_index_csv.Open + df_index_csv.Close)/2 # get the mid point value of stock each day
- spans = abs(df_index_csv.Close-df_index_csv.Open) # get the over size of candle stick body per day
- inc = df_index_csv.Close > df_index_csv.Open # Determine if stock increased each day
- dec = df_index_csv.Open > df_index_csv.Close # Determine if stock decreased each day
- w = 12*60*60*1000 # half day in ms
- output_file("candlestick.html") # Create an HTML file that will be display in browser
- TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
- p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1500, toolbar_location="right") # consfigure the chart
- p.title.text = "Carlos's Index" # Print the title of the chart
- p.segment(idx, df_index_csv.High, idx, df_index_csv.Low, color="black") # draw the body of the candle stick
- p.rect(idx[inc], mids[inc], w, spans[inc], fill_color="#7CFC00", line_color="black") # If day increased paint candle body Green
- p.rect(idx[dec], mids[dec], w, spans[dec], fill_color="#FF0000", line_color="black") # If day increased paint candle body red
- p.xaxis.major_label_orientation = pi/4
- p.grid.grid_line_alpha=0.3
- show(p) # open a browser
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement