#!/usr/bin/perl
# ----------------------------------------------------------------------------
#
#  (c) Copyright 2006 TradeExtender
# 
#  This software is proprietary to and embodies the confidential technology 
#  of TradeExtender. Possession, use, duplication or dissemination of the 
#  software and media is authorized only pursuant to a valid written license 
#  from TradeExtender.
#

use strict;
use lib "/var/www/vhosts/kantelpunten.com/httpdocs/pl/lib";
use lib "lib";
BEGIN {
    delete $INC{"Kantelpunten/Helper.pm"};
#    delete $INC{"Kantelpunten/Process.pm"};
#    delete $INC{"Kantelpunten/MyRobots.pm"};
    delete $INC{"Kantelpunten/MyAdds.pm"};
}
use Kantelpunten::Helper;
use Kantelpunten::Process;
use Kantelpunten::MyRobots;
use Kantelpunten::MyAdds;
use POSIX qw(strftime);
#use Data::Dumper;

local our $quotes = undef;
local our $param  = undef;
local our $page   = undef;

show_quotes();
exit(0);

sub show_quotes()
{
	my $q = CGI->new;
	
	$quotes = undef;
		
	$param->{stock} 	= $q->param('stock');
	$param->{idx} 		= $q->param('idx');
	$param->{back} 		= $q->param('back');
	$param->{max} 		= $q->param('max');
	$param->{calc}      = $q->param('calc');
	$param->{invest} 	= $q->param('invest');
	$param->{mincost} 	= $q->param('mincost');
	$param->{perccost}	= $q->param('perccost');
	$param->{percgain}	= $q->param('percgain');
	$param->{percloss}	= $q->param('percloss');
	$param->{verbose} 	= $q->param('verbose');
	$param->{rb} 		= $q->param('rb');
	
	my $remote_ip = init_helper();
	$param->{rb} = get_robot();
	set_robot($param->{rb});

	init_process();
	get_user_rec();

	$param->{method} = undef;
	if (is_admin_user())
	{
    	$param->{verbose} = 0 unless defined($param->{verbose});
	    $param->{method}  = 0;
    	$param->{invest}  = 7500;
   		$param->{calc}    = (($param->{verbose} > 0) ? 'calc' : undef) unless defined($param->{calc});
	}
	else
	{
    	$param->{verbose} = 0;
    	$param->{method}  = 0;
    	$param->{invest}  = undef;
    	$param->{calc}    = undef;
	}

	if (!defined($param->{stock}))
	{
    	print_error_doc("Geen aandeel geselecteerd.");
    	exit(0);
	}
	my $stock = get_stock($param->{stock}, "old");
	if (!defined($stock))
	{
    	print_error_doc($param->{stock} . "<br>Dit aandeel wordt niet ondersteund");
    	exit(0);
	}
	
	$param->{max} = 100 if (!defined($param->{max}));
	$param->{back} = $param->{max} if (!defined($param->{back}));

	$page = sprintf("49%03d", $stock->{id});
	add_last_page_cookie($page);
	write_access_data($remote_ip, $page);

	make_quotes_html($stock);
}
    
sub make_quotes_html(@)
{
    my ($stock) = @_;
    my $script = make_home_script($page);
    
    my $title = "Kantelpunten : Historische koersen - $stock->{index_name}";
    my $desc = "Historische koersen van $stock->{index_name}. De open, hoog, laag, slot koersen van de" .
    	" afgelopen maanden. Kantelpunt + stijgend volume = sterker signaal. Daarnaast geven we ook het" .
        " 20 daags gemiddelde en de afwijkingen ervan. Kantelpunten alles op een rijtje voor de actieve" .
        " belegger.";

    my $pop_menu = make_pop_menu(1, $page, "volume", $stock);
    my $top_menu = make_top_menu(1, $page, "volume", $stock);

    print_header("+1d");
    print_pre_doc();
    print_head($title, undef, undef, $stock->{html_name}, $script, "quote", undef, undef, $desc, undef, "top_script");
    print_body("info", undef, undef, undef, undef, undef, (is_admin_user() ? "auto" : undef), "kader", undef, $pop_menu, $top_menu);
    make_quotes($stock);
    print_body("end_kader");
    print_post_doc();
}

sub make_quotes(@)
{
    my ($stock) = @_;

    my $id = $stock->{id};
    my $data = get_quote_data($id);
    my $img;
    if (defined($data))
    {
        $img = make_kantel_img($id, undef, $data);
    }   

    print_div("center");   
    print "<br><br>\n";

    my $heading = sprintf "%s ( %s )", $stock->{html_name}, uc $stock->{market};
    print_heading($heading); 
#    print_interim_heading(undef, $stock->{comment}); 

    $quotes = read_quotes($stock->{stock}, $param->{back}, $param->{max}, $param->{calc}, 
                            (($param->{verbose} > 4) ? $data->{trigger_time} : undef)   );
    if (!defined($quotes))
    {
	    if (is_old_stock($stock))
	    {
			print "<br><span class=\"dred\">Dit aandeel is van de markt gehaald!</span><br>\n";
		}
		else
		{
	        print "<small><br></small>\n";
    	    print "No historical data found<br>\n";
	    }
    }
    else
    {
        my $upd = strftime("%d-%m-%Y\n", localtime($data->{quote_last}));
        print <<EndOfText;
<span><small><small>
<b>Bijgewerkt t/m het slot van $upd.</b><br>
</small></small></span>
EndOfText

		print "<br><span class=\"dred\">Dit aandeel is van de markt gehaald!</span><br>\n" if (is_old_stock($stock));
       	print_banner_ads("main_banner", "info", 1, 2) if (show_adds("hoofd"));
        my $groen = "<span style=\"color: rgb(0, 153, 0)\">Groene</span>";
        my $rood = "<span style=\"color: rgb(153, 0, 0)\">rode</span>";
        print_our_graph($stock, "kantelk", "/pl/ShowStock.pl?stock=$stock->{stock}", "Toon de Aandelen informatie.", "scan") if (!is_old_stock($stock));
		print <<EndOfText;
<span><small>
<br><br>
Kantelpunt + stijgend volume = zeer sterk signaal.<br>
<br>
<small><b>
"verschil" toont het volumeverschil ten opzichte van het 20 daags gemiddelde.<br>
$groen of $rood cijfers duiden aan dat er minstens 50 % verschil is.<br>
</b><br>
</small></small></span>
EndOfText

        print_google_search("quotes", $stock->{market});
        print_table(0, undef, 0, 0, 0, undef, "100%");
        print "<tr>\n";

        # left buttons
        print " <td width=\"15%\" valign=\"top\" align=\"center\">\n";
        print_div("center");
        if (1 || is_old_stock($stock))
        {
	        print "&nbsp;\n";
        }
        else
        {   
	        print_buttons(undef, $page, 'homepage');
    	    print "<br>\n";
        	print_buttons($stock->{stock}, undef, 'fav_add');
	        print_banner_ads("quotes_links", "info", 4) if (show_adds("quotes"));
        }
        print_div("end");
        print " </td>\n";

        # center quotes
        print " <td width=\"70%\">\n";
        if ($param->{verbose} > 1)
        {
        	print_table(0, $stock->{market}, 0, 0, 0, undef, "100%", "center", "font-size: 11px;");
        }
        else
        {
        	print_table(0, $stock->{market}, 0, 0, 0, undef, "100%", "center", "font-size: 13px;");
    	}
        print "<tr>\n";
        if ($param->{verbose} <= 4)
        {
        print " <th class=\"quote\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>\n" if ($param->{verbose} <= 1);
        print " <th class=\"quote\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 1);
        print " <th class=\"quote\">&nbsp;&nbsp;open&nbsp;&nbsp;</th>\n";
        print " <th class=\"quote\">&nbsp;&nbsp;high&nbsp;&nbsp;</th>\n";
        print " <th class=\"quote\">&nbsp;&nbsp;low&nbsp;&nbsp;&nbsp;</th>\n";
        print " <th class=\"quote\">&nbsp;&nbsp;close&nbsp;&nbsp;</th>\n";
        print " <th class=\"quoter\">&nbsp;&nbsp;&nbsp;volume&nbsp;</th>\n";
        print " <th class=\"quote\">&nbsp;avg_vol&nbsp;</th>\n" if ($param->{verbose} > 0);
        print " <th class=\"quoter\">&nbsp;verschil&nbsp;</th>\n";
        print " <th class=\"quote\">&nbsp;vola&nbsp;</th>\n" if ($param->{verbose} > 0);
        print " <th class=\"quote\">&nbsp;avg_vola&nbsp;</th>\n" if ($param->{verbose} > 0);
        print " <th class=\"quote\">&nbsp;verschil&nbsp;</th>\n" if ($param->{verbose} > 0);
        print " <th class=\"quote\">&nbsp;mav7&nbsp;</th>\n" if ($param->{verbose} > 3);
        print " <th class=\"quote\">&nbsp;mav6&nbsp;</th>\n" if ($param->{verbose} > 3);
        print " <th class=\"quote\">&nbsp;mav3&nbsp;</th>\n" if ($param->{verbose} > 3);
        print " <th class=\"quote\">&nbsp;ema6&nbsp;</th>\n" if ($param->{verbose} > 3);
        print " <th class=\"quote\">&nbsp;ema3&nbsp;</th>\n" if ($param->{verbose} > 3);
        print " <th class=\"quote\">&nbsp;&nbsp;dmav7&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 3);
        print " <th class=\"quote\">&nbsp;&nbsp;dmav6&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 3);
        print " <th class=\"quote\">&nbsp;&nbsp;dema6&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 3);
        print " <th class=\"quote\">&nbsp;trend&nbsp;</th>\n" if ($param->{verbose} > 3);        
        print " <th class=\"quote\">&nbsp;kantel&nbsp;</th>\n" if ($param->{verbose} > 0);
        print " <th class=\"quote\">&nbsp;bs&nbsp;</th>\n" if ($param->{verbose} > 0);
        print " <th class=\"quote\">&nbsp;ss&nbsp;</th>\n" if ($param->{verbose} > 0);
        print " <th class=\"quote\">&nbsp;cs&nbsp;</th>\n" if ($param->{verbose} > 0);
        print " <th class=\"quote\">&nbsp;mkt&nbsp;</th>\n" if ($param->{verbose} > 0);
        print " <th class=\"quote\">&nbsp;&nbsp;&nbsp;ref_date&nbsp;&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 0);
        if (($param->{verbose} > 0) && ($param->{verbose} <= 3))
        {
        	print " <th class=\"quote\">&nbsp;trigger&nbsp;</th>\n";
        }
        else
        {
	        print " <th class=\"quote\">&nbsp;extr3l&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;extr3h&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;stop&nbsp;set&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;sell&nbsp;at&nbsp;</th>\n" if ($param->{verbose} > 3);
	        print " <th class=\"quote\">&nbsp;koop&nbsp;trigger&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;stop&nbsp;loss&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;stop&nbsp;min&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;stop&nbsp;wrong&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;stop&nbsp;low&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;stop&nbsp;avg&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;calc&nbsp;loss&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;calc&nbsp;gain&nbsp;</th>\n" if ($param->{verbose} > 3);
        }
        if (($param->{verbose} > 0) && ($param->{verbose} <= 3))
        {
        	print " <th class=\"quote\">&nbsp;trade&nbsp;date&nbsp;</th>\n";
        }
        else
        {
	        print " <th class=\"quote\">&nbsp;bought&nbsp;on&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;bought&nbsp;for&nbsp;</th>\n" if ($param->{verbose} > 3);
        	print " <th class=\"quote\">&nbsp;&nbsp;&nbsp;sold&nbsp;on&nbsp;&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 3);
        	print " <th class=\"quote\">&nbsp;&nbsp;&nbsp;sold&nbsp;for&nbsp;&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 3);
        	print " <th class=\"quote\">&nbsp;&nbsp;dif&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 3);        
        	print " <th class=\"quote\">&nbsp;&nbsp;&nbsp;sold&nbsp;on2&nbsp;&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 3);
        	print " <th class=\"quote\">&nbsp;&nbsp;&nbsp;sold&nbsp;for2&nbsp;&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 3);
        	print " <th class=\"quote\">&nbsp;&nbsp;dif2&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 3);        
        	print " <th class=\"quote\">&nbsp;dif&nbsp;low&nbsp;</th>\n" if ($param->{verbose} > 3);        
        	print " <th class=\"quote\">&nbsp;dif&nbsp;high&nbsp;</th>\n" if ($param->{verbose} > 3);        
        }
        print " <th class=\"quote\">&nbsp;ok&nbsp;</th>\n" if ($param->{verbose} > 0);
        print " <th class=\"quote\">&nbsp;&nbsp;&nbsp;min&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 0);
        print " <th class=\"quote\">&nbsp;&nbsp;&nbsp;max&nbsp;&nbsp;</th>\n" if ($param->{verbose} > 0);
        if ($param->{method} & 0x1)
        {
	        print " <th class=\"quote\">&nbsp;mkt&nbsp;1&nbsp;</th>\n" if ($param->{verbose} > 2);
    	    print " <th class=\"quote\">&nbsp;buy&nbsp;1&nbsp;</th>\n" if ($param->{verbose} > 2);        
	        print " <th class=\"quote\">&nbsp;sold&nbsp;1&nbsp;</th>\n" if ($param->{verbose} > 2);        
    	    print " <th class=\"quote\">&nbsp;stocks&nbsp;1&nbsp;</th>\n" if ($param->{verbose} > 2);
        	print " <th class=\"quote\">&nbsp;costs&nbsp;1&nbsp;</th>\n" if ($param->{verbose} > 3);
	        print " <th class=\"quote\">&nbsp;cash&nbsp;1&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;value&nbsp;1&nbsp;</th>\n" if ($param->{verbose} > 1);
	        print " <th class=\"quote\">&nbsp;gain&nbsp;1&nbsp;</th>\n" if ($param->{verbose} > 1);
        }
        if ($param->{method} & 0x2)
        {
    	    print " <th class=\"quote\">&nbsp;buy&nbsp;2&nbsp;</th>\n" if ($param->{verbose} > 2);        
        	print " <th class=\"quote\">&nbsp;stocks&nbsp;2&nbsp;</th>\n" if ($param->{verbose} > 2);
	        print " <th class=\"quote\">&nbsp;costs&nbsp;2&nbsp;</th>\n" if ($param->{verbose} > 3);
    	    print " <th class=\"quote\">&nbsp;cash&nbsp;2&nbsp;</th>\n" if ($param->{verbose} > 3);
        	print " <th class=\"quote\">&nbsp;value&nbsp;2&nbsp;</th>\n" if ($param->{verbose} > 1);
	        print " <th class=\"quote\">&nbsp;gain&nbsp;2&nbsp;</th>\n" if ($param->{verbose} > 1);
        }
        }
        else
        {
        	# special mode temproray for makeng data
        	print " <th class=\"quote\">&nbsp;special&nbsp;</th>\n";
        }
    	print "</tr>\n";
        
        my $num = 0;
        if ($param->{verbose} <= 4)
        {
        foreach my $time (reverse sort keys %$quotes)
        {
            print quote($time, $num);
            $num++;
        }
        }
        else
        {
        foreach my $time (sort keys %$quotes)
        {
            print quote($time, $num);
            $num++;
        }
        }
        
        print_table(0, "end");
        print " </td>\n";    
        
        # right buttons
        print " <td width=\"15%\" valign=\"top\" align=\"center\">\n";
        print_div("center");   
        print "&nbsp;\n";
#        print_buttons($stock->{stock}, undef, 'kantel');
#        print "<br>\n";
#		my $ktitle = sprintf("%d juist van de %d adviezen.", $data->{num_ok}, $data->{num_ok}+$data->{num_nok});
#    	print_pie_graph('small', $data->{num_ok}, $data->{num_nok}, $ktitle, $stock->{market}, 
#    					"ShowKantelpunten.pl?stock=$stock->{stock}");
#        print "<br>\n";
#        print_buttons($stock->{stock}, undef, 'aandeel', 'nieuws', 'intraday');
        print_banner_ads("quotes_rechts", "info", 1) if (show_adds("quotes"));
        print_div("end");
        print " </td>\n";
        print_table(0, "end");

       	print_banner_ads("quotes_midden", "info", 1, 1) if (show_adds("quotes_tabel"));
       	print_banner_ads("quotes_midden", "info", undef, undef) if (show_adds("quotes_tabel"));
    }
    print "<br><br>\n";
    print_voet_start();
    print_ads("yahoo_klein");    
    print_empty_lines(1, 0);
    print_div("end");
}

