Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. from numpy import asarray, cumprod, convolve, exp, ones, nan
  2. from numpy.random import lognormal, gamma, uniform
  3.  
  4. from bokeh.models import ColumnDataSource, Slider, VBox, HBox, Select
  5. from bokeh.plotting import ColumnDataSource, curdoc, Figure
  6. from bokeh.driving import count
  7.  
  8. BUFSIZE = 200
  9. MA12, MA26, EMA12, EMA26 = '12-tick Moving Avg', '26-tick Moving Avg', '12-tick EMA', '26-tick EMA'
  10.  
  11. source = ColumnDataSource(dict(
  12. time=[], average=[], low=[], high=[], open=[], close=[],
  13. ma=[], macd=[], macd9=[], macdh=[], color=[]
  14. ))
  15.  
  16. p = Figure(plot_width=1200, plot_height=600, toolbar_location="left")
  17. p.x_range.follow = "end"
  18. p.x_range.follow_interval = 100
  19. p.background_fill_color = "#eeeeee"
  20.  
  21. p.line(x='time', y='average', alpha=0.2, line_width=3, color='navy', source=source)
  22. p.line(x='time', y='ma', alpha=0.8, line_width=2, color='orange', source=source)
  23. p.segment(x0='time', y0='low', x1='time', y1='high', source=source, color='black', line_width=2)
  24. p.segment(x0='time', y0='open', x1='time', y1='close', line_width=8, color='color', source=source)
  25.  
  26. p2 = Figure(plot_width=1200, plot_height=250, toolbar_location="left", tools="pan", x_range=p.x_range)
  27.  
  28. p2.line(x='time', y='macd', color='red', source=source)
  29. p2.line(x='time', y='macd9', color='blue', source=source)
  30. p2.segment(x0='time', y0=0, x1='time', y1='macdh', line_width=6, color='black', alpha=0.5, source=source)
  31.  
  32. mean = Slider(title="mean", value=0, start=-0.01, end=0.01, step=0.001)
  33. stddev = Slider(title="stddev", value=0.04, start=0.01, end=0.1, step=0.01)
  34. mavg = Select(value=MA12, options=[MA12, MA26, EMA12, EMA26])
  35.  
  36. curdoc().add_root(VBox(HBox(mean, stddev, mavg), VBox(p, p2)))
  37.  
  38. def _create_prices(t):
  39. last_average = 100 if t==0 else source.data['average'][-1]
  40. returns = asarray(lognormal(mean.value, stddev.value, 1))
  41. average = last_average * cumprod(returns)
  42. high = average * exp(abs(gamma(1, 0.03, size=1)))
  43. low = average / exp(abs(gamma(1, 0.03, size=1)))
  44. delta = high - low
  45. open = low + delta * uniform(0.05, 0.95, size=1)
  46. close = low + delta * uniform(0.05, 0.95, size=1)
  47. return open[0], high[0], low[0], close[0], average[0]
  48.  
  49. def _moving_avg(prices, days=10):
  50. if len(prices) < days: return [100]
  51. return convolve(prices[-days:], ones(days, dtype=float), mode="valid") / days
  52.  
  53. def _ema(prices, days=10):
  54. if len(prices) < days or days < 2: return [100]
  55. a = 2.0 / (days+1)
  56. kernel = ones(days, dtype=float)
  57. kernel[1:] = 1 - a
  58. kernel = a * cumprod(kernel)
  59. # The 0.8647 is to normalize out the fact that we are stopping
  60. # the EMA after a finite number of terms.
  61. return convolve(prices[-days:], kernel, mode="valid") / (0.8647)
  62.  
  63. @count()
  64. def update(t):
  65. open, high, low, close, average = _create_prices(t)
  66. color = "green" if open < close else "red"
  67. data = dict()
  68. data['time'] = source.data['time'][-BUFSIZE:] + [t]
  69. data['open'] = source.data['open'][-BUFSIZE:] + [open]
  70. data['high'] = source.data['high'][-BUFSIZE:] + [high]
  71. data['low'] = source.data['low'][-BUFSIZE:] + [low]
  72. data['close'] = source.data['close'][-BUFSIZE:] + [close]
  73. data['average'] = source.data['average'][-BUFSIZE:] + [average]
  74. data['color'] = source.data['color'][-BUFSIZE:] + [color]
  75.  
  76. ma12 = _moving_avg(data['close'][-12:], 12)[0]
  77. ma26 = _moving_avg(data['close'][-26:], 26)[0]
  78. ema12 = _ema(data['close'][-12:], 12)[0]
  79. ema26 = _ema(data['close'][-26:], 26)[0]
  80. if mavg.value == MA12: data['ma'] = source.data['ma'][-BUFSIZE:] + [ma12]
  81. elif mavg.value == MA26: data['ma'] = source.data['ma'][-BUFSIZE:] + [ma26]
  82. elif mavg.value == EMA12: data['ma'] = source.data['ma'][-BUFSIZE:] + [ema12]
  83. elif mavg.value == EMA26: data['ma'] = source.data['ma'][-BUFSIZE:] + [ema26]
  84.  
  85. macd = ema12 - ema26
  86. data['macd'] = source.data['macd'][-BUFSIZE:] + [macd]
  87. macd9 = _ema(data['macd'][-26:], 9)[0]
  88. data['macd9'] = source.data['macd9'][-BUFSIZE:] + [macd9]
  89. macdh = macd - macd9
  90. data['macdh'] = source.data['macdh'][-BUFSIZE:] + [macdh]
  91.  
  92. source.data = data
  93.  
  94. curdoc().add_periodic_callback(update, 100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement