How To Measure Skewness Of A Trading Strategy Using Python

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

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.

Python-related resources

We have written many articles about Python, and you might find these interesting:

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:

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

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:

How to calculate skewness

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.
How To Measure Skewness Of A Trading Strategy Using Python

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:

Skewness using Python

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 (?).

FAQ:

What is skewness in the context of a trading strategy?

Skewness measures the symmetry of a distribution, particularly in the context of daily returns in a trading strategy. A normal distribution has zero skewness, while positive or negative skewness indicates a shift to the right or left, respectively.

How is skewness calculated in a trading strategy using Python?

Skewness in a trading strategy can be calculated using Pearson’s coefficients. Pearson’s first coefficient involves subtracting the mean from the mode, while the second coefficient substitutes the mode for the median. Python, with libraries like scipy.stats, can be used to calculate skewness efficiently.

How does Python assist in visualizing the skewness of a trading strategy?

The mean and median values provide insights into the central tendency of returns. In a negatively skewed strategy, the mean is typically less than the median, indicating that extreme negative returns pull the mean to the left. Python, with libraries like matplotlib.pyplot, helps visualize the skewness of a trading strategy. Plots, histograms, and statistical annotations can be generated to illustrate the distribution of returns and key metrics like mean and median.

Similar Posts