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

RE: Tick data to 5 minute converter??



PureBytes Links

Trading Reference Links

> Does anybody know a program which converts tick data into a ascii
> format of open/high/low/close/vol etc for a certain period?

The simplest way to do this is just open a 5min (or whatever) chart 
in TradeStation and write the resulting O/H/L/C out to a file.  In 
TS4 you open the Data Window, right-click in the Data Window, and 
select "Send to file..."

If that doesn't work for some reason -- like if you want to convert 
years of tick data and your TS4 can only display 6 months of 5min 
charts -- here's a Perl script that might do the trick.  It will 
construct N-minute OR N-tick bars; however, TS4 (and I suspect TS2k) 
can't really use N-tick bars, and they're probably not very useful.

It expects a tick file that looks like this:

"Date","Time","Close"
990111,934,189.00000
990111,934,189.00000
  ...etc...

or it can also accept the standard format written out by TS.

Warning:  I haven't used this in over a year but it still seems to 
work OK.  

Warning:  I didn't need volume stats, so I never computed it.  

You're welcome to address those problems yourself.  :-)

Gary

============================================

#
# Compress tick files into N-minute bars or N-tick bars.
# Assumes data is clean, i.e. no out-of-sequence ticks.
#
# In TS, the XX:YY bar includes all ticks up to BUT NOT INCLUDING
# those that happen in the XX:YY minute.
#
# Gary Fritz  4/26/99

$debug = 0;

$infile = $ARGV[0];
$n = $ARGV[1];
$mt = $ARGV[2];

if (($#ARGV != 2) || (($mt ne "m") && ($mt ne "t"))) {
  print "Usage:  perl bars.pl TICKFILE NUM TYPE\n";
  print "  where TICKFILE is the tick data file,\n";
  print "        NUM = number of minutes or ticks per bar,\n";
  print "        TYPE = 'm' for minutes or 't' for ticks.\n";
  print "E.g.:  'perl bars.pl DELL.CSV 10 m' for 10min bars,\n";
  print "       'perl bars.pl MSFT.CSV 5 t' for 5tick bars.\n";
  print "Writes its output to an appropriately-named file, such as\n";
  print "'DELL-1m.CSV' for 1-min bars of DELL, etc.\n";
  exit 1;
  }

$outfile = $infile;
$outfile =~ s/\.CSV$/-$ARGV[1]$ARGV[2].CSV/i;

open(INFILE, $infile) or die("Can't open $infile!\n");
open(OUTFILE, ">" . $outfile) or die("Can't open $outfile for writing!\n");

$line = <INFILE>;   # Discard Date,Time,Close
print OUTFILE "Date,Time,Open,High,Low,Close,U,D\n";

$high = 0;
$low = 99999999;
$date = "";

if ($mt eq "t") {
  $nticks = $n;
  $tick = 0;
  while ($line = <INFILE>) {
    chomp($line);
    $lastdate = $date;
    ($date, $time, $tickval) = split(",", $line);
    if ($lastdate eq "") { $lastdate = $date; }
    else { if ($date ne $lastdate) { print "Processing $date...\n"; } }
    if ($tick == 0) { $open = $tickval; }
    if ($tickval > $high) { $high = $tickval; }
    if ($tickval < $low) { $low = $tickval; }
    $tick++;
    $close = $tickval;
    if (($tick == $nticks) || ($date != $lastdate)) {
      print OUTFILE "$date,$time,$open,$high,$low,$close,0,0\n";
      $tick = 0;
      $high = 0;
      $low = 99999999;
      }
    }
  print OUTFILE "$date,$time,$open,$high,$low,$close\n";
  }
else {
  $mins = $n;
  $basetime = 9*60+30;  # Use 9:30am open as base minute
  $offset = $mins - $basetime%$mins;
  $timemod = 0;
  $nextbar = 0;
  $open = -1;
  $time = "0";
  while ($line = <INFILE>) {
    chomp($line);
    $lastdate = $date;
    $lasttime = $time;
    ($date, $time, $tickval) = split(",", $line);
    if ($lastdate eq "") { $lastdate = $date; }
    else { if ($date ne $lastdate) { print "Processing $date...\n"; } }
    $timemins = 60*int($time/100) + $time % 100;
    $timemod = ($timemins-1-$basetime) % $mins;
 if ($debug) { print "date = $date, time = $time, tmins = $timemins, nxbar = $nextbar, tmod = $timemod\n"; }
    if (($time != $lasttime) 
        && (($timemins >= $nextbar) || ($mins == 1) || ($date ne $lastdate))) {
 if ($debug) { print "  new bar! prev bar time = $nextbartime\n"; }
      if ($open != -1) { print OUTFILE "$lastdate,$nextbartime,$open,$high,$low,$close,0,0\n"; }
      $open = -1;
      $high = 0;
      $low = 99999999;
      }
    $nextbar = $mins * (1+int(($timemins+$offset)/$mins)) - $offset;
    $nextbartime = 100*int($nextbar/60) + $nextbar%60;
    if ($open == -1) { $open = $tickval; }
    if ($tickval > $high) { $high = $tickval; }
    if ($tickval < $low) { $low = $tickval; }
    $close = $tickval;
    }
    if ($open != -1) { print OUTFILE "$lastdate,$nextbartime,$open,$high,$low,$close\n"; }
  }

print "\n\nFile written to $outfile\n";