策略为王源代码扩展系列-K线图(3)-为K线图当日实时数据合并到K线

Published

思路一:

设置一个当日变量,读取当日实时分笔数据,转换成分时线,由分时线转换成1分钟线,1分钟转5分钟,5分钟转日K线。然后开始合并数据到历史数据。

思路二:

 

 

 

src/Client/StkLib/Include/Stock.h

/************custom extend 分笔数据转1分钟、1分钟转5分钟、5分钟转日线转换程序,用于当日数据转换**********************/
	
	
	static  int CKData::Min1ToMin5(CKData& kdm1, CKData& kdm5);
	static  int CKData::Min5ToDay(CKData& kdm5, CKData& kdday);
	/***********************************/

src/Client/StkLib/Src/KData.cpp

/************custom extend 分笔数据转1分钟、1分钟转5分钟、5分钟转日线转换程序,用于当日数据转换**********************/


int CKData::Min1ToMin5(CKData& kdm1, CKData& kdm5)
{
	SP_ASSERT(ktypeMin1 == kdm1.GetKType());
	SP_ASSERT(ktypeMin5 == kdm5.GetKType());
	return ConvertKData(kdm1, kdm5, 5);

	
}

int CKData::Min5ToDay(CKData& kdm5, CKData& kdday)
{
	SP_ASSERT(ktypeMin5 == kdm5.GetKType());
	SP_ASSERT(ktypeDay == kdm5.GetKType());
	return ConvertKData(kdm5, kdday, 48);
}

/***********************************/

src\Client\StkUI\View\StockGraph.cpp

