Questions from Beginners MQL5 MT5 MetaTrader 5 - page 142

 
forexman77:

Still, I will need the variable "st" not only in Alert, but also for calculations, and when compiling the warning "possible data loss due to type conversion" comes out.

I assume this is because it turns out to be a 10-digit number, while the maximum value in int is 2 147 483 647, I currently have 1 380 758 400. Maybe it makes sense to use long, and probably the Expert Advisor will consume a lot of resources in this part?

Don't you understand the principle of type conversion? It's that simple:

int st=(int)tp[0];
 

I want to draw on canvas. To have a picture on canvas redrawn in OnInit, OnTimer, OnTick and in onChartEvent. I declare CCanvas canvas in global variables and call canvas.methods everywhere I want. But the compiler complains about unresolved static variable 'CCanvas::m_default_colors' (what does that have to do with colors?)

the code is like this

#include <Canvas\Canvas.mqh>

CCanvas canvas;

int OnInit()
{
//CCanvas canvas; if I declare it here, it draws but then how do I redraw it in OnTick?

canvas.CreateBitmapLabel("ProbeGraph", 0, 0, width ,heigth, COLOR_FORMAT_ARGB_RAW);

canvas.Circle(Xcentr,Ycentr,Rmin,ColorToARGB(clrBlue, 115)); canvas.Update();

}

void OnTick()
{
canvas.Circle(Xcentr,Ycentr,Rmin,ColorToARGB(clrBlue, 115)); canvas.Update();
}

How do I negotiate with the compiler? Is there an example with canvas in expert?
Документация по MQL5: Основы языка / Переменные / Глобальные переменные
Документация по MQL5: Основы языка / Переменные / Глобальные переменные
  • www.mql5.com
Основы языка / Переменные / Глобальные переменные - Документация по MQL5
 
Virty:

I want to draw on canvas in Expert Advisor. I want the image on canvas to be redrawn in OnInit, OnTimer, OnTick and in onChartEvent. I declare CCanvas canvas in global variables and call canvas.methods everywhere I want. But the compiler complains about unresolved global declaration

...

How do you negotiate with the compiler? Is there an example with canvas in EA?

Well you are not a beginner! Publish the code correctly!

 
DC2008:

Well you're not a beginner! Publish the code correctly!

The code is right here. But it's a lot worse to dig into. And it doesn't work either. Error unresolved static variable 'CCanvas::m_default_colors' ProbeGraph.mq5 21 9

And a heap of warrants about overlapping heigth and width variables. As far as I understand, the compiler doesn't want to call CCanvas class constructor in global variables, but wants to call the constructor at OnInit. And then how to refer to an instance of the class outside of OnInit, I do not understand.

Also. Before, the body of the Draw function was inside onInit, but the Draw itself wasn't. Everything worked, but inside OnInit. I've already started to twist here, trying to pass a reference to an instance of the class into the Draw function, because global variables don't work.

In general, the code is being written, the problem with creating a global instance of the CCanvas class.


//+------------------------------------------------------------------+
//|                                                   ProbeGraph.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

#include <Canvas\Canvas.mqh>
int heigth=500;
int width=500;
int Xcentr=round(width/2);
int Ycentr=round(width/2);
int Rmin=25;
int Rmax=round(heigth/2-1);


#define  ArrayS 100000 
double arrayopen[ArrayS];
CCanvas canvas;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//CCanvas canvas;
   canvas.CreateBitmapLabel("ProbeGraph",0,0,width,heigth,COLOR_FORMAT_ARGB_RAW);

//Draw(canvas);


//--- create timer
   EventSetTimer(60);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
//canvas.Update();
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---

  }
//+------------------------------------------------------------------+


