/* A C program to query a postgres database of stockmarket index data
 *
 * RAW 9/16/98
 * RAW 01/21/00 Drop sql for dxy0, cry0 - index data discontinued!?
 */
#include <stdio.h>
#include <stdlib.h>
#include "/usr/local/pgsql/include/libpq-fe.h"

#define ERROR 1
#define QUERY_STRING_LENGTH 256
#define DATADIR internet/stocks/data

PGconn* conn;
PGresult* qresult;
int rq(char *query_string), rq_val(char *query_string), list_qresult();
double normval;
char *endptr,tempstring[QUERY_STRING_LENGTH];
FILE *outfile;

main ()
{
char *pghost=NULL, *pgport=NULL, *pgoptions=NULL, *pgtty=NULL;
char* dbName="rindex";


/* Make the database connection. */
conn=PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);

/* Check that the db connection was made. If not, exit. */
if (PQstatus(conn) == CONNECTION_BAD)
  {
  fprintf(stderr, "Connection to database %s failed. Program end.\n", dbName);
  fprintf(stderr, "%s\n", PQerrorMessage(conn)); 
  PQfinish(conn);
  exit(1);
  }

/* Command and Query the db. */
rc("set DateStyle to 'SQL'");

/* quarter - 91 days */
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/indu.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp0 from index_rec where symbol='INDU' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp0;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); /* Watch out! If getvalue returns zero rows - segmentation error :( */
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp0;",normval);
rq(tempstring);
rc("drop table xtemp0;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/tran.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp1 from index_rec where symbol='TRAN' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp1;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp1;",normval);
rq(tempstring);
rc("drop table xtemp1;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/util.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp2 from index_rec where symbol='UTIL' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp2;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp2;",normval);
rq(tempstring);
rc("drop table xtemp2;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/compx.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp3 from index_rec where symbol='COMPX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp3;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp3;",normval);
rq(tempstring);
rc("drop table xtemp3;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/inx.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp4 from index_rec where symbol='INX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp4;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp4;",normval);
rq(tempstring);
rc("drop table xtemp4;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/oex.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp5 from index_rec where symbol='OEX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp5;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp5;",normval);
rq(tempstring);
rc("drop table xtemp5;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mid.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp6 from index_rec where symbol='MID' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp6;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp6;",normval);
rq(tempstring);
rc("drop table xtemp6;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/iix.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp7 from index_rec where symbol='IIX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp7;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp7;",normval);
rq(tempstring);
rc("drop table xtemp7;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mmx.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp8 from index_rec where symbol='MMX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp8;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp8;",normval);
rq(tempstring);
rc("drop table xtemp8;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/wsx.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp9 from index_rec where symbol='WSX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp9;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp9;",normval);
rq(tempstring);
rc("drop table xtemp9;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/rut.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp10 from index_rec where symbol='RUT' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp10;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp10;",normval);
rq(tempstring);
rc("drop table xtemp10;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/pse.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp11 from index_rec where symbol='PSE' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp11;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp11;",normval);
rq(tempstring);
rc("drop table xtemp11;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/xci.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp12 from index_rec where symbol='XCI' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp12;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp12;",normval);
rq(tempstring);
rc("drop table xtemp12;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mex.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp13 from index_rec where symbol='MEX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp13;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp13;",normval);
rq(tempstring);
rc("drop table xtemp13;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/to35.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp14 from index_rec where symbol='TO35' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp14;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp14;",normval);
rq(tempstring);
rc("drop table xtemp14;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/sox.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp15 from index_rec where symbol='SOX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp15;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp15;",normval);
rq(tempstring);
rc("drop table xtemp15;");
fclose(outfile);

/* bad table cry0
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/cry0.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp16 from index_rec where symbol='CRY0' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp16;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp16;",normval);
rq(tempstring);
rc("drop table xtemp16;");
fclose(outfile);
*/

/* bad table dxy0
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/dxy0.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp17 from index_rec where symbol='DXY0' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp17;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp17;",normval);
rq(tempstring);
rc("drop table xtemp17;");
fclose(outfile);
*/

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/xau.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp18 from index_rec where symbol='XAU' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp18;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp18;",normval);
rq(tempstring);
rc("drop table xtemp18;");
fclose(outfile);

/* 2 weeks - 14 days */
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/indu_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp19 from index_rec where symbol='INDU' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp19;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp19;",normval);
rq(tempstring);
rc("drop table xtemp19;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/tran_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp20 from index_rec where symbol='TRAN' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp20;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp20;",normval);
rq(tempstring);
rc("drop table xtemp20;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/util_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp21 from index_rec where symbol='UTIL' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp21;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp21;",normval);
rq(tempstring);
rc("drop table xtemp21;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/compx_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp22 from index_rec where symbol='COMPX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp22;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp22;",normval);
rq(tempstring);
rc("drop table xtemp22;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/inx_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp23 from index_rec where symbol='INX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp23;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp23;",normval);
rq(tempstring);
rc("drop table xtemp23;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/oex_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp24 from index_rec where symbol='OEX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp24;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp24;",normval);
rq(tempstring);
rc("drop table xtemp24;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mid_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp25 from index_rec where symbol='MID' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp25;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp25;",normval);
rq(tempstring);
rc("drop table xtemp25;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/iix_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp26 from index_rec where symbol='IIX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp26;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp26;",normval);
rq(tempstring);
rc("drop table xtemp26;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mmx_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp27 from index_rec where symbol='MMX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp27;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp27;",normval);
rq(tempstring);
rc("drop table xtemp27;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/wsx_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp28 from index_rec where symbol='WSX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp28;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp28;",normval);
rq(tempstring);
rc("drop table xtemp28;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/rut_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp29 from index_rec where symbol='RUT' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp29;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp29;",normval);
rq(tempstring);
rc("drop table xtemp29;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/pse_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp30 from index_rec where symbol='PSE' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp30;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp30;",normval);
rq(tempstring);
rc("drop table xtemp30;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/xci_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp31 from index_rec where symbol='XCI' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp31;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp31;",normval);
rq(tempstring);
rc("drop table xtemp31;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mex_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp32 from index_rec where symbol='MEX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp32;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp32;",normval);
rq(tempstring);
rc("drop table xtemp32;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/to35_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp33 from index_rec where symbol='TO35' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp33;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp33;",normval);
rq(tempstring);
rc("drop table xtemp33;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/sox_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp34 from index_rec where symbol='SOX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('90 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp34;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp34;",normval);
rq(tempstring);
rc("drop table xtemp34;");
fclose(outfile);

/* bad table
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/cry0_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp35 from index_rec where symbol='CRY0' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp35;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp35;",normval);
rq(tempstring);
rc("drop table xtemp35;");
fclose(outfile);
*/

