0% found this document useful (0 votes)
19 views11 pages

Backtesting Trading Strategies Challenge

AlgoXpert is seeking Developer Interns to create a backtesting engine for three trading strategies using the SPY ETF dataset from Yahoo Finance. Candidates must implement Moving Average Crossover, Mean Reversion, and Breakout strategies, optimizing parameters and comparing results while adhering to specific input and output formats. The submission includes source code, an input file, an AI usage essay, and a strategy report detailing performance and trade-offs.

Uploaded by

nam.leo1805
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

Backtesting Trading Strategies Challenge

AlgoXpert is seeking Developer Interns to create a backtesting engine for three trading strategies using the SPY ETF dataset from Yahoo Finance. Candidates must implement Moving Average Crossover, Mean Reversion, and Breakout strategies, optimizing parameters and comparing results while adhering to specific input and output formats. The submission includes source code, an input file, an AI usage essay, and a strategy report detailing performance and trade-offs.

Uploaded by

nam.leo1805
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Part A – Coding Challenge

Multi-Strategy Backtest & Optimization (MA, Mean


Reversion, Breakout)

1. Background
AlgoXpert is hiring Developer Interns (C++/Python) who can:

● Work with large time-series datasets


● Design reusable, well-structured code
● Optimize performance under constraints
● Compare trading strategies and reason about trade-offs

Your task is to build a simple backtesting engine for three trading strategies on one
instrument, and then compare their results.

You may use any AI or search tools, but the final code, parameters, and reports must
reflect your own understanding and decisions.

2. Official Dataset (Same for All Candidates, via yfinance)


To ensure fairness, all candidates must use the same dataset:

● Source: Yahoo Finance data, downloaded via the Python library yfinance
● Symbol: [translate:SPY] (SPDR S&P 500 ETF)
● Interval: Daily
● Date range: from 2010-01-01 to 2024-12-31 (inclusive)
Recommended way to obtain the data:

import yfinance as yf

data = [Link](
ticker="SPY",
start="2010-01-01",
end="2024-12-31",
interval="1d"
)
close = data["Close"].dropna() # daily closing prices
prices = [Link]()
N = len(prices)

● Use the Close column as your price series price_i, in chronological order
(oldest to newest).
● Let N be the number of data points in this series; you will use this N in your
input.

You may wrap this into your own script to generate an input file (example in Section
8).
3. Data & Input Format
Your backtest program must read from standard input (stdin) with the following
format:

N
C0
S_short S_long
MR_window MR_threshold
BO_window BO_threshold
price_1
price_2
...
price_N

Where:

● N – number of data points in your time series (daily [translate:SPY] Close from
2010-01-01 to 2024-12-31)
● C0 – initial capital (integer, 1 ≤ C0 ≤ 10^9)

Strategy Parameters (chosen and optimized by you)

1. Moving Average (MA) Crossover


● S_short – window size of the short moving average
● S_long – window size of the long moving average (with S_long ≥
S_short)

2. Mean Reversion (MR) around MA


● MR_window – window size of the moving average used as “mean”
● MR_threshold – percentage deviation threshold (e.g., 0.02 = 2%)
3. Breakout (BO)
● BO_window – lookback window size in data points
● BO_threshold – breakout percentage threshold (e.g., 0.01 = 1%)

Then follow N lines:


● price_i – closing price at data point i (real number > 0), taken from the
[translate:SPY] Close series.

4. Strategy Definitions

4.1 Strategy 1 – Moving Average Crossover


Using S_short and S_long:

● Compute SMA_short(i) and SMA_long(i) at data point i (only when you have
enough history).
● Buy with full capital when SMA_short crosses above SMA_long and you
currently hold no position.
● Sell all when SMA_short crosses below SMA_long and you currently hold a
position.
● No transaction costs, no margin.
● At most one action per data point (either buy or sell).

4.2 Strategy 2 – Mean Reversion around Moving Average


Using MR_window and MR_threshold:

● Compute MA_MR(i) = moving average of the last MR_window prices.


● If you do not hold any shares and
price_i ≤ MA_MR(i) × (1 − MR_threshold)

→ Buy with full capital.


● If you hold shares and
price_i ≥ MA_MR(i)

→ Sell all.

4.3 Strategy 3 – Breakout


Using BO_window and BO_threshold:
● Let max_high(i) be the maximum closing price over the previous BO_window
data points (excluding i).
● If you do not hold any shares and
price_i ≥ max_high(i) × (1 + BO_threshold)

→ Buy with full capital.


● If you hold shares and
price_i ≤ max_high(i)

→ Sell all.

5. Common Trading Rules


For each strategy, simulate trades independently:

● Start state:
● Capital = C0
● Shares = 0
● Buy rule:
● Use all available capital.
● Shares bought = floor(capital / price_i)
● Capital = capital − shares × price_i
● Sell rule:
● Sell all shares at price_i.
● Capital increases by shares × price_i
● Shares = 0
● Capital and shares are integers (prices are real numbers).
● At the end of data point N, if you still hold shares, sell everything at price_N.
● You cannot buy and sell on the same data point within the same strategy.
● Each strategy runs with its own capital and position.

