/*	system_rank.js
	011409 hac		v1.0

	Processes a form containg the data to produce a system rank
	and returns the corresponding google-meter representing the rank

*/


//encode chart url
function encodecharturl(rank, min, max, index){
	var chartstr	= "";
	var chs, chd, chds, cht, chtt, chco, chdl, chls	="";
	var chxt, chxl, chg, chm	= "";
	var dx, dy = 0;
	
	dx 			= Math.log(rank)/2.303;			// scale for meter
	if (dx < -1) dx = -1;
	if (dx > 1) dx = 1;
	
	rank		= rank.toPrecision(2);		// one decimal place for label
	
	chs			= "chs=300x200";
	
    chd 		= "chd=t:" + dx.toString();
    
    chds 		= "chds=" + min.toString() + "," + max.toString();

    cht			= "cht=gom";
    
    chtt		= "chtt=System+Rank";
    
   	chco		= "chco=ff0000,ffff00,adff2f,32cd32"; 				// colors
   	
   	chl			= "chl=" + rank.toString();		// label
    
    
    chartstr 	= "http://chart.apis.google.com/chart?" 
    				+ chs + "&amp;" 
    				+ chd + "&amp;" 
    				+ chds + "&amp;"
    				+ chco + "&amp;" 
    				+ cht + "&amp;" 
    				+ chtt + "&amp;"
    				+ chl;
    
	return chartstr;
}


//returns the url to fetch the chart from google
function getcharturl(rank, min, max){
	var chartstr = "";
	
    
    //encode url from the median series for plotting
    chartstr = encodecharturl(rank, min, max, 2);
    
    /*if (window.console) {
			window.console.log("url: " + chartstr);
	}*/
    
    return chartstr;
}


function systemrank (form) {
	var avgmo		= new Number(form.avgmo.value);
	var avgtrade 	= new Number(form.avgtrade.value);	// avg trade
	var c			= 2;							// const
	//var llt			= new Number(form.llt.value);		// llt
	//var margin		= new Number(form.margin.value);	// margin
	var n			= new Number(form.n.value);			// no trades
	var p			= new Number(form.p.value);			// prob of win
	var rfr			= .036/12;				// risk free monthly return
	var sd			= new Number(form.sd.value);		// sd of trades
	var sdmo		= new Number(form.sdmo.value);		// sd of monthly 
	var wl			= new Number(form.wl.value);		// w/l ratio

    var f			= 0;					// optimal f
    var fdollar		= 0;					// optimal f$
    var sharpe		= 0;					// sharpe ratio monthly
    var z			= 0;					// z score
    
    var max			= new Number(1);					// max chart value
    var min			= new Number(-1);					// min chart value
    var t			= "";
    
    
	// calc z
	z = Math.sqrt(n) * avgtrade/sd;
	if (z > 10) z = 10;						//throttle large z
	
	// calc sharpe
	if (sdmo > 0) 
		sharpe = (avgmo - rfr)/sdmo
	else
		sharpe = 0;
	
	// calc f$
	if (wl > 0) 
		f = (((wl+1)*p)-1)/wl	//error check f == 0
	else
		f = 0;
			
	/*if (f != 0) 
		fdollar = Math.abs(llt)/f
	else
		fdollar = 0;*/
	
	// calc rank
	// 01/15/09 changed const to margin as normalizing var
	rank = z * sharpe * f * c;	
	/*if (fdollar != 0) 
		rank = (z * sharpe)/fdollar * margin			// great intermarket measure
	else
		rank = 0; */
	
	if (rank <= 0) rank = .1
	if (rank > 10) rank = 10;
    
    // build out the url to get a chart from google
	charturl = getcharturl(rank, min, max);
	
	// display the chart
	t = "<img src=" + charturl + " />"
			+ "<br /> <b>&nbsp &nbsp &nbsp &nbsp &nbsp Worse &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Better</b>"	
			+ "<br /><br />";
			
	document.getElementById("chart").innerHTML = t;
    
}


