stock.cpp
上传用户:egreat
上传日期:2007-07-13
资源大小:29k
文件大小:16k
- #include "config.h"
- #include <string>
- #include <vector>
- #include <set>
- #include <map>
- #include <iostream>
- //#include <boost/assign/std/vector.hpp>
- //#include <boost/assign/list_inserter.hpp>
- #include <boost/thread/detail/config.hpp>
- #include <boost/thread/thread.hpp>
- #include <boost/thread/recursive_mutex.hpp>
- #include <boost/date_time/gregorian/gregorian.hpp>
- #include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp
- #include "boost/filesystem/fstream.hpp" // ditto
- // #include <boost/smart_assert/assert.hpp>
- #include <boost/foreach.hpp>
- #include "stockio.h"
- #include "util.hpp"
- #include "stock.h"
- using namespace boost;
- BOOST_CLASS_TRACKING(StockMarket::GBBQMap, serialization::track_never);
- namespace StockMarket{
- // using namespace assign;
- const float MarketInfo::tax = 0.01f; // 1 %
- unsigned int MarketInfo::stocks_count[MarketInfo::MARKET_MAX];
- StringSet MarketInfo::stocks_set[MarketInfo::MARKET_MAX];
- recursive_mutex transact_mutex;
- recursive_mutex bid_mutex;
- StockTransact::DateTransact today_transact;
- StockTransact::TransactMap hist_transact;
- StockBid::BidMap instant_price_list;
- #if 0
- int StockBasicInfo::apply_gbbq_front(const string &stock_code, const gregorian::date& ref_dt
- , const gregorian::date& dst_dt, int ref_price)
- {
- float new_price = static_cast<float>(ref_price);
- uint uint_ref_dt = date_to_uint(ref_dt);
- uint uint_dst_dt = date_to_uint(dst_dt);
- DateGBBQ &dgb = stock_gbbq_info[stock_code];
- for(DateGBBQ::const_iterator iter = dgb.begin(); iter < dgb.end(); ++iter)
- {
- if(iter->dt <= uint_ref_dt)
- continue;
- else if(iter->dt <= uint_dst_dt)
- {
- if(1 == iter->chg_type && iter->data.bonus.cash > 0.1) // 派发现金
- {
- new_price -= iter->data.bonus.cash * 10;
- }
- if(1 == iter->chg_type && iter->data.bonus.sell_price > 0.1) // 配股
- {
- float sell_price = 0, sell_count = 0;
- sell_price = iter->data.bonus.sell_price * 100; // 转化为分
- sell_count = iter->data.bonus.sell_count;
- new_price += ((sell_price * sell_count) / 10);
- }
- else if(1 == iter->chg_type&& iter->data.bonus.give_count > 0) // 送或配股或转增股上市
- {
- new_price = (new_price * 10) / (10 + iter->data.bonus.give_count);
- }
- else if(6 == iter->chg_type && iter->data.bonus.sell_price > 0.1) // 增发
- {
- }
- else if(8 == iter->chg_type) // 增发上市
- {
- }
- }
- else
- {
- break;
- }
- }
- if(new_price < 0) new_price = 0;
- return static_cast<int>(new_price);
-
- }
- #endif
- string StockBasicInfo::get_gbbq_file_name(uint tr_date)
- {
- const string stock_gbbq_file = "F:\Develop\stock\data\G";
- stringstream ss;
- ss << stock_gbbq_file << tr_date << ".mat";
- return ss.str();
- }
- bool StockBasicInfo::is_gbbq_file_exist(uint tr_date)
- {
- return is_file_exist(get_gbbq_file_name(tr_date));
- }
- void StockBasicInfo::clear_gbbq_info()
- {
- stock_gbbq_info.clear();
- }
- #if 0
- bool StockBasicInfo::load_gbbq_info(gregorian::date tr_date, int day_count)
- {
- while(day_count--)
- {
- if(is_gbbq_file_exist(date_to_uint(tr_date)))
- {
- string filename = get_gbbq_file_name(date_to_uint(tr_date));
- ifstream ifs(filename.c_str(), ios::binary);
- if(ifs)
- {
- archive::binary_iarchive ia(ifs);
- ia >> stock_gbbq_info;
- std::cout << "GBBQ file " << filename << " appliedrn";
- return true;
- }
- }
- tr_date -= gregorian::date_duration(1);
- }
- std::cout << "No GBBQ found!rn";
- return false;
- }
- #endif
- void StockBasicInfo::save_gbbq_info(uint tr_date)
- {
- if (is_gbbq_file_exist(tr_date))
- {
- return;
- }
- string filename = get_gbbq_file_name(tr_date);
- int maxDealCount = 0;
- GBBQMap::const_iterator iter;
- for(iter = stock_gbbq_info.begin();iter != stock_gbbq_info.end(); ++iter)
- {
- if(iter->second.size() > (uint)maxDealCount)
- {
- maxDealCount = iter->second.size();
- }
- }
- const int col_count = 8;
- int dims[3];
- dims[0] = col_count;
- dims[1] = maxDealCount;
- dims[2] = stock_gbbq_info.size();
- mxArray *mx_t0_data = mxCreateNumericArray(3, &dims[0], mxDOUBLE_CLASS, mxREAL);
- double *p_mxT0data = mxGetPr(mx_t0_data), *p_temp_mxT0data = p_mxT0data;
-
- const gregorian::date christ_date(2000,1,1);
- for(GBBQMap::const_reverse_iterator iter = stock_gbbq_info.rbegin(); iter != stock_gbbq_info.rend(); ++iter)
- {
- for(DateGBBQ::const_iterator iter_code = iter->second.begin();iter_code != iter->second.end(); ++iter_code)
- {
- p_temp_mxT0data[0] = iter->first;
- p_temp_mxT0data[1] = (uint_to_date(iter->first) - christ_date).days() + 730486; // 相当于 matlab 的 date_num
- p_temp_mxT0data[2] = atoi(iter_code->code.c_str());
- p_temp_mxT0data[3] = iter_code->chg_type;
- p_temp_mxT0data[4] = iter_code->data.gb.old_cir;
- p_temp_mxT0data[5] = iter_code->data.gb.old_ttl;
- p_temp_mxT0data[6] = iter_code->data.gb.new_cir;
- p_temp_mxT0data[7] = iter_code->data.gb.new_ttl;
- p_temp_mxT0data += col_count;
- }
- p_mxT0data += maxDealCount * col_count;
- p_temp_mxT0data = p_mxT0data;
- }
- matlab_engine::Instance().saveVarToFile(filename, mx_t0_data, "gbbq_info");
- mxDestroyArray(mx_t0_data);
- }
- bool StockBasicInfo::load_basic_info()
- {
- // 加载股票基本资料
- // 基本信息
- stock_base_info.clear();
- BaseInfo bs;
- const string stock_data_path = "C:\Program Files\xyzq\T0002\";
- string full_path = stock_data_path + "BASE.DBF";
- fstream fs(full_path.c_str(), ios::in);
- // fstream ofs("d:\temp\t.txt", ios::out);
- if(!fs) return false;
- int record_off = 0x522;
- fs.seekg(record_off);
- char c;
- fs >> c;
- while(!fs.eof() && -1 != fs.tellg())
- {
- fs >> bs;
- record_off += BaseInfo::record_len;
- stock_base_info.insert(make_pair(bs.stock_code, bs));
- // ofs << bs.stock_code << endl;
- fs.seekg(record_off);
- fs >> c;
- }
- return true;
- }
- void StockBasicInfo::save_basic_info()
- {
- const string filename = "F:\Develop\stock\data\basic_info.mat";
- StockBaseInfoMap::const_iterator iter_code;
- const int deal_col_count = 31;
- int dims[2];
- dims[0] = deal_col_count;
- dims[1] = stock_base_info.size();
- mxArray *mx_t0_data = mxCreateNumericArray(2, &dims[0], mxDOUBLE_CLASS, mxREAL);
- double *p_mxT0data = mxGetPr(mx_t0_data), *p_temp_mxT0data = p_mxT0data;
-
- for(iter_code = stock_base_info.begin();iter_code != stock_base_info.end(); ++iter_code)
- {
- p_temp_mxT0data[0] = atoi(iter_code->first.c_str());
- p_temp_mxT0data[1] = iter_code->second.update_time;
- p_temp_mxT0data[2] = iter_code->second.ttl_amount;
- p_temp_mxT0data[3] = iter_code->second.state_own_amount;
- p_temp_mxT0data[4] = iter_code->second.init_amount;
- p_temp_mxT0data[5] = iter_code->second.corp_amount;
- p_temp_mxT0data[6] = iter_code->second.b_amount;
- p_temp_mxT0data[7] = iter_code->second.h_amount;
- p_temp_mxT0data[8] = iter_code->second.cir_amount;
- p_temp_mxT0data[9] = iter_code->second.empl_amount;
- p_temp_mxT0data[10] = iter_code->second.ttl_asset;
- p_temp_mxT0data[11] = iter_code->second.varible_asset;
- p_temp_mxT0data[12] = iter_code->second.firm_asset;
- p_temp_mxT0data[13] = iter_code->second.invisible_asset;
- p_temp_mxT0data[14] = iter_code->second.long_term_invest; // 长期投资
- p_temp_mxT0data[15] = iter_code->second.varible_debt; // 流动负债
- p_temp_mxT0data[16] = iter_code->second.long_term_debt; // 长期负债
- p_temp_mxT0data[17] = iter_code->second.accu_fund; // 公积金
- p_temp_mxT0data[18] = iter_code->second.net_asset; // 净资产
- p_temp_mxT0data[19] = iter_code->second.major_income; // 主营收入
- p_temp_mxT0data[20] = iter_code->second.major_profit; // 主营利润
- p_temp_mxT0data[21] = iter_code->second.bussiness_income; // 营业收入
- p_temp_mxT0data[22] = iter_code->second.invest_income; // 营业收入
- p_temp_mxT0data[23] = iter_code->second.allowance; // 补贴收入
- p_temp_mxT0data[24] = iter_code->second.non_bussiness_income; // 业外收入
- p_temp_mxT0data[25] = iter_code->second.income_adjustment; // 收入调整
- p_temp_mxT0data[26] = iter_code->second.ttl_profit; // 利润总额
- p_temp_mxT0data[27] = iter_code->second.unknown3; //
- p_temp_mxT0data[28] = iter_code->second.net_profit; // 税后利润
- p_temp_mxT0data[29] = iter_code->second.undist_profit; // 未分配利润
- p_temp_mxT0data[30] = iter_code->second.per_net_assert2; // 每股净资产2
- p_temp_mxT0data += deal_col_count;
- }
- matlab_engine::Instance().saveVarToFile(filename, mx_t0_data, "basic_info");
- mxDestroyArray(mx_t0_data);
- }
- bool StockBasicInfo::load_block_info()
- {
- // 加载板块资料
- stock_block_info.clear();
- const string stock_data_path = "C:\Program Files\xyzq\T0002\";
- string full_path = stock_data_path + "BLOCK.DAT";
- fstream fs(full_path.c_str(), ios::in | ios::binary);
- if(!fs) return false;
- int p = 0;
- fs.seekg(0x182);
- while(!fs.eof() && -1 != (p =fs.tellg()))
- {
- char temp[10];
- fs.read(&temp[0],9);
- if(0 == temp[0]) break;
- string name(temp); // block name
- unsigned short cnt = 0;
- fs.read((char*)&cnt, 2);
- fs.seekg(2, ios::cur);
- while(cnt -- > 0)
- {
- fs.read(&temp[0],7);
- if(0 == temp[0]) break;
- string member(temp, 6);
- stock_block_info[name].push_back(member);
- }
- fs.seekg(p + 0x0afd);
- }
- return true;
- }
- void StockBasicInfo::save_block_info()
- {
- const string filename = "F:\Develop\stock\data\block_info.mat";
- int deal_col_count = 0;
- StockGroupMap::const_iterator iter_code;
- for(iter_code = stock_block_info.begin();iter_code != stock_block_info.end(); ++iter_code)
- {
- if(iter_code->second.size() > (uint)deal_col_count)
- {
- deal_col_count = iter_code->second.size();
- }
- }
- ++deal_col_count; // 加上名称
- int dims[2];
- dims[0] = deal_col_count;
- dims[1] = stock_block_info.size();
- int block_index = 1;
- mxArray *mx_t0_data = mxCreateNumericArray(2, &dims[0], mxDOUBLE_CLASS, mxREAL);
- double *p_mxT0data = mxGetPr(mx_t0_data), *p_temp_mxT0data = p_mxT0data;
-
- for(iter_code = stock_block_info.begin();iter_code != stock_block_info.end(); ++iter_code, ++block_index)
- {
- p_temp_mxT0data[0] = block_index; // Block name
- int block_member_index = 1;
- for(StringVector::const_iterator iter_i = iter_code->second.begin();iter_i < iter_code->second.end();++iter_i, ++block_member_index)
- {
- p_temp_mxT0data[block_member_index] = atoi(iter_i->c_str());
- }
- p_temp_mxT0data += deal_col_count;
- }
- matlab_engine::Instance().saveVarToFile(filename, mx_t0_data, "block_info");
- mxDestroyArray(mx_t0_data);
- }
- void StockBasicInfo::save_stock_set()
- {
- uint d = date_to_uint(gregorian::day_clock::local_day());
- const string filename = "F:\Develop\stock\data\StockSet";
- stringstream ss;
- ss << filename << d << ".mat";
- if(is_file_exist(ss.str()))return;
-
- int deal_col_count = 0; int max_stock_count = 0;
- for(short m = MarketInfo::MARKET_FIRST; m < MarketInfo::MARKET_MAX; m++)
- {
- max_stock_count += MarketInfo::stocks_set[m].size();
- }
- int dims[2];
- dims[0] = 1;
- dims[1] = max_stock_count;
- mxArray *mx_t0_data = mxCreateNumericArray(2, &dims[0], mxDOUBLE_CLASS, mxREAL);
- double *p_mxT0data = mxGetPr(mx_t0_data), *p_temp_mxT0data = p_mxT0data;
-
- for(short m = MarketInfo::MARKET_FIRST; m < MarketInfo::MARKET_MAX; m++)
- {
- for(StringSet::const_iterator iter = MarketInfo::stocks_set[m].begin();iter != MarketInfo::stocks_set[m].end();++iter, ++p_temp_mxT0data)
- {
- *p_temp_mxT0data = atoi(iter->c_str());
- }
- }
- matlab_engine::Instance().saveVarToFile(ss.str(), mx_t0_data, "stock_set");
- mxDestroyArray(mx_t0_data);
- }
- void StockBasicInfo::add_stock_gbbq(uint dt, const GBBQ& gbbq)
- {
- stock_gbbq_info[dt].push_back(gbbq);
- }
- #if 0
- int StockBasicInfo::get_cir_amount(const string &stock_code, const gregorian::date& dt)
- {
- DateGBBQ &dgb = stock_gbbq_info[stock_code];
- uint uint_dt = date_to_uint(dt);
- float cir_amount = 0;
- for(DateGBBQ::const_iterator iter = dgb.begin(); iter < dgb.end(); ++iter)
- {
- if(2 == iter->chg_type || 5 == iter->chg_type || 8 == iter->chg_type)
- {
- if(iter->dt == uint_dt)
- {
- cir_amount = iter->data.gb.new_cir;
- break;
- }
- else if(iter->dt > uint_dt)
- {
- cir_amount = iter->data.gb.old_cir;
- break;
- }
- else
- {
- cir_amount = iter->data.gb.new_cir;
- }
- }
- }
- if(cir_amount < 1.0) // 没有股本变迁数据。取当前值
- {
- if(stock_base_info.find(stock_code) != stock_base_info.end())
- cir_amount = static_cast<float>(stock_base_info[stock_code].cir_amount);
- else
- cir_amount = 10000.0;
- }
- return static_cast<int>(cir_amount);
- }
- #endif
- bool StockTransact::Transact::operator == (const StockTransact::Transact& t)
- {
- return (minute == t.minute
- && price == t.price
- && vol == t.vol
- && bs == t.bs);
- }
- const int BaseInfo::record_len = 0x1e1;
- fstream& operator >> (fstream& fs, BaseInfo& bs)
- {
- char code[MarketInfo::StocksCodeLen];
- fs.read(code, MarketInfo::StocksCodeLen);
- bs.stock_code = string(&code[0], MarketInfo::StocksCodeLen);
- fs >> bs.update_time;
- fs >> bs.ttl_amount;
- fs >> bs.state_own_amount; // 国家股
- fs >> bs.init_amount; // 发起股本
- fs >> bs.corp_amount; // 法人股本
- fs >> bs.b_amount; // B 股本
- fs >> bs.h_amount; // H 股本
- fs >> bs.cir_amount; // 流通 股本
- fs >> bs.empl_amount; // 职工 股本
- fs >> bs.unknown1; //
- fs >> bs.ttl_asset; // 总资产
- fs >> bs.varible_asset; // 流动 资产
- fs >> bs.firm_asset; // 固定 资产
- fs >> bs.invisible_asset; // 无形 资产
- fs >> bs.long_term_invest; // 长期投资
- fs >> bs.varible_debt; // 流动负债
- fs >> bs.long_term_debt; // 长期负债
- fs >> bs.accu_fund; // 公积金
- fs >> bs.net_asset; // 净资产
- fs >> bs.major_income; // 主营收入
- fs >> bs.major_profit; // 主营利润
- fs >> bs.unknown2; //
- fs >> bs.bussiness_income; // 营业收入
- fs >> bs.invest_income; // 投资收入
- fs >> bs.allowance; // 补贴收入
- fs >> bs.non_bussiness_income; // 业外收入
- fs >> bs.income_adjustment; // 收入调整
- fs >> bs.ttl_profit; // 利润总额
- fs >> bs.unknown3; //
- fs >> bs.net_profit; // 税后利润
- fs >> bs.undist_profit; // 未分配利润
- fs >> bs.per_net_assert2; // 每股净资产2
- return fs;
-
- }
- extern void output_short_index_data();
- uint date_to_uint(gregorian::date d)
- {
- return d.year() * 10000 + d.month() * 100 + d.day();
- }
- gregorian::date uint_to_date(uint d)
- {
- return gregorian::date(d / 10000, d % 10000 / 100, d % 100);
- }
- string MarketInfo::get_first_stock()
- {
- // set the first stock
- for(short m = MarketInfo::MARKET_FIRST; m < MarketInfo::MARKET_MAX; m++)
- {
- StringSet::const_iterator iter = MarketInfo::stocks_set[m].begin();
- if(iter != MarketInfo::stocks_set[m].end())
- {
- return *iter;
- }
- }
- return "";
- }
- string MarketInfo::find_next_stock(const string& stock_code)
- {
- bool first_in_market = false;
- for(short m = MarketInfo::get_market_type(stock_code); m < MarketInfo::MARKET_MAX; m++)
- {
- StringSet::const_iterator iter ;
- if(! first_in_market)
- {
- iter = MarketInfo::stocks_set[m].find(stock_code);
- if( iter != MarketInfo::stocks_set[m].end())
- {
- ++iter;
- if(iter != MarketInfo::stocks_set[m].end())
- {
- return *iter;
- }
- }
- first_in_market = true;
- }
- else
- {
- iter = MarketInfo::stocks_set[m].begin();
- if(iter != MarketInfo::stocks_set[m].end())
- {
- return *iter;
- }
- }
- }
- return "";
- }
- void show_curr_bid(char const* first, char const* last )
- {
- cout << "show_curr_bid calledrn";
- recursive_mutex::scoped_lock guard(bid_mutex);
- StockBid::BidMap::const_iterator i = instant_price_list.begin();
- // has data
- if(i != instant_price_list.end())
- {
- // print title here
- }
- while(i++ != instant_price_list.end())
- {
- cout << i->first << endl;
- }
- }
-
-
- }