6. Output Format
Write to standard output (stdout) exactly 6 lines:

final_equity_strategy1
trades_strategy1
final_equity_strategy2
trades_strategy2
final_equity_strategy3
trades_strategy3

Where:

● final_equity_strategyX – final account value (capital + value of shares


after forced liquidation at price_N), rounded down to an integer.
● trades_strategyX – total number of trades (buy orders + sell orders).

Any extra output (debug prints, prompts, logging) may cause your solution to be
judged incorrect.

7. Performance & Algorithm Requirements


● The official [translate:SPY] dataset (2010–2024 daily) has

only a few thousand data points, but you should design your

solution so that it can scale to time series of length up to

N ≈ 2,000,000.
● Target limits:
● ~2 seconds in C++
● ~4–5 seconds in Python
● Memory: ≤ 256 MB

Recommended complexity:

● Use sliding windows / prefix sums to compute moving averages in O(N).


● Use efficient structures (e.g., deque / monotonic queue) to maintain max_high
in O(N).
● Avoid naive nested loops with O(N × window_size).
8. Example Data Preparation with yfinance (Guidance
Only)
Candidates may use the following approach to generate an input file:

python

import yfinance as yf

data = [Link](
ticker="SPY",
start="2010-01-01",
end="2024-12-31",
interval="1d"
)
close = data["Close"].dropna()
prices = [Link]()
N = len(prices)

# Choose your own parameters here


C0 = 100000
S_short, S_long = 20, 50
MR_window, MR_threshold = 20, 0.02
BO_window, BO_threshold = 50, 0.01

with open("input_SPY.txt", "w") as f:


[Link](f"{N}\n")
[Link](f"{C0}\n")
[Link](f"{S_short} {S_long}\n")
[Link](f"{MR_window} {MR_threshold}\n")
[Link](f"{BO_window} {BO_threshold}\n")
for p in prices:
[Link](f"{p}\n")

You can then run your program as:

bash

g++ [Link] -O2 -o main


./main < input_SPY.txt > [Link]

You are not required to use exactly these parameters; they are only an example.

9. Parameter Optimization
You may freely choose and optimize:

● C0
● S_short, S_long
● MR_window, MR_threshold
● BO_window, BO_threshold

AlgoXpert will run your backtest with the parameters you encoded in your input file /
code.
Use the 3–5 days to experiment and submit the configuration you believe is best for
the official [translate:SPY] dataset.

10. Required Report – [Link] (200–400 words,


English)
Submit a report named:

REPORT_[YourName].txt or REPORT_[YourName].pdf
Answer:

1. Which strategy achieved the highest final_equity on the official


[translate:SPY] dataset?
2. Which strategy produced the fewest trades?
3. If AlgoXpert wants to prioritize both reasonably stable profit and fewer trades
(to reduce costs),
which strategy would you choose and why?

Your report should:

● Refer to your actual output numbers.


● Explain the trade-off between profit and number of trades.
● Briefly describe how you chose/optimized your parameters.
● Optionally mention how you used AI tools during experimentation (shortly; the
main AI details go in Part A).

11. What to Submit


Candidates must submit:

1. Source Code
● [Link] (C++) or [Link] (Python)
2. Input File
● e.g., input_SPY.txt, containing:
● N, C0, your chosen parameters
● N lines of [translate:SPY] Close prices as described
3. AI Usage Essay
● AI_USAGE_[YourName].txt/.pdf (Part B)
4. Strategy Report
● REPORT_[YourName].txt/.pdf (Part A)
5. Optional [Link]
● Build and run instructions, if not already obvious (e.g., compiler flags,
Python version).
12. Evaluation Criteria
● Correctness – adheres to the problem specification and I/O format;
reproducible on the official dataset.
● Efficiency – scales well and runs within reasonable time/memory limits.
● Code Quality – clear structure, naming, and minimal duplication.
● Thinking & Communication – quality of AI_USAGE and REPORT.
● Reproducibility – AlgoXpert can re-run your code with your input file and
obtain the same results

Part B – Short Essay (AI Usage in the Coding Challenge)


Question (150–300 words, English)

AlgoXpert builds high-performance trading and research systems and uses AI tools
(ChatGPT, Claude, Copilot, etc.) heavily in daily work.

You are given a 3–5 day coding challenge that involves performance optimization
and backtesting. You are allowed to use any AI or search tools you like.

Describe in detail how you would use AI during this challenge to:

1. Speed up your work without blindly copying code.


2. Improve the quality and performance of your solution (design, algorithms,
testing, refactoring).
3. Avoid typical risks of overusing AI (wrong assumptions, hidden bugs,
plagiarism, security issues).

Your answer should be concrete: mention specific steps, prompts, or workflows you
would use, and where you would not rely on AI.

Submit this as a separate file named:

AI_USAGE_[YourName].txt or AI_USAGE_[YourName].pdf

You might also like