To my understanding you are trying to implement a rolling window approach to calculate Value at Risk (VaR) using a normal distribution for a stock.
In the code shared by you, I can identify a couple of issues.
- The indexing inside the loop (i = TestWindow - TestWindowStart + 1) is incorrect. You should use t as the index instead of TestWindow. Additionally, you need to ensure that the loop iterates over the correct range of values for t.
- You haven't correctly set up the rolling window for the estimation period. The estimation window should move along with the rolling window for the test period.
Here is the right approach to fix these issues:
- Correctly set up the loop to iterate over the test window (t ranges from EstimationWindowSize + 1 to SampleSize).
- Inside the loop, define the estimation window based on the current value of t.
- Calculate the standard deviation (Sigma) for the data within the estimation window.
- Calculate VaR using the normal distribution and store the results in the Normal95 and Normal99 arrays.
Hope this helps!