# Forecasting Stock Prices in PHP [closed]

Has anyone implemented stock prices forecasting, using php only.

Like we give data sets of 1 yr of open,high,low,close,volume and get prediction for next 15 or 30 days?

Just thought if we give a array of 1 to 10.. can next number be got - yes 11 is got, but couldnt go further than this? like getting next 5 data..

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');

$anData = array(1,2,3,4,5,6,7,8,9,10); print_r(forecastHoltWinters($anData));

function forecastHoltWinters($anData,$nForecast = 1, $nSeasonLength = 1,$nAlpha =     0.2, $nBeta = 0.01,$nGamma = 0.01, $nDevGamma = 0.1) { // Calculate an initial trend level$nTrend1 = 0;
for($i = 0;$i < $nSeasonLength;$i++) {
$nTrend1 +=$anData[$i]; }$nTrend1 /= $nSeasonLength;$nTrend2 = 0;
for($i =$nSeasonLength; $i < 2*$nSeasonLength; $i++) {$nTrend2 += $anData[$i];
}
$nTrend2 /=$nSeasonLength;

$nInitialTrend = ($nTrend2 - $nTrend1) /$nSeasonLength;

// Take the first value as the initial level
$nInitialLevel =$anData[0];

// Build index
$anIndex = array(); foreach($anData as $nKey =>$nVal) {
$anIndex[$nKey] = $nVal / ($nInitialLevel + ($nKey + 1) *$nInitialTrend);
}

// Build season buffer
$anSeason = array_fill(0, count($anData), 0);
for($i = 0;$i < $nSeasonLength;$i++) {
$anSeason[$i] = ($anIndex[$i] + $anIndex[$i+$nSeasonLength]) / 2; } // Normalise season$nSeasonFactor = $nSeasonLength / array_sum($anSeason);
foreach($anSeason as$nKey => $nVal) {$anSeason[$nKey] *=$nSeasonFactor;
}

$anHoltWinters = array();$anDeviations = array();
$nAlphaLevel =$nInitialLevel;
$nBetaTrend =$nInitialTrend;
foreach($anData as$nKey => $nVal) {$nTempLevel = $nAlphaLevel;$nTempTrend = $nBetaTrend;$nAlphaLevel = $nAlpha *$nVal / $anSeason[$nKey] + (1.0 - $nAlpha) * ($nTempLevel + $nTempTrend);$nBetaTrend = $nBeta * ($nAlphaLevel - $nTempLevel) + ( 1.0 -$nBeta ) * $nTempTrend;$anSeason[$nKey +$nSeasonLength] = $nGamma *$nVal / $nAlphaLevel + (1.0 -$nGamma) * $anSeason[$nKey];

$anHoltWinters[$nKey] = ($nAlphaLevel +$nBetaTrend * ($nKey + 1)) *$anSeason[$nKey];$anDeviations[$nKey] =$nDevGamma * abs($nVal -$anHoltWinters[$nKey]) + (1-$nDevGamma)
* (isset($anDeviations[$nKey - $nSeasonLength]) ?$anDeviations[$nKey -$nSeasonLength] : 0);
}

$anForecast = array();$nLast = end($anData); for($i = 1; $i <=$nForecast; $i++) {$nComputed = round($nAlphaLevel +$nBetaTrend * $anSeason[$nKey + $i]); if ($nComputed < 0) { // wildly off due to outliers
$nComputed =$nLast;
}
$anForecast[] =$nComputed;
}

return \$anForecast;
}
?>


## closed as off-topic by Andy, Stephan Kolassa, whuber♦Nov 29 '14 at 20:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

• random walk which is arima(0,1,0) with drift is the most accurate (according to several published articles in the literature) for forecasting stock prices. R's forecast has a function called rwf, you could check that out. – forecaster Nov 29 '14 at 19:49
• @forecaster Could you provide links for those articles? I'd love to read them— I'd always believed the naive forecast was the most accurate for stock data. – Zach Nov 29 '14 at 19:56
• @Ajmal You're question has been put on hold because it appears to be off-topic. Take a look here, and then try re-wording your question. – Zach Nov 29 '14 at 21:46
• @Zach.. thanks for your concerns, this is the first q I have posted in this group, I wanted to learn if anyone has implemented forecasting in php? – Ajmal Nov 29 '14 at 23:34
• @Ajmal Probably not. PHP isn't a great language for statistical programming. You'd probably be better off doing the foresting in python or R (or another statistical programming language) and displaying the results in PHP. – Zach Nov 30 '14 at 0:45

• @Ajmal I don't know PHP, but lets assume you have the 5 arrays of numbers: open, high, low, close, volume for each stock. If those arrays are sorted in date order, simply select the last element of the close array, and that is your forecast! – Zach Nov 29 '14 at 19:49