int ChartVisibleBars(const long chart_ID=0)
  {
//--- подготовим переменную для получения значения свойства
   long result=-1;
//--- сбросим значение ошибки
   ResetLastError();
//--- получим значение свойства
   if(!ChartGetInteger(chart_ID,CHART_VISIBLE_BARS,0,result))
     {
      //--- выведем сообщение об ошибке в журнал "Эксперты"
      Print(__FUNCTION__+", Error Code = ",GetLastError());
     }
//--- вернем значение свойства графика
   return((int)result);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw(CCanvas *canvas)
  {
   ArrayInitialize(arrayopen,0);
   double r;
   double A, Amin=0, Amax=2*3.1415;
   int x,y, xprev=0, yprev=0;

//canvas.Erase(ColorToARGB(clrRed, 115));
   canvas.Circle(Xcentr,Ycentr,Rmin,ColorToARGB(clrBlue, 115));
   canvas.Circle(Xcentr,Ycentr,Rmax,ColorToARGB(clrBlue, 115));

   int Nbars=CopyOpen(Symbol(),Period(),TimeCurrent(),ChartVisibleBars(0),arrayopen); Print("Nbars=",Nbars);
   double KursMin=1000000,KursMax=0;
   for(int i=0; i<Nbars; i++)
     {
      if(KursMin>arrayopen[i]) KursMin=arrayopen[i];
      if(KursMax<arrayopen[i]) KursMax=arrayopen[i];
     };
   Print("KursMin=",KursMin); Print("KurMax=",KursMax);

   for(int i=0; i<Nbars; i++)
     {
      if((KursMax-KursMin)>0) r=Rmin+(Rmax-Rmin)*(arrayopen[i]-KursMin)/(KursMax-KursMin);
      if(Nbars>0) A=Amin+(Amax-Amin)*i/(Nbars);
      x=round(Xcentr-r*MathSin(A));
      y=round(Ycentr+r*MathCos(A));
      canvas.PixelSetAA(x,y,ColorToARGB(clrGreen,155));
      if(xprev>0 && yprev>0) canvas.Line(xprev,yprev,x,y,ColorToARGB(clrBlue,155));
      xprev=x; yprev=y;

     };

   canvas.Update();

  }
//+------------------------------------------------------------------+
Документация по MQL5: Основы языка / Типы данных / Структуры и классы
Документация по MQL5: Основы языка / Типы данных / Структуры и классы
  • www.mql5.com
Основы языка / Типы данных / Структуры и классы - Документация по MQL5
 

One thing I can't understand: if the compiler swears, why aren't the errors corrected? А?

 
DC2008:

One thing I can't understand: if the compiler swears, why aren't the errors corrected? А?

I don't know how to fix it. That's why I'm asking.
 
//+------------------------------------------------------------------+
//|                                                   ProbeGraph.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

#include <Canvas\Canvas.mqh>
int heigth=500;
int width_1=500;
int Xcentr=int(round(width_1/2));
int Ycentr=int(round(width_1/2));
int Rmin=25;
int Rmax=int(round(heigth/2-1));


#define  ArrayS 100000 
double arrayopen[ArrayS];
CCanvas *canvas;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//CCanvas canvas;
   canvas=new CCanvas;
//--- проверка указателя перед его использованием
   if(CheckPointer(canvas)==POINTER_INVALID)
     {
      Print(__FUNCTION__," переменная 'canvas' не инициализирована!");
      return(INIT_SUCCEEDED);
     }
   canvas.CreateBitmapLabel("ProbeGraph",0,0,width_1,heigth,COLOR_FORMAT_ARGB_RAW);

   Draw();

//--- create timer
   EventSetTimer(60);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
//canvas.Update();
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---

  }
//+------------------------------------------------------------------+

int ChartVisibleBars(const long chart_ID=0)
  {
//--- подготовим переменную для получения значения свойства
   long result=-1;
//--- сбросим значение ошибки
   ResetLastError();
//--- получим значение свойства
   if(!ChartGetInteger(chart_ID,CHART_VISIBLE_BARS,0,result))
     {
      //--- выведем сообщение об ошибке в журнал "Эксперты"
      Print(__FUNCTION__+", Error Code = ",GetLastError());
     }
//--- вернем значение свойства графика
   return((int)result);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Draw()
  {
   ArrayInitialize(arrayopen,0);
   double r=0;
   double A=0, Amin=0, Amax=2*3.1415;
   int x,y,xprev=0,yprev=0;

//canvas.Erase(ColorToARGB(clrRed, 115));
   canvas.Circle(Xcentr,Ycentr,Rmin,ColorToARGB(clrBlue, 115));
   canvas.Circle(Xcentr,Ycentr,Rmax,ColorToARGB(clrBlue, 115));

   int Nbars=CopyOpen(Symbol(),Period(),TimeCurrent(),ChartVisibleBars(0),arrayopen); Print("Nbars=",Nbars);
   double KursMin=1000000,KursMax=0;
   for(int i=0; i<Nbars; i++)
     {
      if(KursMin>arrayopen[i]) KursMin=arrayopen[i];
      if(KursMax<arrayopen[i]) KursMax=arrayopen[i];
     };
   Print("KursMin=",KursMin); Print("KurMax=",KursMax);

   for(int i=0; i<Nbars; i++)
     {
      if((KursMax-KursMin)>0) r=Rmin+(Rmax-Rmin)*(arrayopen[i]-KursMin)/(KursMax-KursMin);
      if(Nbars>0) A=Amin+(Amax-Amin)*i/(Nbars);
      x=int(round(Xcentr-r*MathSin(A)));
      y=int(round(Ycentr+r*MathCos(A)));
      canvas.PixelSetAA(x,y,ColorToARGB(clrGreen,155));
      if(xprev>0 && yprev>0) canvas.Line(xprev,yprev,x,y,ColorToARGB(clrBlue,155));
      xprev=x; yprev=y;

     };

   canvas.Update();

  }
//+------------------------------------------------------------------+
 
barabashkakvn:
Copied the suggested code in full. The error is the same as the unresolved static variable 'CCanvas::m_default_colors' prob1.mq5 21 9
 
Virty:
Copied suggested code completely. The error is the same as unresolved static variable 'CCanvas::m_default_colors' prob1.mq5 21 9

I don't have any errors. The build is the latest.

ProbeGraph

 
Virty:
Copied the suggested code in full. The error is the same as the unresolved static variable 'CCanvas::m_default_colors' prob1.mq5 21 9
so the error is in your file prob1.mq5 .
Reason: