Finance-Quant

 view release on metacpan or  search on metacpan

lib/Finance/Quant.pm  view on Meta::CPAN

								$iu++;
						}
    }
    $xguru .="</ul><h3>check:gurus nasdaq</h3><ul>";
		foreach my $gu (@guru){
		    $xguru .= sprintf("<li>raiting:[%s]\t\t%s</li>",$gu->{pct},$gu->{methode});
		}
		#	}
					$str = "<html><head><title>$stock</title></head>".
									"<body bgcolor=\"#00000\" text=\"ffffff\">".
									"<div style='float:left;padding:20;'>".$xguru."</div>".
									"<div style='float:right;'>\n";

		$str .= "<p><img src=\"data:image/png;base64," . plotlog($stock, $q, $ma) . "\">\n<br />";
		$str .= "<img src=\"data:image/png;base64," . plotdiff($stock, $q, $ma, $diff) . "\"></p>\n";
		#$str .=  _tbl($stock, $q);
		$str .= "</div></body></html>\n";


		$str .=  _tbl($stock, $q);
		$str .= "</center></body></html>\n";
		return $str;
	}
	
	sub plotlog {
		my ($stock, $q, $diff) = @_;
		my $img = $stock . "log.jpg";
		print "generating $img...\n";
		my ($s, $lines) = ([],[]);
		my $y_format = sub { sprintf " \$%.2f", exp $_[0] };
		
		$s = ts::logs($q->{Close});
		$lines->[0] = {	name => 'Log of Closing Price', color => 'marine', data => $s };
		$lines->[1] = {	name => "MA($diff) (Moving Avg)", color => 'cyan', data => ts::ma($lines->[0]->{data}, $diff) };
		
		return plotlines($img, $stock, $q->{Date}, $lines, $y_format);
		
	}

	sub plotdiff {
		my ($stock, $q, $lag, $diff) = @_;
		my $img = $stock . "diff.jpg";
		print "generating $img...\n";
		my ($s, $lines) = ([],[]);
		my $y_format = sub { sprintf "  %.2f", $_[0] };

		$s = ts::logs($q->{Close});
		$lines->[0] = {	name => "Diff($diff)", color => 'marine', data => ts::diff($s, $diff) };
		$lines->[1] = {	name => "MA($lag) (Moving Avg)", color => 'cyan', data => ts::ma($lines->[0]->{data}, $lag) };
		$s = ts::stdev($lines->[0]->{data}, $lag);
		$s = ts::nstdev_ma($s, $lines->[1]->{data}, 2);
		$lines->[2] = {	name => 'MA + 2 Std Dev', color => 'lred', data => $s->[0] };
		$lines->[3] = {	name => 'MA - 2 Std Dev', color => 'lred', data => $s->[1] };
		
		return plotlines($img, $stock, $q->{Date}, $lines, $y_format);

	}
	
	sub plotlines {
		my ($file, $stock, $x, $lines, $y_format) = @_;
		my @legend;
		my ($data, $colors) = ([], []);
		
		$data->[0] = $x;   # x-axis labels
	
		for (0..$#{$lines}) {
			$data->[(1+$_)] = $lines->[$_]->{data};
			$colors->[$_] = $lines->[$_]->{color};
			$legend[$_] = $lines->[$_]->{name};
		}
	
		my $graph = GD::Graph::lines->new(740,420);
		$graph->set (dclrs => $colors) or warn $graph->error;
		$graph->set_legend(@legend) or warn $graph->error;
		$graph->set (legend_placement => 'BC') or warn $graph->error;
		$graph->set(y_number_format => $y_format) if $y_format;
		$graph->set (
			title => "stock: $stock",
			boxclr => 'black',
			bgclr => 'dgray',
			axislabelclr => 'white',
			legendclr => 'white',
			textclr => 'white',
			r_margin => 20,
			tick_length => -4,
			y_long_ticks => 1,
			axis_space => 10,
			x_labels_vertical => 1,
			x_label_skip => int(0.2*scalar(@{$data->[0]}))
		) or warn $graph->error;	
		my $gd = $graph->plot($data) or warn $graph->error;

	  if(defined($gd)){
      my $png = $gd->png();	    
	    return  encode_base64($png);   
	     
	  }else{
	  
	    return  ""; 
	  
	  }
	  
	  
   

	}
	
	
				sub meanx {
		my ($stock, $q, $lag, $diff) = @_;
		my $img = $stock . "diff.jpg";
		my ($s, $lines) = ([],[]);
		my $y_format = sub { sprintf "  %.2f", $_[0] };
		$s = ts::logs($q->{Close});
		my $diffx = ts::diff($s, $diff);
		$lines->[0] = {	name => "Diff($diff)", color => 'marine', data => $diffx };
		$lines->[1] = {	name => "MA($lag) (Moving Avg)", color => 'cyan', data => ts::ma($lines->[0]->{data}, $lag) };
		$s = ts::stdev($lines->[0]->{data}, $lag);
		$s = ts::nstdev_ma($s, $lines->[1]->{data}, 2);
		$lines->[2] = {	name => 'MA + 2 Std Dev', color => 'lred', data => $s->[0] };
		$lines->[3] = {	name => 'MA - 2 Std Dev', color => 'lred', data => $s->[1] };
		my(@ty,@tx,@tu);
		@ty =  @{$lines->[0]->{data}};
		#my $mean   = sprintf("%3.3f",); # array refs are ok too
		return  [$#ty,mean(@ty)];
	}



	sub extended{
			my ($stocksymbol, $q, $lag, $diff) = @_;
my  @meanx = meanx($stocksymbol, $q, $lag, $diff);
my $check = diffcheck($stocksymbol, $q, $lag, $diff);
my @hl = checkHL($stocksymbol,$q);
my $output= {"position"=>($check==0?"middle":($check==1?"bottom":"top")),
				"days"=>$meanx[0][0],
				"momentum"=>sprintf("%3.8f",$meanx[0][1]),
				"avg-day-range-pct"=>$hl[0][0],
				"avg-vol"=>$hl[0][1]};
				return $output;
		}
		sub diffcheck {



( run in 2.233 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )