项目作者: ibaloyan

项目描述 :
Create VBA script for Stock Data Analysis, loop through Stock Market Data and return Yearly Stock Performance Results.
高级语言: Visual Basic
项目地址: git://github.com/ibaloyan/Stock_Analysis_with_VBA.git
创建时间: 2018-12-25T04:59:48Z
项目社区:https://github.com/ibaloyan/Stock_Analysis_with_VBA

开源协议:

下载


Stock_Analysis_with_VBA

Create VBA script for Stock Data Analysis, loop through Stock Market Data and return Yearly Stock Performance Results.

Initial Stock Market Data:

Stock Data

The Objective of the Project:

The objective of this project is to create a VBA script that provides easy analysis of yearly stock performance. The worksheets are organized by year and contain the starting, final, highest, and lowest value of each stock for each day of the year. The VBA script will give the yearly increase/decrease in stock value for each stock, the percentage change over the year, and the total volume for the year. In addition the stock will give the greatest increase, decrease, and total volume.

The data has been sorted such that the stocks are grouped together by ticker value in alphabetical order
The data is sorted by date such that the dates are in order with the first date for a given stock is the begining of the year and the last value for a give stock is the last date of the year traded.
Non-zero stock value is not assumed. The data in the spreadsheets represents the complete data with no missing values.

  • Extract Ticker Symbols:
    The unique ticker symbols are extracted and inserted into a column.

  • Extract Initial and Final Values for Stock Price:
    The initial and final values for the stock price. As non-zero values are not assumed, the first non-zero value is taken to avoid division by zero.

  • Calculate Yearly Change, Percentage Change, Total Volume:
    The values are calculated for yearly change in stock values, yearly percentage change, and yearly total volume. Values are displayed in spreadsheet next to corresponding ticker value.
    Yearly Change = Final Value - Initial (non-zero) Value
    Percentage Change = Yearly Change / Initial (non-zero) Value
    Total Volume = Sum(Daily Volume)

  • Determine Max and Min values:
    The “Greatest % Increase”, “Greatest % Decrease”, and “Greatest Yearly Volume” are determined from the values calculated in the last step and displayed in the chart.

  • Provide conditional formatting that will highlight positive change in green and negative change in red.

  • VBA script is run once and produces all appropriate adjustments on every worksheet.

Yearly Stock Performance Results ( 2014-2016 )

hard_solution

VBA code:

‘ Hard Solution VB code - Inna Baloyan May2018 Bootcamp
‘ Going through all worksheets

