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

使用Python预测黄金AU9999收盘价 使用Python处理黄金数据集,并构建数据模型来预测收盘价格 阅读详情

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

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

[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;
}





工行贵金属TD计算器 能快速计算TD盈亏,用法简单,不会因为自己计算错误而造成损失 立即下载

相关推荐

国央企创新负责人如何通过平台实现科研成果的快速识别与转化?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

利用python爬虫获取黄金价格

继续练手,根据之前获取汽油价格的方式获取了金价,暂时没钱投资,看看而已 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 获取每天黄金价格 @author: yufei @site: http://www.antuan.com 2017-05-11 """ import re import urllib2,urllib import rand...

一方 7706

PotPlayer自用皮肤

自用 PotPlayer 皮肤,仅微调了字体大小与颜色,是按个人习惯调整,未做适配性调整。

python实时获取和讯网纸黄金价格信息

python实时获取和讯网纸黄金价格信息

等待被风吹过的夏天...... 7108

Bochs源码分析 - 26:8259中断控制器介绍

前言 现在我们跟着《x86/x64体系探索及编程》来分析17章内容 -- 8259中断控制器。这个是涉及硬件的,很遗憾,除了大学期间使用STM32搞过一个计算器,其余就没多少硬件编程经验,我在淘宝上找了下大概是这个东东。 https://pdos.csail.mit.edu/6.828/2010/readings/hardware/8259A.pdf 上面是8259A的芯片手册,里面讲的还是挺详细的,虽然里面很多东西还是看不太懂,但是结合着《x86/...

WriteAnything_的博客 967

CANoe VLAN配置的艺术:如何通过脚本自动化实现高效车载网络测试

本文深入探讨了CANoe VLAN配置的自动化实现,通过CAPL脚本与Python工具链的结合,提升车载网络测试效率。重点解析了硬件侧VLAN配置与软件侧VLAN配置的交互机制,并提供了实用的脚本开发示例和调试技巧,帮助工程师快速构建高效的自动化测试方案。

qqq44的博客 617

感悟(4)——投资贵金属半年有感

2015年一月开始,因为手上已经积攒了5W,也是因为心中一直对于金钱的渴望,知道把钱放在银行里永远跑不过通货膨胀,所以一直想把这笔钱用于理财,开始先是把所有的钱都放进支付宝,因为在支付宝里我相信风险非常小,特别是我知道蚂蚁金服获得许可后我更是深信危险系数可以忽略,那时5W一天差不多能拿6块,想想相对放在银行里划算多了,心中小乐了一下。     后来经常看工行网银里面的投资理财,突然发现可以购买贵

shenzhen 1431

Ubuntu 20.04 + CARLA 0.9.13 长时运行崩溃?手把手教你定位和缓解Segmentation fault

本文详细解析了Ubuntu 20.04环境下CARLA 0.9.13仿真平台长时运行崩溃问题,提供了从内存监控到核心转储分析的系统级诊断方法,并分享内存泄漏缓解、稳定性优化配置等实用解决方案,帮助开发者构建更稳定的自动驾驶仿真环境。

weixin_28362173的博客 253

纸黄金投资五大要素

作者: 杨宏亮 07-27 08:07 来源: 纸黄金网 黄金市场风云变幻,金价走势受供求、政治、经济、技术和心理等诸多因素影响。有时大起大落,风高浪急。有时多空胶着,波澜不兴。那么投资者在金市中如何树立正确的投资理念?如何把握准确的买卖时机呢?如何才能长期稳定的获利呢?我认为应从以下五个方面入手  一、风险控制是第一位的      美国股神巴菲特曾经说过一

gotohbu的专栏 1285

21、时间触发以太网:通信、诊断、故障隔离与参数化详解

本文详细解析了时间触发以太网(TTEthernet)的核心机制,包括团检测算法、诊断服务、故障隔离策略、配置服务以及协议参数化等内容。TTEthernet是一种专为超高可靠性系统设计的网络技术,广泛应用于航空航天、工业自动化和汽车电子等领域。文章还探讨了其未来发展趋势,如技术创新与生态建设,为开发者、企业和研究机构提供了应用与发展的方向。

efc12345678的博客 110

手把手保姆级|Python爬取微信公众号文章完整版,反反爬策略拉满,附可运行完整代码(永久可用)

做Python爬虫的朋友都知道,爬取微信公众号文章 绝对是爬虫领域的「香饽饽」也是「硬骨头」。一方面,微信公众号的文章是极具价值的数据源:不管是自媒体从业者做竞品分析、新媒体运营者整理干货、还是个人学习收集资料,都需要批量爬取公众号文章;另一方面,微信的反爬机制堪称全网最严苛之一 ——腾讯对微信生态的风控做到了极致,动态渲染页面、高频IP封禁、Cookie短时效失效、请求头严格校验、自动化工具秒识别、频繁请求直接拉黑,这些反爬手段让90%的爬虫新手望而却步:要么爬了几篇就被封IP,要么Cookie失效爬取中

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 2624