/* bad table
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/dxy0_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp36 from index_rec where symbol='DXY0' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp36;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp36;",normval);
rq(tempstring);
rc("drop table xtemp36;");
fclose(outfile);
*/

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/xau_14.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp37 from index_rec where symbol='XAU' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('14 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp37;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp37;",normval);
rq(tempstring);
rc("drop table xtemp37;");
fclose(outfile);

/* month - 28 days */
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/indu_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp38 from index_rec where symbol='INDU' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp38;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp38;",normval);
rq(tempstring);
rc("drop table xtemp38;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/tran_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp39 from index_rec where symbol='TRAN' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp39;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp39;",normval);
rq(tempstring);
rc("drop table xtemp39;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/util_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp40 from index_rec where symbol='UTIL' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp40;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp40;",normval);
rq(tempstring);
rc("drop table xtemp40;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/compx_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp41 from index_rec where symbol='COMPX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp41;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp41;",normval);
rq(tempstring);
rc("drop table xtemp41;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/inx_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp42 from index_rec where symbol='INX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp42;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp42;",normval);
rq(tempstring);
rc("drop table xtemp42;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/oex_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp43 from index_rec where symbol='OEX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp43;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp43;",normval);
rq(tempstring);
rc("drop table xtemp43;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mid_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp44 from index_rec where symbol='MID' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp44;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp44;",normval);
rq(tempstring);
rc("drop table xtemp44;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/iix_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp45 from index_rec where symbol='IIX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp45;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp45;",normval);
rq(tempstring);
rc("drop table xtemp45;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mmx_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp46 from index_rec where symbol='MMX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp46;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp46;",normval);
rq(tempstring);
rc("drop table xtemp46;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/wsx_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp47 from index_rec where symbol='WSX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp47;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp47;",normval);
rq(tempstring);
rc("drop table xtemp47;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/rut_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp48 from index_rec where symbol='RUT' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp48;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp48;",normval);
rq(tempstring);
rc("drop table xtemp48;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/pse_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp49 from index_rec where symbol='PSE' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp49;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp49;",normval);
rq(tempstring);
rc("drop table xtemp49;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/xci_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp50 from index_rec where symbol='XCI' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp50;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp50;",normval);
rq(tempstring);
rc("drop table xtemp50;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mex_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp51 from index_rec where symbol='MEX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp51;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp51;",normval);
rq(tempstring);
rc("drop table xtemp51;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/to35_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp52 from index_rec where symbol='TO35' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp52;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp52;",normval);
rq(tempstring);
rc("drop table xtemp52;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/sox_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp53 from index_rec where symbol='SOX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp53;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp53;",normval);
rq(tempstring);
rc("drop table xtemp53;");
fclose(outfile);

/* bad table cry0
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/cry0_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp54 from index_rec where symbol='CRY0' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp54;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp54;",normval);
rq(tempstring);
rc("drop table xtemp54;");
fclose(outfile);
*/

/* bad table dxy0
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/dxy0_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp55 from index_rec where symbol='DXY0' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp55;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp55;",normval);
rq(tempstring);
rc("drop table xtemp55;");
fclose(outfile);
*/

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/xau_28.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp56 from index_rec where symbol='XAU' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('28 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp56;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp56;",normval);
rq(tempstring);
rc("drop table xtemp56;");
fclose(outfile);

/* 6 mos. - 182 days */
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/indu_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp57 from index_rec where symbol='INDU' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp57;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp57;",normval);
rq(tempstring);
rc("drop table xtemp57;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/tran_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp58 from index_rec where symbol='TRAN' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp58;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp58;",normval);
rq(tempstring);
rc("drop table xtemp58;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/util_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp59 from index_rec where symbol='UTIL' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp59;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp59;",normval);
rq(tempstring);
rc("drop table xtemp59;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/compx_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp60 from index_rec where symbol='COMPX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp60;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp60;",normval);
rq(tempstring);
rc("drop table xtemp60;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/inx_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp61 from index_rec where symbol='INX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp61;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp61;",normval);
rq(tempstring);
rc("drop table xtemp61;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/oex_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp62 from index_rec where symbol='OEX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp62;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp62;",normval);
rq(tempstring);
rc("drop table xtemp62;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mid_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp63 from index_rec where symbol='MID' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp63;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp63;",normval);
rq(tempstring);
rc("drop table xtemp63;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/iix_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp64 from index_rec where symbol='IIX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp64;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp64;",normval);
rq(tempstring);
rc("drop table xtemp64;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mmx_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp65 from index_rec where symbol='MMX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp65;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp65;",normval);
rq(tempstring);
rc("drop table xtemp65;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/wsx_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp66 from index_rec where symbol='WSX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp66;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp66;",normval);
rq(tempstring);
rc("drop table xtemp66;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/rut_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp67 from index_rec where symbol='RUT' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp67;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp67;",normval);
rq(tempstring);
rc("drop table xtemp67;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/pse_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp68 from index_rec where symbol='PSE' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp68;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp68;",normval);
rq(tempstring);
rc("drop table xtemp68;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/xci_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp69 from index_rec where symbol='XCI' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp69;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp69;",normval);
rq(tempstring);
rc("drop table xtemp69;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/mex_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp70 from index_rec where symbol='MEX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp70;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp70;",normval);
rq(tempstring);
rc("drop table xtemp70;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/to35_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp71 from index_rec where symbol='TO35' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp71;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp71;",normval);
rq(tempstring);
rc("drop table xtemp71;");
fclose(outfile);

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/sox_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp72 from index_rec where symbol='SOX' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp72;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp72;",normval);
rq(tempstring);
rc("drop table xtemp72;");
fclose(outfile);

/* bad table cry0
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/cry0_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp73 from index_rec where symbol='CRY0' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp73;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp73;",normval);
rq(tempstring);
rc("drop table xtemp73;");
fclose(outfile);
*/

/* bad table dxy0
if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/dxy0_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp74 from index_rec where symbol='DXY0' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp74;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp74;",normval);
rq(tempstring);
rc("drop table xtemp74;");
fclose(outfile);
*/

if((outfile=(FILE*)fopen("/home/rossw/internet/stocks/data/xau_182.dat","w"))==NULL) {printf("Cannot open file for write\n");exit(1);}
rc("select tstamp, symbol, last, change into xtemp75 from index_rec where symbol='XAU' and age(date_trunc('day',tstamp),date_trunc('day','now'::datetime)) > timespan('182 days 2 hours ago'::reltime) order by tstamp;");
rq_val("select * from xtemp75;");
 normval= strtod(PQgetvalue(qresult,0,2),&endptr); 
sprintf(tempstring,"select tstamp, symbol, last, change, (last/%f) as normval from xtemp75;",normval);
rq(tempstring);
rc("drop table xtemp75;");
fclose(outfile);


PQfinish(conn);
} /* End of main() */

int rc(char *command_string) /* doesn't return data */
{
PQclear(qresult);
qresult=PQexec(conn,command_string);
if (PQresultStatus(qresult) != PGRES_COMMAND_OK)
  {
  fprintf(stderr,"Command %s failed.\n",command_string);
  PQfinish(conn);
  exit(ERROR);
  }
return 0;
}

int rq(char *query_string) /* returns data */
  {
  PQclear(qresult);
  qresult=PQexec(conn,query_string);
  if (PQresultStatus(qresult) != PGRES_TUPLES_OK)
    {
    fprintf(stderr, "Query %s failed.\n",query_string);
    PQfinish(conn); 
    exit(ERROR);
    }
  list_qresult();
  return 0;
  }

int list_qresult()
{
int nFields,i,j;

nFields=PQnfields(qresult);
for (i=0;i<PQntuples(qresult);i++)
  {
  for (j=0;j<nFields;j++)
    {
    fprintf(outfile,"%s ",PQgetvalue(qresult,i,j));
    }
  fprintf(outfile,"\n");
  }
return 0;
}

int rq_val(char *query_string) /* returns data */
  {
  PQclear(qresult);
  qresult=PQexec(conn,query_string);
  if (PQresultStatus(qresult) != PGRES_TUPLES_OK)
    {
    fprintf(stderr, "Query %s failed.\n",query_string);
    PQfinish(conn);
    exit(ERROR);
    }
  return 0;
  }


