stock_kline.cpp

来自「股票分析源代码」· C++ 代码 · 共 970 行 · 第 1/3 页

CPP
970
字号
#include <math.h>
#include <iostream>

#include "hbstock2/stock_kline.h"

using namespace std;
using namespace hbstock2;

void hbstock2::get_day_kline_ratio(std::vector<float>& vect_data,
	const VectStockData& vect_stockdata_in)
{
	float f_dist_close_price = 0;
	
	vect_data.clear();
	
	for (unsigned int i=0;i<vect_stockdata_in.size()-1;i++)
	{					
		f_dist_close_price = (vect_stockdata_in[i+1].close_price - 		
			vect_stockdata_in[i].close_price)/ vect_stockdata_in[i].close_price * 100;
		
		if (f_dist_close_price < -10.5)
		{
			f_dist_close_price = (vect_stockdata_in[i+1].close_price - 
				vect_stockdata_in[i+1].open_price)/ vect_stockdata_in[i+1].close_price * 100;
			
		} 
		
		vect_data.push_back(f_dist_close_price);
		
		//printf("date : %s\tf_dist_close_price: %f\n",vect_stockdata_in[i+1].date.c_str(),f_dist_close_price);			
	}	
}

KLINE_TYPE hbstock2::get_kline_type(const StockData& data,
	const float& f_max_ratio_t_star,
	const float& f_max_rate_t_star_high_low,	
	const float& f_min_ratio_high_middle_low,	
	const float& f_max_ratio_high_middle_low,	
	const float& f_min_rate_price,	
	const float& f_max_rate_price)
{
	// init data	
	float f_middle_close_open_price = 0;
	float f_dist_close_open = 0;
	float f_dist_high_middle = 0;
	float f_dist_middle_low = 0;
	float f_dist_high_low = 0;	
	
	// middle result
	float f_ratio_close_open_price = 0;
	float f_ratio_high_middle_low = 0;
	float f_rate_close_open_price = 0;
	float f_rate_high_low = 0;
	
	f_middle_close_open_price = (data.close_price +	data.open_price)/2;
	f_dist_close_open = data.close_price - data.open_price;
	f_dist_high_middle = data.high_price - f_middle_close_open_price;
	f_dist_middle_low = f_middle_close_open_price - data.low_price;
	f_dist_high_low = data.high_price - data.low_price;
	
	f_ratio_close_open_price = f_dist_close_open / f_dist_high_low * 100;
	f_rate_close_open_price = f_dist_close_open / data.open_price * 100;	
	f_ratio_high_middle_low = f_dist_high_middle/(f_dist_high_middle+f_dist_middle_low) * 100 ;
	f_rate_high_low = f_dist_high_low/f_middle_close_open_price * 100;
	
	/*
	if (data.date == "20051101")
	{
		cout  << "f_ratio_close_open_price : " << f_ratio_close_open_price << endl;	
		cout  << "f_rate_close_open_price  : " << f_rate_close_open_price << endl;	
		cout  << "f_ratio_high_middle_low  : " << f_ratio_high_middle_low << endl;	
		cout  << "f_rate_high_low          : " << f_rate_high_low << endl;	
	}
	*/
	
	if (fabsf(f_ratio_close_open_price) < f_max_ratio_t_star ||
		fabsf(f_rate_close_open_price) < f_max_rate_t_star_high_low * 0.9 || 
		fabsf(f_rate_high_low) < f_max_rate_t_star_high_low)
	{
		if (f_ratio_high_middle_low > f_max_ratio_high_middle_low)
		{
			return RT;
		} else if (f_ratio_high_middle_low < f_min_ratio_high_middle_low)
		{
			return T;
		} else
		{
			return CROSS;
		}		
	}
	
	if (f_dist_close_open > 0) // up
	{
		if (f_rate_close_open_price < f_min_rate_price)
		{
			return SMALLUP;
		} else if (f_rate_close_open_price > f_max_rate_price)
		{
			return BIGUP;
		} else
		{
			return MIDDLEUP;
		}
	} else
	{
		if (f_rate_close_open_price < - f_max_rate_price)
		{
			return BIGDOWN;
		} else if (f_rate_close_open_price > - f_min_rate_price)
		{
			return SMALLDOWN;
		} else
		{
			return MIDDLEDOWN;
		}
	}	
	
	return T;
}

