This project follows a structured workflow for generating statistical trading signals and implementing trading strategies. The key steps in the workflow are as follows:
Check for missing values (NA values) and negative values.
The project uses various statistical trading signal strategies to make trading decisions, where each strategy generates buy and sell signals based on specific conditions:
- Buy when a short-term moving average (5-day) crosses above a long-term moving average (20-day).
- Sell when the short-term moving average crosses below the long-term moving average.
- Buy when the price touches or falls below the lower Bollinger Band (20-day, 2-std) and then moves back into the band.
- Sell when the price touches or rises above the upper Bollinger Band and then moves back into the band.
- Buy when the RSI crosses above an oversold threshold (30), indicating a potential upward move.
- Sell when the RSI crosses below an overbought threshold (70), indicating a potential downward move.
- Buy when the MACD line crosses above the signal line.
- Sell when the MACD line crosses below the signal line.
- Buy when the stock's price is significantly below its moving average, indicating it might revert to the mean.
- Sell when the stock's price is significantly above its moving average.
- Use two moving averages (10-day and 50-day). Buy when the short-term MA crosses above the long-term MA and sell when it crosses below.
After generating trading signals, we combine (sum) all the trading signals from the different strategies and execute a trading plan with two simple setups for this tiny project:
- In the beginning, we can long stocks and borrow stocks to short, for which we repurchase them later to cover the loan.
- we trade across all stocks. In other words, our portfolio contains all the stocks provided.
Specifically, for each stock:
-
Sum all trading signals generated from the strategies and filter out the aggregated
buy_signal
(sell_signal
) whose absolute value is less than or equal to 2 (1). -
Initialize
current_balance
, whcih represents the current balance available for trading, to 0. -
Initialize
current_position
, which indicates the current stock position held in the portfolio, to 0. -
If a
buy_signal
is presented:- If
current_position
is 0, we long the stock with the current stock price and unit volume, reducingcurrent_balance
and resulting in positivecurrent_position
. - If
current_position
is negative, we short all currently-hold stocks with the current stock price, increasingcurrent_balance
and resettingcurrent_position
to 0. - If
current_position
is positive, we do nothing.
- If
-
If a
sell_signal
is presented:- If
current_position
is 0, we short the stock with the current stock price and unit volume, increasingcurrent_balance
and resulting in negativecurrent_position
. - If
current_position
is positive, we long all currently-hold stocks with the current stock price, reducingcurrent_balance
and resettingcurrent_position
to 0. - If
current_position
is negative, we do nothing.
- If
-
We stop trading this particular stock with the above procedures until the
current_balance
surpasses a pre-defined threshold. A higher threshold signifies a lengthier period required to achieve the break-even point, but it also offers the potential for greater earnings.
Parameters in trading strategies, such as window size and threshold values, and those in trading executions, such as trading volume and stop-rule threshold, can be further fine-tuned given more time and extensive backtesting.
With the trading strategies and executions described above, the project provides results showing the aggregate gains of the portfolio: