stock.h
资源名称:stocks.rar [点击查看]
上传用户:wenyu2k02
上传日期:2016-04-13
资源大小:28k
文件大小:9k
源码类别:
金融证券系统
开发平台:
Delphi
- #ifndef _TDX_STOCK_H_
- #define _TDX_STOCK_H_
- #include <fstream>
- #include <set>
- #include <boost/thread/detail/config.hpp>
- #include <boost/thread/thread.hpp>
- #include <boost/thread/recursive_mutex.hpp>
- #include <boost/archive/binary_oarchive.hpp>
- #include <boost/archive/binary_iarchive.hpp>
- #include <boost/serialization/base_object.hpp>
- #include <boost/serialization/vector.hpp>
- #include <boost/serialization/map.hpp>
- #include <boost/date_time/posix_time/posix_time_types.hpp>
- #include <loki/singleton.h>
- #include "config.h"
- namespace StockMarket{
- using namespace std;
- using namespace boost;
- using namespace Loki;
- enum CmdId
- {
- CMD_STOCKHOLD_CHANGE = 0x000f,
- CMD_STOCK_LIST = 0x0524,
- CMD_INSTANT_TRANS = 0x0fc5, // 0x0fc5, 0x0faf
- CMD_HIS_TRANS = 0x0fb5,
- CMD_HEART_BEAT = 0x0523,
- };
- typedef set<string> StringSet;
- struct MarketInfo
- {
- enum MarketType
- {
- MARKET_FIRST,
- MARKET_SHANGHAI_A = MARKET_FIRST,
- MARKET_SHANGHAI_B,
- MARKET_SHENZHEN_A,
- MARKET_SHENZHEN_B,
- MARKET_WARRANT, // 权证
- MARKET_INDEX , // 指数
- MARKET_MAX,
- MARKET_UNKNOWN = MARKET_MAX,
- };
- static char get_block_from_market_type(MarketType t)
- {
- switch(t)
- {
- case MARKET_SHANGHAI_A:
- return 0;
- case MARKET_SHANGHAI_B:
- return 1;
- case MARKET_SHENZHEN_A:
- return 2;
- case MARKET_SHENZHEN_B:
- return 3;
- case MARKET_INDEX:
- return 11; // 所有指数
- case MARKET_WARRANT:
- return 13; // 权证
- default:
- throw 0;
- }
- }
- static unsigned int stocks_count[MARKET_MAX];
- static StringSet stocks_set[MARKET_MAX];
- const static int StocksCodeLen = 6; // 100 股一手
- const static int StocksPerHand = 100; // 100 股一手.
- const static float tax; // 0.3 %
- static short get_market_type_from_code(const char* pCode)
- {
- if(pCode[0] == '0')
- {
- if(pCode[1] == '0')
- return MARKET_SHENZHEN_A;
- else
- return MARKET_WARRANT; // 03xxxx
- }
- else if(pCode[0] == '2')
- return MARKET_SHENZHEN_B;
- else if(pCode[0] == '3') // 3xxxxx
- return MARKET_INDEX;
- else if(pCode[0] == '6')
- return MARKET_SHANGHAI_A;
- else if(pCode[0] == '9')
- {
- if(pCode[1] == '0')
- return MARKET_SHANGHAI_B; // 90xxxx
- else
- return MARKET_INDEX; // 99xxxx
- }
- else if(pCode[0] == '5')
- return MARKET_WARRANT;
- else
- return MARKET_UNKNOWN;
- }
- static short get_market_type(const char* pCode)
- {
- return get_market_type_from_code(pCode);
- }
- static short get_market_type(const string& stock_code)
- {
- const char* pCode = stock_code.c_str();
- return get_market_type_from_code(pCode);
- }
- static char get_market_location_from_code(const char* pCode)
- {
- if(pCode[0] <= '4')
- return 0; // 深圳 00xxxx, 03xxxx, 02xxxx, 3xxxxx(深圳指数)
- else
- return 1; //上海 60xxxxx, 58xxxx, 99xxxx (上海指数)
- }
- static char get_market_location(const char* pCode)
- {
- return get_market_location_from_code(pCode);
- }
- static char get_market_location(const string& stock_code)
- {
- const char* pCode = stock_code.c_str();
- return get_market_location_from_code(pCode);
- }
- static string get_first_stock();
- static string find_next_stock(const string& stock_code);
- };
- extern recursive_mutex transact_mutex;
- class StockTransact
- {
- public:
- struct Transact
- {
- friend class boost::serialization::access;
- short minute;
- int price;
- int vol;
- int count; // 此笔成交量的成交次数。 0: 表示未知
- uchar bs; // 表示买成交量还是卖成交量0:buy, 1:sell
- bool operator == (const Transact& t);
- template<class Archive>
- void serialize(Archive & ar, const unsigned int version)
- {
- ar & minute;
- ar & price;
- ar & vol;
- ar & bs;
- }
- };
- typedef vector<Transact> DailyTransact;
- typedef map<string, DailyTransact> DateTransact;
- typedef map<uint, DateTransact> TransactMap;
- };
- extern StockTransact::DateTransact today_transact;
- extern StockTransact::TransactMap hist_transact;
- extern recursive_mutex bid_mutex;
- class StockBid
- {
- public:
- struct Bid // 盘口
- {
- friend class boost::serialization::access;
- int minute;
- int act; // 活跃度 active rate
- int price; // 现价
- int y_close; // 昨收
- int open; // 开盘
- int high; // 最高
- int low; // 最低
- int buy; // 买入
- int sell; // 卖出
- int total_vol; // 总手,以股数计算
- int avail_vol; // 现手,以股数计算
- int inner_vol; // 内盘
- int outer_vol; // 外盘
- int updown; // 单笔升跌
- int buy_price1; // 买价1
- int sell_price1;
- int buy_vol1; // 买量1
- int sell_vol1;
- int buy_price2;
- int sell_price2;
- int buy_vol2;
- int sell_vol2;
- int buy_price3;
- int sell_price3;
- int buy_vol3;
- int sell_vol3;
- int buy_price4;
- int sell_price4;
- int buy_vol4;
- int sell_vol4;
- int buy_price5;
- int sell_price5;
- int buy_vol5;
- int sell_vol5;
- template<class Archive>
- void serialize(Archive & ar, const unsigned int version)
- {
- ar & minute;
- ar & act;
- ar & price;
- ar & y_close;
- ar & open;
- ar & high;
- ar & low;
- ar & buy;
- ar & sell;
- ar & total_vol;
- ar & avail_vol;
- ar & inner_vol;
- ar & outer_vol;
- ar & updown;
- ar & buy_price1; // 买价1
- ar & sell_price1;
- ar & buy_vol1; // 买量1
- ar & sell_vol1;
- ar & buy_price2;
- ar & sell_price2;
- ar & buy_vol2;
- ar & sell_vol2;
- ar & buy_price3;
- ar & sell_price3;
- ar & buy_vol3;
- ar & sell_vol3;
- ar & buy_price4;
- ar & sell_price4;
- ar & buy_vol4;
- ar & sell_vol4;
- ar & buy_price5;
- ar & sell_price5;
- ar & buy_vol5;
- ar & sell_vol5;
- }
- };
- typedef vector<Bid> DailyBid;
- typedef map<uint, DailyBid> DateBid;
- typedef map<string, DateBid> BidMap;
- };
- extern StockBid::BidMap instant_price_list;
- typedef vector<string> StringVector;
- extern recursive_mutex stock_mutex;
- struct BaseInfo
- {
- string stock_code;
- uint update_time;
- double ttl_amount; // 总股本
- double state_own_amount; // 国家股
- double init_amount; // 发起股本
- double corp_amount; // 法人股本
- double b_amount; // B 股本
- double h_amount; // H 股本
- double cir_amount; // 流通 股本
- double empl_amount; // 职工 股本
- double unknown1; //
- double ttl_asset; // 总资产
- double varible_asset; // 流动 资产
- double firm_asset; // 固定 资产
- double invisible_asset; // 无形 资产
- double long_term_invest; // 长期投资
- double varible_debt; // 流动负债
- double long_term_debt; // 长期负债
- double accu_fund; // 公积金
- double net_asset; // 净资产
- double major_income; // 主营收入
- double major_profit; // 主营利润
- double unknown2; //
- double bussiness_income; // 营业收入
- double invest_income; // 营业收入
- double allowance; // 补贴收入
- double non_bussiness_income; // 业外收入
- double income_adjustment; // 收入调整
- double ttl_profit; // 利润总额
- double unknown3; //
- double net_profit; // 税后利润
- double undist_profit; // 未分配利润
- double per_net_assert2; // 每股净资产2
- static const int record_len;
- };
- typedef map<string, BaseInfo> StockBaseInfoMap;
- typedef map<string, StringVector> StockGroupMap;
- extern fstream& operator >> (fstream& ifs, BaseInfo& bs);
- typedef map<string, StringVector> StringVectorMap;
- extern uint date_to_uint(gregorian::date);
- extern gregorian::date uint_to_date(uint);
- struct GBBQ
- {
- string code;
- uchar chg_type;
- // 变更类型
- // 1: 正常
- // 2: 与1相关的除权除息日股本变化
- // 3:股本性质变化(例如职工股上市)
- // 6: 增发.
- // 8: 增发上市
- friend class boost::serialization::access;
- union {
- struct {
- float cash; // 每10 分红
- float sell_price; // 每股配售价格
- float give_count; // 每10 股送股数
- float sell_count; // 每10 股配售数
- }bonus;
- struct
- {
- float old_cir; // 旧流通股
- float old_ttl; // 旧总股本
- float new_cir; // 新流通股
- float new_ttl; // 新总股本
- }gb;
- }data;
- template<class Archive>
- void serialize(Archive & ar, const unsigned int version)
- {
- ar & dt;
- ar & chg_type;
- ar & data.bonus.cash;
- ar & data.bonus.sell_price;
- ar & data.bonus.give_count;
- ar & data.bonus.sell_count;
- }
- };
- typedef vector<GBBQ> DateGBBQ;
- typedef map<uint, DateGBBQ> GBBQMap;
- class StockBasicInfo
- {
- public:
- bool load_basic_info();
- void save_basic_info();
- bool load_block_info();
- void save_block_info();
- void clear_gbbq_info();
- // bool load_gbbq_info(gregorian::date start_date, int day_count);
- void save_gbbq_info(uint tr_date);
- void save_stock_set();
- void add_stock_gbbq(uint dt, const GBBQ& gbbq);
- string get_gbbq_file_name(uint tr_date);
- bool is_gbbq_file_exist(uint tr_date);
- private:
- StockBaseInfoMap stock_base_info;
- StockGroupMap stock_block_info;
- GBBQMap stock_gbbq_info;
- };
- typedef SingletonHolder<StockBasicInfo> stock_basic_info;
- ///////////////////////////////////////////////////////////////////////////////
- //
- // Semantic actions
- //
- ///////////////////////////////////////////////////////////////////////////////
- extern void show_curr_bid(char const* first, char const* last );
- }
- #endif