1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
|
/*
* gstalker stock charter
*
* Copyright (c) 1998 Stefan S. Stratigakos
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include "gstalker.h"
#include <dirent.h>
/***********************************************************************************
converts old chart data to ascii version
**********************************************************************************/
void convert_old_chart (gchar *symbol)
{
gint tint;
FILE *infile, *outfile;
GString *tstring;
extern struct record config;
extern struct old_header oheader;
extern struct old_data odata;
tstring = g_string_new (NULL);
g_string_sprintf (tstring, "%s%s", config.datapath, symbol);
infile = fopen(tstring->str, "rb");
fread(&oheader, sizeof (oheader), 1, infile);
/* save index data */
save_index_file_type (symbol, "chart");
save_index_symbol (oheader.symbol);
save_index_name (oheader.symbol, oheader.name);
g_string_sprintf (tstring, "%ld", oheader.first_date);
save_index_first_date (oheader.symbol, tstring->str);
g_string_sprintf (tstring, "%ld", oheader.last_date);
save_index_last_date (oheader.symbol, tstring->str);
save_index_records (oheader.symbol, oheader.records);
save_index_line_pixelspace (oheader.symbol, oheader.pixelspace);
save_index_ohlc_pixelspace (oheader.symbol, 6);
save_index_threshold (oheader.symbol, oheader.threshold);
/* convert the chart data */
g_string_sprintf (tstring, "%s%s", config.datapath, oheader.symbol);
outfile = fopen (tstring->str, "w");
for (tint = 0; tint < oheader.records; tint++)
{
fread(&odata, sizeof (odata), 1, infile);
g_string_sprintf (tstring, "%ld %g %g %g %g %ld %ld\n", odata.date, odata.open,
odata.high, odata.low, odata.close, odata.volume, odata.openint);
fputs (tstring->str, outfile);
}
fclose (infile);
fclose (outfile);
}
/***********************************************************************************
gets the gstalker file type field from the index file and puts the result into gstring
***********************************************************************************/
void
get_index_file_type (gchar *symbol)
{
gchar *tstringp;
GString *tstring;
extern struct record config;
extern GString *gstring;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/FILE");
tstringp = gnome_config_get_string (tstring->str);
g_string_assign (gstring, tstringp);
g_free (tstringp);
g_string_free (tstring, TRUE);
}
/***********************************************************************************
gets the gstalker symbol field from the index file and puts the result into gstring
***********************************************************************************/
void
get_index_symbol (gchar *symbol)
{
gchar *tstringp;
GString *tstring;
extern struct record config;
extern GString *gstring;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/SYMBOL");
tstringp = gnome_config_get_string (tstring->str);
g_string_assign (gstring, tstringp);
g_free (tstringp);
g_string_free(tstring, TRUE);
}
/***********************************************************************************
gets the name field from the index file and puts the result into gstring
***********************************************************************************/
void
get_index_name (gchar *symbol)
{
gchar *tstringp;
GString *tstring;
extern struct record config;
extern GString *gstring;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/NAME");
tstringp = gnome_config_get_string (tstring->str);
g_string_assign (gstring, tstringp);
g_free (tstringp);
g_string_free(tstring, TRUE);
}
/***********************************************************************************
gets the gstalker first_date field from the index file and puts the result into gstring
***********************************************************************************/
void
get_index_first_date (gchar *symbol)
{
gchar *tstringp;
GString *tstring;
extern struct record config;
extern GString *gstring;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/FIRST_DATE");
tstringp = gnome_config_get_string (tstring->str);
g_string_assign (gstring, tstringp);
g_free (tstringp);
g_string_free(tstring, TRUE);
}
/***********************************************************************************
gets the gstalker last_date field from the index file and puts the result into gstring
***********************************************************************************/
void
get_index_last_date (gchar *symbol)
{
gchar *tstringp;
GString *tstring;
extern struct record config;
extern GString *gstring;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/LAST_DATE");
tstringp = gnome_config_get_string (tstring->str);
g_string_assign (gstring, tstringp);
g_free (tstringp);
g_string_free(tstring, TRUE);
}
/***********************************************************************************
gets the records field from the index file and returns the result as an int
***********************************************************************************/
gint
get_index_records (gchar *symbol)
{
gint tint;
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/RECORDS");
tint = gnome_config_get_int (tstring->str);
g_string_free(tstring, TRUE);
return tint;
}
/***********************************************************************************
gets the line_pixelspace field from the index file and returns the result as an int
***********************************************************************************/
gint
get_index_line_pixelspace (gchar *symbol)
{
gint tint;
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/LINE_PIXELSPACE");
tint = gnome_config_get_int (tstring->str);
g_string_free(tstring, TRUE);
return tint;
}
/***********************************************************************************
gets the ohlc_pixelspace field from the index file and returns the result as an int
***********************************************************************************/
gint
get_index_ohlc_pixelspace (gchar *symbol)
{
gint tint;
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/OHLC_PIXELSPACE");
tint = gnome_config_get_int (tstring->str);
g_string_free(tstring, TRUE);
return tint;
}
/***********************************************************************************
gets the threshold field from the index file and returns the result as a float
***********************************************************************************/
gfloat
get_index_threshold (gchar *symbol)
{
gfloat tfloat;
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/THRESHOLD");
tfloat = gnome_config_get_float (tstring->str);
g_string_free(tstring, TRUE);
return tfloat;
}
/***********************************************************************************
saves the gstalker file type field to the index file
***********************************************************************************/
void
save_index_file_type (gchar *symbol, gchar *type)
{
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/FILE");
gnome_config_set_string (tstring->str, type);
g_string_free (tstring, TRUE);
gnome_config_sync ();
}
/***********************************************************************************
saves the symbol field to the index file
***********************************************************************************/
void
save_index_symbol (gchar *symbol)
{
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/SYMBOL");
gnome_config_set_string (tstring->str, symbol);
g_string_free(tstring, TRUE);
gnome_config_sync ();
}
/***********************************************************************************
saves the name field to the index file
***********************************************************************************/
void
save_index_name (gchar *symbol, gchar *name)
{
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/NAME");
gnome_config_set_string (tstring->str, name);
g_string_free(tstring, TRUE);
gnome_config_sync ();
}
/***********************************************************************************
saves the first_date field to the index ***********************************************************************************/
void
save_index_first_date (gchar *symbol, gchar *first_date)
{
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/FIRST_DATE");
gnome_config_set_string (tstring->str, first_date);
g_string_free(tstring, TRUE);
gnome_config_sync ();
}
/***********************************************************************************
save the last_date field to the index file
***********************************************************************************/
void
save_index_last_date (gchar *symbol, gchar *last_date)
{
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/LAST_DATE");
gnome_config_set_string (tstring->str, last_date);
g_string_free(tstring, TRUE);
gnome_config_sync ();
}
/***********************************************************************************
save the records field to the index file
***********************************************************************************/
void
save_index_records (gchar *symbol, gint records)
{
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/RECORDS");
gnome_config_set_int (tstring->str, records);
g_string_free(tstring, TRUE);
gnome_config_sync ();
}
/***********************************************************************************
save the line_pixelspace field to the index file
***********************************************************************************/
void
save_index_line_pixelspace (gchar *symbol, gint line_pixelspace)
{
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/LINE_PIXELSPACE");
gnome_config_set_int (tstring->str, line_pixelspace);
g_string_free(tstring, TRUE);
gnome_config_sync ();
}
/***********************************************************************************
save the ohlc_pixelspace to the index file
***********************************************************************************/
void
save_index_ohlc_pixelspace (gchar *symbol, gint ohlc_pixelspace)
{
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/OHLC_PIXELSPACE");
gnome_config_set_int (tstring->str, ohlc_pixelspace);
g_string_free(tstring, TRUE);
gnome_config_sync ();
}
/***********************************************************************************
save the threshold field to the index file
***********************************************************************************/
void
save_index_threshold (gchar *symbol, gfloat threshold)
{
GString *tstring;
extern struct record config;
tstring = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/THRESHOLD");
gnome_config_set_float (tstring->str, threshold);
g_string_free(tstring, TRUE);
gnome_config_sync ();
}
/****************************************************************************************
Create a new chart
****************************************************************************************/
gint
create_new_chart (gchar *symbol)
{
gint tint=0;
GString *tstring, *tstring2;
extern struct record config;
tstring = g_string_new(NULL);
tstring2 = g_string_new(NULL);
g_string_sprintf (tstring, "%s%s%s%s", "=", config.indexpath, symbol, "=/Data/");
/* save FILE data */
g_string_sprintf (tstring2, "%s%s", tstring->str, "FILE");
gnome_config_set_string (tstring2->str, "chart");
/* save SYMBOL data */
g_string_sprintf (tstring2, "%s%s", tstring->str, "SYMBOL");
gnome_config_set_string (tstring2->str, symbol);
/* save NAME data */
g_string_sprintf (tstring2, "%s%s", tstring->str, "NAME");
gnome_config_set_string (tstring2->str, symbol);
/* save FIRST_DATE data */
g_string_sprintf (tstring2, "%s%s", tstring->str, "FIRST_DATE");
gnome_config_set_string (tstring2->str, "99999999");
/* save LAST_DATE data */
g_string_sprintf (tstring2, "%s%s", tstring->str, "LAST_DATE");
gnome_config_set_string (tstring2->str, "0");
/* save RECORDS data */
g_string_sprintf (tstring2, "%s%s", tstring->str, "RECORDS");
gnome_config_set_int (tstring2->str, 0);
/* save LINE_PIXELSPACE data */
g_string_sprintf (tstring2, "%s%s", tstring->str, "LINE_PIXELSPACE");
gnome_config_set_int (tstring2->str, 1);
/* save OHLC_PIXELSPACE data */
g_string_sprintf (tstring2, "%s%s", tstring->str, "OHLC_PIXELSPACE");
gnome_config_set_int (tstring2->str, 6);
/* save THRESHOLD data */
g_string_sprintf (tstring2, "%s%s", tstring->str, "THRESHOLD");
gnome_config_set_float (tstring2->str, 0);
g_string_free(tstring, TRUE);
g_string_free(tstring2, TRUE);
gnome_config_sync ();
return tint;
}
/*******************************************************************************************
Insert a new record into a chart
******************************************************************************************/
void
insert_chart_record (gchar *symbol, gchar *date, gchar *data)
{
gint flag=0, records;
GString *tstring, *temppath, *first_date, *last_date;
gchar buffer[1024], tdate[10];
FILE *infile=0, *tempfile=0;
extern struct record config;
extern GString *gstring;
tstring = g_string_new(NULL);
temppath = g_string_new(NULL);
first_date = g_string_new(NULL);
last_date = g_string_new(NULL);
/* get some info for the chart */
records = get_index_records(symbol);
get_index_first_date(symbol);
g_string_assign (first_date, gstring->str);
get_index_last_date(symbol);
g_string_assign (last_date, gstring->str);
if (records == 0)
{
flag = 1;
g_string_assign (first_date, date);
g_string_assign (last_date, date);
}
else
{
/* check if data is the newest record */
if (atol (date) < atol(first_date->str))
{
/* yes, so update the string */
g_string_assign (first_date, date);
/* we have to insert as first record */
flag = 2;
}
else
{
/* check if data is the oldest record */
if (atol (date) > atol(last_date->str))
{
/* yes, so update the string */
g_string_assign (last_date, date);
/* great, we only have to append the data */
flag = 1;
}
}
}
/* check if the record already exists */
if (! flag)
{
/* loop through the file and check for record with same date */
g_string_sprintf (tstring, "%s%s", config.datapath, symbol);
infile = fopen(tstring->str, "r");
while (fgets (buffer, sizeof(buffer), infile) != NULL)
{
/* get the date */
sscanf (buffer, "%s", tdate);
/* check for equality */
if (atol(date) == atol(tdate))
{
flag = 3;
break;
}
}
}
switch (flag)
{
case 0: /* here we have to insert a record into the chart file */
/* reset the flag for later use */
flag = 0;
/* increment the record count */
records++;
/* open a temp file */
g_string_sprintf (temppath, "%s%s", config.datapath, "temp.txt");
tempfile = fopen(temppath->str, "w");
/* update the index file */
save_index_records(symbol, records);
save_index_first_date(symbol, first_date->str);
save_index_last_date(symbol, last_date->str);
/* open up the chart file */
if (infile)
rewind(infile);
else
{
g_string_sprintf (tstring, "%s%s", config.datapath, symbol);
infile = fopen(tstring->str, "r");
}
/* loop through the chart file and write out the records to the temp file */
while (fgets (buffer, sizeof(buffer), infile) != NULL)
{
sscanf (buffer, "%s", tdate);
if (atol(tdate) < atol(date))
fputs(buffer, tempfile);
else
{
if (! flag)
{
/* save the new record */
fputs (data, tempfile);
fputs ("\n", tempfile);
/* save the current record as well */
fputs (buffer, tempfile);
flag++;
}
else
{
/* save the current record */
fputs (buffer, tempfile);
}
}
}
/* we are done inserting */
fclose(infile);
fclose(tempfile);
/* delete the old chart file and rename the tempfile to the new chart file */
g_string_sprintf (tstring, "%s%s", config.datapath, symbol);
unlink(tstring->str);
rename(temppath->str, tstring->str);
break;
case 1: /* here we just append the record to the file */
/* increment the record count */
records++;
/* update the index file */
save_index_records(symbol, records);
save_index_first_date(symbol, first_date->str);
save_index_last_date(symbol, last_date->str);
/* open up the data file for appending */
g_string_sprintf (tstring, "%s%s", config.datapath, symbol);
tempfile = fopen(tstring->str, "a");
/* save the new record */
fputs (data, tempfile);
fputs ("\n", tempfile);
/* we are done appending */
fclose(tempfile);
break;
case 2: /* here we insert new record as the first one */
/* increment the record count */
records++;
/* open a temp file */
g_string_sprintf (temppath, "%s%s", config.datapath, "temp.txt");
tempfile = fopen(temppath->str, "w");
/* update the index file */
save_index_records(symbol, records);
save_index_first_date(symbol, first_date->str);
save_index_last_date(symbol, last_date->str);
/* save the new record */
fputs (data, tempfile);
fputs ("\n", tempfile);
/* open up the chart file */
if (infile)
rewind(infile);
else
{
g_string_sprintf (tstring, "%s%s", config.datapath, symbol);
infile = fopen(tstring->str, "r");
}
/* loop through the chart file and write out the records to the temp file */
while (fgets (buffer, sizeof(buffer), infile) != NULL)
fputs(buffer, tempfile);
/* we are done inserting */
fclose(infile);
fclose(tempfile);
/* delete the old chart file and rename the tempfile to the new chart file */
g_string_sprintf (tstring, "%s%s", config.datapath, symbol);
unlink(tstring->str);
rename(temppath->str, tstring->str);
break;
case 3: /* here we have to overwrite an existing record */
/* reset the flag for later use */
flag = 0;
/* open a temp file */
g_string_sprintf (temppath, "%s%s", config.datapath, "temp.txt");
tempfile = fopen(temppath->str, "w");
/* open up the chart file */
if (infile)
rewind(infile);
else
{
g_string_sprintf (tstring, "%s%s", config.datapath, symbol);
infile = fopen(tstring->str, "r");
}
/* loop through the chart file and write out the records to the temp file */
while (fgets (buffer, sizeof(buffer), infile) != NULL)
{
sscanf (buffer, "%s", tdate);
if (atol(tdate) != atol(date))
fputs(buffer, tempfile);
else
{
if (! flag)
{
/* save the new record */
fputs (data, tempfile);
fputs ("\n", tempfile);
/* update the flag so we don't write it again */
flag++;
}
else
{
/* save the current record */
fputs (buffer, tempfile);
}
}
}
/* we are done inserting */
fclose(infile);
fclose(tempfile);
/* delete the old chart file and rename the tempfile to the new chart file */
g_string_sprintf (tstring, "%s%s", config.datapath, symbol);
unlink(tstring->str);
rename(temppath->str, tstring->str);
break;
default: break;
}
g_string_free(tstring, TRUE);
g_string_free(temppath, TRUE);
g_string_free(first_date, TRUE);
g_string_free(last_date, TRUE);
}
/******************************************************************************************
Loads chart data into memory and displays it.
*******************************************************************************************/
void
load_file (gchar *symbol)
{
FILE *infile;
gint records=0;
GString *tstring;
gchar buffer[1024], date_string[25];
guint tuint=0, oldweek, newweek;
gfloat tfloat=0, open=0, high=0, low=0, close=0, wopen=0, whigh=0, wlow=9999999, wclose=0;
gfloat mopen=0, mhigh=0, mlow=9999999, mclose=0;
glong date, volume, openint, wvolume=0, wopenint=0;
glong mvolume=0, mopenint=0;
GtkWidget *dialog;
GDateMonth oldmonth, newmonth;
extern GDate *gdate;
extern gchar *mess_cant_open_chart;
extern gfloat maxhigh, maxlow, range, current_threshold;
extern gulong volume_high;
extern gint chartflag, current_records, current_line_pixelspace, current_ohlc_pixelspace;
extern gint array_size;
extern struct record config;
extern GtkWidget *main_window;
extern GArray *open_array, *high_array, *low_array, *close_array, *volume_array;
extern GArray *openint_array, *date_array;
extern GString *current_symbol, *current_name, *current_file_type, *current_first_date;
extern GString *current_last_date, *gstring;
tstring = g_string_new(NULL);
maxhigh = 0;
maxlow = 9999999;
volume_high = 0;
/* check if file exists */
g_string_sprintf (tstring, "%s%s", config.datapath, symbol);
infile = fopen (tstring->str, "r");
if (! infile)
return;
else
fclose (infile);
/* check for older chart version */
g_string_sprintf (tstring, "%s%s", config.indexpath, symbol);
infile = fopen (tstring->str, "r");
if (! infile)
{
/* it's an older file so convert it to new format */
convert_old_chart (symbol);
}
else
fclose (infile);
/* free mem for chart data */
if (date_array)
{
g_array_free (date_array, TRUE);
date_array = g_array_new(FALSE, FALSE, sizeof(glong));
}
else
date_array = g_array_new(FALSE, FALSE, sizeof(glong));
if (open_array)
{
g_array_free (open_array, TRUE);
open_array = g_array_new(FALSE, FALSE, sizeof(gfloat));
}
else
open_array = g_array_new(FALSE, FALSE, sizeof(gfloat));
if (high_array)
{
g_array_free (high_array, TRUE);
high_array = g_array_new(FALSE, FALSE, sizeof(gfloat));
}
else
high_array = g_array_new(FALSE, FALSE, sizeof(gfloat));
if (low_array)
{
g_array_free (low_array, TRUE);
low_array = g_array_new(FALSE, FALSE, sizeof(gfloat));
}
else
low_array = g_array_new(FALSE, FALSE, sizeof(gfloat));
if (close_array)
{
g_array_free (close_array, TRUE);
close_array = g_array_new(FALSE, FALSE, sizeof(gfloat));
}
else
close_array = g_array_new(FALSE, FALSE, sizeof(gfloat));
if (volume_array)
{
g_array_free (volume_array, TRUE);
volume_array = g_array_new(FALSE, FALSE, sizeof(glong));
}
else
volume_array = g_array_new(FALSE, FALSE, sizeof(glong));
if (openint_array)
{
g_array_free (openint_array, TRUE);
openint_array = g_array_new(FALSE, FALSE, sizeof(glong));
}
else
openint_array = g_array_new(FALSE, FALSE, sizeof(glong));
/* load the chart header data */
get_index_symbol (symbol);
g_string_assign (current_symbol, gstring->str);
get_index_name (symbol);
g_string_assign (current_name, gstring->str);
get_index_file_type (symbol);
g_string_assign (current_file_type, gstring->str);
get_index_first_date (symbol);
g_string_assign (current_first_date, gstring->str);
get_index_last_date (symbol);
g_string_assign (current_last_date, gstring->str);
current_records = get_index_records (symbol);
current_line_pixelspace = get_index_line_pixelspace (symbol);
current_ohlc_pixelspace = get_index_ohlc_pixelspace (symbol);
current_threshold = get_index_threshold (symbol);
/* open the chart file */
g_string_sprintf (tstring, "%s%s", config.datapath, symbol);
infile = fopen(tstring->str, "r");
if (! infile)
{
dialog = gnome_app_error (GNOME_APP(main_window), mess_cant_open_chart);
gtk_widget_show(dialog);
return;
}
if (strstr(config.chart_type, "Weekly"))
{
array_size = 0;
fgets (buffer, sizeof(buffer), infile);
sscanf (buffer, "%s", date_string);
create_gdate_from_string (date_string);
oldweek = g_date_monday_week_of_year (gdate);
rewind(infile);
while (fgets (buffer, sizeof(buffer), infile) != NULL)
{
/* tokenize the data */
sscanf (buffer, "%s %g %g %g %g %ld %ld", date_string, &open, &high, &low, &close,
&volume, &openint);
create_gdate_from_string (date_string);
newweek = g_date_monday_week_of_year (gdate);
if (newweek != oldweek)
{
oldweek = newweek;
chart_array_append(atol(tstring->str),wopen,whigh,wlow,wclose,wvolume,wopenint);
g_string_assign (tstring, date_string);
wopen = open;
whigh = high;
wlow = low;
wclose = close;
wvolume = volume;
wopenint = openint;
array_size++;
}
else
{
g_string_assign (tstring, date_string);
wclose = close;
wvolume = wvolume + volume;
wopenint = openint;
/* find the highest close value */
if (high != 0)
{
if (high > whigh)
whigh = high;
}
else
{
if (close > whigh)
whigh = close;
}
/* find the lowest close value */
if (low != 0)
{
if (low < wlow)
wlow = low;
}
else
{
if (close < wlow)
wlow = close;
}
/* find the highest volume value */
if (volume > volume_high)
volume_high = volume;
}
}
chart_array_append(atol(tstring->str),wopen,whigh,wlow,wclose,wvolume,wopenint);
array_size++;
}
if (strstr (config.chart_type, "Daily"))
{
/* check for maximum data points to load */
if (config.bars == 0)
{
/* loop through all the records */
while (fgets (buffer, sizeof(buffer), infile) != NULL)
{
/* tokenize the data */
sscanf (buffer, "%ld %g %g %g %g %ld %ld", &date, &open, &high, &low, &close,
&volume, &openint);
chart_array_append (date, open, high, low, close, volume, openint);
}
}
else
{
/* get the smallest number of data to load, either total records or maximum */
if (current_records < config.bars)
records = current_records;
else
{
records = config.bars;
/* read through the file until you get to the right one */
for (tuint = 0; tuint < (current_records - records); tuint++)
fgets (buffer, sizeof(buffer), infile);
}
/* loop through and load the remaining records */
while (fgets (buffer, sizeof(buffer), infile) != NULL)
{
sscanf (buffer, "%ld %g %g %g %g %ld %ld", &date, &open, &high, &low, &close,
&volume, &openint);
chart_array_append (date, open, high, low, close, volume, openint);
}
}
}
if (strstr(config.chart_type, "Monthly"))
{
if (current_line_pixelspace < 3)
current_line_pixelspace = 3;
array_size = 0;
fgets (buffer, sizeof(buffer), infile);
sscanf (buffer, "%s", date_string);
create_gdate_from_string (date_string);
oldmonth = g_date_month (gdate);
rewind(infile);
while (fgets (buffer, sizeof(buffer), infile) != NULL)
{
/* tokenize the data */
sscanf (buffer, "%s %g %g %g %g %ld %ld", date_string, &open, &high, &low, &close,
&volume, &openint);
create_gdate_from_string (date_string);
newmonth = g_date_month (gdate);
if (newmonth != oldmonth)
{
oldmonth = newmonth;
chart_array_append(atol(tstring->str),mopen,mhigh,mlow,mclose,mvolume,mopenint);
g_string_assign (tstring, date_string);
mopen = open;
mhigh = high;
mlow = low;
mclose = close;
mvolume = volume;
mopenint = openint;
array_size++;
}
else
{
g_string_assign (tstring, date_string);
mclose = close;
mvolume = wvolume + volume;
mopenint = openint;
/* find the highest close value */
if (high != 0)
{
if (high > mhigh)
mhigh = high;
}
else
{
if (close > mhigh)
mhigh = close;
}
/* find the lowest close value */
if (low != 0)
{
if (low < mlow)
mlow = low;
}
else
{
if (close < mlow)
mlow = close;
}
/* find the highest volume value */
if (volume > volume_high)
volume_high = volume;
}
}
chart_array_append (atol(tstring->str), mopen, mhigh, mlow, mclose, mvolume, mopenint);
array_size++;
}
fclose(infile);
/* calculate the range between the highest and lowest close for chart drawing */
tfloat = (maxhigh - maxlow) / 100;
maxhigh = maxhigh + tfloat;
maxlow = maxlow - tfloat;
range = maxhigh - maxlow;
update_name_display ();
/* update the flag so we know there is an active chart */
chartflag = 1;
/* update the data bar */
update_stats_bar ();
/* create the ma arrays */
if (config.moving_status)
create_ma_array(config.moving_type);
if (config.moving2_status)
create_ma2_array(config.moving2_type);
if (config.moving3_status)
create_ma3_array(config.moving3_type);
/* i have to do this in order update the display properly */
resize_chart ();
g_string_free(tstring, TRUE);
}
/***********************************************************************************
scans datapath for all charts and inserts info into the specified clist box
************************************************************************************/
void
show_chart_list (GtkWidget * chartlist)
{
GString *tstring;
struct dirent **dirlist;
gint tint, tint2;
extern struct record config;
tstring = g_string_new(NULL);
gtk_clist_freeze (GTK_CLIST (chartlist));
gtk_clist_clear (GTK_CLIST (chartlist));
/* get the list of files in the data directory */
tint = scandir (config.datapath, &dirlist, NULL, alphasort);
/* check if the directory is empty */
if (tint < 2)
return;
/* skip the . and .. directory files */
tint2 = tint;
for (tint = 2; tint < tint2; tint++)
{
/* get the chart info to put into the clist box */
display_chart_details (dirlist[tint]->d_name, chartlist);
}
gtk_clist_thaw (GTK_CLIST (chartlist));
g_string_free(tstring, TRUE);
}
/********************************************************************************************
This function gets the symbol, name, first date and last date data from filename and
appends that into the clist.
********************************************************************************************/
void
display_chart_details (gchar *filename, GtkWidget * clist)
{
GString *tstring, *tstring2, *tstring3, *tstring4;
gchar *line[4];
extern GString *gstring;
tstring = g_string_new(NULL);
tstring2 = g_string_new(NULL);
tstring3 = g_string_new(NULL);
tstring4 = g_string_new(NULL);
get_index_symbol (filename);
g_string_assign (tstring, gstring->str);
line[0] = tstring->str;
get_index_name (filename);
g_string_assign (tstring2, gstring->str);
line[1] = tstring2->str;
/* get the first date field */
/* convert the date from YYYYMMDD to MM/DD/YYYY */
get_index_first_date (filename);
g_string_assign (tstring3, gstring->str);
convert_date(tstring3->str);
g_string_assign (tstring3, gstring->str);
line[2] = tstring3->str;
/* get the last date field */
/* convert the date from YYYYMMDD to MM/DD/YYYY */
get_index_last_date (filename);
g_string_assign (tstring4, gstring->str);
convert_date(tstring4->str);
g_string_assign (tstring4, gstring->str);
line[3] = tstring4->str;
gtk_clist_append (GTK_CLIST (clist), line);
g_string_free(tstring, TRUE);
g_string_free(tstring2, TRUE);
g_string_free(tstring3, TRUE);
g_string_free(tstring4, TRUE);
}
|