⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 do_portfolio.cpp

📁 股票分析源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include "hbstock2/hbstock_config.h"#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>


#include "gnu/usage_tool.h"
#include "gnu/config_file_map.h"

#include "hbstock2/stock_data_manger.h"
#include "hbstock2/portfolio.h"
#include "hbstock2/dll_object_manger.h"
#include "hbstock2/do_portfolio.h"
/* must be at the end of include */
#include "gnu/i18n_debug.h"

using namespace std;
using namespace hbstock2;
using namespace gnu;

static const char* LOGFILE = gnu::FILENAME(__FILE__);

typedef DllObjectManger< PortfolioTool > PflObjectManger;	typedef ACE_Singleton<PflObjectManger,Hbstock_Mutex > PflObjectMangerInst;

bool hbstock2::PflFileDataSort (const PflFileData& data1,
	const PflFileData& data2)
{	
	return (data1.name < data2.name);
}

bool hbstock2::PflDataSort (const PflData& data1,
	const PflData& data2)
{
	string str_stock1 = data1.code;
	string str_stock2 = data2.code;
	
	return (str_stock1 < str_stock2);
}

bool hbstock2::PflDataSort_selector_name (const PflData& data1,
	const PflData& data2)
{
	string str_data1 = data1.selector_title;
	string str_data2 = data2.selector_title;
	
	return (str_data1 > str_data2);
}

/**
* Specialization DllObjectManger<T>::loadDllObjectFromDefaultDir for PortfolioTool
*/

namespace hbstock2
{
template <>
int DllObjectManger<hbstock2::PortfolioTool>::load_dllobject_from_default_dir(void)
{
	int i_ret = 0;
	
	if (m_set_dllobj_ptr->size() == 0)
	{
		// init data loader
		AppConfig* p_app_config = AppConfigInst::instance();
		strcpy(m_p_ch_default_dir,p_app_config->get_root_dir().c_str());
		strcat(m_p_ch_default_dir,"/plugin/portfolio");	
		memset(m_p_ch_file_prefix_default,0,sizeof(m_p_ch_file_prefix_default));
  		strcpy(m_p_ch_file_prefix_default,"pfl_");
  	
		strcpy(m_p_ch_symbolname,"maker_pfl");
		i_ret = load_dllobject_from_dir(m_p_ch_default_dir,m_p_ch_file_prefix_default);
	}
	
	return i_ret;
}} //namespace hbstock2

bool hbstock2::check_pfl_tool()
{
	bool isTrue = false;
	
	PflObjectManger* p_manger = PflObjectMangerInst::instance();
	
	if (p_manger->get_all_dllobject()->size()>0)
	{
		isTrue = true;	
	}
	
	return isTrue;
}

void hbstock2::check_pfl_file_data(VectPflFileData& vect_pfl_file_data,
	const std::string& str_select)
{
	if (str_select == "a")
	{
		for (unsigned int i=0;i<vect_pfl_file_data.size();i++)
		{
			if (vect_pfl_file_data[i].is_cluster)
			{
				vect_pfl_file_data[i].is_selected = true;
			} else
			{
				vect_pfl_file_data[i].is_selected = false;
			}
		}
	} else // if (str_select == "a")
	{
		std::vector<std::string> vect_str;
		str_split(str_select.c_str(),",",vect_str);
		
		if (vect_str.size() == 0)
		{
			return;
		}
		
		int i_index = 0;
		for (unsigned int i=0;i<vect_str.size();i++)
		{
			i_index = atoi(vect_str[i].c_str());
			
			//cout << "i_index : " << i_index << endl;
			
			if (i_index < 1)
			{
				continue;
			}
			-- i_index;
			
			for (unsigned int j=0;j<vect_pfl_file_data.size();j++)
			{
				if (j == (unsigned int) i_index)
				{
					vect_pfl_file_data[j].is_selected = true;
					break;
				} 		
			} //for (unsigned int j=0;j<vect_pfl_file_data.size();j++)
		} //for (i=0;i<vect_str.size();i++)
	} //if (str_select == "a")
}

bool hbstock2::get_pfl_file_data_from_file(VectPflFileData& vect_pfl_file_data,
	std::string& str_file_name)
{
	bool isTrue = false;
	
	vect_pfl_file_data.clear();
	
	string str_key = "pfl_data_file";
	string str_value = "w";
	string str_config_file_name = "w";
		
	str_config_file_name = get_pfl_data_file_name();
	
	AppConfig* p_app_config = AppConfigInst::instance();
	p_app_config->get_value(str_key,str_value);
	if (str_value.size() > 5)
	{
		str_file_name = str_value.substr(0,str_value.size()-5);
	}	
	
	ConfigFileMap config_file_map;
	
	isTrue = config_file_map.load_file(str_config_file_name.c_str());
	if (!isTrue)
	{
		//I18N_ERROR(_("%s [%s:%l] Can't open pfl_data_file : %s, system exit.\n"),str_config_file_name.c_str());		
		create_pfl_config_file(str_config_file_name.c_str());
		config_file_map.load_file(str_config_file_name.c_str());		
	}
	
	std::vector<std::string> vect_section;
	
	isTrue = config_file_map.get_sections(vect_section);
	if (!isTrue)
	{
		I18N_ERROR(_("%s [%s:%l] Can't read section,system exit.\n"));
		return isTrue;
	}
	
	string str_sect_prefix = "pfl_";
	string str_cmb_prefix = "cmb_";
	
	for (unsigned int i=0;i<vect_section.size();i++)
	{
		if (vect_section[i].size() < str_sect_prefix.size() || 
			str_tolower(vect_section[i].substr(0,str_sect_prefix.size())) != str_sect_prefix)
		{
			continue;	
		}
				
		PflFileData pfl_file_data;
		pfl_file_data.name = vect_section[i];
		pfl_file_data.title = "w";
				
		VectConfigFileItem vect_config_item;
		isTrue = config_file_map.get_section_items(vect_config_item,vect_section[i]);
		if (!isTrue)
		{
			I18N_ERROR(_("%s [%s:%l] Can't read section : %s content.\n"),vect_section[i].c_str());
			continue;
		}
		
		for (unsigned int j=0;j<vect_config_item.size();j++)
		{
			if (str_tolower(vect_config_item[j].key) == "title")
			{
				pfl_file_data.title = vect_config_item[j].value;
				
			} else if (str_tolower(vect_config_item[j].key) == "is_cluster")
			{
				if (str_tolower(vect_config_item[j].value) == "false")
				{
					pfl_file_data.is_cluster = false;
				} else
				{
					pfl_file_data.is_cluster = true;
				}				
				
			}else if (str_tolower(vect_config_item[j].key.substr(0,str_cmb_prefix.size())) == str_cmb_prefix)
			{
				pfl_file_data.vect_item_ptr->push_back(vect_config_item[j]);
				//I18N_DEBUG(_("%s [%s:%l] add stock : %s \n"),vect_config_item[j].value.c_str());
			}
		}
	
		vect_pfl_file_data.push_back(pfl_file_data);
		
	} // for (unsigned int i=0;i<vect_section.size();i++)
	
	stable_sort(vect_pfl_file_data.begin(),
		vect_pfl_file_data.end(),
		hbstock2::PflFileDataSort);
					
	return isTrue;
}

void hbstock2::init_portfolio_tool()
{	
  	PflObjectManger* p_manger = PflObjectMangerInst::instance();
	p_manger->load_dllobject_from_default_dir();		
	I18N_DEBUG(_("%s [%s:%l] PortfolioTool -> init ...\n"));
	int argc = 0;
	ACE_TCHAR* argv[] = {NULL};
  	p_manger->init(argc,argv);  	  	
}
	
void hbstock2::fini_portfolio_tool()
{
	PflObjectManger* p_manger = PflObjectMangerInst::instance();
	p_manger->fini();
}

void hbstock2::get_last_cmb_key(std::string& str_cmb_key,const unsigned int i_size)
{
	unsigned int i_index = 0;
	i_index = i_size % 10000;
	
	char p_ch_seq[50];
	memset(p_ch_seq,0,sizeof(p_ch_seq));
	sprintf(p_ch_seq,"%04d",i_index);
	
	str_cmb_key = "cmb_" + gnu::time_now("%y%m%d%H%M%S") + p_ch_seq;
	
	I18N_DEBUG("%s [%s:%l] cmb_key : %s vect size : %d.\n",str_cmb_key.c_str(),i_size);
}

/*
unsigned int hbstock2::get_last_stock_no(const std::string& str_cmb_key,
	const unsigned int i_size)
{
	unsigned int i_num = 0;
	
	I18N_DEBUG("%s [%s:%l] cmb_key : %s vect size : %d.\n",str_cmb_key.c_str(),i_size);
	
	std::vector<std::string> vect_str;
	str_split(str_cmb_key.c_str(),"_",vect_str);
	
	if (vect_str.size() != 2)
	{
		return i_num;
	}
	
	i_num =  atol(vect_str[1].c_str());
	
	if (i_num > UINT_MAX -1)
	{
		i_num = 0;
	}
	
	if (i_num < i_size)
	{
		i_num = i_size;
	}
	
	return i_num;
}
*/

std::string hbstock2::get_pfl_data_file_name()
{
	string str_key = "pfl_data_file";
	string str_value = "w";	
	string str_config_file_name = "w";
	bool isTrue = false;
	
	AppConfig* p_app_config = AppConfigInst::instance();
	isTrue = p_app_config->get_value(str_key,str_value);
	if (!isTrue)
	{
		I18N_DEBUG(_("%s [%s:%l] Don't find key : pfl_data_file in config file, now insert it.\n"));
		str_value = "pfl_self.conf";
		p_app_config->set_value(str_key,str_value);
	}
	
	str_config_file_name = p_app_config->get_root_dir() + "/etc/" + str_value;
	
	return str_config_file_name;
}

void hbstock2::write_self_pfl_title(const SetStockSelectorPtr& set_selector_ptr)
{
	SetStockSelector::const_iterator iter;
	
	bool isTrue = false;
	string str_config_file_name = "w";
	ConfigFileMap config_file_map;
	
	str_config_file_name = get_pfl_data_file_name();
	isTrue = config_file_map.load_file(str_config_file_name.c_str());
	if (!isTrue)
	{
		//I18N_ERROR(_("%s [%s:%l] Can't open pfl_data_file : %s, system exit.\n"),str_config_file_name.c_str());		
		create_pfl_config_file(str_config_file_name.c_str());
		config_file_map.load_file(str_config_file_name.c_str());		
	}		
	
	string str_section = "w";
	string str_title_key = "title";
	string str_value = "w";
	for (iter=set_selector_ptr->begin();iter != set_selector_ptr->end();iter++)
	{
		str_section = string("pfl_") + (*iter)->get_name();
		// set title
		isTrue = config_file_map.get_value(str_section,str_title_key,str_value);
		if (!isTrue)
		{				
			isTrue = config_file_map.set_value(str_section,str_title_key,(*iter)->get_title());
		}
	}
}

void hbstock2::check_pfl_flag(bool& is_write_self_stocks,
	bool& is_include_test_data,
	bool& is_only_include_selected_data)
{
	string str_key = "is_write_self_stocks";
	string str_value = "w";
	int i_value = 0;
	bool isTrue = false;
	
	AppConfig* p_app_config = AppConfigInst::instance();
	isTrue = p_app_config->get_value(str_key,i_value);
	if (!isTrue)
	{
		I18N_DEBUG(_("%s [%s:%l] Don't find key : is_write_self_stocks in config file, now insert it.\n"));
		i_value = 1;
		p_app_config->set_value(str_key,i_value);
	}
	
	if (i_value == 0)
	{
		is_write_self_stocks = false;
	} else
	{
		is_write_self_stocks = true;
	}
	
	str_key = "is_include_test_data";
	i_value = 0;
	isTrue = p_app_config->get_value(str_key,i_value);
	if (!isTrue)
	{
		I18N_DEBUG(_("%s [%s:%l] Don't find key : is_include_test_data in config file, now insert it.\n"));
		i_value = 0;
		p_app_config->set_value(str_key,i_value);
	}
	
	if (i_value == 0)
	{
		is_include_test_data = false;
	} else
	{
		is_include_test_data = true;
	}
	
	str_key = "is_only_include_selected_data";
	i_value = 0;
	isTrue = p_app_config->get_value(str_key,i_value);
	if (!isTrue)
	{
		I18N_DEBUG(_("%s [%s:%l] Don't find key : is_only_include_selected_data in config file, now insert it.\n"));
		i_value = 1;
		p_app_config->set_value(str_key,i_value);
	}
	
	if (i_value == 0)
	{
		is_only_include_selected_data = false;
	} else
	{
		is_only_include_selected_data = true;
	}
}

void hbstock2::write_self_pfl_stocks_pfldata(const MapPflSumData& map_data,
	const bool is_test_treate )
{
	bool isTrue = false;
	bool is_write_self_stocks = true;
	bool is_include_test_data = false;
	bool is_only_include_selected_data = false;
	
	check_pfl_flag(is_write_self_stocks,
		is_include_test_data,
		is_only_include_selected_data);
	// get config of is_write_self_stocks				
	if (!is_write_self_stocks)
	{
		return;
	}
	
	if (is_test_treate && !is_include_test_data)
	{
		return;
	}
	
	string str_key = "w";
	string str_value = "w";
	str_key = "pfl_data_file";	
	string str_config_file_name = "w";
	
	str_config_file_name = get_pfl_data_file_name();
	
	ConfigFileMap config_file_map;
	
	isTrue = config_file_map.load_file(str_config_file_name.c_str());
	if (!isTrue)
	{
		//I18N_ERROR(_("%s [%s:%l] Can't open pfl_data_file : %s, system exit.\n"),str_config_file_name.c_str());		
		create_pfl_config_file(str_config_file_name.c_str());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -