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); }
public function run($context) { $bond_count = 0; $yield_count = 0; $bond_factory = new Bond(); $today = date('Y-m-d'); foreach ($context->stocks as $stock) { if ($stock->tipo != 'obbligazione') { // printf("%s is not a bond, skipping\n", $stock->isin); continue; } // $bonds = $bond_factory->find_all(array('where_clause' => "`stacco` != '0000-00-00' AND `cadenza` > 0 AND `scadenza` > '{$today}'")); $bond = new Bond(); if (!$bond->find_by_id($stock->isin)) { // printf("%s does not have a bond, skipping\n", $stock->isin); continue; } // Cleanup yields beyond expiration $cedola_factory = new Cedola(); $cedole = $cedola_factory->find_all(array('where_clause' => "`isin` = '{$bond->isin}' " . "AND `stacco` > '{$bond->scadenza}'")); if (count($cedole) > 0) { foreach ($cedole as $cedola) { // printf("Deleting yield of stock %s beyond expiration %s\n", $stock->isin, $cedola->stacco); $cedola->delete(); } } // $bonds = $bond_factory->find_all(array('where_clause' => "`stacco` != '0000-00-00' AND `cadenza` > 0 AND `scadenza` > '{$today}'")); $bonds = $bond_factory->find_all(array('where_clause' => '`cadenza` > 0 ' . "AND `isin` = '{$stock->isin}' " . "AND `zero_coupon` = 0 " . "AND `scadenza` > '{$today}'")); if (count($bonds) < 1) { // 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); }
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); }
public function run($context) { $bond_count = 0; $bond_zero_stacco = 0; $bond_zero_emissione = 0; $bond_zero_tasso = 0; $bond_zero_stacco_emissione = 0; $bond_factory = new Bond(); foreach ($context->stocks as $stock) { if ($stock->tipo != 'obbligazione') { // printf("%s is not a bond, skipping\n", $stock->isin); continue; } // $bonds = $bond_factory->find_all(array('where_clause' => "`stacco` != '0000-00-00' AND `cadenza` > 0 AND `scadenza` > '{$today}'")); $bond = new Bond(); if (!$bond->find_by_id($stock->isin)) { // printf("%s does not have a bond, skipping\n", $stock->isin); 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); }
public function timeline() { $isin = isset($_REQUEST['id']) ? $_REQUEST['id'] : $_REQUEST['isin']; $conn = $this->get_connection(); $transaction_factory = new Transaction(); $transaction = $transaction_factory->find_all(array('where_clause' => "`isin` = '{$conn->escape($isin)}'", 'order_by' => '`data` ASC', 'limit' => 1))[0]; if ($transaction) { $month_from = date('Y-m-01', strtotime($transaction->data)); } else { $month_from = date('Y-m-01'); } $bond = new Bond(); $bond->find_by_id($isin); // print_r($bond); $month_to = date('Y-m-d', mktime(0, 0, 0, date('m', strtotime($bond->scadenza)) + 1, 0, date('Y', strtotime($bond->scadenza)))); // FIXME: avoid redirects $this->redirect_to(array('action' => 'index', 'query_string' => QueryString::from_assoc(array('isin' => $isin, 'month-from' => $month_from, 'month-to' => $month_to)))); }
public static function yields($isin, $month_from, $month_to) { $payments = array(); $conn = Db::get_connection(); $stock = new Stock(); if (!$stock->find_by_id($isin)) { return; } switch ($stock->tipo) { case 'azione': $dividend_factory = new Dividend(); $dividend = $dividend_factory->find_all(array('where_clause' => "`isin` = '{$isin}'"))[0]; $payment = new Payment(); $payment->stock = $stock; $payment->dividend = $dividend; $payment->timestamp = strtotime($dividend->stacco); $payment->importo = $dividend->importo; $payment->tipo = 'dividendo'; $payments[] = $payment; break; case 'obbligazione': $bond = new Bond(); if (!$bond->find_by_id($isin)) { return; } if ($bond->zero_coupon == 1) { return; } if (!is_numeric($bond->cadenza)) { return; } if ($bond->tasso == 0) { return; } if ($bond->emissione == '0000-00-00' && $bond->stacco == '0000-00-00') { return; } if (!empty($bond->stacco)) { $stacco = strtotime($bond->stacco); // FIXME: bug, se emissione è vuoto, il prossimo stacco utile può avvenire in una data precedente if ($bond->emissione == '0000-00-00') { $stacco_precedente = $stacco; do { $stacco = $stacco_precedente; $stacco_precedente = mktime(0, 0, 0, date("m", $stacco) - $bond->cadenza, date("d", $stacco), date("Y", $stacco)); } while ($stacco_precedente >= strtotime($month_from)); } while ($stacco < strtotime($month_from)) { // Sommo i mesi di cadenza fino a giungere ad una data futura $stacco = mktime(0, 0, 0, date("m", $stacco) + $bond->cadenza, date("d", $stacco), date("Y", $stacco)); } if ($stacco > strtotime($bond->scadenza) || $stacco > strtotime($month_to)) { break; } do { $payment = new Payment(); $payment->stock = $stock; $payment->bond = $bond; $payment->timestamp = $stacco; $payment->importo = $bond->tasso / (100 * $bond->cedole_per_anno()); $payment->tipo = 'cedola'; $payments[] = $payment; $stacco = mktime(0, 0, 0, date("m", $stacco) + $bond->cadenza, date("d", $stacco), date("Y", $stacco)); } while ($stacco <= strtotime($bond->scadenza) && $stacco <= strtotime($month_to)); } break; } Db::close_connection($conn); return $payments; }
/** * @fn edit * @short Edits an instrument * @details Action to edit details of an instrument, handles both the display of * the instrument to edit, as well as the postback action to save the model. */ public function edit() { if ($this->request->is_post()) { $this->stock = new Stock($_POST); $this->stock->save(); if ($this->stock->tipo == 'obbligazione') { $bond = new Bond(); $this->bond = new Bond($_POST); if (!$bond->find_by_id($this->stock->isin)) { $this->bond->_force_create = TRUE; } $this->bond->save(); } $this->expire_cached_page(array('controller' => 'json', 'action' => 'titoli')); $this->redirect_to(array('action' => 'browse', 'id' => $this->stock->isin)); } else { $this->stock = new Stock(); if (!$this->stock->find_by_id($_GET['id'])) { HTTP::error(404); } if ($this->stock->tipo == 'obbligazione') { $this->stock->has_one('bond'); } } }
public function run($context) { // printf("ISIN;Emissione;Scadenza;Cadenza;Stacco;Tasso\n"); $updated_count = 0; foreach ($context->stocks as $stock) { if ($stock->tipo != 'obbligazione' || !$stock->attivo) { continue; } // print_r($stock); $bond = new Bond(); if ($bond->find_by_id($stock->isin)) { if ($bond->scadenza != EPOCH_ZERO && $bond->emissione != EPOCH_ZERO && $bond->stacco != EPOCH_ZERO) { continue; } } // printf("%s;%s;%s;%s;%s;%s\n", // $bond->isin, // $bond->emissione, // $bond->scadenza, // $bond->cadenza, // $bond->stacco, // $bond->tasso // ); switch ($stock->mercato) { case "eurotlx": case "tlx": $params = isin2bond_tlx($stock->isin, $context->preferences->isin_lookup_eurotlx); break; } if ($params === FALSE) { continue; } $bond_new = new Bond($params); $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); }
<?php $bond = new Bond(); $bond->find_by_id($this->stock->isin); ?> <tr class="row1"> <th></th> <th>Evento</th> <th>Data</th> <th>Importo lordo</th> <th>Tasso</th> </tr> <tr class="row0"> <td class="cell10"><div class="vstrut17"></div></td> <td class="cell10">Emissione</td> <td class="cell10"><b><?php print $bond->emissione(); ?> </b></td> <td class="cell10"></td> <td class="cell10"></td> </tr> <tr class="row1"> <td class="cell10"><div class="vstrut17"></div></td> <td class="cell10">Scadenza</td> <td class="cell10"><b><?php print $bond->scadenza(); ?> </b></td>
<?php ini_set('max_execution_time', 3000); require_once dirname(__FILE__) . "/lib.inc.php"; require_once dirname(__FILE__) . "/../include/db.inc.php"; require_once dirname(__FILE__) . "/../include/" . DB_ADAPTER . "_adapter.php"; require_once dirname(__FILE__) . "/../models/preference.php"; require_once dirname(__FILE__) . "/../models/bond.php"; require_once dirname(__FILE__) . "/../models/stock.php"; 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_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'); printf("%d bonds found\n", count($stocks)); $updated_count = 0; foreach ($stocks as $stock) { // print_r($stock); $bond = new Bond(); $bond->find_by_id($stock->isin); // print_r($bond); $bond->emissione = isin2emissione_ariva($stock->isin); print_r($bond); if ($bond->emissione != '0000-00-00') { $bond->save(); $updated_count++; } } printf("%d updated bonds\n", $updated_count);