Home Python Trading Strategy How To Measure Skewness Of A Trading Strategy Using Python – (Code,...

# How To Measure Skewness Of A Trading Strategy Using Python – (Code, Setup, Example)

Many metrics and statistics are used to quantify a trading strategy’s performance. CAGR, standard deviation, Sharpe Ratio, and maximum drawdown are among the most popular indicators. However, today, we will look at one that is not used very often: How to measure the skewness of a trading strategy using Python.

The skewness is a measure of the symmetry of a distribution. Usually, it is the distribution of daily returns of the strategy. The skewness signals whether the distribution is normal or shifted to the right or left. But what does this really mean, and how is it calculated?

In this article, we will look at the skewness, the formula to calculate it, and how to do it using Python.

Related reading: â€“ Are you looking for other Python trading systems? (We have plenty more)

## What is skewness?

Skewness is a measure of the symmetry of a distribution. If the distribution is normal, it is said that it has no skewness, as it is symmetrical on both sides. However, if the bell curve is shifted to the left or the right, it is said to be skewed.

There are several different types of distributions and skews. On the one hand, if the distribution is shifted to the left, it is negatively skewed. On the other hand, if the distribution is shifted to the right, it is a positively skewed distribution.

Obviously, the more the distribution is shifted to the right, the better – exactly what you are looking for in a trading strategy. This means that the skew should preferably be positive in trading.

## What is the formula to calculate the skewness of a distribution?

There are two ways of calculating the skewness of a distribution.

Pearson’s first and second coefficients are the most common methods. Pearson’s first coefficient of skewness is calculated by subtracting the mean value from the mode value and dividing the result by the standard deviation. The formula is:

On the contrary, Pearson’s second coefficient of skewness swaps the mode value for the median value and then multiplies the subtraction results by 3. The formula is:

Pearsonâ€™s first coefficient of skewness is helpful if the data exhibit a strong mode. Pearson’s second coefficient may be preferable if the data have a weak mode or multiple modes, as it does not rely on mode as a measure of central tendency.

## How do we measure the skewness of a trading strategy in Python?

We will calculate the skewness of the weekly rotating system between the S&P 500 and utilities using Python. First, we will plot the strategy’s weekly returns distribution using the library matplotlib.pyplot. This is what each of the code lines does:

1. Creates a new figure of 10 inches wide and 7 inches high.
2. This line plots a histogram of the ‘returns’ column, which is the weekly returns of the strategy. The second parameter, 150, specifies the number of bins or bars in the histogram.
3. Adds a vertical line at the mean of the ‘returns’ columns, with a dashed linestyle and colored red.
4. The same as the previous line but with the median value instead of the mean.
5. Adds a text annotation to the plot of the mean value of the â€˜returnsâ€™ column in the x-coordinate 1.02 and the y-coordinate 60.
6. The same as the previous line but with the median value instead of the mean.
7. Displays the chart.

The weekly mean return of the strategy is 0.1448%, and the median is 0.2493%. However, this doesn’t necessarily mean the strategy has a positive skew. To calculate the skewness, we can either do it with the formula mentioned above or a function from the scipy library.Â

In the first case, we have to use Pearson’s second coefficient formula. Here is the result:

The strategy has a negative skew! This means that the distribution of returns is shifted to the left. The other, and more straightforward, way to calculate it is to import skew from scipy.stats and use the skew function:

As you can see, the skew is negative as well. This means that the tail of the distribution is more extended to the left than the right side.

## How to measure the skewness of a trading strategy using Python – conclusion

To sum up, today, we learned how to calculate the skewness of a distribution of returns of a trading strategy using Python. Skewness is often an overlooked performance metric but should be included in every backtest (?).