[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GT] Re: Re: perl floating point great-equal bug?



Hi again,

im using the 699 svn version

here the Order.pm and Tools.pm differences

--- GeniusTraderSvn699Originale/GT/Tools.pm    2009-10-16 12:48:34.000000000
+0200
+++ Tools.pm	2009-10-28 14:46:21.000000000 +0100
@@ -16,10 +16,10 @@
		extract_object_number
		resolve_alias resolve_object_alias long_name short_name
		isin_checksum isin_validate isin_create_from_local
-		 get_timeframe_data parse_date_str find_calculator check_dates
+		 get_timeframe_data parse_date_str find_calculator check_dates
f_eq f_ge f_le
		);
%EXPORT_TAGS = ("math" => [qw(min max PI sign)],
-	 "generic" => [qw(extract_object_number)],
+	 "generic" => [qw(extract_object_number f_eq f_ge f_le)],
	"conf" => [qw(resolve_alias resolve_object_alias long_name
short_name)],
	"isin" => [qw(isin_checksum isin_validate isin_create_from_local)],
		"timeframe" => [qw(get_timeframe_data parse_date_str
find_calculator check_dates)]
@@ -785,4 +785,24 @@
=back

=cut
+
+sub f_eq {
+    my( $float1, $float2, $delta ) = @_;
+    $delta ||= 0.000001;  # default value of delta
+    abs( $float1 - $float2 ) < $delta
+}
+
+sub f_ge {
+    my( $float1, $float2, $delta ) = @_;
+    $delta ||= 0.000001;  # default value of delta
+    $float1 > $float2 - $delta
+}
+
+sub f_le {
+    my( $float1, $float2, $delta ) = @_;
+    $delta ||= 0.000001;  # default value of delta
+    $float1 < $float2 + $delta
+}
+
+
1;


--- GeniusTraderSvn699Originale/GT/Portfolio/Order.pm	 2009-10-16
12:48:33.000000000 +0200
+++ Order.pm	2009-11-05 12:04:58.000000000 +0100
@@ -7,10 +7,11 @@
our @ISA = qw(GT::Serializable);

use strict;
+#use warnings;
#ALL# use Log::Log4perl qw(:easy);
use GT::Prices;
use GT::Serializable;
-
+use GT::Tools qw(:generic);
=head1 NAME

GT::Portfolio::Order - An order within the portfolio
@@ -52,6 +53,7 @@
    return $self;
}

+
=item C<< $o->set_sell_order() >>

=item C<< $o->set_buy_order() >>
@@ -186,6 +188,7 @@
sub price	 { $_[0]->{'price'} }
sub second_price { $_[0]->{'price2'} }

+
=item C<< $o->set_source($source) >>

=item C<< $o->source() >>
@@ -350,8 +353,8 @@
    $price = $calc->prices->at($i - 1)->[$CLOSE] if ($i >= 1);
    } elsif ($self->is_type_limited) {
    # At limited price
-    if (($self->price >= $prices->[$LOW]) &&
-	 ($self->price <= $prices->[$HIGH]))
+    if (f_ge($self->price,$prices->[$LOW]) &&
+	 f_le($self->price,$prices->[$HIGH]))
    {
	$price = $self->price;
    } else
@@ -367,12 +370,12 @@
    } elsif ($self->is_type_stop) {
    # On stop (conditional order)
    if (($self->is_buy_order) &&
-	 ($prices->[$HIGH] >= $self->price))
+	 f_ge($prices->[$HIGH],$self->price))
    {
	$price = ($prices->[$FIRST] > $self->price) ?
	      $prices->[$FIRST] : $self->price;
    } elsif (($self->is_sell_order) &&
-	  ($prices->[$LOW] <= $self->price))
+	  f_le($prices->[$LOW],$self->price))
    {
	$price = ($prices->[$FIRST] < $self->price) ?
	      $prices->[$FIRST] : $self->price;
@@ -380,14 +383,14 @@
    } elsif ($self->is_type_stop_limited) {
    # On stop (conditional order)
    if (($self->is_buy_order) &&
-	 ($prices->[$HIGH] >= $self->price) &&
-	 ($prices->[$LOW] <= $self->second_price))
+	 f_ge($prices->[$HIGH],$self->price) &&
+	 f_le($prices->[$LOW],$self->second_price))
    {
	$price = ($prices->[$HIGH] < $self->second_price) ?
	      $prices->[$HIGH] : $self->second_price;
    } elsif (($self->is_sell_order) &&
-	  ($prices->[$LOW] <= $self->price) &&
-	  ($prices->[$HIGH] >= $self->second_price))
+	  f_le($prices->[$LOW],$self->price) &&
+	  f_ge($prices->[$HIGH],$self->second_price))
    {
	$price = ($prices->[$LOW] > $self->second_price) ?
	      $prices->[$LOW] : $self->second_price;




fg


Robert A. Schmied ha scritto:
> fr.gamberale
AT
tiscali.it wrote:
>> Hello,
>>     i'm new to this	mailing list, i started using GT at the beginning of
this year but i didnt realize the project was going on, so i used the older
version and i modified it a little bit.
>> Now that i discoved the svn version im switching to it.
>>
>> One of the things that i changed on the library that seems not corrected in
the svn version is a little bug using the >= and <= operators in floating
context.
>>
>> The problem is that if you create the order using for the price
($order->price) a division like ($var/100)*100, the $order->price will not be
exactly $var but $var  0.000....01 (this is a known perl problem)
>> and the is_executed function could return false if the $order->price is
equal to the HIGH or the LOW of the bar.
>>
>> In particular i created 2 functions in Tools.pm and changed all the <= and
>= with these functions in
>>
>> Portfolio/Order.pm
>> is_executed function
>> lines 372 373 389 394 402 403 408 409
>>
>> I dont know if this problem is present in other part of GT but i fixed only
in Order.pm
>>
>> If someone is interested in the problem i can send  the Portfolio/Order.pm
and Tools.pm modified.
>>
>> I hope this is usefull.
>>
>> PS: im using the http://www.geniustrader.org/wws/compose_mail/devel to write
this post but i cant attach anything here.Is possible to send a post
>> from thunderbird?
>>
>> thx
>
>
> aloha fr.gamberale
>
> your observation could very well be the cause of many of the odd results one
> sees from time to time.
>
> as there are three (at least) gt branches on the svn at
http://geniustrader.org/
> you need to identify the one you're using so we can get on the same page (or
at
> least identify it as a reference).
>
> as far as sharing your fixes you can either post differences or the complete
files.
> differences might be the preferred method, but it probably doesn't matter
much.
> see man page diff(1) and/or info diff for details on how to create difference
files.
> for reference diff -u orig_file changed_file. there are probably diff
equivalents
> built-in in svn, but i don't know about them, so you are on your own there.
>
> if you have subscribed to the gt devel mailing list you should be able to
post
> messages with attachments to the list with any email client. see the gt
website
> (http://geniustrader.org/) main page 'Mailing lists' section. for reference
you
> post to devel
AT
geniustrader.org.
>
> ciao
>
> ras