huseinzol05 / stock-prediction-models Goto Github PK
View Code? Open in Web Editor NEWGathers machine learning and deep learning models for Stock forecasting including trading bots and simulations
License: Apache License 2.0
Gathers machine learning and deep learning models for Stock forecasting including trading bots and simulations
License: Apache License 2.0
In the model of https://github.com/huseinzol05/Stock-Prediction-Models/blob/master/agent/evolution-strategy-bayesian-agent.ipynb
I get the following:
print('Best AGENT accuracy value: %f' % NN_BAYESIAN.res['max']['max_val'])
3 print('Best AGENT parameters: ', NN_BAYESIAN.res['max']['max_params'])
TypeError: list indices must be integers or slices, not str
Could you perhaps clear up those 2 lines and provide some context there? Thanks for the great research.
First of all thank you for sharing your code. Its interesting. I have a question how do i save a Trained agent and reuse it?
i don't need flask. what the code list function to be added to make the code run on live markets agent.buy() or ages.trade(data) ?
Also part from buy and sell call API whether we need to connect other data webnbsockets to the app.py if yes. plz guide which one and how ?
@huseinzol05
I know this is dummy beginner question but how do I calculate mean directional accuracy for stack-rnn-arima-xgb or any deep learning experiments?
E.g. instead of mean square error I want to calculate mean signum difference between current and previous value.
Thanks for your help
Hi,
I am running your jupyter notebook, the models I have tested, are all working, however I am getting slightly different results to you. Given that the models and data are the same I am assuming that there is something different with my Python Environment. In a separate issue, I have found the below package versions listed:
seaborn==0.9.0
pandas==0.23.4
numpy==1.14.5
matplotlib==3.0.2
Are you able to let me know the other package versions you are using please? Keras, Tensorflow, python and anything else that you think is significant.
Thanks
Hi,
nice collection, all together. Maybe one suggestion to add ARIMA model, looks good on some stocks.
I have question, I created account at https://iexcloud.io/docs/api/#streaming to get updates in real-time on stock, so I would like to try the models run not only on daily, but per hour/minute basis. I will store the data in some kind of database or even CSV with range timestamp.
Q: Is there any issue I should expect when adapting from daily to higher time resolution on models (parameters changes, etc.)?
Q: Are agents at moment ready to update trained model on latest data continuously? (I am still discovering from yesterday this repo :-) ). Lets say I will stream 100 stocks, and 30 stocsk I will use for testing, but the models need to be continuously updated(or new versions of those, we can always store each version, so we can also see convergence between versions in time to discover accuracy change) on sime interval basis)
Dear husein,
In the (https://github.com/huseinzol05/Stock-Prediction-Models/blob/master/stacking/stack-rnn-arima-xgb.ipynb), I can not import model in line 11. Is the file (model.py) missing?
Thank you very much!
The DNC example looks broken. We should focus more attention to DNCs, I think.
For the realtime agent i am trying to add evolution-strategy-bayesian-agent how would i do that? i tried rewriting and changing lots of parts in the realtime agent and i am stuck with errors and not any clear way to do it please if i can get example code it would will help or what exactly to change to make it work?
also in the scriot what do these variables mean?
. Capitial = I guess how much money you have?
. inventory_size = I guess How much stock to trade?
. mean_inventory = ?
i am not sure what they mean
I tried to use LSTM as well as vanilla-2path for prediction.
With the GOOG-year.csv dataset everything went right , but when using the btcusd.csv dataset from TensorTom or even getting my own Data from the bitmex exchange (same format as GOOG-year.csv) I get following error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1334, in _do_call
return fn(*args)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.UnimplementedError: TensorArray has size zero, but element shape [?,128] is not fully defined. Currently only static shapes are supported when packing zero-size TensorArrays.
[[{{node rnn/TensorArrayStack/TensorArrayGatherV3}}]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "LSTM_Custom.py", line 177, in <module>
LSTM(exchange='bitmex' , symbol='BTC/USD' , start_date='2018-05-01T00:00:00Z' , timeframe='1d' , future_day=50 , fetch_size=25)
File "LSTM_Custom.py", line 136, in LSTM
modelnn.hidden_layer: init_value,
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 929, in run
run_metadata_ptr)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1152, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1328, in _do_run
run_metadata)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1348, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.UnimplementedError: TensorArray has size zero, but element shape [?,128] is not fully defined. Currently only static shapes are supported when packing zero-size TensorArrays.
[[node rnn/TensorArrayStack/TensorArrayGatherV3 (defined at LSTM_Custom.py:41) ]]
Caused by op 'rnn/TensorArrayStack/TensorArrayGatherV3', defined at:
File "LSTM_Custom.py", line 177, in <module>
LSTM(exchange='bitmex' , symbol='BTC/USD' , start_date='2018-05-01T00:00:00Z' , timeframe='1d' , future_day=50 , fetch_size=25)
File "LSTM_Custom.py", line 83, in LSTM
0.01, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate
File "LSTM_Custom.py", line 41, in __init__
drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py", line 671, in dynamic_rnn
dtype=dtype)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py", line 883, in _dynamic_rnn_loop
final_outputs = tuple(ta.stack() for ta in output_final_ta)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py", line 883, in <genexpr>
final_outputs = tuple(ta.stack() for ta in output_final_ta)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/tensor_array_ops.py", line 1128, in stack
return self._implementation.stack(name=name)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/tensor_array_ops.py", line 292, in stack
return self.gather(math_ops.range(0, self.size()), name=name)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/tensor_array_ops.py", line 306, in gather
element_shape=element_shape)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_data_flow_ops.py", line 6291, in tensor_array_gather_v3
element_shape=element_shape, name=name)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
op_def=op_def)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 1801, in __init__
self._traceback = tf_stack.extract_stack()
UnimplementedError (see above for traceback): TensorArray has size zero, but element shape [?,128] is not fully defined. Currently only static shapes are supported when packing zero-size TensorArrays.
[[node rnn/TensorArrayStack/TensorArrayGatherV3 (defined at LSTM_Custom.py:41) ]]
Any idea why that happens? CSV file from TensorTom: https://github.com/TensorTom/Timeseries-Prediction-Models/blob/master/dataset/btcusd.csv
Love the repository! Great work, keep doing what you're doing.
I was playing with the forecasting-js and noticed that the candlestick colours were backwards (a bullish candlestick was red, and bearish was green), so to fix that, just edit the init.js file, find the candlestick part and make sure it reads:
}, {
type: 'candlestick',
name: 'STOCK',
data: stocks,
itemStyle: {
normal: {
color: '#14b143',
color0: '#ef232a',
borderColor: '#14b143',
borderColor0: '#ef232a'
},
Also, would be amazing to have a dropdown option to choose which model/agent to use when training and forecasting your own data :D
Trying to run: https://github.com/huseinzol05/Stock-Prediction-Models/blob/master/deep-learning/17.lstm-seq2seq-bidirectional-attention.ipynb
With the data: https://gist.github.com/TensorTom/d37f4bd605d6a13dfc75015c5e150b82
But this happens during training epochs (Or just at or after the last epoch):
---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call last)
/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1333 try:
-> 1334 return fn(*args)
1335 except errors.OpError as e:
/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1318 return self._call_tf_sessionrun(
-> 1319 options, feed_dict, fetch_list, target_list, run_metadata)
1320
/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1406 self._session, options, feed_dict, fetch_list, target_list,
-> 1407 run_metadata)
1408
UnimplementedError: TensorArray has size zero, but element shape [?,128] is not fully defined. Currently only static shapes are supported when packing zero-size TensorArrays.
[[{{node decoder/bidirectional_rnn/fw/fw/TensorArrayStack/TensorArrayGatherV3}}]]
[[{{node decoder/bidirectional_rnn/bw/bw/while/Exit_3}}]]
During handling of the above exception, another exception occurred:
UnimplementedError Traceback (most recent call last)
<ipython-input-8-07a23d0b1dbd> in <module>
22 modelnn.X: np.expand_dims(df_log.iloc[upper_b:], axis = 0),
23 modelnn.backward_hidden_layer: init_value_backward,
---> 24 modelnn.forward_hidden_layer: init_value_forward,
25 },
26 )
/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
927 try:
928 result = self._run(None, fetches, feed_dict, options_ptr,
--> 929 run_metadata_ptr)
930 if run_metadata:
931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1150 if final_fetches or final_targets or (handle and feed_dict_tensor):
1151 results = self._do_run(handle, final_targets, final_fetches,
-> 1152 feed_dict_tensor, options, run_metadata)
1153 else:
1154 results = []
/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1326 if handle is None:
1327 return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1328 run_metadata)
1329 else:
1330 return self._do_call(_prun_fn, handle, feeds, fetches)
/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1346 pass
1347 message = error_interpolation.interpolate(message, self._graph)
-> 1348 raise type(e)(node_def, op, message)
1349
1350 def _extend_graph(self):
UnimplementedError: TensorArray has size zero, but element shape [?,128] is not fully defined. Currently only static shapes are supported when packing zero-size TensorArrays.
[[node decoder/bidirectional_rnn/fw/fw/TensorArrayStack/TensorArrayGatherV3 (defined at <ipython-input-5-7fc0ccd4ec49>:97) ]]
[[node decoder/bidirectional_rnn/bw/bw/while/Exit_3 (defined at <ipython-input-5-7fc0ccd4ec49>:97) ]]
Caused by op 'decoder/bidirectional_rnn/fw/fw/TensorArrayStack/TensorArrayGatherV3', defined at:
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/lib/python3.7/site-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/lib/python3.7/site-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/usr/lib/python3.7/site-packages/ipykernel/kernelapp.py", line 505, in start
self.io_loop.start()
File "/usr/lib/python3.7/site-packages/tornado/platform/asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.7/asyncio/base_events.py", line 539, in run_forever
self._run_once()
File "/usr/lib/python3.7/asyncio/base_events.py", line 1775, in _run_once
handle._run()
File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/lib/python3.7/site-packages/tornado/ioloop.py", line 758, in _run_callback
ret = callback()
File "/usr/lib/python3.7/site-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 1233, in inner
self.run()
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 1147, in run
yielded = self.gen.send(value)
File "/usr/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 370, in dispatch_queue
yield self.process_one()
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 346, in wrapper
runner = Runner(result, future, yielded)
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 1080, in __init__
self.run()
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 1147, in run
yielded = self.gen.send(value)
File "/usr/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 357, in process_one
yield gen.maybe_future(dispatch(*args))
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 267, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/lib/python3.7/site-packages/ipykernel/kernelbase.py", line 534, in execute_request
user_expressions, allow_stdin,
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 326, in wrapper
yielded = next(result)
File "/usr/lib/python3.7/site-packages/ipykernel/ipkernel.py", line 294, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/lib/python3.7/site-packages/ipykernel/zmqshell.py", line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2819, in run_cell
raw_cell, store_history, silent, shell_futures)
File "/usr/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2845, in _run_cell
return runner(coro)
File "/usr/lib/python3.7/site-packages/IPython/core/async_helpers.py", line 67, in _pseudo_sync_runner
coro.send(None)
File "/usr/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3020, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3185, in run_ast_nodes
if (yield from self.run_code(code, result)):
File "/usr/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-6-2e75d5202b26>", line 2, in <module>
modelnn = Model(0.01, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate)
File "<ipython-input-5-7fc0ccd4ec49>", line 97, in __init__
dtype = tf.float32,
File "/usr/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "/usr/lib/python3.7/site-packages/tensorflow/python/ops/rnn.py", line 443, in bidirectional_dynamic_rnn
time_major=time_major, scope=fw_scope)
File "/usr/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "/usr/lib/python3.7/site-packages/tensorflow/python/ops/rnn.py", line 671, in dynamic_rnn
dtype=dtype)
File "/usr/lib/python3.7/site-packages/tensorflow/python/ops/rnn.py", line 883, in _dynamic_rnn_loop
final_outputs = tuple(ta.stack() for ta in output_final_ta)
File "/usr/lib/python3.7/site-packages/tensorflow/python/ops/rnn.py", line 883, in <genexpr>
final_outputs = tuple(ta.stack() for ta in output_final_ta)
File "/usr/lib/python3.7/site-packages/tensorflow/python/ops/tensor_array_ops.py", line 1128, in stack
return self._implementation.stack(name=name)
File "/usr/lib/python3.7/site-packages/tensorflow/python/ops/tensor_array_ops.py", line 292, in stack
return self.gather(math_ops.range(0, self.size()), name=name)
File "/usr/lib/python3.7/site-packages/tensorflow/python/ops/tensor_array_ops.py", line 306, in gather
element_shape=element_shape)
File "/usr/lib/python3.7/site-packages/tensorflow/python/ops/gen_data_flow_ops.py", line 6291, in tensor_array_gather_v3
element_shape=element_shape, name=name)
File "/usr/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "/usr/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/usr/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
op_def=op_def)
File "/usr/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1801, in __init__
self._traceback = tf_stack.extract_stack()
UnimplementedError (see above for traceback): TensorArray has size zero, but element shape [?,128] is not fully defined. Currently only static shapes are supported when packing zero-size TensorArrays.
[[node decoder/bidirectional_rnn/fw/fw/TensorArrayStack/TensorArrayGatherV3 (defined at <ipython-input-5-7fc0ccd4ec49>:97) ]]
[[node decoder/bidirectional_rnn/bw/bw/while/Exit_3 (defined at <ipython-input-5-7fc0ccd4ec49>:97) ]]
When I run app.py from the realtime using the realtime-evolution-strategy.ipynb i get this error how can I fix it?
feed = np.dot(inputs, self.weights[0]) + self.weights[-2]
ValueError: shapes (1,79) and (399,500) not aligned: 79 (dim 1) != 399 (dim 0)
Hi Husein,
In the class Deep_Evolution_Strategy, this piece standardises the rewards
rewards = (rewards - np.mean(rewards)) / np.std(rewards)
If the rewards contains the same value in a row, then np.std(rewards) will be zero caused the whole array NaN.
My debug log:
before rewards <class 'numpy.ndarray'> (15,) : [37.1118028 37.1118028 37.1118028 37.1118028 37.1118028 37.1118028 37.1118028 37.1118028 37.1118028 37.1118028 37.1118028 37.1118028 37.1118028 37.1118028 37.1118028]
normalized rewards <class 'numpy.ndarray'> (15,) : [nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan]
Probably we might need to amend:
rewards = (rewards - np.mean(rewards)) / np.std(rewards) if np.std(rewards) > 0 else rewards
Regards,
Steve
Hi,
How do I save the trained Neuroevolution model and reuse it? agent/21.neuro-evolution-agent.ipynb
Users/swapnilbhadade/venv/lib/python3.7/site-packages/sklearn/gaussian_process/gpr.py:357: UserWarning: Predicted variances smaller than 0. Setting those variances to 0. warnings.warn("Predicted variances smaller than 0. " /Users/swapnilbhadade/venv/lib/python3.7/site-packages/bayes_opt/helpers.py:109: RuntimeWarning: invalid value encountered in true_divide z = (mean - y_max - xi)/std /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py:877: RuntimeWarning: invalid value encountered in greater return (self.a < x) & (x < self.b) /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py:877: RuntimeWarning: invalid value encountered in less return (self.a < x) & (x < self.b) /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py:1748: RuntimeWarning: invalid value encountered in greater_equal cond2 = (x >= self.b) & cond0 /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py:874: RuntimeWarning: invalid value encountered in greater_equal return (self.a <= x) & (x <= self.b) /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py:874: RuntimeWarning: invalid value encountered in less_equal return (self.a <= x) & (x <= self.b) /Users/swapnilbhadade/venv/lib/python3.7/site-packages/numpy/core/_methods.py:26: RuntimeWarning: invalid value encountered in reduce return umr_maximum(a, axis, None, out, keepdims) /Users/swapnilbhadade/venv/lib/python3.7/site-packages/sklearn/gaussian_process/gpr.py:357: UserWarning: Predicted variances smaller than 0. Setting those variances to 0. warnings.warn("Predicted variances smaller than 0. " /Users/swapnilbhadade/venv/lib/python3.7/site-packages/bayes_opt/helpers.py:109: RuntimeWarning: invalid value encountered in true_divide z = (mean - y_max - xi)/std /Users/swapnilbhadade/venv/lib/python3.7/site-packages/sklearn/gaussian_process/gpr.py:357: UserWarning: Predicted variances smaller than 0. Setting those variances to 0. warnings.warn("Predicted variances smaller than 0. " /Users/swapnilbhadade/venv/lib/python3.7/site-packages/bayes_opt/helpers.py:109: RuntimeWarning: invalid value encountered in true_divide z = (mean - y_max - xi)/std /Users/swapnilbhadade/venv/lib/python3.7/site-packages/sklearn/gaussian_process/gpr.py:357: UserWarning: Predicted variances smaller than 0. Setting those variances to 0. warnings.warn("Predicted variances smaller than 0. " /Users/swapnilbhadade/venv/lib/python3.7/site-packages/bayes_opt/helpers.py:109: RuntimeWarning: invalid value encountered in true_divide z = (mean - y_max - xi)/std Traceback (most recent call last): File "evolution-strategy-bayesian-agent.py", line 322, in <module> NN_BAYESIAN.maximize(init_points = 30, n_iter = 50, acq = 'ei', xi = 0.0) File "/Users/swapnilbhadade/venv/lib/python3.7/site-packages/bayes_opt/bayesian_optimization.py", line 259, in maximize **self._acqkw) File "/Users/swapnilbhadade/venv/lib/python3.7/site-packages/bayes_opt/helpers.py", line 60, in acq_max method="L-BFGS-B") File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/optimize/_minimize.py", line 601, in minimize callback=callback, **options) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/optimize/lbfgsb.py", line 335, in _minimize_lbfgsb f, g = func_and_grad(x) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/optimize/lbfgsb.py", line 280, in func_and_grad f = fun(x, *args) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/scipy/optimize/optimize.py", line 300, in function_wrapper return function(*(wrapper_args + args)) File "/Users/swapnilbhadade/venv/lib/python3.7/site-packages/bayes_opt/helpers.py", line 57, in <lambda> res = minimize(lambda x: -ac(x.reshape(1, -1), gp=gp, y_max=y_max), File "/Users/swapnilbhadade/venv/lib/python3.7/site-packages/bayes_opt/helpers.py", line 97, in utility return self._ei(x, gp, y_max, self.xi) File "/Users/swapnilbhadade/venv/lib/python3.7/site-packages/bayes_opt/helpers.py", line 108, in _ei mean, std = gp.predict(x, return_std=True) File "/Users/swapnilbhadade/venv/lib/python3.7/site-packages/sklearn/gaussian_process/gpr.py", line 314, in predict X = check_array(X) File "/Users/swapnilbhadade/venv/lib/python3.7/site-packages/sklearn/utils/validation.py", line 573, in check_array allow_nan=force_all_finite == 'False') File "/Users/swapnilbhadade/venv/lib/python3.7/site-packages/sklearn/utils/validation.py", line 56, in _assert_all_finite raise ValueError(msg_err.format(type_err, X.dtype)) ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
please guide
I tried to pass the output down automatically as seen here but wound up with the error you can see in the notebook of:
ValueError: shapes (1,43) and (30,500) not aligned: 43 (dim 1) != 30 (dim 0)
I think I need to pass model parameters.
Is there a chance to investigate whether the One-Shot Reptile optimizer can deliver results comparable to Bayesian / Evolution?
I observed that the Bayesian / Evolution combo usually requires data samples in excess of a certain threshold but for some reasons, every once in a while, it performs well on a dataset with just 100 samples. Obviously, that begs the question if the Reptile K-shot approach would work well on more small datasets?
Reptile seems to be computationally fairly inexpensive, offers exponential loss decay within the first 10 fits, and when implemented in PyTorch, it can run on the GPU - a nice bonus for training on larger datasets.
Notebook:
https://github.com/AdrienLE/ANIML/blob/master/ANIML.ipynb
Paper:
https://arxiv.org/abs/1703.03400
Article:
https://towardsdatascience.com/paper-repro-deep-metalearning-using-maml-and-reptile-fd1df1cc81b0
i have hired one developer. . I have following queries one by one
What is the final command to run the app.py: is it agent.buy()
or agent.trade(data)
where data = [close(i), volume (i)]
.
If we can feed json data to agent.trade(data)
from request.ipynb then why there is line 347 app.py we have :
df = pd.read_csv('TWTR.csv') real_trend = df['Close'].tolist() parameters = [df['Close'].tolist(), df['Volume'].tolist()] minmax = MinMaxScaler(feature_range = (100, 200)).fit(np.array(parameters).T) scaled_parameters = minmax.transform(np.array(parameters).T).T.tolist() initial_money = np.max(parameters[0]) * 2
why we need to feed again this data in app.py ?
def trade():
where the if action == 1 : buy = buy_market
ORdef buy():
where the if action == 1 : buy = buy_market
Hi Husein,
I am trying to swap the data source from stock to Forex price. When I train the model, the reward keep zeros.
iter 10. reward: 0.000000
iter 20. reward: 0.000000
iter 30. reward: 0.000000
:
Then I did some experiment:
1 magnify FX close price by 10000, the training resumes normal
2. use TSLA close price divided by 1000 and the reward stuck to zero
It looks to me if the price is small, training will stuck. Do you have idea why it is?
Regards,
Steve
I got the script running by using Pipenv. Please see this readme and the Pipfile I made. The problem now is that I'm getting:
Best AGENT accuracy value: 3.244000
Is this because I am training on the GOOG.csv dataset? The Tesla file you used is not included in the repo. I'm wondering what the difference is and why I got low accuracy.
thanks
When you have time and are back to coding here, please consider trying to use more than just the close price. We should use the full OHLCV as well as 5 day MA, 50 day MA, and possibly VWAP. Currently, model is only using closing price. I will work at this as well. I have some custom indicators as well (It's a specialty of mine).
Obviously don't want too many dimensions but adding some should improve the score.
hi i find some problem, like (14.lstm-seq2seq-attention.ipynb), the output self.logits it has shape shape=(1, 6),but self.Y has the shape shape=(5, 6),are you sure this? think you ans.
Is it possible to feed the Prediction function more close prices and make continual predictions? Or does the Model need to be trained again every time new data is available?
i am stuck with the code :
i want to know we are feeding live data to the agent.trade() function with request.ipynb then why we need the same data again to be feed here in df ?
df = pd.read_csv('TWTR.csv') real_trend = df['Close'].tolist() parameters = [df['Close'].tolist(), df['Volume'].tolist()] minmax = MinMaxScaler(feature_range = (100, 200)).fit(np.array(parameters).T) scaled_parameters = minmax.transform(np.array(parameters).T).T.tolist() initial_money = np.max(parameters[0]) * 2
here we are using the same csv file right ? or we are comparing past data with live data ?
@huseinzol05 pax guide or anyone can guide on right method to feed live data .
i run the agent.ipynb to train with new data and got this error :
`Traceback (most recent call last):
File "/Users/swapnilbhadade/venv/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2890, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Close'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "res.py", line 32, in
parameters = [df['Close'].tolist(), df['Volume'].tolist()]
File "/Users/swapnilbhadade/venv/lib/python3.7/site-packages/pandas/core/frame.py", line 2975, in getitem
indexer = self.columns.get_loc(key)
File "/Users/swapnilbhadade/venv/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2892, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Close'`
My data columns looks like this : 'Date', 'Open', 'Close', 'Volume'.
@huseinzol05 plz guide
Hi,
Great work on the attention is all you need repo. However, I am confused as to learned_position_encoding()
is defined, but never used in the notebook? Other than this, the problem is that the model only works with the parameters used by you (timestamp=5,future_day=50). If I change timestamp or future day to any other value (e.g. timestamp=4), I get the error
---> 18 df_log.loc[df_log.shape[0]] = out_logits[-1, :]
IndexError: index -1 is out of bounds for axis 0 with size 0
As per my understanding, this problem is happening in all the deep-learning notebooks. Other than this, the second issue I already created _Getting Unimplemented: TensorArray has size zero error while using my data _ #48 (#48) is a bit confusing. Can you suggest or commit a work around on this please? Other than this, everything is really good and the models provide good results. Cheers.
Questions on live agent - after running this command python3 app.py
generates error there is no
model.pkl file
?
plz guide how to solve it .
As i try to run the realtime-evolution-stratgegy.py so to save model.pkl and then app.py but doesn't work the either way .
@plz guide.
Can you please provide documentation to install and run this project?
how to feed the string data from the websocket directly to the neural net reward function ?
hi, i am new to this area and i don't understand how the data used for train and test in agent/6.evolution-strategy-agent, as i know the train and test data set should be different.
But it seem that the agent train the whole data agent.fit(iterations = 500, checkpoint = 10)
and predict the action for each day in func def buy(self)
.
Is that mean the model already know the whole data and predict the data? thx :D
Hi this is really impressive and useful for me because i am new to all this. My question is how can i get the bayesian agent to predict into the future for example using past data make a forecast of where for it to buy and sell for example make it give a prediction a few days in the future. i have a python script that performs crpto trading and i would like to use predictions from this model to enhance it
Hi,
Thank you for sharing your project. I'm trying to run updated-NES-google.ipynb(converted to py) with a crypto data I attached and I'm getting this output with no rewards so no buy-sell signals. Everything works fine with yahoo stock data including stocks and other crypto currency data in yahoo but when I put any data with BTC as the opposite currency(more than one 0 digits on the right side) the below outpus happens; it should be abs/division error something but could not figure out where it is. do you have an idea why?
RuntimeWarning: invalid value encountered in true_divide
rewards = (rewards - np.mean(rewards)) / np.std(rewards)
iter 10. reward: 0.000000
iter 20. reward: 0.000000
iter 30. reward: 0.000000
iter 40. reward: 0.000000
iter 50. reward: 0.000000
iter 60. reward: 0.000000
iter 70. reward: 0.000000
iter 80. reward: 0.000000
iter 90. reward: 0.000000
iter 100. reward: 0.000000
iter 110. reward: 0.000000
iter 120. reward: 0.000000
iter 130. reward: 0.000000
iter 140. reward: 0.000000
iter 150. reward: 0.000000
iter 160. reward: 0.000000
iter 170. reward: 0.000000
iter 180. reward: 0.000000
iter 190. reward: 0.000000
iter 200. reward: 0.000000
iter 210. reward: 0.000000
iter 220. reward: 0.000000
iter 230. reward: 0.000000
iter 240. reward: 0.000000
iter 250. reward: 0.000000
iter 260. reward: 0.000000
iter 270. reward: 0.000000
iter 280. reward: 0.000000
iter 290. reward: 0.000000
iter 300. reward: 0.000000
iter 310. reward: 0.000000
iter 320. reward: 0.000000
iter 330. reward: 0.000000
iter 340. reward: 0.000000
iter 350. reward: 0.000000
iter 360. reward: 0.000000
iter 370. reward: 0.000000
iter 380. reward: 0.000000
iter 390. reward: 0.000000
iter 400. reward: 0.000000
iter 410. reward: 0.000000
iter 420. reward: 0.000000
iter 430. reward: 0.000000
iter 440. reward: 0.000000
iter 450. reward: 0.000000
iter 460. reward: 0.000000
iter 470. reward: 0.000000
iter 480. reward: 0.000000
iter 490. reward: 0.000000
iter 500. reward: 0.000000
time taken to train: 360.73433113098145 seconds
when I debug the line it warns I see that everything is 0. It is always 0...
-> rewards = (rewards - np.mean(rewards)) / np.std(rewards)
(Pdb) rewards
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
(Pdb) rewards
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
Hi Thanks for sharing your codes, they provide good examples to get started with. Could you help clarify my thinking on some of these examples. For example the stack-encoder-ensemble-xgb case seems to show rather impressive ability to predict, however the entire data set is being used for many operations. If that is correct then where is the predictive capability. Are these examples just showing a better curve fitting method ? I'm new to this field and could be way off in my initial interpretation...
batch_x = np.expand_dims( df_log.iloc[k : index, :].values, axis = 0 )
batch_y = df_log.iloc[k + 1 : index + 1, :].values
the shape of batch_y should be (1, output_size).
but in the code is (timestamp, output_size)
Bayesian optimization can quickly become computationally expensive, just increase population, network size, or both. On the other hand, a lot of agent optimization can be done within the evolutional strategy, so why not generate a population, select the "elite" agent, the one with the highest score, and take that one as a blueprint to explore the parameter set further through mutation?
Example:
The example above converges very fast to a global optimum (in about 50 generations), but more importantly, it requires a lot less computational power and thus allows super fast model re-generation.
Thanks for the sharing great research
I have been going through the code and one part that seems to be an issue is that the state variable determining action may include the the trade price itself.
action, buy = self.act(state)
...
total_buy = buy_units * close[t]
close[t] seems to be part of the data in the state used to determine action. isn't that including information not available when trading if the trade price is included in the state to determine action?
Thanks for the clarification in advance.
Does the data just need to be further fit for intraday? I haven't looked into it yet but I know it causes an error.
Maybe it's a newbie question but how do I predict the next day closing price and print the output? By "predict the next day closing price" I mean the closing price in the future, outside the training data, say I have the trading data with the last date 10 July 2019, how do I get the predicted closing price for 11 July 2019?
Any help is greatly appreciated!
Thanks
PS: Amazing work!
When I run the code against intraday data, where data is a 1-minute interval and when fit() executes 75% of the time I am seeing the same error below and rewards are showing as 0.00
RuntimeWarning: invalid value encountered in true_divide
rewards = (rewards - np.mean(rewards)) / np.std(rewards)
iter 100. reward: 0.000000
iter 200. reward: 0.000000
iter 300. reward: 0.000000
when used bayesian parameters 90% of the time same error but when used custom hard coded parameters model = Model(input_size = 32, layer_size = 500, output_size = 3) , agent =Agent(population_size = 15, sigma = 0.1, learning_rate = 0.03, model = model, money = 10000, max_buy = 100, max_sell = 100, skip = 1, window_size = 32) 75% of time same error is showing up
can you help me out to fix this issue and any suggestions?
Oh, you trained on Google trend but predict on Tesla trend. Ya, you can't. the model doesn't have a good generalization between two different trends. It need to train real time, if trained on Google, it only can predict on Google.
Maybe you can try to train more than 10 different trends, and try to predict 11th trend, let see how is good the model generalization. Never tested this theory. Problem with reinforcement learning, let say the agent trained to play DOOM shooting game. After that you put the agent on different shooting game, counter strike. Even both are shooting game, but the agent never learn how to adapt inside counter strike environment. I never saw a reinforcement learning agent able to adapt different environment to test in any research papers, correct me if I am wrong, cited with paper link.
Originally posted by @huseinzol05 in #5 (comment)
Referencing this reply you made, what we need now is to generalize to other datasets/timeframes. Here is a working version with parameters automatically passed: https://github.com/TensorTom/Stock-Prediction-Models/blob/master/agent/evolution-strategy-bayesian-agent.ipynb
Feel free to merge but still will be better if we can generalize.
Hi! first of all, congratulations for the project! I dont know if im following right the steps. But i've received the error:
Traceback (most recent call last):
File "app.py", line 359, in
minmax = minmax)
File "app.py", line 135, in init
self.model.get_weights(),
AttributeError: 'Agent' object has no attribute 'get_weights'
I made:
Thanks
the code starts to give "cannot convert float NaN to integer" errors in some pairs ( it does profit in pairs that it works). I still cannot find the exact reason for it.
@huseinzol05 Any guidance on how to create app.py for free agent like your first one evolution-strategy-bayes-opt.ipynb ? Running deep copy(model) & pickle.dump would work ?
Thank you for an amazing repo. However, when I am trying to use the Deep Learning 1 (LSTM) syntax in your repo with my own data (not related to stock markets but with a similar structure in a data frame), I get the following error:-
UnimplementedError: 2 root error(s) found.
(0) Unimplemented: TensorArray has size zero, but element shape [?,128] is not fully defined. Currently only static shapes are supported when packing zero-size TensorArrays.
[[{{node rnn/TensorArrayStack/TensorArrayGatherV3}}]]
(1) Unimplemented: TensorArray has size zero, but element shape [?,128] is not fully defined. Currently only static shapes are supported when packing zero-size TensorArrays.
[[{{node rnn/TensorArrayStack/TensorArrayGatherV3}}]]
[[dense/BiasAdd/_39]]
0 successful operations.
0 derived errors ignored.
During handling of the above exception, another exception occurred:
UnimplementedError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1368 pass
1369 message = error_interpolation.interpolate(message, self._graph)
-> 1370 raise type(e)(node_def, op, message)
1371
1372 def _extend_graph(self):
UnimplementedError: 2 root error(s) found.
(0) Unimplemented: TensorArray has size zero, but element shape [?,128] is not fully defined. Currently only static shapes are supported when packing zero-size TensorArrays.
[[node rnn/TensorArrayStack/TensorArrayGatherV3 (defined at <ipython-input-117-2e6c344df6fd>:27) ]]
(1) Unimplemented: TensorArray has size zero, but element shape [?,128] is not fully defined. Currently only static shapes are supported when packing zero-size TensorArrays.
[[node rnn/TensorArrayStack/TensorArrayGatherV3 (defined at <ipython-input-117-2e6c344df6fd>:27) ]]
[[dense/BiasAdd/_39]]
0 successful operations.
0 derived errors ignored.
The model trains fine, but after training the problem arises in the output_predict cell part of the code. Everything runs smoothly until this error occurs. If you can please clarify and suggest what may be going wrong, it would be much appreciated. I checked SO and other resources for this error but still cannot figure out why the model fails. Thanks!
What is the version of the DNC package? I try to run in Colaboratory and I get: TypeError: init() got an unexpected keyword argument 'access_config'
I tried !pip install dnc==0.0.9 but still not working.
Hi Husein,
Thanks for sharing the great works. I am newbie to AI, can understand ES in general.
When I read your code, I have few questions. Grateful if you can shed some light:
def predict(self, inputs):
feed = np.dot(inputs, self.weights[0]) + self.weights[-1]
decision = np.dot(feed, self.weights[1])
buy = np.dot(feed, self.weights[2])
return decision, buy
Best Regards,
Steve
Hi while we are at it, you have note cited the original creators of these models, that is a loss to them too. You simply collect an reapply it in a finance domain, that does not absolve you from citation. By merely changing the name of these models, you don't suddenly become the owner of them.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.