From: Matthew J. <ma...@el...> - 2004-11-25 20:14:43
|
Oliver, Raphael what do you think of this stuff? Matthew Jurgens wrote: > The purpose of this patch is to remove any trace of HTML "coding" from > GT. Fortunately at the moment there is not too much HTML "code" in GT. > I've introduced an HTML Templating system using HTML::Template. > Now the HTML "coding" is separate to GT and you can easily change any > formatting items you like by simpy changing the template. > Initially I've only targetted the PortfolioHTML subroutine in > Report.pm (this is accessed by using ./backtest.pl --html SYSTEMALIAS > SYMBOL). > If this looks ok, I'd like to expand this to HTMLise other outputs > using the same templating system. I also realise that there is some > HTML in scan.pl, which I'll remove later if this change looks ok. > > This patch changes the following 1 file: > > GT/Report.pm > > There is one additional configuration item in the ./gt/options file. > If the option is not present then an error message like > "HTML::Template->new called with empty filename parameter! at > ../GT/Report.pm line 107" will be printed. > >> The additional option (configured with the default value) is >> GT::Report::TemplatePortfolioAnalysis >> ../HTML/TemplatePortfolioAnalysis.html >> > There is a new directory structure that will need to be included to > store the templates. I've put a directory called HTML at the same > level as GT and Scripts. In this directory is a file called > TemplatePortfolioAnalysis.html. I have attached this file as well. > > Here's the patch. > > ===================== PATCH 1 ========================= > > diff -ru gtcvs/GT/Report.pm gt/GT/Report.pm --- > gtcvs/GT/Report.pm 2004-03-19 17:37:25.000000000 +1100 > +++ gt/GT/Report.pm 2004-11-02 20:38:49.997915611 +1100 > @@ -5,6 +5,7 @@ > # version 2 or (at your option) any later version. > > use strict; > +use HTML::Template; > > use GT::Portfolio; > use GT::CacheValues; > @@ -99,18 +100,30 @@ > =cut > sub PortfolioHTML { > my ($p, $detailed) = @_; > - > + + # get the template name from the configuration > + my $htmltemplate = > GT::Conf::get("GT::Report::TemplatePortfolioAnalysis"); > + # start a new template object > + my $template = HTML::Template->new(filename => "$htmltemplate"); > + > + # variables to hold HTML data > + my @loop; > + my $htmldata_tradenum; > + my $htmldata_code; > + my $htmldata_type; > + my $htmldata_source; > + my $htmldata_shares; > + my $htmldata_entrydate; > + my $htmldata_entryprice; > + my $htmldata_exitdate; > + my $htmldata_exitprice; > + my $htmldata_return; > + my $htmldata_duration; > + $detailed = 0 if (! defined($detailed)); > my $db = create_db_object(); > - print OUT "<h2>History of the portfolio</h2>\n"; > - print OUT "<table border='1' cellpadding='5' cellspacing='0'>\n"; > - print OUT "<tr><th>Trade #</th><th>Code</th><th>Type</th>"; > - if ($detailed) { > - print OUT "<th>Source</th>"; > - } > - print OUT "<th>Shares</th><th>Entry Date</th><th>Entry > Price</th><th>Exit Date</th><th>Exit > Price</th><th>Return</th><th>Duration</th></tr>\n"; > foreach my $position (@{$p->{'history'}}) > { > my $pstats = $position->stats($p); > @@ -124,23 +137,26 @@ > $variation = ($pstats->{'sold'} !=0) ? > ($diff / $pstats->{'sold'}) : 0; > } > - my $bg = ($variation > 0 ) ? "bgcolor='#55FF55'" : > "bgcolor='#FF5555'"; > - print OUT "<tr><td $bg>" . $position->id . "</td>"; > + my $bgcolor = ($variation > 0 ) ? "#55FF55" : "#FF5555"; > + > + $htmldata_tradenum=$position->id;; > my $name = $db->get_name($position->code); > if ($name) { > - print OUT "<td $bg>" . $name . " - " . $position->code . > "</td>"; > + $htmldata_code=$name . " - " . $position->code; > } else { > - print OUT "<td $bg>" . $position->code . "</td>"; > + $htmldata_code=$position->code; > } > if ($position->is_long) > { > - print OUT "<td $bg>Long</td>"; > + $htmldata_type="Long"; > } else { > - print OUT "<td $bg>Short</td>"; > + $htmldata_type="Short"; > } > if ($detailed) > { > - print OUT "<td $bg>" . $position->source . "</td>"; > + $htmldata_source=$position->source; > + } else { > + $htmldata_source=""; > } > my $n = 0; > my ($start_date, $start_price, $end_date, $end_price); > @@ -148,7 +164,7 @@ > { > if ($n == 0) > { > - print OUT "<td $bg>" . $order->quantity . "</td>"; > + $htmldata_shares=$order->quantity; > $start_date = $order->submission_date; > $start_price = $order->price; > } > @@ -163,15 +179,40 @@ > if (defined($position->timeframe)) { > $timeframe = $position->timeframe; > } > - my $duration = (GT::DateTime::map_date_to_time($timeframe, > $end_date) - GT::DateTime::map_date_to_time($timeframe, $start_date)) > / 86400; > + $htmldata_duration = > (GT::DateTime::map_date_to_time($timeframe, $end_date) - > GT::DateTime::map_date_to_time($timeframe, $start_date)) / 86400; > > - printf OUT ("<td $bg>%s</td><td $bg>%.4f</td>", $start_date, > $start_price); > - printf OUT ("<td $bg>%s</td><td $bg>%.4f</td>", $end_date, > $end_price); > - printf OUT ("<td $bg>%.2f%%</td>", $variation * 100); > - printf OUT ("<td $bg>%s</td>", $duration); > - print OUT "</tr>\n"; > + $htmldata_entrydate=$start_date; > + $htmldata_entryprice=sprintf("%.4f", $start_price); > + $htmldata_exitdate=$end_date; > + $htmldata_exitprice=sprintf("%.4f", $end_price); > + $htmldata_return=sprintf("%.2f%%", $variation * 100); > + + # build the row data for the html template from all the data we > have collected above > + my %row = ( > + bgcolor =>$bgcolor, > + tradenum =>$htmldata_tradenum, > + code =>$htmldata_code, > + type =>$htmldata_type, > + source =>$htmldata_source, > + shares =>$htmldata_shares, > + entrydate =>$htmldata_entrydate, > + entryprice =>$htmldata_entryprice, > + exitdate =>$htmldata_exitdate, > + exitprice =>$htmldata_exitprice, > + return =>$htmldata_return, > + duration =>$htmldata_duration, > + detailed =>$detailed > + ); > + # now put this row onto our html data array > + push(@loop,\%row); > + > } > - print OUT "</table>\n"; > + > + # set the final html template variables and pass in the html data > + $template->param(detailed => $detailed, trade_loop => \@loop); > + # write the template > + $template->output(print_to => *OUT); > + > } > > =item C<< GT::Report::OpenPositions($portfolio, $detailed) >> > >------------------------------------------------------------------------ > > > History of the portfolio > >Trade # Code Type Source Shares Entry Date Entry Price Exit Date Exit >Price Return Duration > > > > > > > > > > > > > > > |