//CStockGraph::PrepareStockData( BOOL bReload )加载画图所需要的数据 by freeman
BOOL CStockGraph::PrepareStockData(BOOL bReload)
{
	if (!m_CurStock.GetStockInfo().IsValidStock())
		return FALSE;

	CStockInfo	info;
	if (AfxGetStockContainer().GetStockInfo(m_CurStock.GetStockCode(), &info)
		&& m_CurStock.GetStockInfo().IsEqualTo(info.GetMarket(), info.GetStockCode()))
	{
		m_CurStock.SetStockInfo(&info);
		m_CurStockDown.SetStockInfo(&info);
	}

	int nOldSize = m_CurStock.GetKData(m_nCurKType).GetSize();

	// Prepare Data
	AfxPrepareStockData(&AfxGetDB(), m_CurStock, m_nCurKType, m_nCurKFormat, m_nCurMaindataType, FALSE, bReload);
	AfxPrepareStockData(&AfxGetDB(), m_CurStockDown, m_nCurKType, CKData::formatXDRdown, m_nCurMaindataType, FALSE, bReload);



	/********custom extend 添加当日数据by freeman*******************/
	CStock		m_CurStockCurDay;

	m_CurStockCurDay.SetStockInfo(&info);

	m_CurStockCurDay.GetReport().RemoveAll();
	m_CurStockCurDay.GetMinute().RemoveAll();
	m_CurStockCurDay.GetOutline().RemoveAll();

	// ------读取本地文件:先读当天的分笔数据,然后将分笔数据转换为分时数据,再将分时数据转换为1分钟数据
	m_CurStockCurDay.SetDatabase(&AfxGetDB());

	// 从磁盘加载分笔数据
	m_CurStockCurDay.PrepareData(CStock::dataReport, CKData::ktypeDay, TRUE);
	//加载分时数据,实际是从分笔数据转换而来
	m_CurStockCurDay.PrepareData(CStock::dataMinute, CKData::ktypeDay, TRUE);


	//获取成交明细
	CReport& aReport = m_CurStockCurDay.GetReport();

	CKData& kdata = m_CurStockCurDay.GetKData(m_nCurKType);

	//kdata.DayToMonth

	CKData& kdataMin1 = m_CurStockCurDay.GetKData(CKData::ktypeMin1);
	CKData kdataMin5 = m_CurStockCurDay.GetKData(CKData::ktypeMin5);
	CKData kdm15 = m_CurStockCurDay.GetKData(CKData::ktypeMin15);
	CKData kdataDay = m_CurStockCurDay.GetKData(CKData::ktypeDay);

	kdataMin1.SetKType(CKData::ktypeMin1);
	kdataMin5.SetKType(CKData::ktypeMin5);
	kdataDay.SetKType(CKData::ktypeDay);

	//	kdata.Min5ToMin15(m_CurStockCurDay.GetKDataMin5(), m_CurStockCurDay.GetKDataMin15());



	CMinute& minute = m_CurStockCurDay.GetMinute();

	//将分时线转换为分钟线
	for (int i = 0; i < minute.GetSize(); i++)
	{
		MINUTE& min = minute.ElementAt(i);

		KDATA	kd;
		memset(&kd, 0, sizeof(kd));
		kd.m_dwMarket = min.m_dwMarket;
		strncpy(kd.m_szCode, min.m_szCode, sizeof(kd.m_szCode) - 1);
		kd.m_time = min.m_time;
		CSPTime sptime(min.m_time);
		kd.m_date = sptime.ToStockTimeMin();
		kd.m_time = min.m_time;
		kd.m_fOpen = (min.m_fHigh + min.m_fLow + min.m_fNew) / 3;
		kd.m_fHigh = min.m_fHigh;
		kd.m_fLow = min.m_fLow;
		kd.m_fClose = min.m_fNew;
		kd.m_fVolume = min.m_fVolume;
		kd.m_fAmount = min.m_fAmount;
		kdataMin1.Add(kd);

	}


	CKData::Min1ToMin5(kdataMin1, kdataMin5);
	//CKData::Min5ToDay(kdataMin5, kdataDay);






	CKData	kdMin(CKData::ktypeMin5);

	KDATA	dataDest;

	//分笔转换为分钟线
	for (int i = 0; i < aReport.GetSize(); i++)

	{
		KDATA	dataDest;
		memset(&dataDest, 0, sizeof(dataDest));
		REPORT pReport = aReport.GetAt(i);

		dataDest.m_dwMarket = 1;	// 1 min

		dataDest.m_dwMarket = pReport.m_dwMarket;
		strncpy(dataDest.m_szCode, pReport.m_szCode, min(sizeof(dataDest.m_szCode) - 1, sizeof(pReport.m_szCode)));

		time_t	temp = 60 * (pReport.m_time / 60);
		if (temp < pReport.m_time)
			temp += 60;
		dataDest.m_time = temp;
		dataDest.m_fClose = pReport.m_fNew;
		dataDest.m_fHigh = pReport.m_fHigh;
		dataDest.m_fLow = pReport.m_fLow;
		dataDest.m_fVolume = pReport.m_fVolume;
		dataDest.m_fAmount = pReport.m_fAmount;


	}




	//UpdateStockInfoByREPORT(m_CurStock.GetStockInfo(), &(pCommPacket->m_pReport[i]))

	/***************************/

	if (m_CurStock.GetKData(m_nCurKType).GetSize() != nOldSize)
		ResetIndex();

	AfxGetStkReceiver().RequestKData(&m_CurStock, m_nCurKType);

	m_techparam.Clear();
	m_techparam.SetKData(&(m_CurStockDown.GetKData(m_nCurKType)));
	m_techparam.volume.SetKData(&(m_CurStock.GetKData(m_nCurKType)));
	m_techparam.ma.SetKData(&(m_CurStock.GetKData(m_nCurKType)));
	m_techparam.bbi.SetKData(&(m_CurStock.GetKData(m_nCurKType)));
	m_techparam.boll.SetKData(&(m_CurStock.GetKData(m_nCurKType)));
	m_techparam.pv.SetKData(&(m_CurStock.GetKData(m_nCurKType)));
	m_techparam.sar.SetKData(&(m_CurStock.GetKData(m_nCurKType)));
	m_techparam.dj.SetKData(&(m_CurStock.GetKData(m_nCurKType)));
	m_techparam.mike.SetKData(&(m_CurStock.GetKData(m_nCurKType)));

	m_techparam.dj2.SetKData(&(m_CurStock.GetKData(m_nCurKType)));

	return TRUE;
}