//+------------------------------------------------------------------+ //| ChartSample.mq5 | //| Copyright 2000-2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2000-2025, MetaQuotes Ltd." #property link "https://www.mql5.com" //--- #include #include #include //--- #include "ChartSampleInit.mqh" //+------------------------------------------------------------------+ //| Script to demonstrate the use of class CChart. | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Chart Sample script class | //+------------------------------------------------------------------+ class CChartSample { protected: CChart m_chart; // instance of the class to access properties chart CChartObjectButton *m_button[NUM_BUTTONS]; // array of pointers other buttons CChartObjectButton *m_button_tf[20]; // array of pointers period buttons CChartObjectButton *m_button_sym[]; // array of pointers symbol buttons int m_num_symbols; // number of symbol CChartObjectEdit *m_edit[NUM_EDITS]; // array of pointers other edits CChartObjectEdit *m_edit_color[13]; // array of pointers to colors show CChartObjectEdit *m_edit_rgb[13][3]; // array of pointers to RGB show CChartObjectLabel *m_label[NUM_LABELS]; // array of pointers to labels CChartObjectPanel m_panel[NUM_PANELS]; // array of panels public: CChartSample(void); ~CChartSample(void); //--- initialization bool Init(void); void Deinit(void); //--- processing void Processing(void); private: void CheckPanelModes(void); void CheckPanelAnothers(void); void CheckPanelScales(void); void CheckPanelShows(void); void CheckPanelTimeframes(void); void CheckPanelSymbols(void); void CheckPanelColors(void); void CheckPanelReadOnly(void); }; //--- CChartSample ExtScript; //+------------------------------------------------------------------+ //| Constructor. | //+------------------------------------------------------------------+ CChartSample::CChartSample(void) : m_num_symbols(0) { } //+------------------------------------------------------------------+ //| Destructor. | //+------------------------------------------------------------------+ CChartSample::~CChartSample(void) { //--- does not perform any action //--- all dynamic objects created in the method Init(), //--- will be deleted when deleting panels, //--- to which they were added } //+------------------------------------------------------------------+ //| Method CheckPanelModes. | //+------------------------------------------------------------------+ void CChartSample::CheckPanelModes(void) { if(m_button[0].State() && m_chart.Mode()!=CHART_BARS) { //--- Set Bars Mode m_button[1].State(false); m_button[2].State(false); m_chart.Mode(CHART_BARS); } if(m_button[1].State() && m_chart.Mode()!=CHART_CANDLES) { //--- Set Candles Mode m_button[0].State(false); m_button[2].State(false); m_chart.Mode(CHART_CANDLES); } if(m_button[2].State() && m_chart.Mode()!=CHART_LINE) { //--- Set Line Mode m_button[0].State(false); m_button[1].State(false); m_chart.Mode(CHART_LINE); } } //+------------------------------------------------------------------+ //| Method CheckPanelAnothers. | //+------------------------------------------------------------------+ void CChartSample::CheckPanelAnothers(void) { int i,j; //--- Set Autoscroll if(m_button[3].State()) { if(!m_chart.AutoScroll()) m_chart.AutoScroll(true); } else { if(m_chart.AutoScroll()) m_chart.AutoScroll(false); } //--- Set Shift if(m_button[4].State()) { if(m_edit[0].Description()=="") m_edit[0].Description(DoubleToString(m_chart.ShiftSize())); else { i=(int)StringToInteger(m_edit[0].Description()); j=i; if(i>50) i=50; if(i<10) i=10; if(j!=i) m_edit[0].Description(IntegerToString(i)); if(i!=m_chart.ShiftSize()) m_chart.ShiftSize(i); } if(!m_chart.Shift()) m_chart.Shift(true); } else { m_edit[0].Description(""); if(m_chart.Shift()) m_chart.Shift(false); } //--- Set Shift Size if(m_button[6].State()) { if(m_button[4].State()) { //--- Set Shift Size Up i=(int)StringToInteger(m_edit[0].Description()); if(i<50) m_chart.ShiftSize(++i); m_edit[0].Description(IntegerToString(i)); } m_button[6].State(false); } if(m_button[7].State()) { if(m_button[4].State()) { //--- Set Shift Size Down i=(int)StringToInteger(m_edit[0].Description()); if(i>10) m_chart.ShiftSize(--i); m_edit[0].Description(IntegerToString(i)); } m_button[7].State(false); } m_edit[2].Description(DoubleToString(m_chart.ShiftSize())); //--- Set Foreground if(m_button[5].State()) { if(!m_chart.Foreground()) m_chart.Foreground(true); } else { if(m_chart.Foreground()) m_chart.Foreground(false); } } //+------------------------------------------------------------------+ //| Method CheckPanelScales. | //+------------------------------------------------------------------+ void CChartSample::CheckPanelScales(void) { int i; double d; //--- Set Scale fix if(m_button[8].State()) { if(m_edit[4].Description()=="") { m_edit[4].Description(DoubleToString(m_chart.PriceMax(0),4)); m_edit[5].Description(DoubleToString(m_chart.PriceMin(0),4)); } if(!m_chart.ScaleFix()) m_chart.ScaleFix(true); } else { if(m_edit[4].Description()!="") { m_edit[4].Description(""); m_edit[5].Description(""); } if(m_chart.ScaleFix()) m_chart.ScaleFix(false); } //--- Set Scale fix 1 to 1 if(m_button[9].State()) { if(!m_chart.ScaleFix_11()) m_chart.ScaleFix_11(true); } else { if(m_chart.ScaleFix_11()) m_chart.ScaleFix_11(false); } //--- Set Scale if(m_button[10].State()) { //--- Set Scale Up i=(int)StringToInteger(m_edit[1].Description()); if(i<5) { i++; m_chart.Scale(i); m_edit[1].Description(IntegerToString(i)); } m_button[10].State(false); } if(m_button[11].State()) { //--- Set Scale Down i=(int)StringToInteger(m_edit[1].Description()); if(i>0) { i--; m_chart.Scale(i); m_edit[1].Description(IntegerToString(i)); } m_button[11].State(false); } m_edit[3].Description(IntegerToString(m_chart.Scale())); //--- Set Fixed Max if(m_button[12].State()) { if(m_chart.ScaleFix()) { d=StringToDouble(m_edit[4].Description()); if(m_chart.FixedMax()!=d) m_chart.FixedMax(d); m_edit[4].Description(DoubleToString(d,4)); } else m_edit[4].Description(""); m_button[12].State(false); } //--- Set Fixed Min if(m_button[13].State()) { if(m_chart.ScaleFix()) { d=StringToDouble(m_edit[5].Description()); if(m_chart.FixedMin()!=d) m_chart.FixedMin(d); m_edit[5].Description(DoubleToString(d,4)); } else m_edit[5].Description(""); m_button[13].State(false); } //--- Set Scale PPB if(m_button[14].State()) { if(m_edit[6].Description()=="") { d=m_chart.PointsPerBar(); if(d==0.0) { d=1.0; m_chart.PointsPerBar(d); } m_edit[6].Description(DoubleToString(d,4)); } if(!m_chart.ScalePPB()) m_chart.ScalePPB(true); d=StringToDouble(m_edit[6].Description()); if(m_chart.PointsPerBar()!=d) m_chart.PointsPerBar(d); } else { m_edit[6].Description(""); if(m_chart.ScalePPB()) m_chart.ScalePPB(false); } } //+------------------------------------------------------------------+ //| Method CheckPanelShows. | //+------------------------------------------------------------------+ void CChartSample::CheckPanelShows(void) { //--- Set Show OHLC if(m_button[15].State()) { if(!m_chart.ShowOHLC()) m_chart.ShowOHLC(true); } else { if(m_chart.ShowOHLC()) m_chart.ShowOHLC(false); } //--- Set Show Bid if(m_button[16].State()) { if(!m_chart.ShowLineBid()) m_chart.ShowLineBid(true); } else { if(m_chart.ShowLineBid()) m_chart.ShowLineBid(false); } //--- Set Show Ask if(m_button[17].State()) { if(!m_chart.ShowLineAsk()) m_chart.ShowLineAsk(true); } else { if(m_chart.ShowLineAsk()) m_chart.ShowLineAsk(false); } //--- Set Show Last if(m_button[18].State()) { if(!m_chart.ShowLastLine()) m_chart.ShowLastLine(true); } else { if(m_chart.ShowLastLine()) m_chart.ShowLastLine(false); } //--- Set Show Separator if(m_button[19].State()) { if(!m_chart.ShowPeriodSep()) m_chart.ShowPeriodSep(true); } else { if(m_chart.ShowPeriodSep()) m_chart.ShowPeriodSep(false); } //--- Set Show Grid if(m_button[20].State()) { if(!m_chart.ShowGrid()) m_chart.ShowGrid(true); } else { if(m_chart.ShowGrid()) m_chart.ShowGrid(false); } //--- Set Show Objects Descriptor if(m_button[21].State()) { if(!m_chart.ShowObjectDescr()) m_chart.ShowObjectDescr(true); } else { if(m_chart.ShowObjectDescr()) m_chart.ShowObjectDescr(false); } //--- Set Show Not Volumes if(m_button[22].State()) { m_chart.ShowVolumes((ENUM_CHART_VOLUME_MODE)0); m_button[22].State(false); } //--- Set Show Tick Volumes if(m_button[23].State()) { m_chart.ShowVolumes((ENUM_CHART_VOLUME_MODE)1); m_button[23].State(false); } //--- Set Show Real Volumes if(m_button[24].State()) { m_chart.ShowVolumes((ENUM_CHART_VOLUME_MODE)2); m_button[24].State(false); } } //+------------------------------------------------------------------+ //| Method CheckPanelTimeframes. | //+------------------------------------------------------------------+ void CChartSample::CheckPanelTimeframes(void) { int i,j; //--- No Set Period PERIOD_MN if(m_button_tf[19].State()) m_button_tf[19].State(false); //--- Set Period for(i=0;i<20;i++) if(m_button_tf[i].State()) { if(m_chart.Period()!=tf_int[i]) m_chart.SetSymbolPeriod(m_chart.Symbol(),(ENUM_TIMEFRAMES)tf_int[i]); else continue; for(j=0;j<20;j++) if(i!=j) m_button_tf[j].State(false); } } //+------------------------------------------------------------------+ //| Method CheckPanelSymbols. | //+------------------------------------------------------------------+ void CChartSample::CheckPanelSymbols(void) { int i,j; //--- Set Symbol for(i=0;i>16)); m_edit_rgb[i][1].Description((string)((c&0xFF00)>>8)); m_edit_rgb[i][2].Description((string)(c&0xFF)); } } //+------------------------------------------------------------------+ //| Method CheckPanelReadOnly. | //+------------------------------------------------------------------+ void CChartSample::CheckPanelReadOnly(void) { int i,j; //--- Get VisibleBars m_edit[7].Description((string)m_chart.VisibleBars()); //--- Get WindowsTotal m_edit[8].Description((string)(j=m_chart.WindowsTotal())); j%=6; //--- Get WindowIsVisible[i] for(i=0;i=7) m_label[i].FontSize(8); if(l_pan[i]16) x+=28; if(i>17) x+=28; if((m_button_tf[i]=new CChartObjectButton)==NULL) return(false); m_button_tf[i].Create(m_chart.ChartId(),"ButtonTF"+IntegerToString(i),0,20+x,16*(i/11),sx,16); if(m_chart.Period()==tf_int[i]) m_button_tf[i].State(true); m_button_tf[i].Description(tf_str[i]); m_button_tf[i].Color(clrBlue); m_button_tf[i].FontSize(8); m_panel[4].Attach(m_button_tf[i]); } //--- creation m_button_sym[] ArrayResize(m_button_sym,m_num_symbols); for(i=0;i