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

金融证券系统

开发平台:

Visual C++

  1. #include <fstream>
  2. #include <sstream>
  3. #include <boost/thread/detail/config.hpp>
  4. #include <boost/thread/thread.hpp>
  5. #include <boost/thread/recursive_mutex.hpp>
  6. #include <boost/date_time/posix_time/posix_time_types.hpp>
  7. #include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp
  8. #include "boost/filesystem/fstream.hpp"    // ditto
  9. #include <iomanip>
  10. #include "Stock.h"
  11. #include "request.h"
  12. #include "response.h"
  13. #include "commands.h"
  14. #include "util.hpp"
  15. #include "StockIo.h"
  16. using namespace std;
  17. using namespace boost;
  18. BOOST_CLASS_TRACKING(StockMarket::StockBid::BidMap, serialization::track_never);
  19. BOOST_CLASS_TRACKING(StockMarket::StockTransact::TransactMap, serialization::track_never);
  20. namespace StockMarket
  21. {
  22. void AddStockListRequest(bool all)
  23. {
  24. recursive_mutex::scoped_lock guard(req_queue_mutex);
  25. CmdQueue_t &q = CmdQueue::Instance();
  26. int total, oneReqCnt;
  27. // 因为 socket 底层组包不能太长的关系, 所以这里不能超过 70
  28. // 开始的时候不知道 stocks_count, 所以用一个很大的数字
  29. for(MarketInfo::MarketType m = MarketInfo::MARKET_FIRST;
  30. m < MarketInfo::MARKET_MAX; m=(MarketInfo::MarketType)(m+1))
  31. {
  32. if(all)
  33. {
  34. total = MarketInfo::stocks_count[m];
  35. oneReqCnt = 70;
  36. }
  37. else
  38. {
  39. total = oneReqCnt = 1;
  40. }
  41. CmdData_t r(new StockListReq(m, 0, oneReqCnt, total));
  42. q.push(r);
  43. }
  44. }
  45. void AddInstantTransRequest( int count)
  46. {
  47. recursive_mutex::scoped_lock guard(req_queue_mutex);
  48. CmdQueue_t &q = CmdQueue::Instance();
  49. CmdData_t r(new InstantTransReq(count));
  50. q.push(r);
  51. }
  52. string get_transaction_filename(uint d)
  53. {
  54. stringstream ss;
  55. const string stock_transaction_path = "F:\Develop\stock\data\transaction\";
  56. ss << stock_transaction_path << d << ".mat";
  57. return ss.str();
  58. }
  59. // generate the proper filename // for example: B200510B20051011.dat
  60. static string get_bid_filepath(uint d)
  61. {
  62. const string stock_bid_path = "F:\Develop\stock\data\bid\";
  63. stringstream ss;
  64. ss << stock_bid_path << 'B' << (d /100) << '\';
  65. return ss.str();
  66. }
  67. static string get_bid_filename(uint d)
  68. {
  69. stringstream ss;
  70. ss << 'B' << d << ".dat";
  71. return ss.str();
  72. }
  73. void AddHistTransRequest(const gregorian::date& from, const gregorian::date& to)
  74. {
  75. // check the last date of each data, then send request to get the missing ones
  76. recursive_mutex::scoped_lock guard(req_queue_mutex);
  77. CmdQueue_t &q = CmdQueue::Instance();
  78. gregorian::date current(from);
  79. for(;current <= to; current += gregorian::date_duration(1))
  80. {
  81. uint d = date_to_uint(current);
  82. string full_filename = get_transaction_filename(d);
  83. if(! is_file_exist(full_filename))
  84. {
  85. CmdData_t r(new HisTransReq(d));
  86. q.push(r);
  87. }
  88. }
  89. return ;
  90. }
  91. void AddGBBQRequest()
  92. {
  93. // 加载权息数据
  94. recursive_mutex::scoped_lock guard(req_queue_mutex);
  95. CmdQueue_t &q = CmdQueue::Instance();
  96. for(MarketInfo::MarketType m = MarketInfo::MARKET_FIRST;
  97. m < MarketInfo::MARKET_MAX; m=(MarketInfo::MarketType)(m+1))
  98. {
  99. for(StringSet::iterator i = MarketInfo::stocks_set[m].begin();
  100. i != MarketInfo::stocks_set[m].end();)
  101. {
  102. StockHoldChgReq* r1 = new StockHoldChgReq;
  103. for(int j = 0; j < StockHoldChgReq::max_stocks_a_request && i != MarketInfo::stocks_set[m].end(); ++i, ++j)
  104. {
  105. r1->add_stock(*i);
  106. }
  107. CmdData_t r(r1);
  108. q.push(r);
  109. }
  110. }
  111. }
  112. void save_day_transaction(uint dt, const StockTransact::DateTransact & transact)
  113. {
  114. if(transact.empty()) return;
  115. string stock_day_deal_file = get_transaction_filename(dt);
  116. StockTransact::DateTransact::const_iterator iter_code;
  117. if (is_file_exist(stock_day_deal_file))
  118. {
  119. return;
  120. }
  121. int maxDealCount = 0; int stock_count = 0;
  122. for(iter_code = transact.begin();iter_code !=  transact.end(); ++iter_code)
  123. {
  124. if(iter_code->second.empty()) continue;
  125. ++ stock_count;
  126. if(iter_code->second.size() > (unsigned int)maxDealCount)
  127. {
  128. maxDealCount = iter_code->second.size();
  129. }
  130. }
  131. const int deal_col_count = 6;
  132. int dims[3];
  133. dims[0] = deal_col_count;
  134. dims[1] = maxDealCount;
  135. dims[2] = stock_count;
  136. mxArray *mx_t0_data = mxCreateNumericArray(3, &dims[0], mxDOUBLE_CLASS, mxREAL);
  137. double *p_mxT0data = mxGetPr(mx_t0_data), *p_temp_mxT0data = p_mxT0data;
  138. for(iter_code = transact.begin();iter_code !=  transact.end(); ++iter_code)
  139. {
  140. if(iter_code->second.empty()) continue;
  141. bool is_open_price = true;
  142. for(StockTransact::DailyTransact::const_iterator dtr_i = iter_code->second.begin() ;dtr_i != iter_code->second.end();++dtr_i)
  143. {
  144. int bs = dtr_i->bs;
  145. int tr_count = dtr_i->count <= 0?1:dtr_i->count;
  146. p_temp_mxT0data[0] = atoi(iter_code->first.c_str());
  147. p_temp_mxT0data[1] = dtr_i->price;
  148. p_temp_mxT0data[2] = dtr_i->vol;
  149. p_temp_mxT0data[3] = tr_count;
  150. p_temp_mxT0data[4] = bs;
  151. p_temp_mxT0data[5] = dtr_i->minute;
  152. p_temp_mxT0data += deal_col_count;
  153. }
  154. p_mxT0data += maxDealCount * deal_col_count;
  155. p_temp_mxT0data = p_mxT0data;
  156. }
  157. matlab_engine::Instance().saveVarToFile(stock_day_deal_file, mx_t0_data, "t0_data");
  158. mxDestroyArray(mx_t0_data);
  159. }
  160. void save_day_bid(const StockBid::BidMap& bid)
  161. {
  162. recursive_mutex::scoped_lock guard(bid_mutex);
  163. if(! bid.empty())
  164. {
  165. StockBid::BidMap::const_iterator iter = bid.begin();
  166. if(! iter->second.empty())
  167. {
  168. StockBid::DateBid::const_iterator iter2 = iter->second.begin();
  169. string filepath = get_bid_filepath(iter2->first);
  170. string filename = filepath + get_bid_filename(iter2->first);
  171. filesystem::path full_path( filesystem::initial_path() );
  172. full_path = filesystem::system_complete( filesystem::path( filepath, filesystem::native ) );
  173. try{
  174. // not exist or this month
  175. if(!filesystem::exists(full_path))
  176. {
  177. filesystem::create_directory(full_path);
  178. }
  179. // just overwrite
  180. ofstream ofs(filename.c_str(), ios::binary);
  181. if(ofs)
  182. {
  183. archive::binary_oarchive oa(ofs);
  184. oa << bid;
  185. }
  186. }
  187. catch(exception&e)
  188. {
  189. cout << e.what();
  190. }
  191. }
  192. }
  193. }
  194. bool transact_data_exsist(const gregorian::date& dt)
  195. {
  196. uint d = date_to_uint(dt);
  197. string full_filename = get_transaction_filename(d);
  198. return is_file_exist(full_filename);
  199. }
  200. bool load_one_day_transact_data(const gregorian::date& current, StockTransact::TransactMap& outTr)
  201. {
  202. recursive_mutex::scoped_lock guard(transact_mutex);
  203. // generate the proper filename // for example: T200510T20051011.dat
  204. uint d = date_to_uint(current);
  205. string full_filename = get_transaction_filename(d);
  206. ifstream ifs(full_filename.c_str(), ios::binary);
  207. if(ifs.is_open())
  208. {
  209. archive::binary_iarchive ia(ifs);
  210. outTr.clear();
  211. ia >> outTr;
  212. return true;
  213. }
  214. return false;
  215. }
  216. int load_transact_data(const gregorian::date& from, const gregorian::date& to, StockTransact::TransactMap& outTr)
  217. {
  218. StockTransact::TransactMap tempTr;
  219. for(gregorian::date current(from);current < to; current += gregorian::date_duration(1))
  220. {
  221. if(false == load_one_day_transact_data(current, tempTr))
  222. continue;
  223. outTr.insert(tempTr.begin(), tempTr.end());
  224. }
  225. return 0;
  226. }
  227. void load_one_day_bid_data(const gregorian::date& current, StockBid::BidMap& outBid)
  228. {
  229. recursive_mutex::scoped_lock guard(bid_mutex);
  230. // generate the proper filename // for example: B200510B20051011.dat
  231. uint d = date_to_uint(current);
  232. string full_filename = get_bid_filepath(d) + get_bid_filename(d);
  233. ifstream ifs(full_filename.c_str(), ios::binary);
  234. if(ifs.is_open())
  235. {
  236. archive::binary_iarchive ia(ifs);
  237. outBid.clear();
  238. ia >> outBid;
  239. }
  240. }
  241. int load_bid_data(const gregorian::date& from, const gregorian::date& to, StockBid::BidMap& outBid)
  242. {
  243. StockBid::BidMap tempBid;
  244. for(gregorian::date current(from); current < to; current += gregorian::date_duration(1))
  245. {
  246. load_one_day_bid_data(current, tempBid);
  247. outBid.insert(tempBid.begin(), tempBid.end());
  248. }
  249. return 0;
  250. }
  251. malabEng::malabEng():pEngine_(engOpen("")){}
  252. malabEng::~malabEng()
  253. {
  254. engClose(pEngine_);
  255. }
  256. void malabEng::putVariable(const mxArray *pInput, const char* var)
  257. {
  258. engPutVariable(pEngine_, var, pInput);
  259. }
  260. void malabEng::evalString(const char* var)
  261. {
  262. engEvalString(pEngine_, var);
  263. }
  264. mxArray* malabEng::getVariable(const char* var)
  265. {
  266. return engGetVariable(pEngine_, var);
  267. }
  268. void malabEng::saveVarToFile(const string &filename, const mxArray *var, const string &var_name)
  269. {
  270. string eval_str = "save " + filename + + " " + var_name;
  271. putVariable(var, var_name.c_str());
  272. evalString(eval_str.c_str());
  273. }
  274. #if 0
  275. mxArray* DayNnSim(mxArray *pInput)
  276. {
  277. engPutVariable(pEngine_, "P", pInput);
  278. engEvalString(pEngine_, "r = sim(daynet, P)");
  279. return engGetVariable(pEngine_,"r");
  280. }
  281. mxArray* RtNnSim(mxArray *pInput)
  282. {
  283. engPutVariable(pEngine_, "P", pInput);
  284. engEvalString(pEngine_, "r = sim(rtnet, P)");
  285. return engGetVariable(pEngine_,"r");
  286. }
  287. #endif
  288. }