我的学习笔记01-从工行网页抓取白银价格

白银价格来源:http://www.icbc.com.cn/ICBCDynamicSite/Charts/GoldTendencyPicture.aspx 工具:jsoup 前期弯路: 来源  1 发现绝大部分白银数据来源来自网站www.kitco.cn,免费提供网站嵌入曲线gif图,一分钟刷新一次,但数据是图片,没有单纯的数值源,无法抓取 2 百度“白银价格”,可以得到数值,但更新慢,

wan_cong 2991

信号处理算法:自适应滤波_(8).自适应陷波器

自适应陷波器是一种强大的工具,广泛应用于电力系统、通信系统和生物医学信号处理等领域。通过自适应调整滤波器的参数,自适应陷波器可以在特定频率处形成陷波,从而有效地抑制噪声或干扰信号。常见的自适应算法包括LMS和RLS,它们各有优缺点,可以根据实际需求选择合适的算法。

2401_87715305的博客 1371

关于struct的初始化

在对struct 类型的使用时,要注意对其进行初始化,特别是其中的指针类型。因为在对已定义的struct的函数调用当中,会出现对它的进行非空判断后释放内存的危险行为。     如下例:#include #include typedef struct _filter_config_t{ char *category;

晴空's Blog 2387

五种音频器件综合对比——《器件手册--音频器件》

音频器件当在蜂鸣片的两片电极之间接通交流音频信号时,蜂鸣片会根据信号的大小和频率发生振动,从而产生相应的声音。结构简单、造价低廉。广泛应用于玩具、电子表、电子仪器、电子钟表、定时器等电子电器方面。蜂鸣器是一种一体化结构的电子讯响器,采用直流电源供电,广泛应用于各种电子产品中作发声器件有源蜂鸣器:内部带震荡源,通电即发声,程序控制方便。无源蜂鸣器:内部不带震荡源,需用方波驱动,声音频率可控,价格相对便宜。计算机、打印机、复印机、报警器、电子玩具、汽车电子设备、电话机、定时器等电子产品。

XU157303764的博客 1535

ardupilot 为什么要采样四元数姿态控制

本节主要说明清楚ardupilot姿态控制为什么要用到四元数,欢迎批评指正!!!

陌城烟雨 1117

++a和a++

今天的笔试题. 名扬/* 写出判断ABCD四个表达式的是否正确, 若正确, 写出经过表达式中 a的值(3分) * int a = 4; * (A)a += (a++); (B) a += (++a) ;(C) (a++) += a;(D) (++a) += (a++); * a = ? */#include int main(){ int a = 4, b;

晴空's Blog 1315

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

switch-case 中的分支中变量定义 编译不通过问题

如下的函数代码,程序无法编译通过。#include void test_switch(int val){ switch (val) { case 1: int abc; printf("you input 1/n"); break; case 2: printf("you input

晴空's Blog 1273

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

C/C++编程Trick

1. 数组名和数组名解引用等同.    #include int main() { int a[10]; printf("a= 0x%0X, &a=0x%0X/n", a, &a); // Array nanme a is equal to &a. return 0; } 

晴空's Blog 914

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

C基本库函数实现

#include #include #include #include // memset char *str_copy(char *s, char *t) { assert (s != NULL && t != NULL); char *p =

晴空's Blog 831

qs, jx笔试总结

#include #include #include /* strlen, strcpy, strncpy */#include #define S(a) a*a/* qs, jx笔试总结 */int getsum(int n);int *tfunc(int input);void loopmove(char *pstr, int steps);

晴空's Blog 574

String的Big-Three

#include #include using namespace std;//林锐P72class String {public: String(const char *str = NULL); //普通构造函数 String(const String &other); // 拷贝构造函数 ~String(void); // 析构函数 String&

晴空's Blog 498

C++的继承关系

#include using namespace std;class Base {public: void f(int x) { cout << "Base::f(int) " << x << endl; } void f(float x) { cout << "Base::f(float) " << x << endl; } void h(int x) {

晴空's Blog 467

C++ 虚函数表

C++中的虚函数的作用主要是实现了多态的机制。关于多态,简而言之就是用父类型别的指针指向其子类的实例,然后通过父类的指针调用实际子类的成员函数。虚表指针所指向的一张表(虚函数的地址表),表的项数就为虚函数的个数(非虚函数不入表),子类要为其重写了的虚函数重写自己的虚表,没有重写的就直接指向其父类的地址,从而达到多态的作用,这些都是在编译的时候由编译器自动完成的。 参考来源:

晴空's Blog 448
上一篇: [Linux设备驱动第三版]小记 [4-6章]
下一篇: 如何真正清空Linux终端的已显示Buffer
linuw
博客等级 码龄18年 2粉丝 45原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值