bool Portfolio::readyForRun() { string func_name(" [Portfolio::readyForRun]"); if (!m_se) { HKU_WARN("m_se is null!" << func_name); return false; } if (!m_tm) { HKU_WARN("m_tm is null!" << func_name); return false; } if (!m_af) { HKU_WARN("m_am is null!" << func_name); return false; } reset(); //将影子账户指定给资产分配器 m_tm_shadow = m_tm->clone(); m_af->setTM(m_tm_shadow); return true; }
Indicator Indicator::getResult(size_t num) const { if (!m_imp) { HKU_WARN("indicator imptr is null! [Indicator::getResult]"); return Indicator(); } return m_imp->getResult(num); }
void Weave::_calculate(const Indicator& ind) { size_t total = ind.size(); if (m_result_num != 0 && total != size()) { HKU_ERROR("ind's size must be equal weave's size! [Weave::_calculate]"); return; } if (total == 0) { return; } size_t old_m_result_num = m_result_num; m_result_num = ind.getResultNumber() + m_result_num; if (m_result_num > MAX_RESULT_NUM) { HKU_WARN("Weave only can contains " << MAX_RESULT_NUM << "reult_num! [Weave::_calculate]"); m_result_num = MAX_RESULT_NUM; } if (m_discard < ind.discard()) { m_discard = ind.discard(); } price_t null_price = Null<price_t>(); for (size_t i = old_m_result_num; i < m_result_num; ++i) { m_pBuffer[i] = new PriceList(total, null_price); for (size_t j = m_discard; j < total; ++j) { (*m_pBuffer[i])[j] = ind.get(j, i-old_m_result_num); } } }
PriceList Indicator::getResultAsPriceList(size_t num) const { if (!m_imp) { HKU_WARN("indicator imptr is null! [Indicator::getResultAsPriceList]"); return PriceList(); } return m_imp->getResultAsPriceList(num); }
Stock::Data::Data(const string& market, const string& code, const string& name, hku_uint32 type, bool valid, const Datetime& startDate, const Datetime& lastDate, price_t tick, price_t tickValue, int precision, size_t minTradeNumber, size_t maxTradeNumber) : m_market(market), m_code(code), m_name(name), m_type(type), m_valid(valid), m_startDate(startDate), m_lastDate(lastDate), m_tick(tick), m_tickValue(tickValue), m_precision(precision), m_minTradeNumber(minTradeNumber), m_maxTradeNumber(maxTradeNumber) { if (0.0 == m_tick) { HKU_WARN("tick should not be zero! [Stock::Data::Data]"); m_unit = 1.0; } else { m_unit = m_tickValue / m_tick; } boost::to_upper(m_market); m_market_code = m_market + m_code; for (int i = 0; i < KQuery::INVALID_KTYPE; ++i) { pKData[i] = KRecordListPtr(); } }
KRecordList Stock ::getKRecordList(size_t start_ix, size_t end_ix, KQuery::KType ktype) const { KRecordList result; if (!m_data) return result; if (m_data->pKData[ktype]) { size_t total = m_data->pKData[ktype]->size(); if (start_ix >= end_ix || start_ix > total) { HKU_WARN("Invalid param! (" << start_ix << ", " << end_ix << ") [Stock::getKRecordList]"); return result; } size_t end = end_ix > total ? total : end_ix; result.reserve(end - start_ix); for (size_t i = start_ix; i < end; ++i) { result.push_back((*m_data->pKData[ktype])[i]); } return result; } KRecordListPtr plist(new KRecordList); m_kdataDriver->loadKData(m_data->m_market, m_data->m_code, ktype, start_ix, end_ix, plist); size_t total = plist->size(); result.reserve(total); for (size_t i = 0; i < total; i++) { result.push_back((*plist)[i]); } return result; }
bool MySQLKDataDriver::_init() { string func_name(" [MySQLKDataDriver::MySQLKDataDriver]"); string default_host("127.0.0.1"); string default_usr("root"); string default_pwd(""); try { m_host = m_params.get<string>("host"); } catch(...) { m_host = default_host; HKU_WARN("Can't get mysql host! " << func_name); } try { m_port = m_params.get<int>("port"); } catch(...) { m_port = 3306; } try { m_usr = m_params.get<string>("usr"); } catch(...) { m_usr = default_usr; } try { m_pwd = m_params.get<string>("pwd"); } catch(...) { m_pwd = default_pwd; } shared_ptr<MYSQL> mysql(new MYSQL, MySQLCloser()); if (!mysql_init(mysql.get())) { HKU_ERROR(" Initial MySQL handle error!" << func_name); return false; } if (!mysql_real_connect(mysql.get(), m_host.c_str(), m_usr.c_str(), m_pwd.c_str(), NULL, m_port, NULL, 0) ) { HKU_ERROR(" Failed to connect to database!" << func_name); return false; } if (mysql_set_character_set(mysql.get(), "utf8")) { HKU_ERROR(" mysql_set_character_set error!" << func_name); return false; } m_mysql = mysql; return true; }
bool BaseInfoDriver::checkType() { bool result = false; try { string type = getParam<string>("type"); boost::to_upper(type); if (type == m_name) { result = true; } else { result = false; HKU_WARN("Type of driver mismatch! (" << type << " != " << m_name << ") " << "[BaseInfoDriver::checkType]"); } } catch(...) { result = false; HKU_ERROR("Can't get type of driver! [BaseInfoDriver::checkType]"); } return result; }
PriceList HistoryFinanceReader ::getHistoryFinanceInfo(Datetime date, const string& market, const string& code) { string funcname(" [HistoryFinanceReader::getHistoryFinanceInfo]"); PriceList result; string filename(m_dir + "/gpcw" + boost::lexical_cast<string>(date.number() / 10000) + ".dat"); FILE *fp = fopen(filename.c_str(), "rb"); if (NULL == fp) { HKU_INFO("Can't found " << filename << funcname); return result; } unsigned int report_date = 0; unsigned short max_count = 0; unsigned long report_size = 0; char header_buf[20]; if (!fread(header_buf, 1, 20, fp)) { HKU_ERROR("read data failed! " << filename << funcname); fclose(fp); return result; } memcpy(&report_date, header_buf + 2, 4); memcpy(&max_count, header_buf + 6, 2); memcpy(&report_size, header_buf + 12, 4); char stock_code[7]; hku_uint32 address = 0; for (int i = 0; i < max_count; i++) { if (!fread(stock_code, 1, 7, fp)) { HKU_ERROR("read stock_code failed! " << filename << funcname); fclose(fp); return result; } if (!fread(&address, 4, 1, fp)) { HKU_ERROR("read stock_item address failed! " << filename << funcname); fclose(fp); return result; } stock_code[6] = '\0'; if (strcmp(stock_code, code.c_str()) == 0) { break; } } if (address != 0) { const int MAX_COL_NUM = 350; float result_buffer[MAX_COL_NUM]; int report_fields_count = int(report_size / 4); if (report_fields_count >= MAX_COL_NUM) { HKU_WARN("Over MAX_COL_NUM! " << filename << funcname); report_fields_count = MAX_COL_NUM; } fseek(fp, address, SEEK_SET); if (!fread(result_buffer, 4, report_fields_count, fp)) { HKU_ERROR("read col data failed! " << filename << funcname); fclose(fp); return result; } result.reserve(report_fields_count); price_t null_price = Null<price_t>(); for (int i = 0; i < report_fields_count; i++) { if (result_buffer[i] == 0xf8f8f8f8) { result.push_back(null_price); } else { result.push_back(result_buffer[i]); } } } else { HKU_ERROR("Invalid address(0)! " << filename << funcname); } fclose(fp); return result; }
void MySQLKDataDriver:: loadKData(const string& market, const string& code, KQuery::KType kType, size_t start_ix, size_t end_ix, KRecordListPtr out_buffer) { string func_name(" [MySQLKDataDriver::loadKData]"); if (!m_mysql) { //HKU_ERROR("Null m_mysql!" << func_name); return; } //if (kType >= KQuery::INVALID_KTYPE || start_ix >= end_ix) { if (start_ix >= end_ix) { HKU_WARN("ktype(" << kType << ") is invalid or start_ix(" << start_ix << ") >= endix(" << end_ix << ")" << func_name); return; } MYSQL_RES *result; MYSQL_ROW row; string table(_getTableName(market, code, kType)); std::stringstream buf (std::stringstream::out); buf << "select date, open, high, low, close, amount, count from " << table << " order by date limit " << start_ix << ", " << (end_ix - start_ix); if (!_query(buf.str())) { //HKU_ERROR("mysql_query error!" << func_name); return; } result = mysql_store_result(m_mysql.get()); if (!result) { HKU_ERROR("mysql_store_result error!" << func_name); return; } int i = 0; while ((row = mysql_fetch_row(result))) { try { KRecord k; hku_uint64 d = boost::lexical_cast<hku_uint64>(row[0]); k.datetime = Datetime(d); k.openPrice = boost::lexical_cast<price_t>(row[1]); k.highPrice = boost::lexical_cast<price_t>(row[2]); k.lowPrice = boost::lexical_cast<price_t>(row[3]); k.closePrice = boost::lexical_cast<price_t>(row[4]); k.transAmount = boost::lexical_cast<price_t>(row[5]); k.transCount = boost::lexical_cast<price_t>(row[6]); out_buffer->push_back(k); i++; } catch (...) { HKU_INFO("Can't fecth No." << i << " record in " << table << func_name); i++; continue; } } mysql_free_result(result); return; }