I'm a huge fan of Sentdex, he really tought me a lot about Python in his tutorials.
Have a look at his website and his Youtube channel, for instance there's a tutorial for Python beginners.
Posts made by antinomy
-
RE: Python
-
RE: Local Development with Notifications
It's safe to ignore these notices but if they bother you, you can set the variables together with your API key using the defaults and the messages go away:
import os os.environ['API_KEY'] = 'YOUR-API-KEY' os.environ['DATA_BASE_URL'] = 'https://data-api.quantiacs.io/' os.environ['CACHE_RETENTION'] = '7' os.environ['CACHE_DIR'] = 'data-cache'
-
Fundamental Data
Hello @support
Could you please add CIKs to the NASDAQ100 stock list?
In order to load fundamental data from secgov we need the CIKs for the stocks but they're currently not in the list we get from qnt.data.stocks_load_ndx_list().
Allthough it is still possible to get fundamentals using qnt.data.stocks_load_list(), it takes a little bit acrobatics like this for instance:import pandas as pd import qnt.data as qndata stocks = qndata.stocks_load_ndx_data() df_ndx = pd.DataFrame(qndata.stocks_load_ndx_list()).set_index('symbol') df_all = pd.DataFrame(qndata.stocks_load_list()).set_index('symbol') idx = sorted(set(df_ndx.index) & set(df_all.index)) df = df_ndx.loc[idx] df['cik'] = df_all.cik[idx] symbols = list(df.reset_index().T.to_dict().values()) fundamentals = qndata.secgov_load_indicators(symbols, stocks.time)
It would be nice if we could get them with just 2 lines like so:
stocks = qndata.stocks_load_ndx_data() fundamentals = qndata.secgov_load_indicators(qndata.stocks_load_ndx_list(), stocks.time)
Also, the workaround doesn't work locally because qndata.stocks_load_list() seems to return the same list as qndata.stocks_load_ndx_list().
Thanks in advance!
-
RE: Local Development Error "No module named 'qnt'"
@eddiee Try step 4 without quotes, this should start jupyter notebook. And if that's your real API-key we see in the image, delete your last post. It's a bad idea to post it in a public forum
-
RE: Q17 Neural Networks Algo Template; is there an error in train_model()?
Yes, I noticed that too. And after fixing it the backtest takes forever...
Another thing to consider is that it redefines the model with each training but I belive you can retrain already trainded NNs with new Data so they learn based on what they previously learned. -
RE: Weights different in testing and submission
About the slicing error, I had that too a while ago. It took me some time to figure out that it wasn't enough to have the right pandas version in the environment. Because I had another python install with the same version in my PATH, the qntdev-python also looked there and always used the newer pandas. So I placed the -s flag everywhere the qntdev python is supposed to run (PyCharm, Jupyter, terminal) like this
/path/to/quantiacs/python -s strategy.py
Of course one could simply remove the other python install from PATH but I needed it there.
-
RE: Saving and recalling a dictionary of trained models
@alfredaita
In case you don't want to run init.py every time in order to install external libraries, I came up with a solution for this. You basically install the library in a folder in your home directory and let the strategy create symlinks to the module path at runtime. More details in this post. -
Erroneous Data?
Hello @support
Since the live period for the Q16 contest is coming to an end I'm watching my participating algorithms more closely and noticed something odd:
The closing prices are the same on 2022-02-24 and 2022-02-25 to the last decimal for allmost all cryptos (49 out of 54).import qnt.data as qndata crypto = qndata.cryptodaily.load_data(tail=10) c = crypto.sel(field='close').to_pandas().iloc[-3:] liquid = crypto.sel(field='is_liquid').fillna(0).values.astype(bool)[-3:] # only showing the cryptos which were liquid for the last 3 days: c.iloc[:, liquid.all(axis=0)] asset ADA AVAX BNB BTC DOGE DOT ETH LINK SOL XRP time 2022-02-23 0.8664 73.47 365.6 37264.053 0.1274 15.97 2580.9977 13.34 84.64 0.696515 2022-02-24 0.8533 76.39 361.2 38348.744 0.1242 16.16 2598.0195 13.27 89.41 0.696359 2022-02-25 0.8533 76.39 361.2 38348.744 0.1242 16.16 2598.0195 13.27 89.41 0.696359
(c.values[-1] == c.values[-2]).sum(), c.shape[1] (49, 54)
Could you please have a look?
Thanks! -
RE: External Libraries
@support
Yes, pip is way faster. Thanks!
I might have found an even faster solution but I guess I have to wait a few hours to find out if it really works.Here's what I did:
- I created a folder in /root/books called "modules" to install cvxpy there to make it persistent:
!mkdir modules && pip install --target=modules cvxpy
- Then if the import fails in the strategy, it creates symbolic links in /usr/local/lib/python3.7/site-packages/ that point to the content of /root/books/modules/
try: import cvxpy as cp except ImportError: import os source = '/root/book/modules/' target = '/usr/local/lib/python3.7/site-packages/' for dirpath, dirnames, filenames in os.walk(source): source_path = dirpath.replace(source, '') target_path = os.path.join(target, source_path) if not os.path.exists(target_path) and not os.path.islink(target_path): os.symlink(dirpath, target_path) continue for file in filenames: source_file = os.path.join(dirpath, file) target_file = os.path.join(target, source_path, file) if not os.path.exists(target_file) and not os.path.islink(target_file): os.symlink(source_file, target_file) import cvxpy as cp
Creating the symlinks only takes 0.07 seconds, so fingers crossed
UPDATE (a few hours later):
It actually worked. When I just reopened the strategy, the environment was newly initialized. First I tried just importing cvxpy and got the ModuleNotFoundError. Then I ran the strategy including the code above: cvxpy was imported correctly and the strategy ran.I'm not sure if that solution works for every module because I don't know if pip might also write something to other directories than site-packages.
Anyway, I'm happy with this solution.
Regards -
RE: External Libraries
@support
It's actually the same strategy / environment, not a new one.
If I haven't used it for a while (say, a few hours or a day) and open it again by clicking on the Jupyter button, it says:
Initialization of the virtual environment. The notebook will be ready in 15 seconds.
And when I try to run the strategy that worked fine a few hours or a day ago, I get the ModuleNotFoundError and have to install the module again.
Everything else is still there as it was before - the strategy, custom files - just not cvxpy. -
External Libraries
Hello @support ,
I've been using cvxpy in the server environment which I installed by running
!conda install -y -c conda-forge cvxpy
in init.ipynb. But whenever this environment is newly initialized, the module is gone and I have to run this cell again (which takes awfully long).
Is this normal or is there something wrong with my environment?
My current workaround is placing these lines before the importtry: import cvxpy as cp except ImportError: import subprocess cmd = 'conda install -y -c conda-forge cvxpy'.split() rn = subprocess.run(cmd) import cvxpy as cp
Is there a better way?
Best regards.
-
Issues with the Legacy Website
Hello,
I'm having 2 issues with legacy.quantiacs.com:
- Trouble accessing the site
Since a few days ago Firefox won't open the legacy website, shwoing this message:
- Accessing the website with Chromium by adding an exception I found that the strategy charts haven't been updated since 2021-10-26.
Could you please take a look?
Thanks! - Trouble accessing the site
-
RE: Output the results in an excel or other format file
@anshul96go
To get the actual statistics you currently have to calculate them like so:import qnt.stats as qns data = qndata.cryptodaily_load_data(min_date="2014-01-01") # or whenever your backtest started stats = qns.calc_stat(data, weights)
And if you really need them as xls file you can do:
stats.to_pandas().to_excel('stats.xls') # I got a ModuleNotFoundError the first time - pip install did the trick.
Allthough I can't recommend xls because at least LibreOffice becomes very slow / unresponsive when handling such a file.
Getting the statistics after a backtest could be a little simpler, which brings me to a feature request:
@support
Do you think you could add a parameter to the backtester which makes it return the statistics? They get calculated anyway by default, but we only see a truncated printout or the plots and can't use them for further analysis.
.
In my local environment I did it like this in qnt.backtester.py:- Add the parameter return_stats: bool = False to the parameters of the backtest function
- From line 353 onward my backtester now looks like this:
qnout.write(result) qnstate.write(state) if return_stats: analyze = True out = [result] if analyze: log_info("---") stats = analyze_results(result, data, competition_type, build_plots, start_date) if return_stats: out.append(stats) if args_count > 1: out.append(state) if len(out) == 1: out = out[0] return out finally: qndc.set_max_datetime(None)
- And of course I made analyze_results return the statistics like so (line 458 in the original):
if not build_plots: log_info(stat_global.to_pandas().tail()) return stat_global # here log_info("---") log_info("Calc stats per asset...") stat_per_asset = qnstat.calc_stat(data, output, per_asset=True) stat_per_asset = stat_per_asset.loc[output.time.values[0]:] if is_notebook(): build_plots_jupyter(output, stat_global, stat_per_asset) else: build_plots_dash(output, stat_global, stat_per_asset) return stat_global # and there
This might not be the most elegant solution but you get the idea.
Now I can get the statistics immediately after the backtest withweights, stats = backtest(...return_stats=True)
and can do further analysis.
For instance, I started to calculate the correlations between my strategies to avoid uploading more of the same to the contest.It would be nice to have this feature in a future version, so I don't have to mess with the backtester after each update
Best regards
-
Correlation Check always fails
Hello,
whenever I run a backtest on the server I get the messageWARNING! Can't calculate correlation.
This has been happening since I started developing for the Q16 contest.
I don't know if this has any influence on the actual submission check and we'll soon have x times the quickstart template in the contest
Anyway, it would be good if the correlation check would work before we submitt algos that will eventually fail the correlation filter. -
RE: The Quantiacs Referral Program
@news-quantiacs
Hello,
about that link, the important part is the one that starts with the question mark, with utm_medium being our unique identifier, right?
So, can we change the link to point to the contest description instead of the login-page, like this?
https://quantiacs.com/contest?utm_source=reference&utm_medium=19014
Then interested people could first read more details about the contest before signing up... -
RE: The Q16 Contest is open!
@support In my posts I was merely thinking about unintentional lookahead bias because when it comes to the intentional kind, there are lots of ways to do that and I believe you never can make all of them impossible.
But I think that's what the rules are for and the live test is also a good measure to call out intentional or unintentional lookahead bias as well as simple innocent overfitting.To clarify the Quantopian example a bit, I don't think what I described was meant to prevent lookahead bias. The 8000 something symbols just was all what they had and the rules for the tradable universe were publicly available (QTradableStocksUS on archive.org). I just thought, providing data for a larger set than what's actually tradable would make the scenarios I mentioned less likely. For that purpose I think both sets could also be openly defined. Let's say the larger one has the top 100 symbols in terms of market cap, dollar volume or whatever and the tradable ones could be the top 10 out of them with the same measurement.
On the other hand, I still don't know if those scenarios could become a real problem. Because what good does this foreknowledge if you can't trade them yet? And after they're in the top 10 it would be legitimate to use the fact that they just entered, because we would also have known this at that time in real life.
-
RE: The Q16 Contest is open!
@support
I totally agree that the indicator usage is not trivial at all regarding lookahead-bias, still trying to wrap my head around it
The symbol list alone could already lead to lookahead bias - in theory, I don't have a realistic example.
Because if the symbol is in the dataset, the algo could know it will be among the top 10 at some point, thus probably go up in price.
I guess we really need to be extra careful avoiding these pitfalls, but they might also become apparent after the submission...From what I understand this contest is kind of a trial run for the stocks contest, so may I make a suggestion?
On Quantopian there was data for around 8000 assets, including non-stocks like ETFs but for the daily contest for instance, the symbols had to be in the subset of liquid stocks they defined (around 2000 I think).The scenarios
- there's a price for that stock, it will become large
- it's included in the asset list, it must go up some day
were not really a problem because there was no way to infer from prices or the symbol being present that it will be included in the filter some day.
Maybe you could do something like that, too?
It doesn't have to be those kind of numbers, but simply providing data for a larger set of assets containing symbols which will never be included in the filter could avoid this problem (plus of course including delisted stocks).For this contest I think your suggestion to retroactively fill the data if a symbol makes it on top again is a good idea.
-
RE: The Q16 Contest is open!
@support
First of all, having looked at various sources for crypto data myself, I know this can be a pain in the neck, so I can appreciate the effort you took to provide it.I get the method for avoiding lookahead-bias by including delisted symbols. The key point would be what you mean exactly by disappeared and from where.
Do you mean they were delisted from the exchange where the winning algos will be traded or did the data source you used just not have data?To name 2 examples: DASH and XMR don't have recent prices but I don't know of an exchange they were delisted from. When I look them up on tradingview they do have prices on all the exchanges available there and are still traded with normal volumes.
Charts for their closing price on quantiacs:
import qnt.data as qndata data = qndata.cryptodaily_load_data(min_date='2020') close = data.sel(field='close').to_pandas() close[['DASH', 'XMR']].plot()
On tradingview:
There are many reasons why we might need prices for symbols that are currently not among the top 10 in terms of market cap. An obvious one would be that they might be included in the filter again any second and algorithms do need historical data. Also, there are many ways to include symbols in computations without trading them: as indicators, to calculate market averages and so on.