📄 chartrsi.cpp
字号:
// ChartRSI.cpp: implementation of the CChartRSI class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LastProject.h"
#include "ChartRSI.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CChartRSI::CChartRSI(CRecords *pRecords,CDirectFace * pFace,int RSI1,int RSI2,int RSI3)
{
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("RSI%3d",RSI1);
pDC->TextOut(140,8,str);
pDC->SetTextColor(RGB(0,255,0));
str.Format("RSI%3d",RSI2);
pDC->TextOut(190,8,str);
pDC->SetTextColor(RGB(255,255,0));
str.Format("RSI%3d",RSI3);
pDC->TextOut(240,8,str);
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("%0.1f",0 + i*12.5);
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> rsi1,rsi2,rsi3;
//计算RSI
double sma1 = 0.5 ,sma2 = 0.5,sma3 = 0.5,sma4 = 0.5,sma5 = 0.5,sma6 = 0.5,rate = 0.5;
for(i = 1;i<pRecords->m_records.size();i++)
{
double temp = pRecords->m_records.at(i).close - pRecords->m_records.at(i-1).close;
sma1 = SMA(temp>0? temp:0,RSI1,1,sma1);
sma2 = SMA(temp>0? temp:-temp,RSI1,1,sma2);
rate = sma1 / sma2;
if(i>=RSI1) rsi1.push_back(rate);
sma3 = SMA(temp>0? temp:0,RSI2,1,sma3);
sma4 = SMA(temp>0? temp:-temp,RSI2,1,sma4);
rate = sma3 / sma4;
if(i>=RSI2) rsi2.push_back(rate);
sma5 = SMA(temp>0? temp:0,RSI3,1,sma5);
sma6 = SMA(temp>0? temp:-temp,RSI3,1,sma6);
rate = sma5 / sma6;
if(i>=RSI3) rsi3.push_back(rate);
}
//画RSI1
nWidth = pFace->m_rect.Width() - 60;
pDC->SelectObject(&pFace->m_RedPen);
for(i = rsi1.size()-1;i>0;i--)
{
if(nWidth<13 ) break;
int X1 = nWidth;
int X2 = nWidth - 10;
int Y1 = Zero - rsi1.at(i) * nHeight;
int Y2 = Zero - rsi1.at(i-1) * nHeight;
pDC->MoveTo(X1,Y1);
pDC->LineTo(X2,Y2);
nWidth -= 10;
}
//画RSI2
nWidth = pFace->m_rect.Width() - 60;
pDC->SelectObject(&pFace->m_GreenPen);
for(i = rsi2.size()-1;i>0;i--)
{
if(nWidth<13 ) break;
int X1 = nWidth;
int X2 = nWidth - 10;
int Y1 = Zero - rsi2.at(i) * nHeight;
int Y2 = Zero - rsi2.at(i-1) * nHeight;
pDC->MoveTo(X1,Y1);
pDC->LineTo(X2,Y2);
nWidth -= 10;
}
//画RSI3
nWidth = pFace->m_rect.Width() - 60;
pDC->SelectObject(&pFace->m_YellowPen);
for(i = rsi3.size()-1;i>0;i--)
{
if(nWidth<13 ) break;
int X1 = nWidth;
int X2 = nWidth - 10;
int Y1 = Zero - rsi3.at(i) * nHeight;
int Y2 = Zero - rsi3.at(i-1) * nHeight;
pDC->MoveTo(X1,Y1);
pDC->LineTo(X2,Y2);
nWidth -= 10;
}
if(rsi1.size()>0) rsi1.clear();
if(rsi2.size()>0) rsi2.clear();
if(rsi3.size()>0) rsi3.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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -