QuXiangjie / Study-Review

自己欠缺的还太多了,希望通过总结每天的学习内容,整理每天的思绪来丰富自己的知识库。我想成为一名优秀的金融数据分析师,并行发展技术与商业业务。博客内容为:数理统计、财务业务、Python(数据分析及可视化)、Excel(数据分析)、SQL、英文
0 stars 0 forks source link

VBA-Beta calculation with condition and build-in function application #18

Open QuXiangjie opened 7 months ago

QuXiangjie commented 7 months ago

Always put the function in to the module

Function Beta(StockReturns As Range, MarketReturns As Range) As Variant
    ' Check if the number of stock returns matches the number of market returns
    If StockReturns.Rows.Count <> MarketReturns.Rows.Count Then
        Beta = CVErr(xlErrValue) ' Return an error if counts don't match
        Exit Function
    End If

    ' Calculate the covariance between stock and market returns
    Dim Covariance As Double
    Covariance = Application.WorksheetFunction.Covar.S(StockReturns, MarketReturns)

    ' Calculate the variance of market returns
    Dim MarketVariance As Double
    MarketVariance = Application.WorksheetFunction.Var.S(MarketReturns)

    ' Calculate beta
    Beta = Covariance / MarketVariance
End Function
QuXiangjie commented 7 months ago

AAPL.csv This data is from Yahoo finance website of apple company, The data is monthly and a 5 yr time period. I will the adjusted close data to verify this function

Issue: can't download the SP500 data from yahoo finance