Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 957

 
Igor Kryuchkov:

Ctrl+B --> "All" button - there's one object, which is what I'm talking about. The problem is that if I create a RECTANGLE the normal way without Canvas. It creates the right number ofRECTANGLE objects.

With Canvas, I can only create one. Why is it like that? I make a unique name for eachRECTANGLE.


Thank you for the programmer, of course.

Why do you think your name is unique? Have you checked it before creating a new object? And do you check the result of object creation? What is the error code for this?

 
Artyom Trishkin:

No fully reproducible code - no concrete discussion. I don't know what you've done there. You've made some kind of problem out of the simplest thing.

#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#include <Canvas\Canvas.mqh>
CCanvas C;

int History = 5000;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
        
//---
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
{


}  
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---




   

           int BarsCounted = IndicatorCounted();
           if (BarsCounted < 0) return (-1);
           if (BarsCounted > 0) BarsCounted--;
           int BarsTotal = Bars - BarsCounted;
           if( BarsTotal > History ) BarsTotal = History;
   

           for (int i = BarsTotal-1; i >=1 ; i--)
           { 
           
         
         
               double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, i);
               
         
               
               if(rsi > 70 ) {
         
                  C.CreateBitmap(0, 0, i, Time[i+5], Close[i+1], 50, 70,COLOR_FORMAT_ARGB_NORMALIZE);
                  C.Rectangle(10, 40, 70, 80, ColorToARGB(clrRed,200));
                  C.Update(true); 
                  
                  Print(i);
                  
               } 
           
               
           
         
           
           }
  

  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
Igor Kryuchkov:

What do you mean by restarts?

I have a calculation of order openings using global variables. For example, if it is 5, it means that five orders have already been opened and I am waiting for the sixth order to go up. If the counter is set to 5, it means that 5 extra orders are opened, although they are already open. The same happens when I close the terminal and open it again. How to fix it?

 
Rustam Bikbulatov:

I have a calculation of order openings using global variables. For example, if it is 5, it means that five orders have already been opened and I am waiting for the sixth order to go up. If the counter is set to 5, it means that 5 extra orders are opened, although they are already open. The same happens when I close the terminal and open it again. How to fix it?

In the int OnInit() function, we have to check the existence of a global variable. If it exists and is higher than zero, we set it to zero.

 
Igor Kryuchkov:
Is that a four?
 
Artyom Trishkin:
Is it a quadruple?

Yes. I noticed that MQL4 when I asked about Canvas

 
Artyom Trishkin:
Is this a four?
On a four it should work too, I suppose.
 
Igor Kryuchkov:
It should work on a four, too, I suppose.
//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|              Copyright 2019, Artem A. Trishkin, Skype artmedia70 |
//|                         https://www.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Artem A. Trishkin, Skype artmedia70"
#property link      "https://www.mql5.com/ru/users/artmedia70"
#property version   "1.00"
#property strict
#property indicator_chart_window
#include <Arrays\ArrayObj.mqh>
#include <Canvas\Canvas.mqh>
class CCanvObj : public CObject
  {
public:
   CCanvas           m_canvas;
  };
int History = 5000;
string prefix=MQLInfoString(MQL_PROGRAM_NAME)+"_";
CArrayObj list_canvas;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom deindicator initialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- indicator buffers mapping
   ObjectsDeleteAll(0,prefix);
//---
   return;
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- Проверка и расчёт количества просчитываемых баров
   int limit=rates_total-prev_calculated;
   if(limit>1)
     {
      limit=rates_total-1;
     }
//--- Цикл расчёта индикатора
   for(int i=fmin(limit,History); i>=0; i--)
     {
      double rsi=iRSI(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,i);
      if(rsi>70)
        {
         //--- Создаём новый объект CCanvas
         CCanvObj *obj=new CCanvObj();
         if(obj==NULL)
            continue;
         if(!list_canvas.Add(obj))
            continue;
         string name=prefix+(string)i;
         //--- Создаём ресурс Bitmap с именем name
         if(obj.m_canvas.CreateBitmap(0,0,name,time[i+5],close[i+1],50,70,COLOR_FORMAT_ARGB_NORMALIZE))
           {
            obj.m_canvas.Rectangle(0,0,49,69,ColorToARGB(clrRed,200));
            obj.m_canvas.Update(true); 
           }
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Artyom Trishkin:

Thank you, it all works.

When scaling the graph can the rectangle be made smaller too?


The scale is tracked. But the indicator only understands it with a new tick,

Sometimes there is a huge delay. What can I do about it?

 
Igor Kryuchkov:

Thank you, it all works.

When scaling the graph can the rectangle be made smaller too?


The scale is tracked. But the indicator only understands it with a new tick,

Sometimes there is a huge delay. What can you do about it?

  1. You wanted to avoid resizing at scaling ...
  2. OnChartEvent() is not tick-dependent
  3. Update all objects created in OnChartEvent(), when the necessary chart event is registered. Otherwise, only on a new tick will be updated.