KLINE_TRACE_TYPE hbstock2::get_kline_trace_type(
	const StockData& data_prev,const StockData& data,	
	const float& f_zero_rate_price)
{
	float f_rate_close = 0;
	float f_rate_open = 0;
	
	f_rate_open = (data.open_price - data_prev.close_price) * 100 / data_prev.close_price;
	f_rate_close = (data.close_price - data.open_price) * 100 / data.open_price;
	
	/*	
	if (data.date == "20050831")
	{
		cout  << "f_rate_open   : " << f_rate_open << endl;	
		cout  << "f_rate_close  : " << f_rate_close << endl;		
	}
	*/	
	int i_open_type = 0;
	int i_close_type = 0;
	
	if (f_rate_open < -f_zero_rate_price)
	{
		i_open_type = -1;
	} else if (f_rate_open > f_zero_rate_price)
	{
		i_open_type = 1;
	}
	
	if (f_rate_close < -f_zero_rate_price)
	{
		i_close_type = -1;
	} else if (f_rate_close > f_zero_rate_price)
	{
		i_close_type = 1;
	}
	
	if (i_open_type == -1 && i_close_type == -1)
	{
		return LOWLOW;
	} else if (i_open_type == -1 && i_close_type == 0)
	{
		return LOWZERO;
	} else if (i_open_type == -1 && i_close_type == 1)
	{
		return LOWHIGH;
	} else if (i_open_type == 0 && i_close_type == -1)
	{
		return ZEROLOW;
	} else if (i_open_type == 0 && i_close_type == 0)
	{
		return ZEROZERO;
	} else if (i_open_type == 0 && i_close_type == 1)
	{
		return ZEROHIGH;
	} else if (i_open_type == 1 && i_close_type == -1)
	{
		return HIGHLOW;
	} else if (i_open_type == 1 && i_close_type == 0)
	{
		return HIGHZERO;
	} else if (i_open_type == 1 && i_close_type == 1)
	{
		return HIGHHIGH;
	}
	
	return LOWLOW;
}

void hbstock2::get_kline_info(KLineInfo& info,
	const VectStockData& vect_data,
	const unsigned int& len,
	const bool& is_div,
	const float& div_rate )
{
	info.is_div = is_div;
	info.div_rate = div_rate;
	info.is_include_div_data = false;
	info.index_div = 0;
	
	unsigned int i_start = 0;
	unsigned int i_end = vect_data.size();
	
	if (len != 0 && len < i_end-1)
	{
		i_start = i_end - len ;
	}
	
	// init data
	info.highest_price = vect_data[i_start].high_price;
	info.lowest_price = vect_data[i_start].low_price;
	info.hc_lowest_price = vect_data[i_start].low_price;
	info.current_price = vect_data[i_end-1].close_price;
	
	strcpy(info.highest_price_day,vect_data[i_start].day);
	strcpy(info.lowest_price_day, vect_data[i_start].day);
	strcpy(info.hc_lowest_price_day ,vect_data[i_start].day);
	strcpy(info.current_price_day, vect_data[i_end-1].day);
	
	info.index_highest = i_start;
	info.index_lowest = i_start;
	info.index_hc_lowest = i_start;
	info.index_current = i_end -1;
	
	float f_close_close_rate = 0;
	
	for (unsigned int i=i_start+1;i<i_end;i++)
	{
		if (info.is_div && !info.is_include_div_data)
		{
			f_close_close_rate = (vect_data[i].close_price-vect_data[i-1].close_price)*100/vect_data[i-1].close_price;	
			if (f_close_close_rate < div_rate)
			{
				info.is_include_div_data = true;
				info.index_div = i;
			}
		}
		
		
		// set lowest price
		if (info.lowest_price >= vect_data[i].low_price)
		{
			info.lowest_price = vect_data[i].low_price;
			info.index_lowest = i;
			strcpy(info.lowest_price_day, vect_data[i].day);
		}
		
		// set highest price
		if (info.highest_price <= vect_data[i].high_price)
		{
			info.highest_price = vect_data[i].high_price;
			info.index_highest = i;
			strcpy(info.highest_price_day, vect_data[i].day);
			
			// change hc_lowest_price when changed highest price
			
			info.hc_lowest_price = vect_data[i].low_price;
			info.index_hc_lowest = i;
			strcpy(info.hc_lowest_price_day, vect_data[i].day);
		}
		
		// set hc_lowest price
		if (info.hc_lowest_price >= vect_data[i].low_price)
		{
			info.hc_lowest_price = vect_data[i].low_price;
			info.index_hc_lowest = i;
			strcpy(info.hc_lowest_price_day, vect_data[i].day);
		}
		
	} //for (unsigned int i=i_start+1;i<i_end;i++)
	
	// set highes ,lowest to close_price
	info.lowest_close_price = vect_data[info.index_lowest].close_price;
	info.hc_lowest_close_price = vect_data[info.index_hc_lowest].close_price;
	info.highest_close_price = vect_data[info.index_highest].close_price;
	
	// set dist
	info.dist_highest_lowest = info.index_lowest - info.index_highest;
	info.dist_highest_hc_lowest = info.index_hc_lowest - info.index_highest ;
	info.dist_highest_current = info.index_current - info.index_highest;
	info.dist_hc_lowest_current = info.index_current - info.index_hc_lowest;
	info.dist_lowest_current = info.index_current - info.index_lowest ;
	
	// set rate
	info.rate_highest_lowest = (info.highest_price-info.lowest_price)*100/info.highest_price;
	info.rate_highest_hc_lowest = (info.highest_price-info.hc_lowest_price)*100/info.highest_price;
	info.rate_highest_current = (info.highest_price-info.current_price)*100/info.highest_price;
	info.rate_hc_lowest_current = (info.current_price-info.hc_lowest_price)*100/info.hc_lowest_price;
	info.rate_hc_lowest_lowest = (info.hc_lowest_price-info.lowest_price)*100/info.hc_lowest_price;
	info.rate_lowest_current = (info.current_price-info.lowest_price)*100/info.lowest_price;
		
	info.rate_highest_lowest_close = (info.highest_close_price-info.lowest_close_price)*100/info.highest_close_price;
	info.rate_highest_hc_lowest_close = (info.highest_close_price-info.hc_lowest_close_price)*100/info.highest_close_price;
	info.rate_highest_current_close = (info.highest_close_price-info.current_price)*100/info.highest_close_price;
	info.rate_hc_lowest_current_close = (info.current_price-info.hc_lowest_close_price)*100/info.hc_lowest_close_price;
	info.rate_hc_lowest_lowest_close = (info.hc_lowest_close_price-info.lowest_close_price)*100/info.hc_lowest_close_price;
	info.rate_lowest_current_close = (info.current_price-info.lowest_close_price)*100/info.lowest_close_price;
	
}


void hbstock2::print_kline_info(const KLineInfo& info)
{
	cout << "====================================================================" << endl;
	
	cout << "highest_price date       : " << info.highest_price_day << endl;
	cout << "lowest_price date        : " << info.lowest_price_day << endl;
	cout << "hc_lowest_price date     : " << info.hc_lowest_price_day << endl;
	cout << "current_price date       : " << info.current_price_day << endl;
	
	cout << "---------------------------------------------------------" << endl;
	
	cout << "highest_price            : " << info.highest_price << endl;
	cout << "highest_close_price      : " << info.highest_close_price << endl;
	cout << "lowest_price             : " << info.lowest_price << endl;
	cout << "lowest_close_price       : " << info.lowest_close_price << endl;
	cout << "hc_lowest_price          : " << info.hc_lowest_price << endl;
	cout << "hc_lowest_close_price    : " << info.hc_lowest_close_price << endl;
	cout << "current_price            : " << info.current_price << endl;
	
	cout << "---------------------------------------------------------" << endl;
	
	cout << "index_highest            : " << info.index_highest << endl;
	cout << "index_lowest             : " << info.index_lowest << endl;
	cout << "index_hc_lowest          : " << info.index_hc_lowest << endl;

⌨️ 快捷键说明

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