public static function add_new($c_name, $info, $ini_s_price, $thres_stock_val = 20, $total_stocks = 1000000) { $database = new Database(); $c_name = $database->escape_value($c_name); $info = $database->escape_value($info); $ini_s_price = $database->escape_value($ini_s_price); $thres_stock_val = (int) $thres_stock_val; $total_stocks = (int) $total_stocks; $query = "INSERT INTO " . self::$table . " (c_name,info,ini_s_price,thres_stock_val,total_stocks,day_open_price,prev_day_close_price,bought_stocks) "; $query .= " VALUES ('{$c_name}','{$info}',{$ini_s_price},{$thres_stock_val},{$total_stocks},{$ini_s_price},0,0)"; $insert = $database->query($query); if ($insert) { $query = "SELECT id FROM " . self::$table; $query .= " WHERE c_name = '" . $c_name . "' LIMIT 1"; $find = $database->query($query); if ($find) { $sc_id = $database->fetch_array($find); $sc_id = $sc_id['id']; if (Stock::addToMarket($sc_id, $ini_s_price)) { if (StockHistory::add($sc_id, $ini_s_price)) { if (StockControl::add($sc_id)) { return true; } else { Log::add("Failed to add to StockControl - {$sc_id}", LOG_MANUAL); return false; } } else { Log::add("Failed to add to StockHistory,StockControl - {$sc_id}", LOG_MANUAL); return false; } } else { Log::add("Failed to add to StockMarket,StockHistory,StockControl - {$sc_id}", LOG_MANUAL); return false; } } else { Log::add("Failed to find new stockCompany record (add in StockMarket,StockHistory,StockControl)-{$sc_id} ", LOG_MANUAL); } } else { 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 }