工行纸黄金收益及收益率计算工具

本文介绍了一款针对工行纸黄金交易的收益及收益率计算工具,该工具能够自动分析从银行导出的纯文本数据,有效地统计多次交易后的总收益和收益率。此外,它还支持对时间有序的多文件数据进行统计操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工行纸黄金不同于股票交易账户, 它直接和银行账户绑定.在进行多次不同重量的买入和卖出后, 统计一段时间的收益和收益率变得比较困难.

下面的这个程序可以对工行的导出纯文本进行自动分析得到收益值和收益率, 并可以对时间有序的多数据文本文件进行统计操作.

[Environment: GCC-4.4.3.]

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h> /* isdigit */

#define GOLDCALC_VERSION "0.0.3 for ICBC paper gold."
#define GOLDCALC_DATE    "2011-12-28"

//#define DTEST
#ifdef DTEST
	#define dprint(x...) printf(x)
#else
	#define dprint(x...) 
#endif

#define MAX_FILE_NUMBER 10
#define MAX_PATH_LENGTH 150

enum {
	UNKOWN = 0,
	BUY,
	SELL,
};

struct one_trade_node {
	char date[11];
#if 0	
	char time[9];
	char no[10];
#endif
	int  type;
	float  price;
	float  weight;
	float  subsum;
	int  line;
	struct one_trade_node *next;
};		

static double current_price, current_weight, current_sum;

static inline int _validline(char *line)
{
	int i = 0x1 & (isdigit(line[0]) && isdigit(line[1]) && isdigit(line[2]) && isdigit(line[3]));
	return ((i) && (line[4] == '-') && (line[7] == '-'));
}

static double _katof(const char *str)
{
	char p[50];
	bzero(p, sizeof(p));
	char *q = p;
	while ( *str != '\0') {
		if (*str != ',')
			*q = *str, q++;
		str++;	
	}
	return (atof(p));	
}

static inline int _date_order_check(const char *new, const char *old)
{
	int i, ret=1;
	for (i=0; i<10; i++) {
		if (new[i]- old[i] != 0) {
			ret = ((new[i] - old[i] > 0) ? 1 : 0);
			break;
		}	
	}
	return (ret);	
}

static double _get_cost_sum(struct one_trade_node *head)
{
	struct one_trade_node *p = head->next;
	
	if (p->type != BUY) {
		dprint("This data file is probable incomplete, normal one should start with a BUY.\n");
	}	

	double cost, cash;
	int i = 0;
	cost = cash = 0;
	while(p) {
		i++;
		if (p->type == BUY) {
			if (p->subsum > cash) {
 				cost += p->subsum - cash;
				cash = 0;
			}	
			else
				cash -= p->subsum;
		}	
		else {
			cash += p->subsum;
		}	
		p = p->next;
		dprint("%2d cost: %0.2f\n", i, cost);
	}
	dprint("cost sum: %0.2f\n", cost);
	return (cost);
}

static int parser(FILE *fp, struct one_trade_node *head)
{
	char *lineptr = NULL;
	unsigned char *p, *q;
	size_t len;
	ssize_t read;
	int line_num = 0;
	int i;
	char *token, *saveptr;
	static char lastdate[11];
	static int inited = 0;
	struct one_trade_node *node;

	if (!inited) {
		bzero(lastdate, sizeof(lastdate));
		inited++;
	}
	
	while (head->next)
		head = head->next;

	while ((read = getline(&lineptr, &len, fp)) != -1) {
		line_num++;
		if (read < 30 || !_validline(lineptr))
			continue;	
		dprint("\n--------\n");
		dprint("line: %d\n", line_num);

		head->next = node = (struct one_trade_node *)malloc(sizeof(struct one_trade_node));
		bzero(node, sizeof(node));
		p = q = (unsigned char *)lineptr; // for recognizing Chinese characters.
		
		strncpy(node->date, (char*)p, 10);
		dprint("Node's date: %s, last node's date: %s\n", node->date, lastdate);
		if (!_date_order_check(node->date, lastdate)) {
			printf("Invalid date file with disorder date:  OLD: %s -> NEW: %s.\n", lastdate, node->date);
			goto ERROR;
		}	
		strncpy(lastdate, (char*)p, 10);
		
		node->line = line_num;
		for (i=0; i<read-4; i++) {
			if (p[i] == 0xc2 && p[i+1] == 0xf2 && p[i+2] == 0xc8 && p[i+3] == 0xeb)
				node->type = BUY; // buy
			else 
			if (p[i] == 0xc2 && p[i+1] == 0xf4 && p[i+2] == 0xb3 && p[i+3] == 0xf6)
				node->type = SELL; // sell
		}	
	
		for (i=0;;q=NULL,i++) {
			token = strtok_r((char*)q, " ", &saveptr);	
			if (token == NULL) break;
			if (i == 6) {
				node->price = _katof(token);
				dprint("Price token: %s, %.02f\n", token, node->price); 
			}
			if (i == 7) {
				node->weight = _katof(token);
				dprint("Weight token: %s, %.02f\n", token, node->weight); 
			}
			if (i == 8) {
				node->subsum = _katof(token);
				dprint("Subsum token: %s, %.02f\n", token, node->subsum); 
			}	
		}	
		if (node->type == 0 || i != 10 )
			goto ERROR;
		head = head->next;
	}

	if (lineptr)
		free(lineptr);
	return 0;
	
ERROR:	
	if (lineptr)
		free(lineptr);
	printf("Parsering error in line: %d\n", line_num);
	return -1;
}