sub quote(@)
{
    my ($time, $num) = @_;
	my $class = (($num % 2) == 0) ? "even" : "odd";
    my $vcolor;
    my $text;
    
    $text = "<tr>";
    if ($param->{verbose} <= 4)
    {
	    $text .= "<td class=\"$class\">" . strftime("%d-%m-%Y", localtime($time)) . "</td>";
	    $text .= "<td class=\"$class\">" . sprintf("%.2f", $quotes->{$time}->{open}) . "</td>";
    	$text .= "<td class=\"$class\">" . sprintf("%.2f", $quotes->{$time}->{high}) . "</td>";
    	$text .= "<td class=\"$class\">" . sprintf("%.2f", $quotes->{$time}->{low}) . "</td>";
    	$text .= "<td class=\"$class\">" . sprintf("%.2f", $quotes->{$time}->{close}) . "</td>";
	    $vcolor = "";
    	$vcolor = " dgreen" if ($quotes->{$time}->{dif_volume} >= 50);
    	$vcolor = " dred"   if ($quotes->{$time}->{dif_volume} <= -50);
    	$text .= "<td class=\"${class}r$vcolor\">" . $quotes->{$time}->{volume} . "&nbsp;</td>";
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{avg_volume}) ? $quotes->{$time}->{avg_volume} : "&nbsp;") . "</td>" if ($param->{verbose} > 0);
 		$text .= "<td class=\"${class}r$vcolor\">" . (defined($quotes->{$time}->{dif_volume}) ? sprintf("%s%.0f%", (($quotes->{$time}->{dif_volume} < 0) ? "-" : "+"), abs($quotes->{$time}->{dif_volume})) : "&nbsp") . "&nbsp;&nbsp;</td>";
	    $vcolor = "";
    	$vcolor = " dgreen" if ($quotes->{$time}->{dif_vola} >= 50);
    	$vcolor = " dred"   if ($quotes->{$time}->{dif_vola} <= -50);
    	$text .= "<td class=\"$class$vcolor\">" . sprintf("%.2f", $quotes->{$time}->{cur_vola}) . "</td>" if ($param->{verbose} > 0);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{avg_vola}) ? sprintf("%.2f", $quotes->{$time}->{avg_vola}) : "&nbsp;") . "</td>" if ($param->{verbose} > 0);
 		$text .= "<td class=\"$class$vcolor\">" . (defined($quotes->{$time}->{dif_vola}) ? sprintf("%s%.0f%", (($quotes->{$time}->{dif_vola} < 0) ? "-" : "+"), abs($quotes->{$time}->{dif_vola})) : "&nbsp") . "</td>" if ($param->{verbose} > 0);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{mav7}) ? sprintf("%.2f", $quotes->{$time}->{mav7}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{mav6}) ? sprintf("%.2f", $quotes->{$time}->{mav6}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{mav3}) ? sprintf("%.2f", $quotes->{$time}->{mav3}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{ema6}) ? sprintf("%.2f", $quotes->{$time}->{ema6}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{ema3}) ? sprintf("%.2f", $quotes->{$time}->{ema3}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{dmav7}) ? sprintf("%.2f%%", $quotes->{$time}->{dmav7}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{dmav6}) ? sprintf("%.2f%%", $quotes->{$time}->{dmav6}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{dema6}) ? sprintf("%.2f%%", $quotes->{$time}->{dema6}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{trend}) ? $quotes->{$time}->{trend} : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{kantel}) ? (($quotes->{$time}->{kantel} eq "") ? "&nbsp" : $quotes->{$time}->{kantel}) : "&nbsp") . "</td>" if ($param->{verbose} > 0);
    	$text .= "<td class=\"$class\">" . $quotes->{$time}->{bstate} . "</td>" if ($param->{verbose} > 0);
    	$text .= "<td class=\"$class\">" . $quotes->{$time}->{sstate} . "</td>" if ($param->{verbose} > 0);
    	$text .= "<td class=\"$class\">" . $quotes->{$time}->{cstate} . "</td>" if ($param->{verbose} > 0);
    	$text .= "<td class=\"$class\">" . $quotes->{$time}->{mkt} . "</td>" if ($param->{verbose} > 0);
       	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{ref_date}) ? strftime("%d-%m-%Y", localtime($quotes->{$time}->{ref_date})) : "&nbsp;") . "</td>" if ($param->{verbose} > 0);
    	if (($param->{verbose} > 0) && ($param->{verbose} <= 3))
    	{
        	if ($quotes->{$time}->{kantel} ne "")
        	{
            	my $trigger = (($quotes->{$time}->{kantel} eq "+") || ($quotes->{$time}->{kantel} eq "B")) ? $quotes->{$time}->{koop_trigger} : 
               	             ((($quotes->{$time}->{kantel} eq "-") || ($quotes->{$time}->{kantel} eq "S")) ? $quotes->{$time}->{stop_loss} : "&nbsp;");
            	$text .= "<td class=\"$class\">$trigger</td>";
        	}
        	else
        	{
            	$text .= "<td class=\"$class\">&nbsp;</td>";
        	}
    	}
    	else
    	{
	    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{extr3_low}) ? sprintf("%.2f", $quotes->{$time}->{extr3_low}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{extr3_high}) ? sprintf("%.2f", $quotes->{$time}->{extr3_high}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{stop_set}) ? $quotes->{$time}->{stop_set} : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{sell_at}) ? $quotes->{$time}->{sell_at} : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{koop_trigger}) ? $quotes->{$time}->{koop_trigger} : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{stop_loss}) ? $quotes->{$time}->{stop_loss} : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{stop_min}) ? $quotes->{$time}->{stop_min} : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{stop_wrong}) ? $quotes->{$time}->{stop_wrong} : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{stop_low}) ? $quotes->{$time}->{stop_low} : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{stop_avg}) ? $quotes->{$time}->{stop_avg} : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{calc_stop}) ? $quotes->{$time}->{calc_stop} : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{calc_gain}) ? $quotes->{$time}->{calc_gain} : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    	}
    	if (($param->{verbose} > 0) && ($param->{verbose} <= 3))
    	{
        	if (!defined($quotes->{$time}->{kantel}) || $quotes->{$time}->{kantel} eq "")
        	{
            	$text .= "<td class=\"$class\">&nbsp;</td>";
        	}
        	else
        	{
            	my $trade_date = (($quotes->{$time}->{kantel} eq "+") || ($quotes->{$time}->{kantel} eq "B")) ? $quotes->{$time}->{bought_on} : $quotes->{$time}->{sold_on};
            	if (!defined($trade_date) || ($trade_date == 0))
            	{
                	$text .= "<td class=\"$class\">" . (($quotes->{$time}->{success} eq "?") ? "Pending" : "No&nbsp;trade") . "</td>";
            	}
            	else
            	{
                	$text .= "<td class=\"$class\">" .  strftime("%d-%m-%Y", localtime($trade_date)) . "</td>";
            	}
        	}
    	}
    	else
    	{
        	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{bought_on}) ? (($quotes->{$time}->{bought_on} == 0) ? "No&nbsp;trade" : strftime("%d-%m-%Y", localtime($quotes->{$time}->{bought_on}))) : "&nbsp;") . "</td>" if ($param->{verbose} > 3);
        	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{bought_for}) ? sprintf("%.2f", $quotes->{$time}->{bought_for}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
        	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{sold_on}) ? (($quotes->{$time}->{sold_on} == 0) ? "No&nbsp;trade" : strftime("%d-%m-%Y\n", localtime($quotes->{$time}->{sold_on}))) : "&nbsp;") . "</td>" if ($param->{verbose} > 3);
	        $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{sold_for}) ? sprintf("%.2f", $quotes->{$time}->{sold_for}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
	        $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{dif}) ? sprintf("%.2f", $quotes->{$time}->{dif}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
        	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{sold_on2}) ? (($quotes->{$time}->{sold_on2} == 0) ? "No&nbsp;trade" : strftime("%d-%m-%Y\n", localtime($quotes->{$time}->{sold_on2}))) : "&nbsp;") . "</td>" if ($param->{verbose} > 3);
	        $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{sold_for2}) ? sprintf("%.2f", $quotes->{$time}->{sold_for2}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
	        $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{dif2}) ? sprintf("%.2f", $quotes->{$time}->{dif2}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
	        $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{dif_low}) ? sprintf("%.2f", $quotes->{$time}->{dif_low}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
	        $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{dif_high}) ? sprintf("%.2f", $quotes->{$time}->{dif_high}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
    	}            
	    $text .= "<td class=\"$class\">" . ((defined($quotes->{$time}->{success}) && ($quotes->{$time}->{success} ne "")) ? $quotes->{$time}->{success} : "&nbsp") . "</td>" if ($param->{verbose} > 0);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{min_dif}) ? sprintf("%.2f", $quotes->{$time}->{min_dif}) : "&nbsp") . "</td>" if ($param->{verbose} > 0);
    	$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{max_dif}) ? sprintf("%.2f", $quotes->{$time}->{max_dif}) : "&nbsp") . "</td>" if ($param->{verbose} > 0);

	    if ($param->{method} & 0x1)
    	{
    		$text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{market1}) ? $quotes->{$time}->{market1} : "&nbsp") . "</td>" if ($param->{verbose} > 2);
		    $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{buy1}) ? sprintf("%.2f", $quotes->{$time}->{buy1}) : "&nbsp") . "</td>" if ($param->{verbose} > 2);
		    $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{sold1}) ? sprintf("%.2f", $quotes->{$time}->{sold1}) : "&nbsp") . "</td>" if ($param->{verbose} > 2);
		    $text .= "<td class=\"$class\">" . (($quotes->{$time}->{stocks1} != 0) ? $quotes->{$time}->{stocks1} : "&nbsp") . "</td>" if ($param->{verbose} > 2);
		    $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{cost1}) ? sprintf("%.2f", $quotes->{$time}->{cost1}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
		    $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{cash1}) ? sprintf("%.2f", $quotes->{$time}->{cash1}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
		    $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{value1}) ? sprintf("%.2f", $quotes->{$time}->{value1}) : "&nbsp") . "</td>" if ($param->{verbose} > 1);
		    $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{gain1}) ? sprintf("%.1f", $quotes->{$time}->{gain1}) : "&nbsp") . "</td>" if ($param->{verbose} > 1);
	    }
    	if ($param->{method} & 0x2)
	    {
		    $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{buy2}) ? sprintf("%.2f", $quotes->{$time}->{buy2}) : "&nbsp") . "</td>" if ($param->{verbose} > 2);
		    $text .= "<td class=\"$class\">" . (($quotes->{$time}->{stocks2} != 0) ? $quotes->{$time}->{stocks2} : "&nbsp") . "</td>" if ($param->{verbose} > 2);
		    $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{cost2}) ? sprintf("%.2f", $quotes->{$time}->{cost2}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
		    $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{cash2}) ? sprintf("%.2f", $quotes->{$time}->{cash2}) : "&nbsp") . "</td>" if ($param->{verbose} > 3);
		    $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{value2}) ? sprintf("%.2f", $quotes->{$time}->{value2}) : "&nbsp") . "</td>" if ($param->{verbose} > 1);
		    $text .= "<td class=\"$class\">" . (defined($quotes->{$time}->{gain2}) ? sprintf("%.1f", $quotes->{$time}->{gain2}) : "&nbsp") . "</td>" if ($param->{verbose} > 1);
	    }
    }
    else
    {
        $text .= "<td class=\"$class\">array(\"\",$time," . sprintf("%.2f", $quotes->{$time}->{close}) . "),</td>";
    }
    $text .= "</tr>\n";
    return $text;
}
    
sub print_heading(@)
{
    my ($heading, $comment, $img) = @_;
    
    printf "\n<!-- heading for %s -->\n", $heading;
    print  "<span style=\"color: rgb(51, 51, 51); font-family: verdana;\">\n";
    print  "$img" if (defined($img));
    printf " <small style=\"font-weight: bold;\"><big><big>%s<br></big></big>\n", $heading;
    print  " </small>\n";
    print  "</span>\n";
    print  "<span style=\"font-family: verdana;\" id=\"smallblue\">\n";
    printf " <small>%s</small>\n", $comment;
    print  "</span>\n";
}
