finance - improvements to this php function to calculate monthly returns from a set of price data -


i have made function should take set of price data , calculate set of monthly returns. idea is should able cope irregular time intervals. example have daily data s&p 500 many hedge funds release price data monthly.

here have come with:

//turns price series array of monthly returns.  function monthlyreturns($dates, $prices, $startdate = "", $enddate = ""){ if ($enddate == "") $enddate = time();  if (count($dates) != count($prices)){ die("the dates array must same length prices array"); }   $i = 0; $firstpass = false; foreach ($dates $date){   $d = strtotime($date);   if ($startdate == "" || ($d >= $startdate && $d <= $enddate )) :     $p = $prices[$i];     $month = date('m',$d);     $year = date('y',$d);      if ($i == 0 || $firstpass == false){       $startprice = $p;       $lastmonth = $month;     }      if ($month != $lastmonth && $i != 0){       $returntable['date'][] = $dates[$i-1];       $returntable['return'][] = $p / $startprice - 1;       $startprice = $p;     }     $lastmonth = $month;     $firstpass = true;   endif;   $i++;   }    return $returntable; } 

i noticed fund monthly data returns off month. can see major flaws function explain or solution solve it.

for info, monthly returns hedge funds come near end of month. e.g.

date       | price ----------------- 2013-01-29 | 25.69 2013-02-28 | 27.62 2013-03-30 | 26.53 2013-04-29 | 28.45 


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -