public function closeDay() { if ($cur_price = Stock::getCurPrice($this->sc_id)) { $this->day_close = $cur_price; $this->status = $this->day_open > $this->day_close ? ((double) $this->day_close / (double) $this->day_open - 1.0) * 100.0 : ((double) $this->day_close / (double) $this->day_open - 1.0) * 100.0; $database = new Database(); $query = "UPDATE " . self::$table; $query .= " SET day_close =" . $this->day_close . ", status=" . $this->status; $query .= " WHERE id=" . $this->id; echo $query; $run = $database->query($query); if ($run) { unset($database); return true; } else { Log("Couldnt execute StockHistory::closeday close manually for {$this->sc_id}", LOG_MANUAL); return false; } } else { Log("Couldnt execute StockHistory::closeday close manually for {$this->sc_id}", LOG_MANUAL); return false; } }
public function stockUpdate() { $stock_control = new StockControl($this->sc_id); $cur_price = Stock::getCurPrice($this->sc_id); if ($stock_control->getOverride() == -1) { $val = mt_rand(-abs($stock_control->low), -abs($stock_control->high)); $new_val = $cur_price + $val; /* * Check if over_value as been reached or not! if it has * then terminate the control function */ if ($stock_control->over_value >= $new_val) { $stock_control->setOverride(0); $stock_control->writeControl(); Stock::addToMarket($this->sc_id, $new_val); return true; } if ($this->threshold < $new_val) { Stock::addToMarket($this->sc_id, $new_val); return true; } else { /* * Reached ground value so turn overide off now! */ $stock_control->setOverride(0); $stock_control->writeControl(); $val = mt_rand(0, abs(2 * $stock_control->high)); $new_val = $cur_price + $val; Stock::addToMarket($this->sc_id, $new_val); return true; } } elseif ($stock_control->getOverride() == 1) { $val = mt_rand(abs($stock_control->low), abs($stock_control->high)); $new_val = $cur_price + $val; /* * Check if over_value as been reached or not! if it has * then terminate the control function */ if ($stock_control->over_value <= $new_val) { $stock_control->setOverride(0); $stock_control->writeControl(); Stock::addToMarket($this->sc_id, $new_val); return true; } Stock::addToMarket($this->sc_id, $new_val); return true; } else { $news = News::getNewsForStock($this->sc_id); if (is_array($news) && !empty($news)) { $new = array_shift($news); if ($new['p_change'] > 0) { if ($new['price_value'] > $cur_price) { $val_diff = $new['price_value'] - $cur_price; if ($val_diff > 400) { $reduce = (int) $val_diff / mt_rand(40, 80); $new_val = $cur_price + $reduce; Stock::addToMarket($this->sc_id, $new_val); return true; } elseif ($val_diff > 200) { $reduce = (int) $val_diff / mt_rand(20, 40); $new_val = $cur_price + $reduce; Stock::addToMarket($this->sc_id, $new_val); return true; } elseif ($val_diff > 100) { $reduce = (int) $val_diff / mt_rand(10, 20); $new_val = $cur_price + $reduce; Stock::addToMarket($this->sc_id, $new_val); return true; } else { $reduce = mt_rand(0, 3); $new_val = $cur_price + $reduce; Stock::addToMarket($this->sc_id, $new_val); return true; } } else { News::newsDone($new['id']); } } else { //Negative price changee in news if ($new['price_value'] < $cur_price) { $val_diff = $cur_price - $new['price_value']; if ($val_diff > 400) { $reduce = (int) $val_diff / mt_rand(40, 80); $new_val = $cur_price - $reduce; Stock::addToMarket($this->sc_id, $new_val); return true; } elseif ($val_diff > 200) { $reduce = (int) $val_diff / mt_rand(20, 40); $new_val = $cur_price - $reduce; Stock::addToMarket($this->sc_id, $new_val); return true; } elseif ($val_diff > 100) { $reduce = (int) $val_diff / mt_rand(10, 20); $new_val = $cur_price - $reduce; Stock::addToMarket($this->sc_id, $new_val); return true; } else { $reduce = mt_rand(0, 3); $new_val = $cur_price - $reduce; /* * If news tries to reduce the price lower than the thereshold price the news is completed automatically */ if ($this->threshold < $new_val) { Stock::addToMarket($this->sc_id, $new_val); return true; } else { $new_val = 2 * $this->threshold - $new_val; News::newsDone($new['id']); Stock::addToMarket($this->sc_id, $new_val); return true; } return true; } } else { News::newsDone($new['id']); } } } //endif (when news was there) /* * When no news is present and override is disabled * Two Options - 1)Do as per trading of stock 2) rand value */ $stock_company = new StockCompany($this->sc_id); $cur_bought_stocks = HolderStocks::getBoughtStocks($this->sc_id); $prev_bought_stocks = $stock_company->bought_stocks; //Update the stock buying for next iteration!! $stock_company->writeBoughtStocks(); //if 1) gives nothing! if ($cur_bought_stocks == $prev_bought_stocks) { $new_val = array(mt_rand(-1, 1), mt_rand(-3, 3), mt_rand(-2, 2), mt_rand(-5, 5), mt_rand(-4, 4), mt_rand(-5, 5), mt_rand(-2, 3), mt_rand(-3, 2), mt_rand(-2, 2), mt_rand(-6, 6)); $index = mt_rand(0, 9); $temp = $new_val[$index]; $new_val = $cur_price + $new_val[$index]; if ($new_val > $this->threshold) { Stock::addToMarket($this->sc_id, $new_val); return true; } else { $new_val = $cur_price + abs($temp); Stock::addToMarket($this->sc_id, $new_val); return true; } } //if choice between 1 and 2 then $choice = mt_rand(1, 100); if ($choice < 50) { $new_val = array(mt_rand(-1, 1), mt_rand(-3, 3), mt_rand(-2, 2), mt_rand(-5, 5), mt_rand(-4, 4), mt_rand(-5, 5), mt_rand(-2, 3), mt_rand(-3, 2), mt_rand(-2, 2), mt_rand(-6, 6)); $index = mt_rand(0, 9); $temp = $new_val[$index]; $new_val = $cur_price + $new_val[$index]; if ($new_val > $this->threshold) { Stock::addToMarket($this->sc_id, $new_val); return true; } else { $new_val = $cur_price + abs($temp); Stock::addToMarket($this->sc_id, $new_val); return true; } } else { if ($cur_bought_stocks > $prev_bought_stocks) { $new_val = $cur_price + mt_rand(1, 10); if ($new_val > $this->threshold) { Stock::addToMarket($this->sc_id, $new_val); return true; } } else { $new_val = $cur_price + mt_rand(-1, -10); if ($new_val > $this->threshold) { Stock::addToMarket($this->sc_id, $new_val); return true; } else { $new_val = $cur_price + mt_rand(1, 10); Stock::addToMarket($this->sc_id, $new_val); return true; } } } } //if there was no override settings }
public static function add($sc_id, $data, $p_change, $live_date) { global $database; $sc_id = (int) $sc_id; $data = $database->escape_value($data); $p_change = (int) $p_change; $date = Database::date(); $price_value = (double) (Stock::getCurPrice($sc_id) * (1.0 + (double) ((double) $p_change / (double) 100.0))); $query = "INSERT INTO " . self::$table . " (sc_id,data,date_uploaded,p_change,live_date,status,price_value,done)"; $query .= "\tVALUES({$sc_id},'" . $data . "'" . ",'{$date}',{$p_change},'{$live_date}',0,{$price_value},0);"; $run = $database->query($query); if ($run) { return true; } else { //TODO:When news update fails! return false; } }
<tr> <th>S.No</th> <th>Stock Name</th> <th>Average Price <i class="icon-money"></i></th> <th>Present Stock Price <i class="icon-money"></i></th> <th>Profit / Loss</th> <th>No. of Stocks</th> <th>Last Transaction</th> <th>Sell Stocks</th> </tr> </thead> <tbody> <?php //for($y = 0;$y< sizeof($stock_arr); $y++ ){ foreach ($holder_stocks->holder_stocks as $stock) { if ($stock['pur_price'] > Stock::getCurPrice($stock['sc_id'])) { $stat = "Loss"; $col = "green"; } else { $stat = "Profit"; $col = "blue"; } $s_no++; $company = new StockCompany($stock['sc_id']); $stock_obj = new Stock($stock['sc_id']); ?> <tr> <td><?php echo $s_no; ?> </td>
<?php include 'includes/header.inc.php'; ?> <!-- Content ================================================== --> <section id="content" class="container"> </section><!-- end .container --> <?php if ($session->is_logged_in()) { $sh_id = $session->sh_id; $holder = new StockHolder($sh_id); $holder_stocks = new HolderStocks($sh_id); $cur_amt = 0; foreach ($holder_stocks->holder_stocks as $stock) { $cur_amt += $stock['s_qty'] * Stock::getCurPrice($stock['sc_id']); } } else { header('Location: index.php'); exit; } ?> <section class="container"> <h1>Marketplace</h1> <div class="loader_market"> <center> <img class="loading-image" src="img/loader.gif" alt="loading.." height="70" width="70"> </center> </div> <div class="row"> <div class="span12">
* close all day_closings!! direct function in stockhistory * also update StockCompany - dayclose = cur price and dayopen = cur price as well! */ $histories = array(); $companies = array(); $controls = array(); $stocks = Stock::getAllStocks(); foreach ($stocks as $stock) { $histories[] = new StockHistory($stock->sc_id); $companies[] = new StockCompany($stock->sc_id); $controls[] = new StockControl($stock->sc_id); } foreach ($histories as $history) { $history->closeDay(); } foreach ($companies as $company) { $cur_val = Stock::getCurPrice($company->id); $company->updateDayClose($cur_val); $company->updateDayOpen($cur_val); } foreach ($controls as $control) { $control->setOverride(0); $control->writeControl(); } StockHolder::updateLeaderBoard(); /* * Make the settings reset once the script has ended * for next execution of the script so that stocks can be initialised first */ reset_setting(); }
public static function getAllStockPrices($sh_id = null) { $database = new Database(); $total_amount = 0; if ($sh_id) { $query = "SELECT sc_id,s_qty FROM " . self::$table; $query .= " WHERE sh_id = " . $sh_id; $holder_stocks_wrapper = $database->query($query); if ($holder_stocks_wrapper) { while ($holder_stock = $database->fetch_array($holder_stocks_wrapper)) { $total_amount = $holder_stock['s_qty'] * Stock::getCurPrice($holder_stock['sc_id']); } unset($database); return $total_amount; } else { unset($database); return 0; } } else { unset($database); return false; } }
public function nStatus() { $database = new Database(); $n_status = 0; $holder_stocks = new HolderStocks($this->sh_id); $cur_price = 0; $base_price = 0; $holder_stocks->holder_stocks; foreach ($holder_stocks->holder_stocks as $stock) { $cur_price = $cur_price + $stock['s_qty'] * Stock::getCurPrice($stock['sc_id']); $base_price += $stock['s_qty'] * $stock['pur_price']; } if ($cur_price > $base_price) { $n_status = (double) (double) $cur_price / (double) $base_price; $n_status = $n_status - 1.0; } elseif ($cur_price < $base_price) { $n_status = (double) (double) $cur_price / (double) $base_price; $n_status = 1.0 - $n_status; } else { $n_status = 0.0; } $n_status *= 100.0; if (isset($this->sh_id)) { $query = "UPDATE " . self::$table; $query .= " SET n_status = " . $n_status; $query .= " WHERE id=" . $this->sh_id; $run = $database->query($query); if ($run) { $this->n_status = $n_status; return $n_status; } else { return $n_status; } } }