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 function leftStocks() { $b_stk = HolderStocks::getBoughtStocks($this->id) ? HolderStocks::getBoughtStocks($this->id) : 0; $this->bought_stocks = $b_stk; return $this->total_stocks - $b_stk; }