yhilpisch / aiif Goto Github PK
View Code? Open in Web Editor NEWJupyter Notebooks and code for the book Artificial Intelligence in Finance (O'Reilly) by Yves Hilpisch.
Home Page: http://home.tpq.io/books/aiif
License: Other
Jupyter Notebooks and code for the book Artificial Intelligence in Finance (O'Reilly) by Yves Hilpisch.
Home Page: http://home.tpq.io/books/aiif
License: Other
Hi, first of all, thanks for writing the book. It is amazing.
When running the notebook 11_risk_management.ipynb
the step
tradingbot.plot_performance(agent)
it is failing with the following error:
---------------------------------------------------------------------------
LinAlgError Traceback (most recent call last)
<ipython-input-13-80d9132e183d> in <module>
----> 1 tradingbot.plot_performance(agent)
2 # plt.savefig('../../images/ch11/figure_rm_02.png');
D:\trade-projects\aiif\code\tradingbot.py in plot_performance(agent)
180 plt.figure(figsize=(10, 6))
181 x = range(1, len(agent.performances) + 1)
--> 182 y = np.polyval(np.polyfit(x, agent.performances, deg=3), x)
183 plt.plot(x, agent.performances[:], label='training')
184 plt.plot(x, y, 'r--', label='regression (train)')
<__array_function__ internals> in polyfit(*args, **kwargs)
D:\anaconda3\envs\aiif\lib\site-packages\numpy\lib\polynomial.py in polyfit(x, y, deg, rcond, full, w, cov)
629 scale = NX.sqrt((lhs*lhs).sum(axis=0))
630 lhs /= scale
--> 631 c, resids, rank, s = lstsq(lhs, rhs, rcond)
632 c = (c.T/scale).T # broadcast scale coefficients
633
<__array_function__ internals> in lstsq(*args, **kwargs)
D:\anaconda3\envs\aiif\lib\site-packages\numpy\linalg\linalg.py in lstsq(a, b, rcond)
2257 # lapack can't handle n_rhs = 0 - so allocate the array one larger in that axis
2258 b = zeros(b.shape[:-2] + (m, n_rhs + 1), dtype=b.dtype)
-> 2259 x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj)
2260 if m == 0:
2261 x[...] = 0
D:\anaconda3\envs\aiif\lib\site-packages\numpy\linalg\linalg.py in _raise_linalgerror_lstsq(err, flag)
107
108 def _raise_linalgerror_lstsq(err, flag):
--> 109 raise LinAlgError("SVD did not converge in Linear Least Squares")
110
111 def get_linalg_error_extobj(callback):
LinAlgError: SVD did not converge in Linear Least Squares
Seems like there are inf or nan values in the data.
Could we add dropna(inplace=True)
?
KeyError Traceback (most recent call last)
in
/content/oandaenv.py in init(self, symbol, start, end, granularity, price, features, window, lags, leverage, min_accuracy, min_performance, mu, std)
35 self.granularity = granularity
36 self.price = price
---> 37 self.api = tpqoa.tpqoa('aiif.cfg')
38 self.features = features
39 self.n_features = len(features)
1 frames
/usr/lib/python3.9/configparser.py in getitem(self, key)
961 def getitem(self, key):
962 if key != self.default_section and not self.has_section(key):
--> 963 raise KeyError(key)
964 return self._proxies[key]
965
KeyError: 'oanda'
I am getting this error when i run this code
%%time
learn_env = oe.OandaEnv(symbol=symbol,
start=f'{date} 08:00:00',
end=f'{date} 13:00:00',
granularity='S30',
price='M',
features=features,
window=20,
lags=3,
leverage=20,
min_accuracy=0.4,
min_performance=0.85
)
For the aiif.cfg this the structure and my credentials are correct as i had used it to run the code that was before this and was able to get historical data and make orders.
[oanda]
account_id = 101-001-25418976-001
access_token = 55c13668f2efac009674500a66b87a83-0aeb2e9467fa8b4d0bca1c29815cb911
account_type = practice
In the notebook 10_vectorized_backtesting.ipynb
, the following step should be
In [55]:
test = data.loc[split:].copy()
In [55]:
test = data.iloc[split:].copy()
Hello,
I'm facing an issue when i'm running oandatb.py because the below error although i saw that inside tradingbot.py eager mode is disabled i'm using the same version TF 2.3:
Traceback (most recent call last):
File "oandatb.py", line 118, in
otb.stream_data(agent.learn_env.symbol)
File "/home/ramy/aiif-main/code/aiif/lib/python3.8/site-packages/tpqoa/tpqoa.py", line 285, in stream_data
self.on_success(msg.time,
File "oandatb.py", line 90, in on_success
prediction = np.argmax(self.agent.model.predict(state)[0, 0])
File "/home/ramy/aiif-main/code/aiif/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1577, in predict
version_utils.disallow_legacy_graph('Model', 'predict')
File "/home/ramy/aiif-main/code/aiif/lib/python3.8/site-packages/tensorflow/python/keras/utils/version_utils.py", line 122, in disallow_legacy_graph
raise ValueError(error_msg)
ValueError: Calling Model.predict
in graph mode is not supported when the Model
instance was constructed with eager mode enabled. Please construct your Model
instance in graph mode or call Model.predict
with eager mode enabled.
Sir,
Here is the piece of code taken from the backtest_strategy method:
state = self.env.get_state(bar)
action = np.argmax(self.model.predict(
self._reshape(state.values))[0, 0])
position = 1 if action == 1 else -1
if self.position in [0, -1] and position == 1:
if self.verbose:
print(50 * '-')
print(f'{date} | *** GOING LONG ***')
if self.position == -1:
self.place_buy_order(bar - 1, units=-self.units)
self.place_buy_order(bar - 1,
amount=self.current_balance)
if self.verbose:
self.print_net_wealth(bar)
self.position = 1
elif self.position in [0, 1] and position == -1:
if self.verbose:
print(50 * '-')
print(f'{date} | *** GOING SHORT ***')
if self.position == 1:
self.place_sell_order(bar - 1, units=self.units)
self.place_sell_order(bar - 1,
amount=self.current_balance)
if self.verbose:
self.print_net_wealth(bar)
self.position = -1
In this code the buy and sell order is being called with "bar-1". Should it be "bar+1" instead since the action was predicted using current "bar". If not, can you please clear my misunderstanding?
Thanks,
Debasish
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.