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

[GT] SVN Commit r591 - trunk/GT



Author: thomas
Date: 2008-03-23 06:28:53 +0100 (Sun, 23 Mar 2008)
New Revision: 591

Modified:
   trunk/GT/Prices.pm
Log:
savetxt uses options set in conf, rather than always using defaults.

Modified: trunk/GT/Prices.pm
===================================================================
--- trunk/GT/Prices.pm	2008-03-23 04:53:51 UTC (rev 590)
+++ trunk/GT/Prices.pm	2008-03-23 05:28:53 UTC (rev 591)
@@ -400,11 +400,52 @@
 
 =cut
 sub savetxt {
-    my ($self, $file) = @_;
+    my ($self, $file, $mark, $date_format, %fields) = @_;
     open(FILE, '>', "$file") || die "Can't write in $file: $!\n";
+    my ($open, $high, $low, $close, $volume, $date);
+    my ($year, $month, $day);
+
+    $mark = $mark || "\t";
+
+    # Set up %fields with the standard fields map : open high low close volume date
+    if (!%fields) {
+	%fields = ('open' => 0, 'high' => 1, 'low' => 2, 'close' => 3, 'volume' => 4, 'date' => 5);
+    }
+    
+    # Set up $date_format to the internal date format
+    if (!$date_format) { $date_format = 0; }
+    
     foreach (@{$self->{'prices'}})
     {
-	print FILE join("\t", @{$_}) . "\n";
+      my @line = @{$_};
+      $open = $line[$fields{'open'}];
+      $high = $line[$fields{'high'}];
+      $low = $line[$fields{'low'}];
+      $close = $line[$fields{'close'}];
+      $volume = $line[$fields{'volume'}];
+      $date = $line[$fields{'date'}];  # No obvious way how to divide date into mulitple fields
+
+      if ($date_format != 0) {
+		
+	if ($date_format eq 1) {
+	  ($year, $month, $day) = split(/-/, $date);
+	  $date = "$month/$day/$year"; 
+	}
+	if ($date_format eq 2) {
+	  ($year, $month, $day) = split(/-/, $date);
+	  $date = "$day/$month/$year"; 
+	}
+      }
+
+      my @newline = ();
+      $newline[$fields{'open'}] = $open;
+      $newline[$fields{'high'}] = $high;
+      $newline[$fields{'low'}] = $low;
+      $newline[$fields{'close'}] = $close;
+      $newline[$fields{'volume'}] = $volume;
+      $newline[$fields{'date'}] = $date;
+      
+      print FILE join($mark, @newline) . "\n";
     }
     close FILE;
 }
@@ -415,10 +456,11 @@
 
 =cut
 sub dump {
-    my ($self) = @_;
+    my ($self, $mark) = @_;
+    $mark = $mark || "\t";
     foreach (@{$self->{'prices'}})
     {
-	print join("\t", @{$_}) . "\n";
+	print join($mark, @{$_}) . "\n";
     }
 }