# Backtest A Simple Trading Strategy Based On Historical Monthly Performance Of Stocks

Hi everyone, in my first blog, I presented how to find best stocks in a certain month in years, so naturally we will ask ourselves how we could make profit based on this idea.

A simple strategy is at the start of each month we will buy the best performing stocks in that month, then hold until the end of month. An alternative strategy is we buy the `x` top performing stocks at the beginning of month, then sell them at the end of month.

Let’s see how it works.

To evaluate the performance of this strategy, I used following assumptions:

• buy at the beginning of month, sell at the end of month
• the buy/sell price is at close price
• only consider stocks that have at least 10-year data history
• If we buy different stocks, we will buy them with equal weight.
• Only consider stocks available at the moment

The data I used is the same data as in the first blog, I created following new features to be utilized later in the code:

• `Month`: month extracted from column Date in current row
• `Year`: year extracted from column Date in current row
• `Lagged_time_month`: Previous month
• `Closing Price of month`: The close price at last trading date in current month.
• `Return_1M`: return of stock in current month
• `Winner`: whether stock price increases/decreases in current month, takes 0–1 value
• `No. winning years`: number of years that stock won in this month
• `Odds of Winning`: empirical odds that stock won in this month
• `Average Return`: average return of stock in this month

The backtest code is as below:

Since it’s April now, so I want to see performance of strategy in April. Here it is:

Based on this backtest result of the first strategy, we should follow the highest odds of winning stocks to get better return.

Let’s go to the second strategy. The code is as below:

The following is the backtest result for April:

Again we can see that the result is better when we buy/sell stocks based on the odds of winning. From my perspective, you should follow the second strategy because it not only has a higher average return but also diversifies your portfolio better.

Til next time all!

Quant Researcher, Data Scientist, Food Hater (so I eat them a lot).

## More from K for What?

Quant Researcher, Data Scientist, Food Hater (so I eat them a lot).