//+------------------------------------------------------------------+
//| CandleGraphic.mq5 |
//| Copyright 2000-2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include <Graphics\Graphic.mqh>
//+------------------------------------------------------------------+
//| 클래스 CCandle |
//| 사용: 캔들을 나타내는 클래스 |
//+------------------------------------------------------------------+
class CCandle: public CObject
{
private:
double m_open;
double m_close;
double m_high;
double m_low;
uint m_clr_inc;
uint m_clr_dec;
int m_width;
public:
CCandle(const double open,const double close,const double high,const double low,
const int width,const uint clr_inc=0x000000,const uint clr_dec=0xF5F5F5);
~CCandle(void);
double OpenValue(void) const { return(m_open); }
double CloseValue(void) const { return(m_close); }
double HigthValue(void) const { return(m_high); }
double LowValue(void) const { return(m_low); }
uint CandleColorIncrement(void) const { return(m_clr_inc); }
uint CandleColorDecrement(void) const { return(m_clr_dec); }
int CandleWidth(void) const { return(m_width); }
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CCandle::CCandle(const double open,const double close,const double high,const double low,
const int width,const uint clr_inc=0x000000,const uint clr_dec=0xF5F5F5):
m_open(open),m_close(close),m_high(high),m_low(low),
m_clr_inc(clr_inc),m_clr_dec(clr_dec),m_width(width)
{
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CCandle::~CCandle(void)
{
}
//+------------------------------------------------------------------+
//| Custom method for plot candles |
//+------------------------------------------------------------------+
void PlotCandles(double &x[],double &y[],int size,CGraphic *graphic,CCanvas *canvas,void *cbdata)
{
//--- check obj
CArrayObj *candles=dynamic_cast<CArrayObj*>(cbdata);
if(candles==NULL || candles.Total()!=size)
return;
//--- plot candles
for(int i=0; i<size; i++)
{
CCandle *candle=dynamic_cast<CCandle*>(candles.At(i));
if(candle==NULL)
return;
//--- 일차 계산
int xc=graphic.ScaleX(x[i]);
int width_2=candle.CandleWidth()/2;
int open=graphic.ScaleY(candle.OpenValue());
int close=graphic.ScaleY(candle.CloseValue());
int high=graphic.ScaleY(candle.HigthValue());
int low=graphic.ScaleY(candle.LowValue());
uint clr=(open<=close) ? candle.CandleColorIncrement() : candle.CandleColorDecrement();
//--- 캔들 플롯
canvas.LineVertical(xc,high,low,0x000000);
//--- 캔들 본체 플롯
canvas.FillRectangle(xc+width_2,open,xc-width_2,close,clr);
canvas.Rectangle(xc+width_2,open,xc-width_2,close,0x000000);
}
}
//+------------------------------------------------------------------+
//| 스크립트 프로그램 시작 함수 |
//+------------------------------------------------------------------+
void OnStart()
{
int count=10;
int width=10;
double x[];
double y[];
ArrayResize(x,count);
ArrayResize(y,count);
CArrayObj candles();
double max=0;
double min=0;
//--- 값 생성
for(int i=0; i<count; i++)
{
x[i] = i;
y[i] = i;
//--- 값 계산
double open=MathRound(50.0+(MathRand()/32767.0)*50.0);
double close=MathRound(50.0+(MathRand()/32767.0)*50.0);
double high=MathRound(MathMax(open,close)+(MathRand()/32767.0)*10.0);
double low=MathRound(MathMin(open,close) -(MathRand()/32767.0)*10.0);
//--- 최대 및 최소 찾기
if(i==0 || max<high)
max=high;
if(i==0 || min>low)
min=low;
//--- 캔들 생성
CCandle *candle=new CCandle(open,close,high,low,width);
candles.Add(candle);
}
//--- 그래픽 생성
CGraphic graphic;
if(!graphic.Create(0,"CandleGraphic",0,30,30,780,380))
{
graphic.Attach(0,"CandleGraphic");
}
//--- 곡선 생성
CCurve *curve=graphic.CurveAdd(x,y,CURVE_CUSTOM,"Candles");
//--- 곡선 속성 설정
curve.CustomPlotFunction(PlotCandles);
curve.CustomPlotCBData(GetPointer(candles));
//--- 그래픽 속성 설정
graphic.YAxis().Max((int)max);
graphic.YAxis().Min((int)min);
//--- 플롯
graphic.CurvePlotAll();
graphic.Update();
}
|