Skip to content

Instantly share code, notes, and snippets.

@thewinterwind
Created June 21, 2014 14:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewinterwind/916e4b31b829678e7ab0 to your computer and use it in GitHub Desktop.
Save thewinterwind/916e4b31b829678e7ab0 to your computer and use it in GitHub Desktop.
Lesson 8 - PHP & Yahoo Finance API
<?php namespace SS\Stock;
use DB, File, Cache;
class Stock {
public function __construct()
{
ini_set("memory_limit", "-1");
set_time_limit(0);
}
/**
* Update the stock streaks after the market has closed
*
* @param $overwrite (whether to update all data from beginning)
* @return void
*/
public function updateStreaks($overwrite = false)
{
$date = DB::table('summaries')->max('date');
$stocks = DB::table('stocks')->select('symbol')->orderBy('symbol', 'asc')->get();
foreach ($stocks as $stock)
{
$stored = DB::table('stocks')
->where('symbol', $stock->symbol)
->where('streak_stored', $date)
->first();
if ($stored && !$overwrite) continue;
$days = DB::table('summaries')
->select('close', 'volume')
->where('symbol', $stock->symbol)
->orderBy('date', 'desc')
->limit(20)
->get();
$streak = 0;
for ($i = 0; $i < count($days); $i++)
{
// if we're at the earliest day recorded for a stock, the streak is over
if ( ! isset($days[$i + 1])) break;
// if the next days price is the same as the current, the streak is over
if ($days[$i]->close === $days[$i + 1]->close) break;
// one time check for the first iteration
if ($i === 0)
{
if ($days[$i] > $days[$i + 1])
{
$streak++; continue;
}
if ($days[$i] < $days[$i + 1])
{
$streak--; continue;
}
}
// check if the winning streak is over or not
if ($streak > 0)
{
// if current day's close is less than the day before it, the winning streak is over
if ($days[$i]->close < $days[$i + 1]->close) break;
// the winning streak continues
$streak++;
}
elseif ($streak < 0)
{
// if the current day's close is more than the day before it, the losing streak is over
if ($days[$i]->close > $days[$i + 1]->close) break;
// the losing streak continues
$streak--;
}
}
$amount = $this->calculateMovePercentage($days, $streak);
$volume = $this->calculateStreakVolume($days, $streak);
DB::table('stocks')
->where('symbol', $stock->symbol)
->update([
'streak' => $streak,
'move_percentage' => $amount,
'streak_volume' => $volume,
'streak_stored' => $date,
]);
print "Symbol: " . $stock->symbol . " # Streak: " . $streak . " # ";
print "Amount: " . $amount . " # Volume: " . $volume . "\n";
}
}
/**
* Update the stock streaks after the market has closed
*
* @param array $days (daily summaries)
* @param int $days (streak)
* @return int (shares traded volume)
*/
public function calculateStreakVolume(array $days, $streak)
{
$daysOnStreak = array_slice($days, 0, abs($streak));
$volume = 0;
foreach ($daysOnStreak as $day) {
$volume += $day->volume;
}
return $volume;
}
/**
* Get the percentage the stock moved over the streak's duration
*
* @param array $days (daily summaries)
* @param int $days (streak)
* @return double (move percentage)
*/
public function calculateMovePercentage(array $days, $streak)
{
if ($streak == 0) return 0;
$days = array_slice($days, 0, abs($streak) + 1);
return round((($days[0]->close / end($days)->close) - 1) * 100, 2);
}
}
@dancodery
Copy link

cool code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment