StockIo.cpp
上传用户:egreat
上传日期:2007-07-13
资源大小:29k
文件大小:9k
- #include <fstream>
- #include <sstream>
- #include <boost/thread/detail/config.hpp>
- #include <boost/thread/thread.hpp>
- #include <boost/thread/recursive_mutex.hpp>
- #include <boost/date_time/posix_time/posix_time_types.hpp>
- #include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp
- #include "boost/filesystem/fstream.hpp" // ditto
- #include <iomanip>
- #include "Stock.h"
- #include "request.h"
- #include "response.h"
- #include "commands.h"
- #include "util.hpp"
- #include "StockIo.h"
- using namespace std;
- using namespace boost;
- BOOST_CLASS_TRACKING(StockMarket::StockBid::BidMap, serialization::track_never);
- BOOST_CLASS_TRACKING(StockMarket::StockTransact::TransactMap, serialization::track_never);
- namespace StockMarket
- {
- void AddStockListRequest(bool all)
- {
- recursive_mutex::scoped_lock guard(req_queue_mutex);
- CmdQueue_t &q = CmdQueue::Instance();
- int total, oneReqCnt;
- // 因为 socket 底层组包不能太长的关系, 所以这里不能超过 70
- // 开始的时候不知道 stocks_count, 所以用一个很大的数字
- for(MarketInfo::MarketType m = MarketInfo::MARKET_FIRST;
- m < MarketInfo::MARKET_MAX; m=(MarketInfo::MarketType)(m+1))
- {
- if(all)
- {
- total = MarketInfo::stocks_count[m];
- oneReqCnt = 70;
- }
- else
- {
- total = oneReqCnt = 1;
- }
- CmdData_t r(new StockListReq(m, 0, oneReqCnt, total));
- q.push(r);
- }
- }
- void AddInstantTransRequest( int count)
- {
- recursive_mutex::scoped_lock guard(req_queue_mutex);
- CmdQueue_t &q = CmdQueue::Instance();
- CmdData_t r(new InstantTransReq(count));
- q.push(r);
- }
- string get_transaction_filename(uint d)
- {
- stringstream ss;
- const string stock_transaction_path = "F:\Develop\stock\data\transaction\";
- ss << stock_transaction_path << d << ".mat";
- return ss.str();
- }
- // generate the proper filename // for example: B200510B20051011.dat
- static string get_bid_filepath(uint d)
- {
- const string stock_bid_path = "F:\Develop\stock\data\bid\";
- stringstream ss;
- ss << stock_bid_path << 'B' << (d /100) << '\';
- return ss.str();
- }
- static string get_bid_filename(uint d)
- {
- stringstream ss;
- ss << 'B' << d << ".dat";
- return ss.str();
- }
- void AddHistTransRequest(const gregorian::date& from, const gregorian::date& to)
- {
- // check the last date of each data, then send request to get the missing ones
- recursive_mutex::scoped_lock guard(req_queue_mutex);
- CmdQueue_t &q = CmdQueue::Instance();
- gregorian::date current(from);
- for(;current <= to; current += gregorian::date_duration(1))
- {
- uint d = date_to_uint(current);
- string full_filename = get_transaction_filename(d);
- if(! is_file_exist(full_filename))
- {
- CmdData_t r(new HisTransReq(d));
- q.push(r);
- }
- }
- return ;
-
- }
- void AddGBBQRequest()
- {
- // 加载权息数据
- recursive_mutex::scoped_lock guard(req_queue_mutex);
- CmdQueue_t &q = CmdQueue::Instance();
- for(MarketInfo::MarketType m = MarketInfo::MARKET_FIRST;
- m < MarketInfo::MARKET_MAX; m=(MarketInfo::MarketType)(m+1))
- {
- for(StringSet::iterator i = MarketInfo::stocks_set[m].begin();
- i != MarketInfo::stocks_set[m].end();)
- {
- StockHoldChgReq* r1 = new StockHoldChgReq;
- for(int j = 0; j < StockHoldChgReq::max_stocks_a_request && i != MarketInfo::stocks_set[m].end(); ++i, ++j)
- {
- r1->add_stock(*i);
- }
- CmdData_t r(r1);
- q.push(r);
- }
- }
- }
- void save_day_transaction(uint dt, const StockTransact::DateTransact & transact)
- {
- if(transact.empty()) return;
- string stock_day_deal_file = get_transaction_filename(dt);
- StockTransact::DateTransact::const_iterator iter_code;
- if (is_file_exist(stock_day_deal_file))
- {
- return;
- }
- int maxDealCount = 0; int stock_count = 0;
- for(iter_code = transact.begin();iter_code != transact.end(); ++iter_code)
- {
- if(iter_code->second.empty()) continue;
- ++ stock_count;
- if(iter_code->second.size() > (unsigned int)maxDealCount)
- {
- maxDealCount = iter_code->second.size();
- }
- }
- const int deal_col_count = 6;
- int dims[3];
- dims[0] = deal_col_count;
- dims[1] = maxDealCount;
- dims[2] = stock_count;
- mxArray *mx_t0_data = mxCreateNumericArray(3, &dims[0], mxDOUBLE_CLASS, mxREAL);
- double *p_mxT0data = mxGetPr(mx_t0_data), *p_temp_mxT0data = p_mxT0data;
-
- for(iter_code = transact.begin();iter_code != transact.end(); ++iter_code)
- {
- if(iter_code->second.empty()) continue;
-
- bool is_open_price = true;
- for(StockTransact::DailyTransact::const_iterator dtr_i = iter_code->second.begin() ;dtr_i != iter_code->second.end();++dtr_i)
- {
- int bs = dtr_i->bs;
- int tr_count = dtr_i->count <= 0?1:dtr_i->count;
- p_temp_mxT0data[0] = atoi(iter_code->first.c_str());
- p_temp_mxT0data[1] = dtr_i->price;
- p_temp_mxT0data[2] = dtr_i->vol;
- p_temp_mxT0data[3] = tr_count;
- p_temp_mxT0data[4] = bs;
- p_temp_mxT0data[5] = dtr_i->minute;
- p_temp_mxT0data += deal_col_count;
- }
- p_mxT0data += maxDealCount * deal_col_count;
- p_temp_mxT0data = p_mxT0data;
- }
- matlab_engine::Instance().saveVarToFile(stock_day_deal_file, mx_t0_data, "t0_data");
- mxDestroyArray(mx_t0_data);
- }
- void save_day_bid(const StockBid::BidMap& bid)
- {
- recursive_mutex::scoped_lock guard(bid_mutex);
- if(! bid.empty())
- {
- StockBid::BidMap::const_iterator iter = bid.begin();
- if(! iter->second.empty())
- {
- StockBid::DateBid::const_iterator iter2 = iter->second.begin();
- string filepath = get_bid_filepath(iter2->first);
- string filename = filepath + get_bid_filename(iter2->first);
-
- filesystem::path full_path( filesystem::initial_path() );
- full_path = filesystem::system_complete( filesystem::path( filepath, filesystem::native ) );
- try{
- // not exist or this month
- if(!filesystem::exists(full_path))
- {
- filesystem::create_directory(full_path);
- }
- // just overwrite
- ofstream ofs(filename.c_str(), ios::binary);
- if(ofs)
- {
- archive::binary_oarchive oa(ofs);
- oa << bid;
- }
- }
- catch(exception&e)
- {
- cout << e.what();
- }
- }
- }
- }
- bool transact_data_exsist(const gregorian::date& dt)
- {
- uint d = date_to_uint(dt);
- string full_filename = get_transaction_filename(d);
- return is_file_exist(full_filename);
- }
- bool load_one_day_transact_data(const gregorian::date& current, StockTransact::TransactMap& outTr)
- {
- recursive_mutex::scoped_lock guard(transact_mutex);
- // generate the proper filename // for example: T200510T20051011.dat
- uint d = date_to_uint(current);
- string full_filename = get_transaction_filename(d);
- ifstream ifs(full_filename.c_str(), ios::binary);
- if(ifs.is_open())
- {
- archive::binary_iarchive ia(ifs);
- outTr.clear();
- ia >> outTr;
- return true;
- }
- return false;
- }
- int load_transact_data(const gregorian::date& from, const gregorian::date& to, StockTransact::TransactMap& outTr)
- {
- StockTransact::TransactMap tempTr;
- for(gregorian::date current(from);current < to; current += gregorian::date_duration(1))
- {
- if(false == load_one_day_transact_data(current, tempTr))
- continue;
- outTr.insert(tempTr.begin(), tempTr.end());
- }
- return 0;
-
- }
- void load_one_day_bid_data(const gregorian::date& current, StockBid::BidMap& outBid)
- {
- recursive_mutex::scoped_lock guard(bid_mutex);
- // generate the proper filename // for example: B200510B20051011.dat
- uint d = date_to_uint(current);
- string full_filename = get_bid_filepath(d) + get_bid_filename(d);
- ifstream ifs(full_filename.c_str(), ios::binary);
- if(ifs.is_open())
- {
- archive::binary_iarchive ia(ifs);
- outBid.clear();
- ia >> outBid;
- }
- }
- int load_bid_data(const gregorian::date& from, const gregorian::date& to, StockBid::BidMap& outBid)
- {
- StockBid::BidMap tempBid;
- for(gregorian::date current(from); current < to; current += gregorian::date_duration(1))
- {
- load_one_day_bid_data(current, tempBid);
- outBid.insert(tempBid.begin(), tempBid.end());
- }
- return 0;
- }
- malabEng::malabEng():pEngine_(engOpen("