Q22 Quick Start S&P500¶
Introduction to S&P500 stocks, a quick-start example strategy that dynamically selects low-volatility stocks and allocates capital based on their transaction volume.
You can clone and edit this example there (tab Examples).
Introduction to S&P500 stocks¶
This template demonstrates how to build a trading strategy, specifically designed for use with a dataset containing historical constituents of the S&P 500. The strategy can only trade stocks that are part of the S&P 500 index, and only when the stock is considered liquid. A company is deemed liquid if it is included in the S&P 500 at the observed point in time.
The strategy takes a low-risk approach by allocating weights to stocks based on their volatility. It assigns capital only when the ratio of the Average True Range (ATR(14)) to the closing price is below 0.0205, signaling lower volatility. The allocation is further refined by the stock's transaction money flow, where stocks with higher liquidity and trading volume receive a larger portion of the capital (weight). This approach ensures that more capital is directed towards assets with higher market activity, while also maintaining a risk-aware allocation.
Important Considerations:
- Trade only on historical S&P 500 stocks, and only when they are considered liquid (i.e., part of the index at the time of observation).
- The in-sample period begins on 2006-01-01. Earlier data may be used for testing and training purposes.
- The strategy must achieve a minimum in-sample Sharpe Ratio of 0.70 to be considered valid.
- The maximum allocation to any single asset is capped at 10% of total capital. If a weight exceeds this, it will be limited to 0.1.
- Manual stock selection or direct hand-picking is not permitted. The allocation process must be automatic.
- The strategy can open both long and short positions.
For official Q22 rules, click here.
Accessing the S&P 500 Stocks Dataset - Data loading¶
For the Q22 contest, a new dataset of S&P 500 stocks is available. You can obtain this dataset by calling the function stocks_load_spx_data() from the data module:
snp_stocks_data = qndata.stocks_load_spx_data(min_date='2005-06-01')
To check the list of S&P 500 companies starting from 2006-01-01, along with additional stock information, use the load_list() function:
snp_stocks_list = qndata.stocks_load_spx_list()
Quantiacs also provides data for other instruments, such as cryptocurrencies, indices, futures, as well as additional fundamental stock data and macroeconomic data. Note that there is also a Nasdaq-100 dataset available, and it is not a subset of the S&P 500. However, there is a significant overlap between the two.
The data is provided in xarray.DataArray format. Check here for more details on manipulating xarray data.
Technical analysis - indicators
Once the data is loaded, various technical indicators from the qnt.ta module can be used to generate trading signals. For a complete list of indicators and examples of their implementation, check the documentation page.
Trading strategy - algorithm¶
# Necessary imports
import xarray as xr
import numpy as np
import qnt.stats as qnstats
import qnt.data as qndata
import qnt.output as qnout
import qnt.ta as qnta
import qnt.backtester as qnbt
import qnt.graph as qngraph
def strategy(data, state=None, mfsp=135, limit=0.0205): # parameter state is used in the backtester (multi-pass) for stateful strategies
vol = data.sel(field="vol") ### Volume values
liq = data.sel(field="is_liquid") ### Liquidity values, 1.0 or 0.0 -> True or False
close = data.sel(field="close") ### close prices
high = data.sel(field="high") ### daily high
low = data.sel(field="low") ### daily low
atrs = qnta.atr(high=high, low=low, close=close, ma=14) ### Average True Range of 14 bars (working days)
ratio = atrs / close ### indicator
weights = xr.where(ratio > limit, 0, 1) ### Strategy condition - zero weight if ratio is bigger than limit
money_vol = vol * liq * close ### Daily money flow per liquid asset
total_money_vol = money_vol.sum(dim='asset', skipna=True) ### Total daily money flow of liquid assets
money_vol_share = money_vol / total_money_vol ### Daily money flow share per liquid asset
mvs_mov = qnta.sma(money_vol_share, mfsp) ### weights allocation by average money flow share in period of 135 days
return mvs_mov * weights
Weights - Single / Multi pass approach¶
In Single pass backtesting, which is significantly faster, weights are calculated using the entire dataset in a single run.
Multi pass backtesting evaluates weights on a day-by-day basis by slicing the dataset for each individual day.
If applicable, we recommend using the single pass approach for efficiency, while verifying strategy statistics with the multi pass backtester. If the statistics from single pass and multi pass match exactly, it indicates that forward-looking bias has likely not been introduced, and the strategy can be confidently submitted as single pass.
In rare cases, even if results are identical between single and multi pass, forward-looking bias might unintentionally occur (e.g., by incorporating global data variables into the logic). Such issues are generally mitigated in production but can result in discrepancies in statistics comparing development and production results.
### SINGLE PASS ###
## The min_date parameter is set to '2005-06-01' to ensure that the dataset includes at least 135 bars (approximately 190 days) of data,
## which is necessary for creating indicators and determining weights for the strategy.
snp_stocks_data = qndata.stocks_load_spx_data(min_date='2005-06-01')
weights = strategy(snp_stocks_data)
weights = qnout.clean(weights, snp_stocks_data) # fix liquidity
0% (0 of 367973) | | Elapsed Time: 0:00:00 ETA: --:--:--
100% (367973 of 367973) |################| Elapsed Time: 0:00:00 Time: 0:00:00
0% (0 of 132449) | | Elapsed Time: 0:00:00 ETA: --:--:--
100% (132449 of 132449) |################| Elapsed Time: 0:00:00 Time: 0:00:00
0% (0 of 13038540) | | Elapsed Time: 0:00:00 ETA: --:--:--
44% (5867325 of 13038540) |##### | Elapsed Time: 0:00:00 ETA: 00:00:00
78% (10300415 of 13038540) |######### | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038540 of 13038540) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 1/23 1s
0% (0 of 13038540) | | Elapsed Time: 0:00:00 ETA: --:--:--
22% (2998855 of 13038540) |## | Elapsed Time: 0:00:00 ETA: 00:00:00
43% (5736940 of 13038540) |##### | Elapsed Time: 0:00:00 ETA: 0:00:00
65% (8605410 of 13038540) |######## | Elapsed Time: 0:00:00 ETA: 0:00:00
87% (11473880 of 13038540) |########## | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038540 of 13038540) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 2/23 2s
0% (0 of 13038540) | | Elapsed Time: 0:00:00 ETA: --:--:--
22% (2998855 of 13038540) |## | Elapsed Time: 0:00:00 ETA: 00:00:00
44% (5867325 of 13038540) |##### | Elapsed Time: 0:00:00 ETA: 0:00:00
67% (8866180 of 13038540) |######## | Elapsed Time: 0:00:00 ETA: 0:00:00
89% (11734650 of 13038540) |########## | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038540 of 13038540) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 3/23 3s
0% (0 of 13038536) | | Elapsed Time: 0:00:00 ETA: --:--:--
22% (2998855 of 13038536) |## | Elapsed Time: 0:00:00 ETA: 00:00:00
44% (5867325 of 13038536) |##### | Elapsed Time: 0:00:00 ETA: 0:00:00
67% (8866180 of 13038536) |######## | Elapsed Time: 0:00:00 ETA: 0:00:00
89% (11734650 of 13038536) |########## | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038536 of 13038536) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 4/23 5s
0% (0 of 13038540) | | Elapsed Time: 0:00:00 ETA: --:--:--
32% (4302705 of 13038540) |#### | Elapsed Time: 0:00:00 ETA: 00:00:00
54% (7171175 of 13038540) |####### | Elapsed Time: 0:00:00 ETA: 0:00:00
75% (9909260 of 13038540) |######### | Elapsed Time: 0:00:00 ETA: 0:00:00
96% (12647345 of 13038540) |########### | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038540 of 13038540) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 5/23 6s
0% (0 of 13038540) | | Elapsed Time: 0:00:00 ETA: --:--:--
22% (2998855 of 13038540) |## | Elapsed Time: 0:00:00 ETA: 00:00:00
44% (5867325 of 13038540) |##### | Elapsed Time: 0:00:00 ETA: 0:00:00
67% (8866180 of 13038540) |######## | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038540 of 13038540) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 6/23 7s
0% (0 of 13038536) | | Elapsed Time: 0:00:00 ETA: --:--:--
29% (3911550 of 13038536) |### | Elapsed Time: 0:00:00 ETA: 00:00:00
73% (9648490 of 13038536) |######### | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038536 of 13038536) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 7/23 8s
0% (0 of 13038540) | | Elapsed Time: 0:00:00 ETA: --:--:--
29% (3911550 of 13038540) |### | Elapsed Time: 0:00:00 ETA: 00:00:00
73% (9648490 of 13038540) |######### | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038540 of 13038540) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 8/23 9s
0% (0 of 13038512) | | Elapsed Time: 0:00:00 ETA: --:--:--
22% (2998855 of 13038512) |## | Elapsed Time: 0:00:00 ETA: 00:00:00
55% (7301560 of 13038512) |####### | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038512 of 13038512) |############| Elapsed Time: 0:00:00 ETA: 00:00:00
100% (13038512 of 13038512) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 9/23 10s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
44% (5867280 of 13038436) |##### | Elapsed Time: 0:00:00 ETA: 00:00:00
88% (11604176 of 13038436) |########## | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 10/23 11s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 11/23 12s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 12/23 14s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 13/23 16s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
16% (2216528 of 13038436) |## | Elapsed Time: 0:00:00 ETA: 00:00:00
39% (5215360 of 13038436) |##### | Elapsed Time: 0:00:00 ETA: 0:00:00
62% (8214192 of 13038436) |######## | Elapsed Time: 0:00:00 ETA: 0:00:00
82% (10821872 of 13038436) |######### | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 14/23 17s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
22% (2998832 of 13038436) |## | Elapsed Time: 0:00:00 ETA: 00:00:00
44% (5867280 of 13038436) |##### | Elapsed Time: 0:00:00 ETA: 0:00:00
67% (8866112 of 13038436) |######## | Elapsed Time: 0:00:00 ETA: 0:00:00
86% (11343408 of 13038436) |########## | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 15/23 18s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
22% (2998832 of 13038436) |## | Elapsed Time: 0:00:00 ETA: 00:00:00
59% (7823040 of 13038436) |####### | Elapsed Time: 0:00:00 ETA: 0:00:00
93% (12256096 of 13038436) |########### | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 16/23 19s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
22% (2998832 of 13038436) |## | Elapsed Time: 0:00:00 ETA: 00:00:00
44% (5867280 of 13038436) |##### | Elapsed Time: 0:00:00 ETA: 0:00:00
64% (8474960 of 13038436) |######## | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 17/23 20s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
44% (5867280 of 13038436) |##### | Elapsed Time: 0:00:00 ETA: 00:00:00
82% (10821872 of 13038436) |######### | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 18/23 21s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
44% (5867280 of 13038436) |##### | Elapsed Time: 0:00:00 ETA: 00:00:00
87% (11473792 of 13038436) |########## | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 19/23 22s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
44% (5867280 of 13038436) |##### | Elapsed Time: 0:00:00 ETA: 00:00:00
88% (11604176 of 13038436) |########## | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 20/23 23s
0% (0 of 13038436) | | Elapsed Time: 0:00:00 ETA: --:--:--
44% (5867280 of 13038436) |##### | Elapsed Time: 0:00:00 ETA: 00:00:00
88% (11604176 of 13038436) |########## | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038436 of 13038436) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 21/23 24s
0% (0 of 13038512) | | Elapsed Time: 0:00:00 ETA: --:--:--
37% (4954630 of 13038512) |#### | Elapsed Time: 0:00:00 ETA: 00:00:00
81% (10691570 of 13038512) |######### | Elapsed Time: 0:00:00 ETA: 0:00:00
100% (13038512 of 13038512) |############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 22/23 25s
0% (0 of 7215332) | | Elapsed Time: 0:00:00 ETA: --:--:--
58% (4257027 of 7215332) |######## | Elapsed Time: 0:00:00 ETA: 00:00:00
100% (7215332 of 7215332) |##############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 23/23 26s
Data loaded 27s
Output cleaning...
fix uniq
ffill if the current price is None...
Check liquidity...
WARNING! Strategy trades non-liquid assets.
Fix liquidity...
Ok.
Check missed dates...
Ok.
Normalization...
Output cleaning is complete.
When using the backtester for weight calculation, there is no need to manually load data or define a "load_data" function as input. It is only required to specify the competition_type (in this case, "stocks_s&p500"), and the corresponding data will be loaded automatically by default.
### MULTI PASS ###
w=qnbt.backtest(
competition_type="stocks_s&p500",
lookback_period=250,
start_date="2006-01-01",
strategy=strategy,
analyze=True,
)
Run last pass...
Load data...
0% (0 of 132449) | | Elapsed Time: 0:00:00 ETA: --:--:--
100% (132449 of 132449) |################| Elapsed Time: 0:00:00 Time: 0:00:00
0% (0 of 9446260) | | Elapsed Time: 0:00:00 ETA: --:--:--
60% (5762182 of 9446260) |######## | Elapsed Time: 0:00:00 ETA: 00:00:00
100% (9446260 of 9446260) |##############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 1/1 9s
Data loaded 9s
Run strategy...
Can't load state. [Errno 2] No such file or directory: '/root/state.in.pickle.gz'
Load data for cleanup...
0% (0 of 2315500) | | Elapsed Time: 0:00:00 ETA: --:--:--
100% (2315500 of 2315500) |##############| Elapsed Time: 0:00:00 Time: 0:00:00
fetched chunk 1/1 8s
Data loaded 8s
Output cleaning...
fix uniq
ffill if the current price is None...
Check liquidity...
Ok.
Check missed dates...
Ok.
Normalization...
Output cleaning is complete.
Write result...
Write output: /root/fractions.nc.gz
Statistics¶
While the multi pass approach automatically displays the strategy’s statistics through the backtester, single pass mode only calculates the weights, so visualizing the stats must be done manually. You can use the helper print_stats function for this purpose:
def print_stats(stat):
display(stat.sel(time=slice('2006-01-01', None)).to_pandas().tail(10))
performance = stat.to_pandas()["equity"]
qngraph.make_plot_filled(performance.index, performance, name="PnL (Equity)", type="log")
def get_sharpe(stat):
return stat.isel(time=-1).sel(field="sharpe_ratio").item()
stat = qnstats.calc_stat(snp_stocks_data, weights.sel(time=slice('2006-01-01', None)))
print_stats(stat)
| field | equity | relative_return | volatility | underwater | max_drawdown | sharpe_ratio | mean_return | bias | instruments | avg_turnover | avg_holding_time |
|---|---|---|---|---|---|---|---|---|---|---|---|
| time | |||||||||||
| 2026-08-26 | 2.149995 | -0.000072 | 0.045497 | -0.003137 | -0.072077 | 0.831630 | 0.037837 | 1.0 | 702.0 | 0.028365 | 27.202908 |
| 2026-08-27 | 2.147958 | -0.000947 | 0.045493 | -0.004081 | -0.072077 | 0.830487 | 0.037782 | 1.0 | 702.0 | 0.028363 | 27.202504 |
| 2026-08-28 | 2.148509 | 0.000256 | 0.045489 | -0.003825 | -0.072077 | 0.830687 | 0.037787 | 1.0 | 702.0 | 0.028359 | 27.200766 |
| 2026-08-31 | 2.147499 | -0.000470 | 0.045485 | -0.004294 | -0.072077 | 0.830080 | 0.037756 | 1.0 | 702.0 | 0.028356 | 27.200202 |
| 2026-09-01 | 2.146955 | -0.000253 | 0.045481 | -0.004546 | -0.072077 | 0.829716 | 0.037736 | 1.0 | 703.0 | 0.028352 | 27.198809 |
| 2026-09-02 | 2.148085 | 0.000526 | 0.045476 | -0.004022 | -0.072077 | 0.830213 | 0.037755 | 1.0 | 703.0 | 0.028348 | 27.196801 |
| 2026-09-03 | 2.149260 | 0.000547 | 0.045472 | -0.003477 | -0.072077 | 0.830734 | 0.037775 | 1.0 | 703.0 | 0.028345 | 27.194856 |
| 2026-09-04 | 2.147550 | -0.000796 | 0.045468 | -0.004270 | -0.072077 | 0.829762 | 0.037728 | 1.0 | 703.0 | 0.028342 | 27.193235 |
| 2026-09-08 | 2.146184 | -0.000636 | 0.045464 | -0.004904 | -0.072077 | 0.828970 | 0.037688 | 1.0 | 703.0 | 0.028338 | 27.191333 |
| 2026-09-09 | 2.145219 | -0.000449 | 0.045460 | -0.005351 | -0.072077 | 0.828387 | 0.037658 | 1.0 | 703.0 | 0.028335 | 27.198981 |
get_sharpe(stat)
0.8283868779053178
Submit strategy to the competition¶
Use Submit button on my strategies page
Make sure that qnout.write(weights) has been added to cell, and the weights have been written. It is not required when using Multi pass backtester.
Note: After submitting the strategy to the contest, any weight exceeding 0.1 will be capped at that limit. You can apply weight normalization functions before submission, which may be more suitable, such as those that maintain the ratio between allocated weights. Details on these functions can be found here.
### Run this cell to get all weights below 0.1. This "hard cut" way is used on production if any weight exceeds 0.1.
### Any other normalization method can be used.
import qnt.exposure as qnexp
weights_capped = qnexp.cut_big_positions(weights=weights, max_weight=0.1)
Compare weights before and after normalization. Check the statistics again, it can change not only in negative way.
qnout.write(weights_capped)
Write output: /root/fractions.nc.gz
Common Reasons for Submission Rejection and Their Solutions¶
Here are some of the frequent reasons causing submission rejection in algorithmic trading competitions, and their corresponding remedies.
1) Missed call to write_output
Save algorithm weights, run code
qnt.output.write(weights)
2) Not eligible send to contest. In-Sample Sharpe must be larger than 0.7
Improve your algorithm. For example, you can use sections and get an algorithm that will pass the filter
- Example Trading System Optimization
- Example of a strategy using technical analysis indicators
Need help? Check the Documentation and find solutions/report problems in the Forum section.
3) Not enough bid information.
Run code
min_time = weights.time[abs(weights).fillna(0).sum('asset')> 0].min()
min_time
min_time must be less than or equal to January 1, 2006.
If min_time is larger than the starting date, we recommend to fill the starting values of the time series with non-vanishing values, for example a simple buy-and-hold strategy.
def get_enough_bid_for(data_, weights_):
time_traded = weights_.time[abs(weights_).fillna(0).sum('asset') > 0]
is_strategy_traded = len(time_traded)
if is_strategy_traded:
return xr.where(weights_.time < time_traded.min(), data_.sel(field="is_liquid"), weights_)
return weights_
weights_new = get_enough_bid_for(data, weights)
weights_new = weights_new.sel(time=slice("2006-01-01",None))