Sub WorksheetsLoop()

  1. ' Set CurrentWs as a worksheet object variable.
  2. Dim CurrentWs As Worksheet
  3. Dim Need_Summary_Table_Header As Boolean
  4. Dim COMMAND_SPREADSHEET As Boolean
  5. Need_Summary_Table_Header = False 'Set Header flag
  6. COMMAND_SPREADSHEET = True 'Hard part flag
  7. ' Loop through all of the worksheets in the active workbook.
  8. For Each CurrentWs In Worksheets
  9. ' Set initial variable for holding the ticker name
  10. Dim Ticker_Name As String
  11. Ticker_Name = " "
  12. ' Set an initial variable for holding the total per ticker name
  13. Dim Total_Ticker_Volume As Double
  14. Total_Ticker_Volume = 0
  15. ' Set new variables for Moderate Solution Part
  16. Dim Open_Price As Double
  17. Open_Price = 0
  18. Dim Close_Price As Double
  19. Close_Price = 0
  20. Dim Delta_Price As Double
  21. Delta_Price = 0
  22. Dim Delta_Percent As Double
  23. Delta_Percent = 0
  24. ' Set new variables for Hard Solution Part
  25. Dim MAX_TICKER_NAME As String
  26. MAX_TICKER_NAME = " "
  27. Dim MIN_TICKER_NAME As String
  28. MIN_TICKER_NAME = " "
  29. Dim MAX_PERCENT As Double
  30. MAX_PERCENT = 0
  31. Dim MIN_PERCENT As Double
  32. MIN_PERCENT = 0
  33. Dim MAX_VOLUME_TICKER As String
  34. MAX_VOLUME_TICKER = " "
  35. Dim MAX_VOLUME As Double
  36. MAX_VOLUME = 0
  37. '----------------------------------------------------------------
  38. ' Keep track of the location for each ticker name
  39. ' in the summary table for the current worksheet
  40. Dim Summary_Table_Row As Long
  41. Summary_Table_Row = 2
  42. ' Set initial row count for the current worksheet
  43. Dim Lastrow As Long
  44. Dim i As Long
  45. Lastrow = CurrentWs.Cells(Rows.Count, 1).End(xlUp).Row
  46. ' For all worksheet except the first one, the Results
  47. If Need_Summary_Table_Header Then
  48. ' Set Titles for the Summary Table for current worksheet
  49. CurrentWs.Range("I1").Value = "Ticker"
  50. CurrentWs.Range("J1").Value = "Yearly Change"
  51. CurrentWs.Range("K1").Value = "Percent Change"
  52. CurrentWs.Range("L1").Value = "Total Stock Volume"
  53. ' Set Additional Titles for new Summary Table on the right for current worksheet
  54. CurrentWs.Range("O2").Value = "Greatest % Increase"
  55. CurrentWs.Range("O3").Value = "Greatest % Decrease"
  56. CurrentWs.Range("O4").Value = "Greatest Total Volume"
  57. CurrentWs.Range("P1").Value = "Ticker"
  58. CurrentWs.Range("Q1").Value = "Value"
  59. Else
  60. 'This is the first, resulting worksheet, reset flag for the rest of worksheets
  61. Need_Summary_Table_Header = True
  62. End If
  63. ' Set initial value of Open Price for the first Ticker of CurrentWs,
  64. ' The rest ticker's open price will be initialized within the for loop below
  65. Open_Price = CurrentWs.Cells(2, 3).Value
  66. ' Loop from the beginning of the current worksheet(Row2) till its last row
  67. For i = 2 To Lastrow
  68. ' Check if we are still within the same ticker name,
  69. ' if not - write results to summary table
  70. If CurrentWs.Cells(i + 1, 1).Value <> CurrentWs.Cells(i, 1).Value Then
  71. ' Set the ticker name, we are ready to insert this ticker name data
  72. Ticker_Name = CurrentWs.Cells(i, 1).Value
  73. ' Calculate Delta_Price and Delta_Percent
  74. Close_Price = CurrentWs.Cells(i, 6).Value
  75. Delta_Price = Close_Price - Open_Price
  76. ' Check Division by 0 condition
  77. If Open_Price <> 0 Then
  78. Delta_Percent = (Delta_Price / Open_Price) * 100
  79. Else
  80. ' Unlikely, but it needs to be checked to avoid program crushing
  81. MsgBox ("For " & Ticker_Name & ", Row " & CStr(i) & ": Open Price =" & Open_Price & ". Fix <open> field manually and save the spreadsheet.")
  82. End If
  83. ' Add to the Ticker name total volume
  84. Total_Ticker_Volume = Total_Ticker_Volume + CurrentWs.Cells(i, 7).Value
  85. ' Print the Ticker Name in the Summary Table, Column I
  86. CurrentWs.Range("I" & Summary_Table_Row).Value = Ticker_Name
  87. ' Print the Ticker Name in the Summary Table, Column I
  88. CurrentWs.Range("J" & Summary_Table_Row).Value = Delta_Price
  89. ' Fill "Yearly Change", i.e. Delta_Price with Green and Red colors
  90. If (Delta_Price > 0) Then
  91. 'Fill column with GREEN color - good
  92. CurrentWs.Range("J" & Summary_Table_Row).Interior.ColorIndex = 4
  93. ElseIf (Delta_Price <= 0) Then
  94. 'Fill column with RED color - bad
  95. CurrentWs.Range("J" & Summary_Table_Row).Interior.ColorIndex = 3
  96. End If
  97. ' Print the Ticker Name in the Summary Table, Column I
  98. CurrentWs.Range("K" & Summary_Table_Row).Value = (CStr(Delta_Percent) & "%")
  99. ' Print the Ticker Name in the Summary Table, Column J
  100. CurrentWs.Range("L" & Summary_Table_Row).Value = Total_Ticker_Volume
  101. ' Add 1 to the summary table row count
  102. Summary_Table_Row = Summary_Table_Row + 1
  103. ' Reset Delta_rice and Delta_Percent holders, as we will be working with new Ticker
  104. Delta_Price = 0
  105. ' Hard part,do this in the beginning of the for loop Delta_Percent = 0
  106. Close_Price = 0
  107. ' Capture next Ticker's Open_Price
  108. Open_Price = CurrentWs.Cells(i + 1, 3).Value
  109. ' Hard part : Populate new Summary table on the right for the current spreadsheet HERE
  110. ' Keep track of all extra hard counters and do calculations within the current spreadsheet
  111. If (Delta_Percent > MAX_PERCENT) Then
  112. MAX_PERCENT = Delta_Percent
  113. MAX_TICKER_NAME = Ticker_Name
  114. ElseIf (Delta_Percent < MIN_PERCENT) Then
  115. MIN_PERCENT = Delta_Percent
  116. MIN_TICKER_NAME = Ticker_Name
  117. End If
  118. If (Total_Ticker_Volume > MAX_VOLUME) Then
  119. MAX_VOLUME = Total_Ticker_Volume
  120. MAX_VOLUME_TICKER = Ticker_Name
  121. End If
  122. ' Hard part adjustments to resetting counters
  123. Delta_Percent = 0
  124. Total_Ticker_Volume = 0
  125. 'Else - If the cell immediately following a row is still the same ticker name,
  126. 'just add to Totl Ticker Volume
  127. Else
  128. ' Encrease the Total Ticker Volume
  129. Total_Ticker_Volume = Total_Ticker_Volume + CurrentWs.Cells(i, 7).Value
  130. End If
  131. ' For debugging MsgBox (CurrentWs.Rows(i).Cells(2, 1))
  132. Next i
  133. ' For hard solution part:
  134. ' Check if it is not the first spreadsheet
  135. ' Record all new counts to the new summary table on the right of the current spreadsheet
  136. If Not COMMAND_SPREADSHEET Then
  137. CurrentWs.Range("Q2").Value = (CStr(MAX_PERCENT) & "%")
  138. CurrentWs.Range("Q3").Value = (CStr(MIN_PERCENT) & "%")
  139. CurrentWs.Range("P2").Value = MAX_TICKER_NAME
  140. CurrentWs.Range("P3").Value = MIN_TICKER_NAME
  141. CurrentWs.Range("Q4").Value = MAX_VOLUME
  142. CurrentWs.Range("P4").Value = MAX_VOLUME_TICKER
  143. Else
  144. COMMAND_SPREADSHEET = False
  145. End If
  146. Next CurrentWs

End Sub