Forecasting Stock Prices in PHP [closed]

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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?

One example I saw is here https://stackoverflow.com/questions/13168568/how-do-i-use-holt-winters-seasonal-dampened-method-to-compute-a-two-month-sales

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;
}
?>
profilepic.png
manpreet 2 years ago

It's very easy to forecast stock prices in any programming language. Given a 1-year dataset of OHLC data, use the most recent close as the forecast for the next 15/30/500 days.

This is called a naive forecast, and while it's not very exciting, it's more accurate than any other method I've ever seen for forecasting stock prices.

It's also trivially easy to implement.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.