- ago
A. Research online shows there are multiple ways to calculate beta:
1. Covariance/Variance Method
2. By Slope Method
3. Correlation Method
Which approach does WL8 use?
B. The Wiki entry for beta uses an Investopedia definition: "..., beta coefficient is a measure
of the volatility (risk) of a security or a portfolio in comparison to the market as a whole
."
So the approach is to compare beta calculated from 2 series, one a stock 8 of interest and an appropriate index.
The wiki example for calculating beta uses the following line:
"Beta beta = Beta.Series( Bars, GetExternalSymbol("^GSPC",true),200); // Yahoo symbol "
This looks like only an index, ^GSPC index for S&P50, is supplied to the function.
How is the stock compared to the index to get beta for the stock?
0
234
5 Replies

Reply

Bookmark

Sort
- ago
#1
WL uses the slope method. So if you plot stock "relative delta" prices vs index "relative delta" prices, the slope of that line is the beta. I never heard of the other methods, but I wonder if they simply compute the least squares fit of the "deltas" using a different approach so they get the same result.

CODE:
      public override void Initialize(BarHistory bars)       {          BarHistory sp500 = GetHistory(bars, "^GSPC");          IndicatorBase deltaPricesIndex = ROC.Series(sp500.Close, 1);          IndicatorBase deltaPricesStock = ROC.Series(bars.Close, 1);       }
Personally, the slope method makes the most intuitive sense to me.

You can use ScottPlot (which is installed with WL) to draw a beta line for you. And its statistics class can also compute the slope of that line via simple (1st-degree) linear regression. It also returns an "r" (correlation coefficient) of the fit. https://scottplot.net/cookbook/4.1/category/statistics/#linear-regression
0
- ago
#2
QUOTE:
This looks like only an index, ^GSPC index for S&P50, is supplied to the function.

The "Bars" parameter contains the other price series
0
- ago
#3
Here is a simple ScottPlot of the beta regression line. Keep in mind that ScottPlots are typically done against the backtest trading results so they are setup in the BacktestComplete{block} where the results become available. But this plot is about the input data, not trading results, so the ScottPlot appears in the Initialize{block} instead, which is weird.

Connecting green lines corresponding to the last five bars and points have been included so one can see where the most recent points fall relative to the beta regression line. The code below creates the plot file on the Windows desktop.
CODE:
using System; //has Environment class using System.Drawing; //has Color class using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using ScottPlot; namespace WealthScript1 {    public class BetaPlot : UserStrategyBase    {       public override void Initialize(BarHistory bars)       {          BarHistory sp500 = GetHistory(bars, "^GSPC");          IndicatorBase deltaPricesIndex = ROC.Series(sp500.Close, 1);          IndicatorBase deltaPricesStock = ROC.Series(bars.Close, 1);          const int periodBeta = 20; //set the period for the beta calculation          Plot plt = new Plot(800, 400);          // Create x & y arrays of length periodBeta          int startIndex = deltaPricesStock.Count - periodBeta;          double[] ys = deltaPricesStock.Values.GetRange(startIndex, periodBeta).ToArray();          double[] xs = deltaPricesIndex.Values.GetRange(startIndex, periodBeta).ToArray();          double x1 = xs[0];          double x2 = xs[xs.Length-1];          // use the linear regression fitter to fit these data          var model = new ScottPlot.Statistics.LinearRegressionLine(xs, ys);          plt.Title($"Beta determination for {bars.Symbol}; period={periodBeta}\n" +             $"Y = {model.slope:0.00}x + {model.offset:0.00} " +             $"(R² = {model.rSquared:0.00})");          // plot the original data and add the regression line          plt.AddScatter(xs, ys, lineWidth: 0);          plt.AddLine(model.slope, model.offset, (x1, x2), lineWidth: 2);                    // connect points for the most recent bars with lines          startIndex = deltaPricesStock.Count - 5;          plt.AddScatter(deltaPricesIndex.Values.GetRange(startIndex, 5).ToArray(),             deltaPricesStock.Values.GetRange(startIndex, 5).ToArray());                    plt.XAxis.Label("Prices changes for " + sp500.SecurityName + " (%price chg)", Color.CornflowerBlue);          plt.YAxis.Label("Prices changes for " + bars.Symbol + " (%price chg)", Color.CornflowerBlue);          plt.SaveFig(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\beta plot.png");       }       public override void Execute(BarHistory bars, int idx) { }    } }
0
- ago
#4
Great answers! Thank you so much for taking time to create these detailed and logical answers. The code is super frosting on the top.

I do have one follow up question. The wiki code example doesn't include any code to address the 'bars' term that is required. How should it be declared and instantiated so that it is correct for the function call?

I look forward to your answers!
0
- ago
#5
QUOTE:
The wiki code example doesn't include any code to address the 'bars' term

The wiki code you talk about is for a WL6 implementation and this is a WL8 forum. If you click on any indicator, Beta included, in the WL8 QuickRef and begin typing your code, WL should offer a code example calling that specific indicator. Try it. But here's an example below. Of course you need to include the using statements and the namespace blocks as well as is shown in Post #3.
CODE:
      public override void Initialize(BarHistory bars)       {          Beta beta = Beta.Series(bars, "^GSPC", 20);          PlotIndicator(beta);       }
1

Reply

Bookmark

Sort