Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently writing a programme to calculate the one day VaR using the historical simulation method. Within the programme I am first calculating the daily returns of the prices which the excel formula is Close_price_today - Close_price_yesterday /
Close_price_yesterday. I would like to compute this into my programme but not sure how to write the code to calculate it? The code below is a code part of the programme that myself and my friend came up with at the start but my teacher said to try write another way of calculating the daily returns using the excel formula way instead. I would be grateful for any help.
By the way, in the code below, y contains the data of daily prices of a stock that were loaded.

What I have tried:

vector <double> dailyreturns;
for (int i = 0; i < y.size(); i++) {
int findex = y.size() - 1;
int previousindex = cprice - 1;
if (i > 0) {
double prices = y[findex] * (y[i] / y[previousindex]);
dailyreturns.push_back(prices);
}
Posted
Comments
Richard MacCutchan 10-Apr-21 11:48am    
What is the problem with writing that formula in your code? It is not clear in your code which of the variables are Close_price_today and Close_price_yesterday.
Bhavini Amin 10-Apr-21 14:46pm    
there's nothing wrong, it just does not look right, i think i am missing something, here's what i rewrote:
vector <double> dailyreturns;
for (int i = 0; i < y.size(); i++) {
int price_today = 0;
int price_yesterday = 0;
double return_prices = y[price_yesterday] - y[price_today] / y[price_today];
dailyreturns.push_back(return_prices);
sort(dailyreturns.begin(), dailyreturns.end());
}
Richard MacCutchan 11-Apr-21 3:03am    
The expression y[price_today] / y[price_today] yields the answer 1 in every case. I suspect that is not the answer you actually want. Is it possible the calculation should be double return_prices = (y[price_yesterday] - y[price_today]) / y[price_today];? Where the difference between the two is divided by one of the values.
Bhavini Amin 11-Apr-21 7:02am    
ahh that makes sense, yes that really helps thank you!
Rick York 10-Apr-21 12:37pm    
Indentation is your friend.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900