Advertisement
Guest User

Carlos's Index

a guest
Jul 30th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. from math import pi
  2. from bokeh.plotting import figure, show, output_file
  3. from datetime import datetime
  4. import pandas_datareader.data as web
  5. import pandas as pd
  6.  
  7. start_year = 2016
  8. start_month = 01
  9. start_day = 01
  10.  
  11.  
  12. end_year = 2016
  13. end_month = 07
  14. end_day = 29
  15.  
  16. 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
  17. 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
  18.  
  19. df_index_csv = df_apple + df_google # Add the stock history together to get Carlos's Index
  20.  
  21. df_index_csv.index = pd.DatetimeIndex(df_index_csv.index) # create time index
  22.  
  23. 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
  24.  
  25. df_index_csv = df_index_csv.reindex(idx, fill_value=0) # Fill in indieces without values with 0
  26.  
  27. test = df_index_csv.shape[0] # get length of Rows
  28.  
  29. df_index_csv.loc[:,'Rows'] = pd.Series(range(test), index=df_index_csv.index) # Create new Column at end with row number
  30.  
  31. df_index_csv.to_csv("test.csv") # Print all data to a CSV file
  32.  
  33. mids = (df_index_csv.Open + df_index_csv.Close)/2 # get the mid point value of stock each day
  34.  
  35. spans = abs(df_index_csv.Close-df_index_csv.Open) # get the over size of candle stick body per day
  36.  
  37. inc = df_index_csv.Close > df_index_csv.Open # Determine if stock increased each day
  38.  
  39. dec = df_index_csv.Open > df_index_csv.Close # Determine if stock decreased each day
  40.  
  41. w = 12*60*60*1000 # half day in ms
  42.  
  43. output_file("candlestick.html") # Create an HTML file that will be display in browser
  44.  
  45. TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
  46.  
  47. p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1500, toolbar_location="right") # consfigure the chart
  48.  
  49. p.title.text = "Carlos's Index" # Print the title of the chart
  50.  
  51. p.segment(idx, df_index_csv.High, idx, df_index_csv.Low, color="black") # draw the body of the candle stick
  52.  
  53. p.rect(idx[inc], mids[inc], w, spans[inc], fill_color="#7CFC00", line_color="black") # If day increased paint candle body Green
  54.  
  55. p.rect(idx[dec], mids[dec], w, spans[dec], fill_color="#FF0000", line_color="black") # If day increased paint candle body red
  56.  
  57.  
  58. p.xaxis.major_label_orientation = pi/4
  59. p.grid.grid_line_alpha=0.3
  60.  
  61. show(p)  # open a browser
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement