public function __construct($sc_id = null) { if ($sc_id) { if (StockCompany::isValid($sc_id)) { $this->sc_id = $sc_id; $this->getDetails(); $this->found = true; } else { $this->sc_id = $sc_id; } } }
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 }
<?php require_once '..' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'bootstrap.config.php'; if ($_GET['company']) { $company_id = (int) $_GET['company']; if (!empty($company_id)) { $stock = new Stock($company_id); if ($stock->isFound()) { $output = "['Time', 'Stock Price'],[0,0],"; $i = 1; foreach ($stock->priceChanges as $price) { $output .= "[" . $i++ . " , " . $price[0] . "],"; } $company = new StockCompany($company_id); $cur_val = Stock::getCurPrice($company->id); if ($cur_val > $company->day_open) { $color = "green"; $direction = "up"; } else { $color = "red"; $direction = "down"; } } else { header('Location: marketplace.php'); } } else { header('Location: marketplace.php'); } } else { header('Location: marketplace.php'); }
} ?> <table class="stacked"> <thead> <tr> <th>S.No</th> <th>Company Name</th> <th>Stock value <i class="icon-money"></i></th> <th>Trend</th> <th>No. of Stocks</th> <th>Buy Stocks</th> </tr> </thead> <tbody> <?php $stocks_id = StockCompany::getStocksID(); $s_no = 1; ?> <?php foreach ($stocks_id as $sc_id) { $stock = new Stock($sc_id); $company = new StockCompany($sc_id); if ($company->day_open > $stock->cur_amt) { $color = 'red'; $direction = 'down'; } else { $color = 'green'; $direction = 'up'; } ?>
<?php require '..' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'bootstrap.config.php'; $company_name = "Company A"; $company_info = "This is a new brand made public recently"; $initial_stock_price = 2000; $threshold_stock_value = 20; $total_stocks = 100000000; StockCompany::add_new($company_name, $company_info, $initial_stock_price, $threshold_stock_value, $total_stocks);