Exemple #1
0
 public function test_register()
 {
     $test_agent = new TestAgent();
     // Registering an agent for the first time returns true
     $this->assertTrue(AgentPool::register($test_agent));
     // Registering an agent twice returns false
     $this->assertFalse(AgentPool::register($test_agent));
 }
    {
        return AgentPool::MEDIUM;
    }
    public function run($context)
    {
        $stock_count = 0;
        foreach ($context->stocks as $stock) {
            if ($stock->attivo == 0 || $stock->tipo != 'obbligazione') {
                // printf("%s stock %s is inactive or not a bond.\n", get_called_class(), $stock->isin);
                continue;
            }
            $bond = new Bond();
            if (!$bond->find_by_id($stock->isin)) {
                // printf("%s bond %s does not exist.\n", get_called_class(), $stock->isin);
                continue;
            }
            if ($bond->scadenza < date('Y-m-d')) {
                $stock->attivo = 0;
                $stock->save();
                $stock_count++;
            }
            // else
            // {
            //   printf("%s bond %s expires in the future\n", get_called_class(), $stock->isin);
            // }
        }
        printf("%s deactivated %d stocks\n", get_called_class(), $stock_count);
    }
}
AgentPool::register(new ExpirationJanitorAgent());
require_once dirname(__FILE__) . "/base_agent.php";
require_once dirname(__FILE__) . "/../bin/lib.inc.php";
require_once dirname(__FILE__) . "/../models/bond.php";
class StockLogoGroomerAgent extends BaseAgent
{
    public $name = 'stock_logo_groomer';
    static $TEMPLATES = array('argentina', 'australia', 'austria', 'banca mondiale', 'bei', 'bers', 'bobl', 'bonos', 'bot', 'brasil', 'btan', 'btp', 'bund', 'cct', 'croazia', 'ctz', 'ebrd', 'efsf', 'eib', 'esm', 'eurofima', 'european', 'gilt', 'grecia', 'iadb', 'ibrd', 'irlanda', 'ishares', 'italia', 'kfw', 'lyxor', 'messico', 'mexico', 'oat', 'obligaciones', 'olo', 'russia', 'schatz', 'turchia', 'turkey', 'ungheria', 'usa', 'venezuela', 'wb', 'world bank');
    public function get_priority($context)
    {
        return AgentPool::MEDIUM;
    }
    public function run($context)
    {
        $stock_count = 0;
        foreach ($context->stocks as $stock) {
            if (!file_exists(dirname(__FILE__) . "/../assets/logos/{$stock->isin}.png")) {
                foreach (self::$TEMPLATES as $template) {
                    if (strpos(strtolower($stock->title), $template) === 0) {
                        printf("%s updating logo for %s\n", get_called_class(), $stock->isin);
                        copy(dirname(__FILE__) . "/../assets/logos/templates/{$template}.png", dirname(__FILE__) . "/../assets/logos/{$stock->isin}.png");
                        $stock_count++;
                        break;
                    }
                }
            }
        }
        printf("%s updated logos for %d stocks\n", get_called_class(), $stock_count);
    }
}
AgentPool::register(new StockLogoGroomerAgent());
<?php

require_once dirname(__FILE__) . "/agent_pool.php";
require_once dirname(__FILE__) . "/base_agent.php";
require_once dirname(__FILE__) . "/../bin/lib.inc.php";
require_once dirname(__FILE__) . "/../models/change.php";
class EuroTLXExchangeRateFetcherAgent extends BaseAgent
{
    public $name = 'eurotlx_exchange_rate_fetcher';
    public function get_priority($context)
    {
        return AgentPool::LOW;
    }
    public function run($context)
    {
        $changes = changes_eurotlx();
        $change = new Change();
        $change->data = date('Y-m-d H:i:s');
        foreach ($changes as $key => $val) {
            $change->{$key} = $val;
        }
        $change->_force_create = TRUE;
        $change->save();
        printf("%s saved currency change rates\n", get_called_class());
    }
}
AgentPool::register(new EuroTLXExchangeRateFetcherAgent());
            if (count($items) > 0) {
                foreach ($items as $item) {
                    $notification_count++;
                    if (!in_array($item->utente, $users)) {
                        $users[] = $item->utente;
                        $user_count++;
                    }
                    // print_r($item);
                }
            }
        }
        switch ($notification_count) {
            case 0:
                $str = "%s did not issue notifications to any user\n";
                break;
            case 1:
                $str = "%s issued 1 notification to 1 user\n";
                break;
            default:
                if ($user_count == 1) {
                    $str = "%s issued %d notifications to 1 user\n";
                } else {
                    $str = "%s issued %d notifications to %d users\n";
                }
                break;
        }
        printf($str, get_called_class(), $notification_count, $user_count);
    }
}
AgentPool::register(new WatchlistNotifierAgent());
                // printf("No valid bond found with isin %s\n", $stock->isin);
                continue;
            }
            $bond = $bonds[0];
            if ($bond->stacco != '0000-00-00') {
                $s = strtotime($bond->stacco);
                $k = 1;
            } else {
                $s = strtotime($bond->scadenza);
                $k = -1;
            }
            $d = $today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
            $c = 0;
            while ($d >= $today && $d <= $s) {
                $m = date('n', $s) + $c;
                $d = mktime(0, 0, 0, $m, date('d', $s), date('Y'));
                $cedola = new Cedola(array('isin' => $bond->isin, 'stacco' => date('Y-m-d', $d), 'tasso' => $bond->tasso));
                $cedola->_force_create = TRUE;
                $cedola->_ignore = TRUE;
                $cedola->save();
                $c += $k * $bond->cadenza;
                $yield_count++;
                // printf("Creating new yield of stock %s on %s\n", $stock->isin, $cedola->stacco);
            }
            $bond_count++;
        }
        printf("%s updated %d yields for %d bonds\n", get_called_class(), $yield_count, $bond_count);
    }
}
AgentPool::register(new BondYieldsUpdaterAgent());
            $bond_new->isin = $stock->isin;
            if (!empty($bond_new->scadenza) && $bond->scadenza != $bond_new->scadenza || !empty($bond_new->emissione) && $bond->emissione != $bond_new->emissione || !empty($bond_new->stacco) && $bond->stacco != $bond_new->stacco) {
                // printf("%s;%s;%s;%s;%s;%s\n",
                //	 $bond->isin,
                //	 $bond->emissione,
                //	 $bond->scadenza,
                //	 $bond->cadenza,
                //	 $bond->stacco,
                //	 $bond->tasso
                // );
                //
                // printf("%s;%s;%s;%s;%s;%s\n\n",
                //	 $bond_new->isin,
                //	 $bond_new->emissione,
                //	 $bond_new->scadenza,
                //	 $bond_new->cadenza,
                //	 $bond_new->stacco,
                //	 $bond_new->tasso
                // );
                // print_r($bond_new);
                $bond_new->_force_create = TRUE;
                $bond_new->_ignore = TRUE;
                $bond_new->save();
                $updated_count++;
            }
        }
        printf("%s updated %d bonds\n", get_called_class(), $updated_count);
    }
}
AgentPool::register(new EuroTLXBondUpdaterAgent());
Exemple #8
0
    $error->text = $errstr;
    $error->save();
}
class SchedulerContext
{
    public $stocks = array();
    public $preferences = NULL;
}
// set_error_handler('handle_errors', E_ALL | E_STRICT | E_NOTICE | ~E_DEPRECATED);
date_default_timezone_set('Europe/Rome');
echo "Reading preferences...";
$preference_factory = new Preference();
$preferences = $preference_factory->find_all()[0];
// print_r($preferences);
echo "done\n";
$stock_factory = new Stock();
$stocks = $stock_factory->find_all(array('limit' => 1500));
// print_r($stocks);
$context = new SchedulerContext();
$context->stocks = $stocks;
$context->preferences = $preferences;
if (isset($argv[1])) {
    $arg = $argv[1];
    if (is_numeric($arg)) {
        $context->level = $argv[1];
    } else {
        $context->agent_name = $arg;
    }
}
AgentPool::run_pool($context);
require_once dirname(__FILE__) . "/agent_pool.php";
require_once dirname(__FILE__) . "/base_agent.php";
require_once dirname(__FILE__) . "/../bin/lib.inc.php";
require_once dirname(__FILE__) . "/../models/bond.php";
require_once dirname(__FILE__) . "/../models/stock.php";
class ArivaBondUpdaterAgent extends BaseAgent
{
    public $name = 'ariva_bond_updater';
    public function get_priority($context)
    {
        return AgentPool::HIGH;
    }
    public function run($context)
    {
        $stock_factory = new Stock();
        $stocks = $stock_factory->find_by_query('SELECT * FROM `creso_titoli` ' . 'LEFT JOIN `creso_obbligazioni` ON `creso_titoli`.`isin` = `creso_obbligazioni`.`isin` ' . 'WHERE ' . "`creso_titoli`.`tipo` = 'obbligazione' " . 'AND ' . "`creso_obbligazioni`.`emissione` = '0000-00-00' " . 'AND ' . '`creso_titoli`.`attivo` = 1 ' . 'LIMIT 1500');
        $updated_count = 0;
        foreach ($stocks as $stock) {
            $bond = new Bond();
            $bond->find_by_id($stock->isin);
            $bond->emissione = isin2emissione_ariva($stock->isin);
            if ($bond->emissione != '0000-00-00') {
                $bond->save();
                $updated_count++;
            }
        }
        printf("%s updated %d bonds\n", get_called_class(), $updated_count);
    }
}
AgentPool::register(new ArivaBondUpdaterAgent());
    public function get_priority($context)
    {
        return AgentPool::LOW;
    }
    public function run($context)
    {
        $preferences = $context->preferences;
        $quote_count = 0;
        $stocks = $context->stocks;
        foreach ($stocks as $stock) {
            if (!$stock->attivo) {
                continue;
            }
            if ($stock->mercato != 'eurotlx' && $stock->mercato != 'tlx') {
                continue;
            }
            $quote = new Quote();
            $quote->isin = $stock->isin;
            $quote->quotazione = isin2value_tlx($quote->isin, $preferences->isin_lookup_eurotlx);
            $quote->data = date('Y-m-d');
            $quote->ora = date('H:i:s');
            if (is_numeric($quote->quotazione)) {
                $quote->save();
                $quote_count++;
            }
        }
        printf("%s fetched %d quotes\n", get_called_class(), $quote_count);
    }
}
AgentPool::register(new EuroTLXQuoteFetcherAgent());
                continue;
            }
            if ($bond->zero_coupon == 1) {
                // printf("bond %s is a zero coupon\n", $bond->isin);
                continue;
            }
            if ($bond->tasso == 0) {
                // printf("bond %s has tasso set to 0\n", $bond->isin);
                $bond_zero_tasso++;
            }
            if ($bond->emissione == '0000-00-00' && $bond->stacco == '0000-00-00') {
                printf("bond %s has emissione and stacco set to 0000-00-00\n", $bond->isin);
                $bond_zero_stacco_emissione++;
            } else {
                if ($bond->stacco == '0000-00-00') {
                    // printf("bond %s has stacco set to 0000-00-00\n", $bond->isin);
                    $bond_zero_stacco++;
                } else {
                    if ($bond->emissione == '0000-00-00') {
                        // printf("bond %s has emissione set to 0000-00-00\n", $bond->isin);
                        $bond_zero_emissione++;
                    }
                }
            }
            $bond_count++;
        }
        printf("%s analyzed %d bonds, %d had zero tasso, %d zero stacco and emissione, %d zero stacco, %d zero emissione\n", get_called_class(), $bond_count, $bond_zero_tasso, $bond_zero_stacco_emissione, $bond_zero_stacco, $bond_zero_emissione);
    }
}
AgentPool::register(new BondDoctorAgent());
    public function get_priority($context)
    {
        return AgentPool::LOW;
    }
    public function run($context)
    {
        $preferences = $context->preferences;
        $quote_count = 0;
        $stocks = $context->stocks;
        foreach ($stocks as $stock) {
            if (!$stock->attivo) {
                continue;
            }
            if ($stock->mercato != 'ita') {
                continue;
            }
            $quote = new Quote();
            $quote->isin = $stock->isin;
            $quote->quotazione = stock2value_ita($stock, $preferences->isin_lookup_ita);
            $quote->data = date('Y-m-d');
            $quote->ora = date('H:i:s');
            if (is_numeric($quote->quotazione)) {
                $quote->save();
                $quote_count++;
            }
        }
        printf("%s fetched %d quotes\n", get_called_class(), $quote_count);
    }
}
AgentPool::register(new BorsaItalianaQuoteFetcherAgent());
                        $stock_count++;
                    } else {
                        if ($stock->mercato == 'ita') {
                            echo "Updating mercato for {$stock->isin}";
                            $stock->mercato = 'eurotlx';
                            $stock->save();
                            $stock_count++;
                        }
                    }
                }
                if (!feof($handle)) {
                    echo "Error: unexpected fgets() fail\n";
                }
                fclose($handle);
            }
        }
        printf("%s updated %d stocks\n", get_called_class(), $stock_count);
    }
    private function unwrap($str)
    {
        if (strpos($str, '"') == 0) {
            $str = substr($str, 1);
        }
        if (strpos($str, '"') == strlen($str) - 1) {
            $str = substr($str, 0, strlen($str) - 1);
        }
        return $str;
    }
}
AgentPool::register(new EuroTLXStockFetcherAgent());