Skip to content

learning how to use VBA in Excel to analyze and predict stock

Notifications You must be signed in to change notification settings

RuthLD/stock-analysis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 

Repository files navigation

stock-analysis

📈 Learning how to use VBA in Excel to analysis and predict stock

Project Overview

The project’s background was to evaluate green energy company stocks to assess the potential for diversification of investment funds. The goal was to learn VBA and to refactor VBA script for optimization.

Results

The initial VBA script was created and run with the file green_stocks. For the 2017 stock data, the script had a run time of 1.621094 seconds, as seen in Before_Refactor_2017 image. Before_Refactor_2017

For 2018 stock data, the script had a run time of 1.292969 seconds, as seen in Before_Refactor_2018 image. Before_Refactor_2018

The file VBA_Challenge was used for to refator the VBA script. After refactoring the script, stock data for 2017 ran in 0.203125 seconds, and stock data for 2018 ran in 0.1953125 seconds. Images VBA_Challenge_2017 and VBA_Challenge_2018 show the reduced run times and that the initial analysis has not changed.

VBA_Challenge_2017 VBA_Challenge_2018

Key Changes in the Script

In the initial analysis of all stock by year, the starting and ending price were initialized as variables.

  • Dim startingPrice As Single
  • Dim endingPrice As Single

The following three changes made when refactoring the script influenced the run time of the script for efficiency.

  1. The creation of a tickerIndex variable.
  • '1a)Create a ticker Index
    • tickerIndex = 0
  1. The creation of output arrays for tickerVolumes, tickerStartingPrices, and tickerEnding Prices.
  • '1b) Create three output arrays
    • Dim tickerVolumes(12) As Long
    • Dim tickerStartingPrices(12) As Single
    • Dim tickerEndingPrices(12) As Single
  1. Ensuring that the arrays’ starting value was set to zero at the beginning of each loop.
  • '2a) Create a for loop to initialize the tickerVolumes to zero.
    • For i = 0 To 11
    • tickerVolumes(i) = 0
    • tickerStartingPrices(i) = 0
    • tickerEndingPrices(i) = 0
    • Next i

Summary

Advantages of Refactoring

Refactoring will start with a preexisting outline of the script, and the code can be used with the modules already in place.

Disadvantages of Refactoring

A firm understanding of the VBA syntax is needed to make the script more efficient.

Pros and Cons

When refactoring the original script for this project, the outline provided had all of the formatting information in place. The ticker array was set up beforehand, saving a lot of time. There was a significant improvement in the run time of the script after refactoring. The syntax’s exact requirements meant that the order I edited the script could return an error before I finished the edit.

About

learning how to use VBA in Excel to analyze and predict stock

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published