Location: PHPKode > projects > Uniform Price Markets > UPmarkets-0.1.0/plot_active_quotes.php
<?php

// (C) 2000 LinuxFutures.com, Inc.
// 
// Part of UPmarkets - the software behind the LinuxFutures game site
//                     visit www.LinuxFutures.com to play
//
//  We are making the software available to you under the GNU General Public License,
//  a copy of which should accompany this software in the file LICENSE.
//
// A web-based copy of the LICENSE is located at http://www.gnu.org/copyleft/gpl.txt
//
// While there is no charge for using this software, there are important restrictions.
//
// This LICENSE does not allow, in general, proprietary modifications or proprietary derivative works.
//
// This software carries ABSOLUTELY NO WARRANTY. It is offered AS-IS, and may not work at all
//  or may have harmful or hazardous side effects.
//
// 
// -------------------- what this file does ---------------------
// plots a market's bid and ask quote curves like a demand and supply from textbook economics
// 


require('inc.d/db_connect_s.inc');
require('inc.d/PlotSteps.inc');
require('inc.d/f_economy_Q_demanded.inc');
require('inc.d/f_economy_Q_supplied.inc');
require('inc.d/f_market_array.inc');

$symbolid = (int) $HTTP_GET_VARS["symbolid"];

if ($symbolid <= 0){ exit(); }

// our y axis limits are just price floor and price ceiling from the market arrary
// don't need a lock to look in here

$market = market_array($symbolid);

// find out what our x axis limits should be

// need a lock to make sure quotes dont change on us while we're graphing

// a LOCK READ lock only blocks LOCK write locks, not INSERT/UPDATE itself. 
// Whoever writes to quotes should LOCK write.

mysql_query("LOCK TABLES quotes READ;",$db_link);
mysql_error_sanity("plot_active_quotes LOCK ");

$qmax_buy  = economy_Q_demanded($symbolid,$market["price_floor"]);
$qmax_sell = economy_Q_supplied($symbolid,$market["price_ceiling"]);

if ($qmax_buy[1] > $qmax_sell[1]) { $qmax = $qmax_buy[1]; } else { $qmax = $qmax_sell[1]; }

if ($qmax > 0){ 

//

$plot = new PlotSteps(	0, // x_low
			$qmax, // x_high
			$market["price_floor"], // y_low
			$market["price_ceiling"], // y_high
			300,  // img_W
			200, // img_H
			"Quantity axis",
			"Price axis"
		  );

// ok the plot area is setup. now grab the data to plot and plot it

// colors .. hmm .. change if you like among red,green,blue,black
$d_color = "blue";
$s_color = "red";

$sym_sql = " WHERE active=1 AND symbolid=".$symbolid." ";
$buy_sql  = "SELECT buy_price, quantity, userid from quotes  ".$sym_sql." ORDER BY buy_price DESC, posted ASC ;";
$sell_sql = "SELECT sell_price, quantity, userid  from quotes ".$sym_sql." ORDER BY sell_price ASC, posted ASC ;";


$r_buy = mysql_query($buy_sql,$db_link);
mysql_error_sanity("plot_active_quotes.php buy_sql ");
$number_of_rows = mysql_num_rows($r_buy);

for($i = 0, $qsum=0, $lastP = $market["price_ceiling"]; $i < $number_of_rows; $i++){ 
	
	$buy_quote = mysql_fetch_array($r_buy);
	
	// plot a vertical step, then a horizontal one

	$plot->vline($qsum,$lastP,$buy_quote["buy_price"],$d_color);

	$plot->hline($qsum,$qsum+$buy_quote["quantity"],$buy_quote["buy_price"],$d_color);

	// setup for the next step			
	$qsum  += $buy_quote["quantity"];
	$lastP  = $buy_quote["buy_price"];
}

// add a vline to the price floor

$plot->vline($qsum,$lastP,$market["price_floor"],$d_color);


mysql_free_result($r_buy);

// now graph the supply side

$r_sell = mysql_query($sell_sql,$db_link);
mysql_error_sanity("plot_active_quotes.php sell_sql ");
$number_of_rows = mysql_num_rows($r_sell);

for($i = 0, $qsum=0, $lastP = $market["price_floor"]; $i < $number_of_rows; $i++){ 
	
	$sell_quote = mysql_fetch_array($r_sell);
	
	// plot a vertical step, then a horizontal one

	$plot->vline($qsum,$lastP,$sell_quote["sell_price"],$s_color);

	$plot->hline($qsum,$qsum+$sell_quote["quantity"],$sell_quote["sell_price"],$s_color);

	// setup for the next step			
	$qsum  += $sell_quote["quantity"];
	$lastP  = $sell_quote["sell_price"];
}

// add a vline to the price ceiling

$plot->vline($qsum,$lastP,$market["price_ceiling"],$s_color);

mysql_free_result($r_sell);

} // matches if qmax>0

// we are done, unlock the table

mysql_query("UNLOCK TABLES;", $db_link);


if ($qmax > 0){ $plot->browser(); }




Return current item: Uniform Price Markets