stock.h
上传用户:egreat
上传日期:2007-07-13
资源大小:29k
文件大小:9k
源码类别:

金融证券系统

开发平台:

Visual C++

  1. #ifndef _TDX_STOCK_H_
  2. #define _TDX_STOCK_H_
  3. #include <fstream>
  4. #include <set>
  5. #include <boost/thread/detail/config.hpp>
  6. #include <boost/thread/thread.hpp>
  7. #include <boost/thread/recursive_mutex.hpp>
  8. #include <boost/archive/binary_oarchive.hpp>
  9. #include <boost/archive/binary_iarchive.hpp>
  10. #include <boost/serialization/base_object.hpp>
  11. #include <boost/serialization/vector.hpp>
  12. #include <boost/serialization/map.hpp>
  13. #include <boost/date_time/posix_time/posix_time_types.hpp>
  14. #include <loki/singleton.h>
  15. #include "config.h"
  16. namespace StockMarket{
  17. using namespace std;
  18. using namespace boost;
  19. using namespace Loki;
  20. enum CmdId
  21. {
  22. CMD_STOCKHOLD_CHANGE = 0x000f,
  23. CMD_STOCK_LIST = 0x0524,
  24. CMD_INSTANT_TRANS = 0x0fc5, // 0x0fc5, 0x0faf
  25. CMD_HIS_TRANS = 0x0fb5,
  26. CMD_HEART_BEAT = 0x0523,
  27. };
  28. typedef set<string> StringSet;
  29. struct MarketInfo
  30. {
  31. enum MarketType
  32. {
  33. MARKET_FIRST,
  34. MARKET_SHANGHAI_A = MARKET_FIRST,
  35. MARKET_SHANGHAI_B,
  36. MARKET_SHENZHEN_A,
  37. MARKET_SHENZHEN_B,
  38. MARKET_WARRANT, // 权证
  39. MARKET_INDEX , // 指数
  40. MARKET_MAX,
  41. MARKET_UNKNOWN = MARKET_MAX,
  42. };
  43. static char get_block_from_market_type(MarketType t)
  44. {
  45. switch(t)
  46. {
  47. case MARKET_SHANGHAI_A:
  48. return 0;
  49. case MARKET_SHANGHAI_B:
  50. return 1;
  51. case MARKET_SHENZHEN_A:
  52. return 2;
  53. case MARKET_SHENZHEN_B:
  54. return 3;
  55. case MARKET_INDEX:
  56. return 11; // 所有指数
  57. case MARKET_WARRANT:
  58. return 13; // 权证
  59. default:
  60. throw 0;
  61. }
  62. }
  63. static unsigned int stocks_count[MARKET_MAX];
  64. static StringSet stocks_set[MARKET_MAX];
  65. const static int StocksCodeLen = 6; // 100  股一手
  66. const static int StocksPerHand = 100; // 100  股一手.
  67. const static float tax; // 0.3 %
  68. static short get_market_type_from_code(const char* pCode)
  69. {
  70. if(pCode[0] == '0')
  71. {
  72. if(pCode[1] == '0')
  73. return MARKET_SHENZHEN_A;
  74. else
  75. return MARKET_WARRANT; // 03xxxx
  76. }
  77. else if(pCode[0] == '2')
  78. return MARKET_SHENZHEN_B;
  79. else if(pCode[0] == '3') // 3xxxxx
  80. return MARKET_INDEX;
  81. else if(pCode[0] == '6')
  82. return MARKET_SHANGHAI_A;
  83. else if(pCode[0] == '9')
  84. {
  85. if(pCode[1] == '0')
  86. return MARKET_SHANGHAI_B; // 90xxxx
  87. else
  88. return MARKET_INDEX; // 99xxxx
  89. }
  90. else if(pCode[0] == '5')
  91. return MARKET_WARRANT;
  92. else
  93. return MARKET_UNKNOWN;
  94. }
  95. static short get_market_type(const char* pCode)
  96. {
  97. return get_market_type_from_code(pCode);
  98. }
  99. static short get_market_type(const string& stock_code)
  100. {
  101. const char* pCode = stock_code.c_str();
  102. return get_market_type_from_code(pCode);
  103. }
  104. static char get_market_location_from_code(const char* pCode)
  105. {
  106. if(pCode[0] <= '4')
  107. return 0; // 深圳 00xxxx, 03xxxx, 02xxxx, 3xxxxx(深圳指数)
  108. else
  109. return 1; //上海 60xxxxx, 58xxxx, 99xxxx (上海指数)
  110. }
  111. static char get_market_location(const char* pCode)
  112. {
  113. return get_market_location_from_code(pCode);
  114. }
  115. static char get_market_location(const string& stock_code)
  116. {
  117. const char* pCode = stock_code.c_str();
  118. return get_market_location_from_code(pCode);
  119. }
  120. static string get_first_stock();
  121. static string find_next_stock(const string& stock_code);
  122. };
  123. extern recursive_mutex transact_mutex;
  124. class StockTransact
  125. {
  126. public:
  127. struct Transact
  128. {
  129. friend class boost::serialization::access;
  130. short minute;
  131. int price;
  132. int  vol;
  133. int  count; // 此笔成交量的成交次数。 0: 表示未知
  134. uchar bs; // 表示买成交量还是卖成交量0:buy, 1:sell
  135. bool operator == (const Transact& t);
  136. template<class Archive>
  137. void serialize(Archive & ar, const unsigned int version)
  138. {
  139. ar & minute;
  140. ar & price;
  141. ar & vol;
  142. ar & bs;
  143. }
  144. };
  145. typedef vector<Transact> DailyTransact;
  146. typedef map<string, DailyTransact> DateTransact;
  147. typedef map<uint, DateTransact> TransactMap;
  148. };
  149. extern StockTransact::DateTransact today_transact;
  150. extern StockTransact::TransactMap hist_transact;
  151. extern recursive_mutex bid_mutex;
  152. class StockBid
  153. {
  154. public:
  155. struct Bid // 盘口
  156. {
  157. friend class boost::serialization::access;
  158. int minute;
  159. int act; // 活跃度 active rate
  160. int price; // 现价
  161. int y_close; // 昨收
  162. int open; // 开盘
  163. int high; // 最高
  164. int low; // 最低
  165. int buy; // 买入
  166. int sell; // 卖出
  167. int total_vol; // 总手,以股数计算
  168. int avail_vol; // 现手,以股数计算
  169. int inner_vol; // 内盘
  170. int outer_vol; // 外盘
  171. int updown; // 单笔升跌
  172. int buy_price1; // 买价1
  173. int sell_price1;
  174. int buy_vol1; // 买量1
  175. int sell_vol1;
  176. int buy_price2;
  177. int sell_price2;
  178. int buy_vol2;
  179. int sell_vol2;
  180. int buy_price3;
  181. int sell_price3;
  182. int buy_vol3;
  183. int sell_vol3;
  184. int buy_price4;
  185. int sell_price4;
  186. int buy_vol4;
  187. int sell_vol4;
  188. int buy_price5;
  189. int sell_price5;
  190. int buy_vol5;
  191. int sell_vol5;
  192. template<class Archive>
  193. void serialize(Archive & ar, const unsigned int version)
  194. {
  195. ar & minute;
  196. ar & act;
  197. ar & price;
  198. ar & y_close;
  199. ar & open;
  200. ar & high;
  201. ar & low;
  202. ar & buy;
  203. ar & sell;
  204. ar & total_vol;
  205. ar & avail_vol;
  206. ar & inner_vol;
  207. ar & outer_vol;
  208. ar & updown;
  209. ar & buy_price1; // 买价1
  210. ar & sell_price1;
  211. ar & buy_vol1; // 买量1
  212. ar & sell_vol1;
  213. ar & buy_price2;
  214. ar & sell_price2;
  215. ar & buy_vol2;
  216. ar & sell_vol2;
  217. ar & buy_price3;
  218. ar & sell_price3;
  219. ar & buy_vol3;
  220. ar & sell_vol3;
  221. ar & buy_price4;
  222. ar & sell_price4;
  223. ar & buy_vol4;
  224. ar & sell_vol4;
  225. ar & buy_price5;
  226. ar & sell_price5;
  227. ar & buy_vol5;
  228. ar & sell_vol5;
  229. }
  230. };
  231. typedef vector<Bid> DailyBid;
  232. typedef map<uint, DailyBid> DateBid;
  233. typedef map<string, DateBid> BidMap;
  234. };
  235. extern StockBid::BidMap instant_price_list;
  236. typedef vector<string> StringVector;
  237. extern recursive_mutex stock_mutex;
  238. struct BaseInfo
  239. {
  240. string stock_code;
  241. uint update_time;
  242. double ttl_amount; // 总股本
  243. double state_own_amount; // 国家股
  244. double init_amount; // 发起股本
  245. double corp_amount; // 法人股本
  246. double b_amount; // B 股本
  247. double h_amount; // H 股本
  248. double cir_amount; // 流通 股本
  249. double empl_amount; // 职工 股本
  250. double unknown1; // 
  251. double ttl_asset; // 总资产
  252. double varible_asset; // 流动 资产
  253. double firm_asset; // 固定 资产
  254. double invisible_asset; // 无形 资产
  255. double long_term_invest; // 长期投资
  256. double varible_debt; // 流动负债
  257. double long_term_debt; // 长期负债
  258. double accu_fund; // 公积金
  259. double net_asset; // 净资产
  260. double major_income; // 主营收入
  261. double major_profit; // 主营利润
  262. double unknown2; // 
  263. double bussiness_income; // 营业收入
  264. double invest_income; // 营业收入
  265. double allowance; // 补贴收入
  266. double non_bussiness_income; // 业外收入
  267. double income_adjustment; // 收入调整
  268. double ttl_profit; // 利润总额
  269. double unknown3; // 
  270. double net_profit; // 税后利润
  271. double undist_profit; // 未分配利润
  272. double per_net_assert2; // 每股净资产2
  273. static const int record_len;
  274. };
  275. typedef map<string, BaseInfo> StockBaseInfoMap;
  276. typedef map<string, StringVector> StockGroupMap;
  277. extern fstream& operator >> (fstream& ifs, BaseInfo& bs);
  278. typedef map<string, StringVector> StringVectorMap;
  279. extern uint date_to_uint(gregorian::date);
  280. extern gregorian::date uint_to_date(uint);
  281. struct GBBQ
  282. {
  283. string code;
  284. uchar chg_type;
  285. // 变更类型 
  286. // 1: 正常 
  287. // 2: 与1相关的除权除息日股本变化 
  288. // 3:股本性质变化(例如职工股上市)
  289. // 6: 增发. 
  290. // 8: 增发上市
  291. friend class boost::serialization::access;
  292. union {
  293. struct {
  294. float cash;   // 每10 分红
  295. float sell_price; // 每股配售价格
  296. float give_count; // 每10 股送股数
  297. float sell_count; // 每10 股配售数
  298. }bonus;
  299. struct 
  300. {
  301. float old_cir;   // 旧流通股
  302. float old_ttl; // 旧总股本
  303. float new_cir; // 新流通股
  304. float new_ttl; // 新总股本
  305. }gb;
  306. }data;
  307. template<class Archive>
  308. void serialize(Archive & ar, const unsigned int version)
  309. {
  310. ar & dt;
  311. ar & chg_type;
  312. ar & data.bonus.cash;
  313. ar & data.bonus.sell_price;
  314. ar & data.bonus.give_count;
  315. ar & data.bonus.sell_count;
  316. }
  317. };
  318. typedef vector<GBBQ> DateGBBQ;
  319. typedef map<uint, DateGBBQ> GBBQMap;
  320. class StockBasicInfo
  321. {
  322. public:
  323. bool load_basic_info();
  324. void save_basic_info();
  325. bool load_block_info();
  326. void save_block_info();
  327. void clear_gbbq_info();
  328. // bool load_gbbq_info(gregorian::date start_date, int day_count);
  329. void save_gbbq_info(uint tr_date);
  330. void save_stock_set();
  331. void add_stock_gbbq(uint dt, const GBBQ& gbbq);
  332. string get_gbbq_file_name(uint tr_date);
  333. bool is_gbbq_file_exist(uint tr_date);
  334. private:
  335. StockBaseInfoMap  stock_base_info;
  336. StockGroupMap stock_block_info;
  337. GBBQMap stock_gbbq_info;
  338. };
  339. typedef SingletonHolder<StockBasicInfo> stock_basic_info;
  340. ///////////////////////////////////////////////////////////////////////////////
  341. //
  342. //  Semantic actions
  343. //
  344. ///////////////////////////////////////////////////////////////////////////////
  345. extern  void show_curr_bid(char const*  first, char const*  last );
  346. }
  347. #endif