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

📄 dlgreadfile.cpp

📁 从联合证券数据读取股票数据以及画出K线。不完善
💻 CPP
字号:
// dlgReadFile.cpp : implementation file
//

#include "stdafx.h"
#include "StockDataParse.h"
#include "dlgReadFile.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



#define ERR_FILE_OPEN			0x01
#define ERR_FILE_LONG			0x02
#define ERR_MEMORY_HANDLE		0x04
#define ERR_MEMORY_ALLOC		0x08
#define ERR_MEMORY_NULL			0x10
#define ERR_DATA_SHORT			0x12




/////////////////////////////////////////////////////////////////////////////
// dlgReadFile dialog


dlgReadFile::dlgReadFile(CWnd* pParent /*=NULL*/)
	: CDialog(dlgReadFile::IDD, pParent)
{
	//{{AFX_DATA_INIT(dlgReadFile)
	m_iCountDay = 30;
	m_iStartDay = 19960000;
	m_iStopDay = 20070000;
	m_IStockEndNum = 999999;
	m_iStockStartNum = 1;
	m_iCycle = 60;
	m_iStockCode = 0;
	//}}AFX_DATA_INIT
	bShowKline = false;
}


void dlgReadFile::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(dlgReadFile)
	DDX_Text(pDX, IDC_EDIT_COUNT_DAY, m_iCountDay);
	DDV_MinMaxInt(pDX, m_iCountDay, 10, 50);
	DDX_Text(pDX, IDC_EDIT_DATE_START, m_iStartDay);
	DDV_MinMaxInt(pDX, m_iStartDay, 19900404, 20080909);
	DDX_Text(pDX, IDC_EDIT_DATE_STOP, m_iStopDay);
	DDV_MinMaxInt(pDX, m_iStopDay, 19910101, 20210000);
	DDX_Text(pDX, IDC_EDIT_ENDNUM, m_IStockEndNum);
	DDV_MinMaxInt(pDX, m_IStockEndNum, 2, 999999);
	DDX_Text(pDX, IDC_EDIT_STARTNUM, m_iStockStartNum);
	DDV_MinMaxInt(pDX, m_iStockStartNum, 0, 999998);
	DDX_Text(pDX, IDC_EDIT_CYCLE, m_iCycle);
	DDV_MinMaxInt(pDX, m_iCycle, 3, 500);
	DDX_Text(pDX, IDC_EDIT_STOCKCODE, m_iStockCode);
	DDV_MinMaxUInt(pDX, m_iStockCode, 0, 999999);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(dlgReadFile, CDialog)
	//{{AFX_MSG_MAP(dlgReadFile)
	ON_BN_CLICKED(IDC_BTN_READ_FILE, OnBtnReadFile)
	ON_BN_CLICKED(IDC_BTN_READ_ONE_FILE, OnBtnReadOneFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// dlgReadFile message handlers

void dlgReadFile::OnBtnReadFile() 
{
	// TODO: Add your control notification handler code here
	//////////////////////////////////////////////////////////////////////////
	CString szFilter = "All Files (*.day)|*.day| other (*.*)|*.*||";
	CFileDialog dlg(TRUE, NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
	const int ci_FILE_LEN=24*1024*1024;
	const int ci_Len=10;
	
	//ST_StoInfo oREC_StoInfo; 
	//List_REC_StoInfoQ oList_REC_StoInfoQ;

	UpdateData(TRUE);
	if (dlg.DoModal() == IDOK )	
	{
		CString strFileName, strHead, strTail;
		strFileName = dlg.GetPathName();
		//////////////////////////////////////////////////////////////////////////
		int pos = strFileName.ReverseFind('\\');
		strHead = strFileName.Left(pos+3);  //find strhead
		int iMaxLen;
		//////////////////////////////////////////////////////////////////////////

		CString strTemp = strHead.Mid(pos+1,2);
		if("sh"==strTemp)
		{
			iMaxLen = MAX_SH_NUMBER;   //sh
		}else
		{
			iMaxLen = MAX_SH_NUMBER;   //sz
		}
		//read all the file in the folder
		for(int i=m_iStockStartNum;i<m_IStockEndNum;i++)   
		{
			/*
			if((strTemp =="sh") && ((i<600000)||(i>602000)) )
						{
							continue;
						}
						if((strTemp =="sz") && (i>1000) )
						{
							continue;
						}*/
			
			strTail.Format("%.6d.day",i);
			strFileName = strHead + strTail;
			strStockName = strFileName.Mid(pos+1,8);
			/*
			strStockName +="\n\n";
						WriteFile(strStockName);*/
			

			//read strFileName
			// List_ST_StockInfoQ oList_ST_StockInfoQ;
			//m_List_ST_StockInfoQ oList_ST_StockInfoQ;
			// if(0==ReadFileData(strFileName,&oList_ST_StockInfoQ))   //file exist
			m_List_ST_StockInfoQ.clear();
			if(0==ReadFileData(strFileName,&m_List_ST_StockInfoQ))   //file exist
			{
				List_ST_StockInfoQ::iterator iter_StockInfo;

				
				//fList fListData;
				bShowKline = true;
				//PlotKLine();


				IntArray iArrayDayData;
				FloatArray fArrayData;
				FloatArray fArrayCountData;
				const int isize = m_List_ST_StockInfoQ.size();
				//float* fDataInfo = new float[isize];
				//memset(fDataInfo,0.0,sizeof(float)*isize);
				
				//get close price

				//int index=0;,)
				for(iter_StockInfo=m_List_ST_StockInfoQ.begin();iter_StockInfo!=m_List_ST_StockInfoQ.end();++iter_StockInfo)
				{
					//if (index>=isize)
					//					{
					//						break;
					//					}
					
					//*(fDataInfo+index) =  iter_StockInfo->stPriceInfo.fClosePrice;

					fArrayData.push_back(iter_StockInfo->stPriceInfo.fClosePrice);   //select close price
					iArrayDayData.push_back(iter_StockInfo->iDate);
					fArrayCountData.push_back(iter_StockInfo->fHandCount);
					//++index;
				}


				//
				int iArrayLen = fArrayData.size();
				
				//if the data is too little, break;
				if(m_iCycle>iArrayLen/3)
				{
					continue;
				}

				strStockName +="\n\n";
				WriteFile(strStockName);

				//caculate the slow mid fast line
				const int ciSlowSpeedDay = 60;
				const int ciMidSpeedDay  = 20;
				const int ciFastSpeedDay = 10;
				FloatArray fArrayDataAveFast(fArrayData);
				FloatArray fArrayDataAveMid(fArrayData);
				FloatArray fArrayDataAveSlow(fArrayData);

				fArrayDataAveFast = CalculateSpeedData(fArrayData,fArrayCountData,ciFastSpeedDay,iArrayLen);  //fast 
				fArrayDataAveMid  = CalculateSpeedData(fArrayData,fArrayCountData,ciMidSpeedDay,iArrayLen);   //mid
				fArrayDataAveSlow = CalculateSpeedData(fArrayData,fArrayCountData,ciSlowSpeedDay,iArrayLen);   //slow
			
				//20081005 cjt
				//int iRet = FunProcessData(fArrayDataAveFast,iArrayDayData,iArrayLen);

				FloatArray fArrayDMA(fArrayData);

				int iLen = fArrayData.size();
				float* pfDIF = new float[iLen];
				float* pfDEA = new float[iLen];

				for(int i=0;i<iLen;i++)
				{
					*(pfDIF+i) = fArrayData[i];
				}

				int iRet = oStockDataProcess.GenerateDifDeaArray(pfDIF,pfDEA,iLen);
			}
							
		}		
	}
	//////////////////////////////////////////////////////////////////////////	
	
	
}

int dlgReadFile::ReadFileData(CString strFileName, List_ST_StockInfoQ* pList_ST_StockInfoQ)
{
	CFile			oFile;	
	CFileException	err;		
	
	
	if( !oFile.Open( strFileName, CFile::modeRead, &err ) )
	{		
		return ERR_FILE_OPEN;						
	}		
	//////////////////////////////////////////////////////////////////////////
	
	int  dwFileSize=oFile.GetLength();
	const int ci_FILE_LEN = 10*1024*1024;
	
	if(dwFileSize > ci_FILE_LEN)
	{
		CString strLen;
		strLen.Format(_T("The File is too long, the lengh is less than %d !"), ci_FILE_LEN);
		AfxMessageBox(strLen);
		return ERR_FILE_LONG;
	}
	
	
	
	HANDLE s_hHeap = NULL;
	s_hHeap = HeapCreate(HEAP_NO_SERIALIZE,0,0);
	
	if(s_hHeap == NULL)
	{
		oFile.Close();		
		return ERR_MEMORY_HANDLE;
	}
	
	
	if (0<dwFileSize) 
	{	
		int iLenInt = dwFileSize/sizeof(int);
		int* pBuf	=(int*) HeapAlloc(s_hHeap,0,dwFileSize);		
		if (!pBuf	)
		{
			oFile.Close();
			
			return ERR_MEMORY_ALLOC;
		}		
		
		int iBuf[8];
		oFile.SeekToBegin();				
		
		oFile.Read(pBuf,  dwFileSize );
		int* piOffset = pBuf;		
		
		int	iLength = sizeof(iBuf);		
		
		int index=1; 
		
		ST_StockInfo stStockInfo;
		while(index++<=(iLenInt/8))
		{
			memcpy(iBuf,piOffset,sizeof(iBuf));
			
			//////////////////////////////////////////////////////////////////////////
			
			memset(&stStockInfo,0,sizeof(stStockInfo));				
			
			stStockInfo.iDate                  = iBuf[0];           
			stStockInfo.stPriceInfo.fOpenPrice = (float)(iBuf[1])/100;
			stStockInfo.stPriceInfo.fHighPrice = (float)(iBuf[2])/100;
			stStockInfo.stPriceInfo.fLowPrice  = (float)(iBuf[3])/100;
			stStockInfo.stPriceInfo.fClosePrice  = (float)(iBuf[4])/100;
			stStockInfo.fHandCount				= (float)(iBuf[6])/100000;	
			
			if( (m_iStartDay<stStockInfo.iDate)
				&&
				(m_iStopDay>stStockInfo.iDate)
				)
			{
				pList_ST_StockInfoQ->push_back(stStockInfo);
			}
			
			
		
			//////////////////////////////////////////////////////////////////////////
			
			
			piOffset+=(sizeof(iBuf)/sizeof(iBuf[0]));
		}
		
		
		

		//////////////////////////////////////////////////////////////////////////		
				
	}
	oFile.Close();		
	HeapDestroy(s_hHeap);
	
	return 0;
}


//process data
int dlgReadFile::FunProcessData(FloatArray oDataArray,IntArray oIntArrayDay, int length)
{

	

	if(length <3*m_iCycle)
	{
		return ERR_DATA_SHORT;
	}

	


	//
	CString StrBullorBear;
	CString strWrite;

	
	//bool bBear_Bull;    //1 bear  0 bull



	for(int i=m_iCountDay;i<length;i++)
	{
		int iNow = oDataArray[i];
		//all data


		/*int iPositiveCount=0,iNegativeCount=0;
		for(int j=0;j<m_iCountDay;j++)
		{
			if(oDataArray[i-j]<=0)
			{
				iNegativeCount++;
			}
			else
			{
				iPositiveCount++;
			}
		}

		//bear or bull
		
		bBear_Bull = (iNegativeCount >iPositiveCount)?true:false;

		iNow = (bBear_Bull)?9:5;

		
		if(bBear_Bull)   // once bear
		{
			int iCount=0;
			for(j=0;j<iNow-1;j++)
			{
				if( oDataArray[i-j] < oDataArray[i-j-1])
				{
					break;
				}
				else if(oDataArray[i-j-1]>0)
				{
					iCount++;
				}
			}

			if(iCount==iNow-2)
			{
				StrBullorBear.Format("时间:%d\t",oIntArrayDay[i]);
				StrBullorBear += "牛市来了 ...你买!\n";
				strWrite += StrBullorBear;
				
			}
		}
		else       //once bull
		{

			int iCount=0;
			for(j=0;j<iNow-1;j++)
			{
				if( oDataArray[i-j] > oDataArray[i-j-1])
				{
					break;
				}
				else if(oDataArray[i-j-1]<0)
				{
					iCount++;
				}
			}
			
			if(iCount==iNow-2)
			{
				StrBullorBear.Format("时间:%d\t",oIntArrayDay[i]);
				StrBullorBear += "熊市来了。。。快跑命!\n";
				strWrite += StrBullorBear;	
				
			}
		}
		
		*/

		/*
		if(( 0 > oDataArray[i-m_iCountDay]) && (iNegativeCount==0))  //bull
				{
					StrBullorBear.Format("时间:%d\t",oIntArrayDay[i]);
					StrBullorBear += "牛市来了 ...你买!\n";
					strWrite += StrBullorBear;
					//WriteFile(StrBullorBear);
				}
		
				if	(( 0 < oDataArray[i-m_iCountDay]) && (iPositiveCount==0))   //bear
				{
					StrBullorBear.Format("时间:%d\t",oIntArrayDay[i]);
					StrBullorBear += "熊市来了。。。快跑命!\n";
					strWrite += StrBullorBear;
					
				}*/
		
		
	}

	/*
	strWrite += "\n\n";
	WriteFile(strWrite);*/

	return 0;

}

int dlgReadFile::WriteFile(CString strWrite)
{

	CFile			oFile1;	
	CFileException	err;	
				
	CString strFileName1 = "D:\\DataStock.txt";
				
				
	if( !oFile1.Open( strFileName1, CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite, &err ) )
	{	
		//AfxMessageBox("Can't open the file , please try again!");
		return -2;	//_T("打开记录文件失败!"));						
	}
	oFile1.SeekToEnd();		
				
	//CString strRealWrite = strStockName +"\n"+strWrite;			
	int iLen = strWrite.GetLength();//sizeof(StrOutput)*
	oFile1.Write(strWrite,iLen);
	
	oFile1.Close();
	return 0;
}

FloatArray dlgReadFile::CalculateSpeedData(FloatArray fArrayDataIn,FloatArray fArrayDataRatio,const int iSpeed,const int length)
{
	FloatArray fArrayRet(fArrayDataIn);
	for(int i=0;i<iSpeed;i++)
	{
		
		float fRet= fArrayDataIn[i];
		fArrayRet[i] = fArrayDataIn[i];
	}
	
	//compute the special average value
	for(i=iSpeed;i<length;i++)
	{
		float fAveVal = 0;
	
		float fRatioTotal = 0;
		for(int j=i-iSpeed;j<i;j++)
		{	
			
			fRatioTotal = fRatioTotal +fArrayDataRatio[j];
		}
		
		
					
		for(j=i-iSpeed;j<i;j++)
		{
			fAveVal += (fArrayDataIn[j]*fArrayDataRatio[j])/fRatioTotal;
		}
					
					
		fArrayRet[i]= fAveVal;
					
				
	}

	return fArrayRet;
}

int dlgReadFile::DrawDCMap(CDC* pDC, CRect Rect, List_ST_StockInfoQ myStockInfoList,int i_listLen)
{



	return 0;
}



void dlgReadFile::OnBtnReadOneFile() 
{
	UpdateData(TRUE);
	if(m_iStockCode ==0)
	{
		AfxMessageBox("股票代码不能为空,请重试!");
		return;
	}
	static char displayname[MAX_PATH];
	static char path[MAX_PATH];
    LPITEMIDLIST pidlBrowse;     
	BROWSEINFO bi;
	bi.hwndOwner = this->m_hWnd;
	bi.pidlRoot = NULL;
	bi.pszDisplayName = displayname;
	bi.lpszTitle = "请选择要保存接收数据的文件夹";
	bi.ulFlags = BIF_EDITBOX ;
	bi.lpfn = NULL;
	pidlBrowse=SHBrowseForFolder( &bi);
	if(pidlBrowse!=NULL)
	{
		SHGetPathFromIDList(pidlBrowse,path);
	}
	CString str=path;  //得到路径
	if(str.IsEmpty()) 
		return;  //如果没有选择,就返回
	//m_strCurPath=str;	//接收路径编辑框对应变量
	 
	// TODO: Add your control notification handler code here
	//CString szFilter = "All Files (*.day)|*.day| other (*.*)|*.*||";
	//	CFileDialog dlg(TRUE, NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
	//	const int ci_FILE_LEN=24*1024*1024;
	//	const int ci_Len=10;
	//	
	//	m_List_ST_StockInfoQ.clear();
	//	
	//	//UpdateData(TRUE);
	//	if (dlg.DoModal() == IDOK )
	//	{
	

	CString strTemp;
	strTemp.Format("\\sh%6d.day",m_iStockCode);
	
	CString strFileName=str+strTemp;
	//strFileName = dlg.GetPathName();
	if(ReadFileData(strFileName,&m_List_ST_StockInfoQ)!=0)
	{
		AfxMessageBox("Eroor when read file");
		bShowKline = false;
	}
	else{
		bShowKline = true;
	}
	// }
	
	
}

⌨️ 快捷键说明

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