static void print(struct one_trade_node *head)
{
	double buysum, sellsum, totalsum, earnsum, costsum, earnratio;
	buysum = sellsum = totalsum = earnsum = earnratio = 0;
	struct one_trade_node *last, *q, *p = head->next; 
	q = p;
	printf("---------------------------------------------------------\n");
	if ((int)current_sum == 0)
	printf("Warning: Please confirm that no gold left currently.     \n");
	printf("---------------------------------------------------------\n");
	printf("                   HISTORY RECORD                        \n");
	printf("                   --------------                        \n");
	printf("DATE          TYPE    PRICE       WEIGHT      SUM        \n");
	printf("---------------------------------------------------------\n");

	while (p) {
		printf("%s:   %-4s    %-8.2f    %-8.2f    %-8.2f\n", p->date, (p->type==1) ? "BUY" : "SELL",
			    p->price, p->weight, p->subsum);
		if (p->type == 1)
			buysum += p->subsum;
		else
			sellsum += p->subsum;
		last = p;
		p = p->next;
	}
	if (p == q) {
		printf("No valid  data can display.\n");
		printf("---------------------------------------------------------\n");
		return;
	}
	printf("---------------------------------------------------------\n");
	sellsum += current_sum;
	totalsum = buysum + sellsum;
	earnsum = sellsum - buysum;
	costsum = _get_cost_sum(head); // costsum is the maximum cost value during the investing period.
	earnratio = earnsum / costsum;
	printf("                       SUMMARY                            \n");
	printf("                   ---------------                        \n");
	printf("Current Sum Pending Sell: %.2f Yuan[%.f gram, %.02f Yuan/g]\n", current_sum, current_weight, current_price);
	printf("Totally Earning:          %0.2f Yuan\n", earnsum);
	printf("Totally Earning Ratio:    %.2f%%\n", earnratio*100);
	printf("Totally Cost:             %0.2f Yuan\n", costsum);
	printf("Date Range:               %s -> %s \n", head->next->date, last->date);
	printf("---------------------------------------------------------\n");
	return;
}

static void clear(struct one_trade_node *head)
{
	struct one_trade_node *q, *p;
	p = head;
	while(p) {
		q = p->next;
		free(p);
		p = q;
	}	
	return;
}

static void usage(char *cmd)
{
	printf("---------------------------------------------------------\n");
	printf("goldcalc Version: %s, Date: %s\n", GOLDCALC_VERSION, GOLDCALC_DATE);
	printf("Usage: %s -f $data_file_path -s $current_sum \n", cmd);
	printf("	   \"-p $current_price\" and \"-w $current_weight\" are optional if without -s option.\n");
	printf("       Note: if more than one data file need be parsed, can use several -f option with date in order.\n");
	printf("---------------------------------------------------------\n");
	return;
}


int main(int argc, char **argv)
{
	int opt, ret, i;
	FILE *fp;
	char **p, *path[MAX_PATH_LENGTH];

	if (argc < 3) {
		usage(argv[0]);
		return -1;
	}
	
	for (i=0; i<MAX_FILE_NUMBER; i++) {
		path[i] = (char*)calloc(MAX_PATH_LENGTH, sizeof(char));
	}
	p = path;
	
	current_price = current_weight = current_sum = 0;
	while ((opt = getopt(argc, argv, "p:w:s:i:f:h")) != -1) {
		switch (opt) {
		case 'p':
			current_price = _katof(optarg);
			break;
		case 'w':
			current_weight = _katof(optarg);
			break;
		case 's':
			current_sum = _katof(optarg);
			break;
		case 'i':
		case 'f':
			strncpy(*p, optarg, MAX_PATH_LENGTH-1);
			p++;
			break;
		case 'h':
			usage(argv[0]);
			return 0;
		default:
			printf("Unknown option: -%c\n", opt);
			return -2;
		}
	}
	dprint("current_price: %.2f, current_weight: %.2f, current_sum: %.2f\n", current_price,
		    current_weight, current_sum);

	if (current_price > 0 && current_sum > 0) {
		if ((int)(current_price * current_weight) != (int)current_sum) {
			printf("Error happen in one of current price/weight/sum value.\n");
			return -3;
		}	
	} else if (current_price > 0 ) {
		current_sum = (current_price*current_weight);
	}

	struct one_trade_node *head = (struct one_trade_node *)malloc(sizeof(struct one_trade_node));
	p = path;
	
	while (*p[0] != '\0') {
		if ((fp = fopen(*p, "r")) == NULL) {
			printf("Invalid file path: %s\n",*p);
			return -4;
		}

		dprint("\nData File Number: %d", p-path+1);
		if ((ret = parser(fp, head)) != 0) {
			printf("Parser error for the %d file, check data file content first.\n", p-path+1);
			fclose(fp);	
			return -5;
		}
		p++;
		fclose(fp);	
	}
	
	print(head);
	clear(head);
	for (i=0; i<MAX_FILE_NUMBER; i++) 
	  free(path[i]);
	
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值