Beispiel #1
0
 public static function roi_with_timeline($isin, $username, $month_from, $month_to)
 {
     $conn = Db::get_connection();
     $stock = new Stock();
     if (!$stock->find_by_id($isin)) {
         return;
     }
     $portfolio_stock_factory = new PortfolioStock();
     $portfolio_stocks = $portfolio_stock_factory->find_all(array('where_clause' => "`isin` = '{$conn->escape($isin)}' " . "AND `utente` = '{$conn->escape($username)}'"));
     if (count($portfolio_stocks) == 0) {
         return;
     }
     $portfolio_stock = $portfolio_stocks[0];
     $quantita = self::quantita_history($isin, $username, $month_from);
     switch ($stock->tipo) {
         case 'obbligazione':
             $investito = $portfolio_stock->prezzo * $portfolio_stock->quantita / 100;
             break;
         default:
             $investito = $portfolio_stock->prezzo * $portfolio_stock->quantita;
     }
     $investito = Change::convert($investito, $stock->divisa);
     $roi = PianificatoreHelper::roi_with_timeline($isin, $quantita, $month_from, $month_to, $investito);
     foreach ($roi->payments as $payment) {
         $payment->portfolio_stock = $portfolio_stock;
     }
     // print_r($roi);
     return $roi;
 }
 public function run($context)
 {
     $stock_count = 0;
     foreach (self::$URLS as $url) {
         $handle = fopen($url, 'r');
         if ($handle) {
             $i = 0;
             while (($buffer = fgets($handle, 4096)) !== false) {
                 if ($i++ == 0) {
                     // Skip header
                     // ISIN;Tipologia;Emittente;Descrizione;"Data di scadenza";"Valuta di negoziazione";S&P;"Q.tà Acquisto";"Prezzo Acquisto";"Prezzo Vendita";"Q.tà Vendita";"Prezzo ultimo contratto";"Rendimento effettivo a scadenza lordo";Sospeso
                     continue;
                 }
                 $details = explode(';', $buffer);
                 // print_r($details);
                 $stock = new Stock();
                 if (!$stock->find_by_id($details[0])) {
                     echo 'New stock';
                     $stock->isin = $details[0];
                     $stock->tipo = 'obbligazione';
                     $stock->title = $this->unwrap($details[3]);
                     $stock->divisa = $details[5];
                     $stock->rating = $this->unwrap($details[6]);
                     $stock->mercato = 'eurotlx';
                     $stock->_force_create = TRUE;
                     print_r($stock);
                     $stock->save();
                     $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);
 }
 public static function timeline($isin, $quantita, $month_from, $month_to)
 {
     $payments = array();
     $conn = Db::get_connection();
     $stock = new Stock();
     if (!$stock->find_by_id($isin)) {
         return;
     }
     $yields = PaymentsHelper::yields($isin, $month_from, $month_to);
     $refunds = PaymentsHelper::refunds($isin, $month_from, $month_to);
     $events = array_merge($yields, $refunds);
     $payments = array();
     foreach ($events as $event) {
         if (is_array($quantita)) {
             // reset() rewinds array's internal pointer to the first element and returns the value of the first array element, or FALSE if the array is empty.
             $q = reset($quantita);
             foreach ($quantita as $date => $value) {
                 if (strtotime($date) < $event->timestamp) {
                     break;
                 }
                 $q = $value;
             }
         } else {
             $q = $quantita;
         }
         if ($event->tipo == 'rimborso') {
             switch ($event->stock->tipo) {
                 case 'obbligazione':
                     $event->importo *= $q / 100;
                     break;
                 default:
                     $event->importo *= $q;
             }
         } else {
             $event->importo *= $q;
         }
         $event->quantita = $q;
         $payments[] = $event;
     }
     // print_r($payments);
     Db::close_connection($conn);
     return $payments;
 }
Beispiel #4
0
    return $str;
}
if (isset($argv[1])) {
    $handle = fopen($argv[1], 'r');
    if ($handle) {
        $i = 0;
        while (($buffer = fgets($handle, 4096)) !== false) {
            if ($i++ == 0) {
                // Skip header
                // ISIN;Tipologia;Emittente;Descrizione;"Data di scadenza";"Valuta di negoziazione";S&P;"Q.tà Acquisto";"Prezzo Acquisto";"Prezzo Vendita";"Q.tà Vendita";"Prezzo ultimo contratto";"Rendimento effettivo a scadenza lordo";Sospeso
                continue;
            }
            $details = explode(';', $buffer);
            // print_r($details);
            $stock = new Stock();
            if (!$stock->find_by_id($details[0])) {
                echo 'New stock';
                $stock->isin = $details[0];
                $stock->tipo = 'obbligazione';
                $stock->title = unwrap($details[3]);
                $stock->divisa = $details[5];
                $stock->rating = unwrap($details[6]);
                $stock->mercato = 'eurotlx';
                $stock->_force_create = TRUE;
                print_r($stock);
                $stock->save();
            } else {
                if ($stock->mercato == 'ita') {
                    echo "Updating mercato for {$stock->isin}";
                    $stock->mercato = 'eurotlx';
                    $stock->save();
Beispiel #5
0
 public function sell()
 {
     //Add logic to make sure stock isn't already sold
     //i.e.
     $this->load->model(array('portfolio', 'stock'));
     $id = $this->input->post('trade_id');
     $stock = Stock::find_by_id($id);
     $stock->sale_time = date("Y-m-d H:i:s");
     $stock->sale_price = $this->input->post('current_val');
     $stock->save();
     $portfolio = Portfolio::find_by_id($this->input->post('portfolio_id'));
     if ($portfolio->commision_bool == 1) {
         $portfolio->current_cap = $portfolio->current_cap + $stock->sale_price * $stock->shares - $portfolio->commision;
     } else {
         $portfolio->current_cap = $portfolio->current_cap + $stock->sale_price * $stock->shares;
     }
     $portfolio->last_trade = $stock->id;
     $portfolio->save();
     redirect('portfolios/view/' . $stock->portfolio_id);
 }
 private function get_rimborsi()
 {
     $payments = array();
     $conn = $this->get_connection();
     $portfolio_stock_factory = new PortfolioStock();
     $portfolio_stocks = $portfolio_stock_factory->find_by_query('SELECT `creso_portfolio`.`id`, `creso_portfolio`.`quantita`, `creso_portfolio`.`isin`, `creso_titoli`.`tipo`, `creso_titoli`.`title` ' . 'FROM `creso_portfolio` ' . 'LEFT JOIN `creso_titoli` ON `creso_portfolio`.`isin` = `creso_titoli`.`isin` ' . "WHERE `utente` = '{$conn->escape($_COOKIE['username'])}' " . (!empty($_REQUEST['mercato']) ? "AND `mercato` = '{$conn->escape($_REQUEST['mercato'])}' " : '') . (!empty($_REQUEST['tipo']) ? "AND `tipo` = '{$conn->escape($_REQUEST['tipo'])}' " : '') . (!empty($_REQUEST['divisa']) ? "AND `divisa` = '{$conn->escape($_REQUEST['divisa'])}' " : '') . (!empty($_REQUEST['isin']) ? "AND `creso_portfolio`.`isin` = '{$conn->escape($_REQUEST['isin'])}' " : '') . 'ORDER BY ' . sprintf('`%s` %s ', $this->get_sort('isin'), $this->get_dir()) . "LIMIT {$conn->escape($this->get_start())}, {$conn->escape($this->get_limit())}");
     if (count($portfolio_stocks) == 0 && !empty($_REQUEST['isin'])) {
         $portfolio_stocks = $this->synthesize_portfolio_stocks();
     }
     if (count($portfolio_stocks) > 0) {
         foreach ($portfolio_stocks as $portfolio_stock) {
             $stock = new Stock();
             $stock->find_by_id($portfolio_stock->isin);
             if ($this->historiful) {
                 $quantita_history = $this->backfill_with_historical_data($portfolio_stock);
             }
             switch ($stock->tipo) {
                 case 'azione':
                     break;
                 case 'obbligazione':
                     $bond_factory = new Bond();
                     $bonds = $bond_factory->find_all(array('where_clause' => "`isin` = '{$portfolio_stock->isin}' " . "AND `scadenza` >= '{$conn->escape($_REQUEST['month-from'])}' " . "AND `scadenza` <= '{$conn->escape($_REQUEST['month-to'])}' "));
                     if (count($bonds) > 0) {
                         $bond = $bonds[0];
                         $payment = new Payment();
                         $q = $portfolio_stock->quantita;
                         foreach ($quantita_history as $date => $quantita) {
                             if ($date < $bond->scadenza) {
                                 break;
                             }
                             $q = $quantita;
                         }
                         $payment->stock = $stock;
                         $payment->portfolio_stock = $portfolio_stock;
                         $payment->bond = $bond;
                         $payment->timestamp = strtotime($bond->scadenza);
                         $payment->importo = $payment->bond->prezzo_rimborso * $q / 100;
                         $payment->quantita = $q;
                         $payment->tipo = 'rimborso';
                         $payments[] = $payment;
                     }
                     break;
             }
         }
     }
     return $payments;
 }
Beispiel #7
0
 public static function refunds($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':
             break;
         case 'obbligazione':
             $bond_factory = new Bond();
             $bonds = $bond_factory->find_all(array('where_clause' => "`isin` = '{$conn->escape($isin)}' " . "AND `scadenza` >= '{$conn->escape($month_from)}' " . "AND `scadenza` <= '{$conn->escape($month_to)}' "));
             if (count($bonds) > 0) {
                 $bond = $bonds[0];
                 $payment = new Payment();
                 $payment->stock = $stock;
                 $payment->bond = $bond;
                 $payment->timestamp = strtotime($bond->scadenza);
                 $payment->importo = $payment->bond->prezzo_rimborso;
                 $payment->tipo = 'rimborso';
                 $payments[] = $payment;
             }
             break;
     }
     return $payments;
 }
 private function get_roi()
 {
     if (!isset($_REQUEST['base'])) {
         $_REQUEST['base'] = 1000;
     }
     if (!isset($_REQUEST['month-to'])) {
         $_REQUEST['month-to'] = date('Y-m-d', mktime(0, 0, 0, date('m') + self::DEFAULT_ROI_HORIZON_MONTHS, 0, date('Y')));
     }
     $this->roi = array();
     // $this->stocks = array();
     // This to ensure we get latest quote from today
     $today = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')));
     $last_month = date('Y-m-d', mktime(0, 0, 0, date('m') - 1, date('d'), date('Y')));
     // print_r($_REQUEST['isin']);
     foreach ($this->isins as $isin) {
         $quote = QuoteHelper::latest_for_range($isin, $last_month, $today);
         // print_r($quote);
         $stock = new Stock();
         if (!$stock->find_by_id($isin)) {
             continue;
         }
         // $this->stocks[$isin] = $stock;
         $investito = Change::convert($quote->quotazione * $_REQUEST['base'] / 100, $stock->divisa);
         $this->roi[$isin] = PianificatoreHelper::roi_with_timeline($isin, $_REQUEST['base'], $today, $_REQUEST['month-to'], $investito);
         unset($this->roi[$isin]->payments);
         $this->roi[$isin]->stock = $stock;
     }
     // print_r($this->roi);
     uasort($this->roi, $this->get_sorter());
 }
Beispiel #9
0
$this->stock->plusminus_per_year = $this->roi[$this->id]->plusminus_per_year;
$this->stock->plusminus_percent_per_year = $this->roi[$this->id]->plusminus_percent_per_year;
$this->render(array('partial' => 'stock', 'object' => $this->stock));
?>

	<tr class="rownull rowfirst">
		<td colspan="12">
			<div class="vstrut17"></div>
		</td>
	</tr>
<?php 
$this->render(array('partial' => 'reinvest_header'));
foreach ($this->roi as $isin => $ignored) {
    if ($isin == $_REQUEST['isin']) {
        continue;
    }
    $bond = $this->sorted_bonds[$isin];
    $id = $isin;
    $stock = new Stock();
    if ($stock->find_by_id($id)) {
        $stock->bond = $bond;
        $stock->plusminus_per_year = $this->roi[$id]->plusminus_per_year;
        $stock->plusminus_percent_per_year = $this->roi[$id]->plusminus_percent_per_year;
        $this->render(array('partial' => 'stock', 'object' => $stock));
    }
}
?>

</table>
</form>