?? chartkdj.cpp
字號:
// ChartKDJ.cpp: implementation of the CChartKDJ class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LastProject.h"
#include "ChartKDJ.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CChartKDJ::CChartKDJ(CRecords *pRecords,CDirectFace * pFace,int N,int M1,int M2)
{
int nWidth = pFace->m_rect.Width() - 60;
int nHeight = pFace->m_rect.Height() - 50;
int Zero = pFace->m_rect.Height() - 20;
double max = 0,min = 9999;
//計算所畫區域中的最大最小值
for(int i = pRecords->m_records.size()-1;i>=0;i--)
{
if(nWidth<8) break;
if(pRecords->m_records.at(i).max>max) max = pRecords->m_records.at(i).max;
if(pRecords->m_records.at(i).min<min) min = pRecords->m_records.at(i).min;
nWidth -= 10;
}
nWidth = pFace->m_rect.Width() - 60;
if(max==min)
{
max = min * 1.4;
min = min * 0.8;
}
double delta = max - min;
CString fmt,str;
HDC layer = NULL;
pFace->m_lpLayer[1]->GetDC(&layer);
CDC *pDC = CDC::FromHandle(layer);
pDC->SetBkColor(RGB(0,0,0));
pDC->SetTextColor(RGB(0,170,0));
//刷清圖層
pDC->SelectObject(::GetStockObject(BLACK_BRUSH));
pDC->SelectObject(&pFace->m_FramePen);
pDC->Rectangle(0,0,pFace->m_rect.Width(),pFace->m_rect.Height());
//畫題頭部分
pDC->SelectObject(&pFace->m_TitleFont);
pDC->TextOut(5,8,CodeToName(pRecords->ExCode));
pDC->TextOut(70,8,pRecords->TableName);
pDC->SetTextColor(RGB(255,0,0));
str.Format("K %3d",M1);
pDC->TextOut(130,8,str);
pDC->SetTextColor(RGB(0,255,0));
str.Format("D%3d",M2);
pDC->TextOut(175,8,str);
pDC->SetTextColor(RGB(255,255,0));
pDC->TextOut(210,8,"J=3*K-2*D");
pDC->SetTextColor(RGB(0,170,0));
//畫坐標軸
pDC->SelectObject(&pFace->m_WhitePen);
pDC->MoveTo(3,Zero);
pDC->LineTo(nWidth+5,Zero);
pDC->LineTo(nWidth+5,30);
int lab = 0;
//畫平行于X軸網格
pDC->SelectObject(&pFace->m_GrayPen);
pDC->SelectObject(&pFace->m_YFont);
for(i = 0;i<=8;i++)
{
int y = (int)(nHeight * i / 8);
str.Format("%d",-50 + i*25);
pDC->TextOut(nWidth+16,Zero-y-6,str);
if(i==0) continue;
pDC->MoveTo(3,Zero - y);
pDC->LineTo(nWidth+5,Zero - y);
}
pDC->SelectObject(&pFace->m_XFont);
for(i = pRecords->m_records.size()-1;i>=0;i--)
{
if(nWidth<8) break;
if(lab++%6 == 0)
{
if(pRecords->TableName=="五分鐘" || pRecords->TableName == "三十分鐘") fmt = "%d %H:%M";
if(pRecords->TableName=="小時") fmt = "%d日%H點";
if(pRecords->TableName=="日" || pRecords->TableName=="周") fmt = "%m月%d日";
if(pRecords->TableName=="月") fmt = "%y年%m月";
pDC->TextOut(nWidth-25,Zero+5,pRecords->m_records.at(i).DateTime.Format(fmt));
pDC->SelectObject(&pFace->m_GrayPen);
pDC->MoveTo(nWidth,Zero);
pDC->LineTo(nWidth,30);
}
nWidth -= 10;
}
deque<double> K,D,J;
//計算K
double sma = 0.5;
for(i=0;i<pRecords->m_records.size();i++)
{
int n = i<(M1 - 1)? i:M1 - 1;
double se = pRecords->m_records.at(i).close;
CString t = pRecords->m_records.at(i).DateTime.Format("%y-%m-%d %H:%M:%S");
double Max = MAX(pRecords,i,n);
double Min = MIN(pRecords,i,n);
double RSV = 0;
if(Max == Min) RSV = 1.00;
else RSV = (pRecords->m_records.at(i).close - Min) / (Max - Min);
sma = SMA(RSV,M1,1,sma);
if(i>M1) K.push_back(sma);
}
//計算D
sma = 0.5;
for(i=0;i<K.size();i++)
{
sma = SMA(K.at(i),M2,1,sma);
if(i>M2) D.push_back(sma);
}
//計算J
sma = 0.5;
int ptK = K.size() -1;
int ptD = D.size() -1;
while(ptK>=0 && ptD >=0)
J.push_front(3 * K.at(ptK--) - 2 * D.at(ptD--));
//畫K線
nWidth = pFace->m_rect.Width() - 60;
pDC->SelectObject(&pFace->m_RedPen);
for(i = K.size()-1;i>0;i--)
{
if(nWidth<13 ) break;
int X1 = nWidth;
int X2 = nWidth - 10;
int Y1 = Zero - (K.at(i) + 0.5) * nHeight / 2.0;
int Y2 = Zero - (K.at(i-1) + 0.5) * nHeight / 2.0;
pDC->MoveTo(X1,Y1);
pDC->LineTo(X2,Y2);
nWidth -= 10;
}
//畫D線
nWidth = pFace->m_rect.Width() - 60;
pDC->SelectObject(&pFace->m_GreenPen);
for(i = D.size()-1;i>0;i--)
{
if(nWidth<13 ) break;
int X1 = nWidth;
int X2 = nWidth - 10;
int Y1 = Zero - (D.at(i) + 0.5) * nHeight / 2.0;
int Y2 = Zero - (D.at(i-1) + 0.5) * nHeight / 2.0;
pDC->MoveTo(X1,Y1);
pDC->LineTo(X2,Y2);
nWidth -= 10;
}
//畫J線
nWidth = pFace->m_rect.Width() - 60;
pDC->SelectObject(&pFace->m_YellowPen);
for(i = J.size()-1;i>0;i--)
{
if(nWidth<13 ) break;
int X1 = nWidth;
int X2 = nWidth - 10;
int Y1 = Zero - (J.at(i) + 0.5) * nHeight / 2.0;
int Y2 = Zero - (J.at(i-1) + 0.5) * nHeight / 2.0;
pDC->MoveTo(X1,Y1);
pDC->LineTo(X2,Y2);
nWidth -= 10;
}
if(K.size()>0) K.clear();
if(D.size()>0) D.clear();
if(J.size()>0) J.clear();
pFace->m_lpLayer[1]->ReleaseDC(layer);
pFace->m_lpLayer[0]->BltFast(0,0,pFace->m_lpLayer[1],CRect(0,0,pFace->m_rect.Width(),pFace->m_rect.Height()),DDBLTFAST_WAIT);
pFace->m_lpPSur->BltFast(pFace->m_rect.left,pFace->m_rect.top,pFace->m_lpLayer[0],CRect(0,0,pFace->m_rect.Width(),pFace->m_rect.Height()),DDBLTFAST_WAIT);
}
double CChartKDJ::MAX(CRecords *pRecords, int index, int N)
{
double max = 0;
for(int i = 0;i<=N;i++)
{
if(max < pRecords->m_records.at(index-i).max)
max = pRecords->m_records.at(index-i).max;
}
return max;
}
double CChartKDJ::MIN(CRecords *pRecords, int index, int N)
{
double min = 9999;
for(int i = 0;i<=N;i++)
{
if(min > pRecords->m_records.at(index-i).min)
min = pRecords->m_records.at(index-i).min;
}
return min;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -