Skip to content

Instantly share code, notes, and snippets.

@thewinterwind
Last active June 13, 2016 13:56
Show Gist options
  • Select an option

  • Save thewinterwind/793eeb2ecff957dd0298 to your computer and use it in GitHub Desktop.

Select an option

Save thewinterwind/793eeb2ecff957dd0298 to your computer and use it in GitHub Desktop.

Revisions

  1. thewinterwind revised this gist Jun 20, 2014. 1 changed file with 3 additions and 7 deletions.
    10 changes: 3 additions & 7 deletions StoringController.php
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,6 @@ class StoringController extends BaseController {

    public function store_streaks()
    {
    ini_set("memory_limit", "-1");
    set_time_limit(0);

    $date = date('Y-m-d');

    $stocks = DB::table('stocks')->select('symbol')->orderBy('symbol', 'asc')->get();
    @@ -54,15 +51,15 @@ public function store_streaks()
    // check if the winning streak is over or not
    if ($streak > 0)
    {
    // if current day's close is less than day before it, streak is over
    // 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 current day's close is more than day before it, streak is over
    // 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
    @@ -80,5 +77,4 @@ public function store_streaks()
    print "Stored a streak of " . $streak . " for: " . $stock->symbol . "\n";
    }
    }

    }
    }
  2. thewinterwind revised this gist Jun 18, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions StoringController.php
    Original file line number Diff line number Diff line change
    @@ -54,15 +54,15 @@ public function store_streaks()
    // check if the winning streak is over or not
    if ($streak > 0)
    {
    // if current day's close is less than the day before it, streak is over
    // if current day's close is less than day before it, 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, streak is over
    // if current day's close is more than day before it, streak is over
    if ($days[$i]->close > $days[$i + 1]->close) break;

    // the losing streak continues
  3. thewinterwind revised this gist Jun 18, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions StoringController.php
    Original file line number Diff line number Diff line change
    @@ -54,15 +54,15 @@ public function store_streaks()
    // 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 current day's close is less than the day before it, 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 the current day's close is more than the day before it, streak is over
    if ($days[$i]->close > $days[$i + 1]->close) break;

    // the losing streak continues
  4. thewinterwind created this gist Jun 18, 2014.
    84 changes: 84 additions & 0 deletions StoringController.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    <?php

    class StoringController extends BaseController {

    public function store_streaks()
    {
    ini_set("memory_limit", "-1");
    set_time_limit(0);

    $date = date('Y-m-d');

    $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) continue;

    $days = DB::table('summaries')
    ->select('close')
    ->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--;
    }
    }

    DB::table('stocks')
    ->where('symbol', $stock->symbol)
    ->update([
    'streak' => $streak,
    'streak_stored' => $date,
    ]);

    print "Stored a streak of " . $streak . " for: " . $stock->symbol . "\n";
    }
    }

    }