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

使用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

Python异步协程爬虫优化:解决aiohttp中ServerDisconnectedError的高效策略

本文深入探讨了在Python异步协程爬虫中使用aiohttp时,如何高效解决常见的ServerDisconnectedError问题。核心策略包括全局复用ClientSession、精细配置TCPConnector连接池、实施请求重试与速率限制,从而构建稳定、高效的异步爬虫架构,有效避免服务器断开连接错误。

pz890123的博客 857

金蝶K3系统物料主数据后台表对应字段一览

我们说到物料表,一般是指t_icitem,但正确的说t_icitem并不是一张有,而是一张视图,不过我们对于数据库物料表中的大部分操作,都是通过此视图来完成。比如经常通过此视图,来成批修改数量,单价精度,是否启用批次等一些字段,有的时候,对于账套已经启用,并且已经有业务发生的物料,有一些关键属性也需要通过修改物料表t_icitem来进行。 当然修改的时候,需要考虑一些前提条件的。 比如说我有一个客户新建的账套,在月底结账时发现,要改一种成本核算方法,启用时用的是加权平法,现在想改成移动平均法,这样的情况,

hzfw2008的博客 1万+

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

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

shenzhen 1431

奇迹MU单机版开服全流程:从零搭建到分辨率调整(附GM工具使用技巧)

本文详细介绍了奇迹MU单机版从零搭建到分辨率调整的完整流程,包括环境配置、服务端启动、GM工具使用技巧等关键步骤。特别针对现代高分辨率显示器提供了优化方案,帮助玩家轻松搭建个性化游戏环境,重温经典MMORPG的独特魅力。

b3n4m5q6w7的博客 679

纸黄金投资五大要素

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

gotohbu的专栏 1285

Java实战:绿盾加密文件批量解密工具Ldterm的实现与优化

本文详细介绍了基于Java开发的绿盾加密文件批量解密工具Ldterm的实现与优化过程。通过分析绿盾加密机制,结合Java流处理能力,实现了高效的文件解密方案,并分享了多线程处理、内存优化等性能提升技巧,适用于企业级数据迁移与备份场景。

weixin_29314405的博客 490

Spring AI Alibaba 1.x 源码分析-架构设计

本文通过分析Spring AI Alibaba 框架的源码,分析出其原理和架构设计,无论是用于项目开发、技术评估还是团队培训都具有重要价值。

program哲学 489

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

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

wan_cong 2991

Oracle EBS R12 主 / 辅助分类帐(主 / 辅助帐套)落地

本文提供了Oracle EBS R12主/辅助分类帐实施的全套文档模板,包含主帐套(法定帐套)和辅助帐套的完整配置清单、科目映射规则模板、4类典型调整分录示例(存货跌价、折旧差异、往来重分类、外币折算)以及税会/IFRS案例分录。重点阐述了主/辅助帐套的配置要点、SLA子分类帐关键配置、科目映射规则(一对一、多对一、重分类等类型)以及调整分录的自动生成逻辑。文档可直接用于项目实施,包含总体架构图、科目映射矩阵表、会计规则集配置等交付物,并附有Excel版模板便于直接使用。适用于需要实现多准则并行记账、税务调

金牌架构师 386

关于struct的初始化

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

晴空's Blog 2387

ResNet改进(45):结合通道混洗(ShuffleNet)的混合架构

本文提出了一种创新的卷积神经网络(CNN)架构CustomResNet,它将ResNet34与通道混洗(ChannelShuffle)技术相结合。该模型通过ShuffleBlock模块替换原始ResNet层,包含三个核心组件:1) ChannelShuffle模块实现通道分组混洗;2) ShuffleBlock模块整合点卷积、分组卷积和残差连接;3) CustomResNet主模型保持ResNet整体架构但替换核心层为ShuffleBlock。这种设计既保留了ResNet的残差学习能力,又通过通道混洗增强特

2401_82355416的博客 210

【Linux命令大全】006.网络通讯之netstat命令(实操篇)

本文详细介绍了Linux系统中netstat网络诊断命令的实用技巧。主要内容包括:1) 基本功能与参数详解,展示如何查看网络连接、路由表和接口统计;2) 安装方法及基础用法,如查看TCP/UDP连接、显示进程信息和路由表;3) 高级应用技巧,包括监听端口检查、连接状态统计和实时监控。文章通过表格和代码示例,系统性地呈现了netstat在系统管理中的核心应用场景,是网络故障排查和性能监控的实用指南。

yuhuasheng1997的博客 705

++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

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

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

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、付费专栏及课程。

余额充值