Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. import numpy as np
  2. from bokeh.layouts import column
  3. from bokeh.plotting import figure, show, output_file
  4. # imports for the example
  5. import random
  6. from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT
  7.  
  8.  
  9. def datetime(x):
  10. return np.array(x, dtype=np.datetime64)
  11.  
  12.  
  13. def create_plot(chart_title):
  14. plot = figure(x_axis_type="datetime", title=chart_title, plot_height=600, width=1000)
  15. plot.grid.grid_line_alpha = 0.3
  16. plot.xaxis.axis_label = 'Date'
  17. plot.yaxis.axis_label = 'Price'
  18. return plot
  19.  
  20.  
  21. def create_plot_oscillator(related_plot):
  22. plot = figure(x_axis_type="datetime", x_range=related_plot.x_range, plot_height=150, width=1000)
  23. plot.grid.grid_line_alpha = 0.3
  24. plot.xaxis.axis_label = 'Date'
  25. plot.yaxis.axis_label = 'Price'
  26. return plot
  27.  
  28.  
  29. def add_line_to_plot(plot, data_title, data_date, data_close, color):
  30. plot.line(data_date, data_close, color=color, legend=data_title)
  31. return plot
  32.  
  33.  
  34. def add_up_to_plot(plot, data_title, data_date, data_close, color="green"):
  35. plot.triangle(x=data_date, y=data_close, color=color, legend=data_title)
  36. return plot
  37.  
  38.  
  39. def add_down_to_plot(plot, data_title, data_date, data_close, color="red"):
  40. plot.inverted_triangle(x=data_date, y=data_close,
  41. color=color, legend=data_title)
  42. return plot
  43.  
  44.  
  45. def show_plot(plot, oscillator=None):
  46. plot.legend.location = "top_left"
  47. output_file("stocks.html", title="stocks.py example")
  48. if oscillator is None:
  49. show(column(children=[plot]))
  50. else:
  51. show(column(children=[plot, oscillator]))
  52.  
  53.  
  54. # Example of how to use this code
  55. plot = create_plot("My chart title")
  56. new_plot = add_line_to_plot(plot, "APPL", datetime(
  57. AAPL['date']), AAPL['adj_close'], '#33A02C')
  58. new_plot = add_up_to_plot(plot, "GOOG", datetime(GOOG['date']), GOOG['adj_close'])
  59. new_plot = add_down_to_plot(plot, "MSFT", datetime(MSFT['date']), MSFT['adj_close'])
  60.  
  61. oscillator = create_plot_oscillator(new_plot)
  62. indicator = []
  63. for x in IBM['adj_close']:
  64. indicator.append(random.random()*100)
  65.  
  66. add_line_to_plot(oscillator, "IBM", datetime(IBM['date']), indicator, 'blue')
  67. show_plot(new_plot, oscillator)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement