<s> from numpy import asarray , cumprod , convolve , exp , ones <EOL> from numpy . random import lognormal , gamma , uniform <EOL> from bokeh . models import ColumnDataSource , Slider , VBox , HBox , Select , GridPlot <EOL> from bokeh . plotting import curdoc , Figure <EOL> from bokeh . driving import count <EOL> BUFSIZE = <NUM_LIT:200> <EOL> MA12 , MA26 , EMA12 , EMA26 = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> source = ColumnDataSource ( dict ( <EOL> time = [ ] , average = [ ] , low = [ ] , high = [ ] , open = [ ] , close = [ ] , <EOL> ma = [ ] , macd = [ ] , macd9 = [ ] , macdh = [ ] , color = [ ] <EOL> ) ) <EOL> p = Figure ( plot_height = <NUM_LIT> , tools = "<STR_LIT>" , x_axis_type = None ) <EOL> p . x_range . follow = "<STR_LIT:end>" <EOL> p . x_range . follow_interval = <NUM_LIT:100> <EOL> p . x_range . range_padding = <NUM_LIT:0> <EOL> p . line ( x = '<STR_LIT:time>' , y = '<STR_LIT>' , alpha = <NUM_LIT> , line_width = <NUM_LIT:3> , color = '<STR_LIT>' , source = source ) <EOL> p . line ( x = '<STR_LIT:time>' , y = '<STR_LIT>' , alpha = <NUM_LIT> , line_width = <NUM_LIT:2> , color = '<STR_LIT>' , source = source ) <EOL> p . segment ( x0 = '<STR_LIT:time>' , y0 = '<STR_LIT>' , x1 = '<STR_LIT:time>' , y1 = '<STR_LIT>' , line_width = <NUM_LIT:2> , color = '<STR_LIT>' , source = source ) <EOL> p . segment ( x0 = '<STR_LIT:time>' , y0 = '<STR_LIT>' , x1 = '<STR_LIT:time>' , y1 = '<STR_LIT>' , line_width = <NUM_LIT:8> , color = '<STR_LIT>' , source = source ) <EOL> p2 = Figure ( plot_height = <NUM_LIT> , x_range = p . x_range , tools = "<STR_LIT>" ) <EOL> p2 . line ( x = '<STR_LIT:time>' , y = '<STR_LIT>' , color = '<STR_LIT>' , source = source ) <EOL> p2 . line ( x = '<STR_LIT:time>' , y = '<STR_LIT>' , color = '<STR_LIT>' , source = source ) <EOL> p2 . segment ( x0 = '<STR_LIT:time>' , y0 = <NUM_LIT:0> , x1 = '<STR_LIT:time>' , y1 = '<STR_LIT>' , line_width = <NUM_LIT:6> , color = '<STR_LIT>' , alpha = <NUM_LIT:0.5> , source = source ) <EOL> mean = Slider ( title = "<STR_LIT>" , value = <NUM_LIT:0> , start = - <NUM_LIT> , end = <NUM_LIT> , step = <NUM_LIT> ) <EOL> stddev = Slider ( title = "<STR_LIT>" , value = <NUM_LIT> , start = <NUM_LIT> , end = <NUM_LIT:0.1> , step = <NUM_LIT> ) <EOL> mavg = Select ( value = MA12 , options = [ MA12 , MA26 , EMA12 , EMA26 ] ) <EOL> curdoc ( ) . add_root ( VBox ( HBox ( mean , stddev , mavg , width = <NUM_LIT> ) , GridPlot ( children = [ [ p ] , [ p2 ] ] ) ) ) <EOL> def _create_prices ( t ) : <EOL> last_average = <NUM_LIT:100> if t == <NUM_LIT:0> else source . data [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] <EOL> returns = asarray ( lognormal ( mean . value , stddev . value , <NUM_LIT:1> ) ) <EOL> average = last_average * cumprod ( returns ) <EOL> high = average * exp ( abs ( gamma ( <NUM_LIT:1> , <NUM_LIT> , size = <NUM_LIT:1> ) ) ) <EOL> low = average / exp ( abs ( gamma ( <NUM_LIT:1> , <NUM_LIT> , size = <NUM_LIT:1> ) ) ) <EOL> delta = high - low <EOL> open = low + delta * uniform ( <NUM_LIT> , <NUM_LIT> , size = <NUM_LIT:1> ) <EOL> close = low + delta * uniform ( <NUM_LIT> , <NUM_LIT> , size = <NUM_LIT:1> ) <EOL> return open [ <NUM_LIT:0> ] , high [ <NUM_LIT:0> ] , low [ <NUM_LIT:0> ] , close [ <NUM_LIT:0> ] , average [ <NUM_LIT:0> ] <EOL> def _moving_avg ( prices , days = <NUM_LIT:10> ) : <EOL> if len ( prices ) < days : return [ <NUM_LIT:100> ] <EOL> return convolve ( prices [ - days : ] , ones ( days , dtype = float ) , mode = "<STR_LIT>" ) / days <EOL> def _ema ( prices , days = <NUM_LIT:10> ) : <EOL> if len ( prices ) < days or days < <NUM_LIT:2> : return [ prices [ - <NUM_LIT:1> ] ] <EOL> a = <NUM_LIT> / ( days + <NUM_LIT:1> ) <EOL> kernel = ones ( days , dtype = float ) <EOL> kernel [ <NUM_LIT:1> : ] = <NUM_LIT:1> - a <EOL> kernel = a * cumprod ( kernel ) <EOL> return convolve ( prices [ - days : ] , kernel , mode = "<STR_LIT>" ) / ( <NUM_LIT> ) <EOL> @ count ( ) <EOL> def update ( t ) : <EOL> open , high , low , close , average = _create_prices ( t ) <EOL> color = "<STR_LIT>" if open < close else "<STR_LIT>" <EOL> new_data = dict ( <EOL> time = [ t ] , <EOL> open = [ open ] , <EOL> high = [ high ] , <EOL> low = [ low ] , <EOL> close = [ close ] , <EOL> average = [ average ] , <EOL> color = [ color ] , <EOL> ) <EOL> close = source . data [ '<STR_LIT>' ] + [ close ] <EOL> ma12 = _moving_avg ( close [ - <NUM_LIT:12> : ] , <NUM_LIT:12> ) [ <NUM_LIT:0> ] <EOL> ma26 = _moving_avg ( close [ - <NUM_LIT> : ] , <NUM_LIT> ) [ <NUM_LIT:0> ] <EOL> ema12 = _ema ( close [ - <NUM_LIT:12> : ] , <NUM_LIT:12> ) [ <NUM_LIT:0> ] <EOL> ema26 = _ema ( close [ - <NUM_LIT> : ] , <NUM_LIT> ) [ <NUM_LIT:0> ] <EOL> if mavg . value == MA12 : new_data [ '<STR_LIT>' ] = [ ma12 ] <EOL> elif mavg . value == MA26 : new_data [ '<STR_LIT>' ] = [ ma26 ] <EOL> elif mavg . value == EMA12 : new_data [ '<STR_LIT>' ] = [ ema12 ] <EOL> elif mavg . value == EMA26 : new_data [ '<STR_LIT>' ] = [ ema26 ] <EOL> macd = ema12 - ema26 <EOL> new_data [ '<STR_LIT>' ] = [ macd ] <EOL> macd_series = source . data [ '<STR_LIT>' ] + [ macd ] <EOL> macd9 = _ema ( macd_series [ - <NUM_LIT> : ] , <NUM_LIT:9> ) [ <NUM_LIT:0> ] <EOL> new_data [ '<STR_LIT>' ] = [ macd9 ] <EOL> new_data [ '<STR_LIT>' ] = [ macd - macd9 ] <EOL> source . stream ( new_data , <NUM_LIT> ) <EOL> curdoc ( ) . add_periodic_callback ( update , <NUM_LIT:50> ) </s>
