Interval change closes expert and chart

 

Hello guys,

I have a problem that I can't find solution for. I've created an expert advisor ane everything works fine as I wanted but strange thing happens when I change the chart interval.

When I change interval for the first time after EA was added to the chart it works fine - EA is uninitialized and initialized again on the refreshed chart with selected interval. But when I change the interval for the second time everything dissapears - EA and the whole chart. 

Here is the strategy log:

0 19:00:41.876 Expert TR Pad 2.0 EURUSD,H1: loaded successfully

0 19:00:43.512 TR Pad 2.0 EURUSD,H1 inputs: inpLot=1.0; inpStopLoss=1500.0; inpTakeProfit=4500.0; inpSlippage=0; inpMaxNumberOfOrders=2; inpBreakEvenOffset=100; inpLimit=10; 

0 19:00:43.690 TR Pad 2.0 EURUSD,H1: initialized

0 19:00:50.040 TR Pad 2.0 EURUSD,H1: uninit reason 3

0 19:00:50.116 TR Pad 2.0 EURUSD,M15: initialized

0 19:00:55.005 TR Pad 2.0 EURUSD,M15: uninit reason 3

3 19:00:55.086 TR Pad 2.0 EURUSD,M5: initialization failed (1)

0 19:00:55.086 TR Pad 2.0 EURUSD,M5: uninit reason 8

0 19:00:55.096 Expert TR Pad 2.0 EURUSD,M5: removed


Could you guys help me with that? I have no idea why is it happening like that...


Thanks in advance!

 
The problem is in your code. How are you expecting help when you didn't post it ?
 
Hoped you could suggest some possible reasons. Anyway please find attached file.
Files:
 
Adam Wróblewski:

Hello guys,

I have a problem that I can't find solution for. I've created an expert advisor ane everything works fine as I wanted but strange thing happens when I change the chart interval.

When I change interval for the first time after EA was added to the chart it works fine - EA is uninitialized and initialized again on the refreshed chart with selected interval. But when I change the interval for the second time everything dissapears - EA and the whole chart. 

Here is the strategy log:

0 19:00:41.876 Expert TR Pad 2.0 EURUSD,H1: loaded successfully

0 19:00:43.512 TR Pad 2.0 EURUSD,H1 inputs: inpLot=1.0; inpStopLoss=1500.0; inpTakeProfit=4500.0; inpSlippage=0; inpMaxNumberOfOrders=2; inpBreakEvenOffset=100; inpLimit=10; 

0 19:00:43.690 TR Pad 2.0 EURUSD,H1: initialized

0 19:00:50.040 TR Pad 2.0 EURUSD,H1: uninit reason 3

0 19:00:50.116 TR Pad 2.0 EURUSD,M15: initialized

0 19:00:55.005 TR Pad 2.0 EURUSD,M15: uninit reason 3

3 19:00:55.086 TR Pad 2.0 EURUSD,M5: initialization failed (1)

0 19:00:55.086 TR Pad 2.0 EURUSD,M5: uninit reason 8

0 19:00:55.096 Expert TR Pad 2.0 EURUSD,M5: removed


Could you guys help me with that? I have no idea why is it happening like that...


Thanks in advance!

if (!Panel.Create(0, s_panel_name, 0, PANEL_INIT_X, PANEL_INIT_Y, PANEL_WIDTH, PANEL_HEIGHT))
    return (INIT_FAILED);

The problem is here.

The Panel.Create method returns false or/and somehow closes the chart.

REASON_REMOVE

1

Program has been deleted from the chart

 
Nelson Wanyama: The problem is here.

REASON_REMOVE

1

Program has been deleted from the chart

The program is not removing itself. The message said that OnInit returned 1, therefor the reason code is 8

Adam Wróblewski:

3 19:00:55.086 TR Pad 2.0 EURUSD,M5: initialization failed (1)

0 19:00:55.086 TR Pad 2.0 EURUSD,M5: uninit reason 8

REASON_INITFAILED 8 This value means that OnInit() handler has returned a nonzero value
 

I know that the problem is in OnInit() function but I have no idea why. TRPad is just a class based on CAppDialog and Create method works as it should when you start the EA. I also works fine when you change interval once but why there is a problem when you change the interval for the second time? And why it closes not only the EA (since there is some init error) but also the whole chart?

Here is the Create method:

bool TRPad::Create(const long chart, const string name, const int subwin, const int x1, const int y1, const int x2, const int y2)
{
   if (!CAppDialog::Create(chart, name, subwin, x1, y1, x2, y2))
      return false;
   if (!CreateButton(BuyButton, 1))
      return false;
   if (!CreateButton(SellButton, 2))
      return false;
   if (!CreateButton(CloseAllButton, 3))
      return false;
   if (!CreateButton(BreakEvenButton, 4))
      return false;
   if (!CreateButton(MoveSLButton, 5))
      return false;
   if (!CreateLabel(PipsLabel, 1))
      return false;
   if (!CreateLabel(ProfitLabel, 2))
      return false;
   if (!CreateLabel(SpreadLabel, 3))
      return false;
   if (!CreateLabel(SwapLabel, 4))
      return false;
   if (!CreateLabel(ExpTimeLabel, 5))
      return false;
   if (!CreateLabel(PipsValue, 1, true))
      return false;
   if (!CreateLabel(ProfitValue, 2, true))
      return false;
   if (!CreateLabel(SpreadValue, 3, true))
      return false;
   if (!CreateLabel(SwapValue, 4, true))
      return false;
   if (!CreateLabel(ExpTimeValue, 5, true))
      return false;
   return true;
}

bool TRPad::CreateButton(CButton &button, int index)
{
   int x1, y1, x2, y2 = 0;
   string name, text;
   color clr = clrGray;

   switch (index)
   {
   case 1:
      x1 = MARGIN_LEFT;
      y1 = MARGIN_TOP;
      x2 = x1 + BUTTON_WIDTH;
      y2 = y1 + BUTTON_HEIGHT;
      name = s_buybutton_name;
      text = s_buybutton_text;
      clr = clrGreen;
      break;
   case 2:
      x1 = MARGIN_LEFT + GAP + BUTTON_WIDTH;
      y1 = MARGIN_TOP;
      x2 = x1 + BUTTON_WIDTH;
      y2 = y1 + BUTTON_HEIGHT;
      name = s_sellbutton_name;
      text = s_sellbutton_text;
      clr = clrRed;
      break;
   case 3:
      x1 = MARGIN_LEFT;
      y1 = MARGIN_TOP + BUTTON_HEIGHT + GAP;
      x2 = x1 + GAP + 2 * BUTTON_WIDTH;
      y2 = y1 + BUTTON_HEIGHT;
      name = s_closeallbutton_name;
      text = s_closeallbutton_text;
      clr = clrDeepSkyBlue;
      break;
   case 4:
      x1 = MARGIN_LEFT;
      y1 = MARGIN_TOP + 2 * BUTTON_HEIGHT + 2 * GAP;
      x2 = x1 + BUTTON_WIDTH;
      y2 = y1 + BUTTON_HEIGHT;
      name = s_breakevenbutton_name;
      text = s_breakevenbutton_text;
      clr = clrLimeGreen;
      break;
   case 5:
      x1 = MARGIN_LEFT + GAP + BUTTON_WIDTH;
      y1 = MARGIN_TOP + 2 * BUTTON_HEIGHT + 2 * GAP;
      x2 = x1 + BUTTON_WIDTH;
      y2 = y1 + BUTTON_HEIGHT;
      name = s_moveslbutton_name;
      text = s_moveslbutton_text;
      clr = clrDarkOrange;
      break;
   default:
      x1 = MARGIN_LEFT;
      y1 = MARGIN_TOP;
      x2 = x1 + BUTTON_WIDTH;
      y2 = y1 + BUTTON_HEIGHT;
      clr = clrGreen;
      break;
   }

   if (!button.Create(0, name, 0, x1, y1, x2, y2))
      return false;
   if (!button.Text(text))
      return false;
   if (!button.ColorBackground(clr))
      return false;
   if (!button.Color(clrWhiteSmoke))
      return false;
   if (!Add(button))
      return false;

   return true;
}

bool TRPad::CreateLabel(CLabel &label, int index, bool value = false)
{
   int x1, y1;
   string name, text;
   x1 = value ? (MARGIN_LEFT + BUTTON_WIDTH + GAP) : MARGIN_LEFT;

   switch (index)
   {
   case 1:
      y1 = MARGIN_TOP + 3 * GAP + 3 * BUTTON_HEIGHT;
      name = value ? s_pipsvalue_name : s_pipslabel_name;
      text = value ? s_defaultvalue_text : s_pipslabel_text;
      break;
   case 2:
      y1 = MARGIN_TOP + 4 * GAP + 3 * BUTTON_HEIGHT + LABEL_HEIGHT;
      name = value ? s_profitvalue_name : s_profitlabel_name;
      text = value ? s_defaultvalue_text : s_profitlabel_text;
      break;
   case 3:
      y1 = MARGIN_TOP + 5 * GAP + 3 * BUTTON_HEIGHT + 2 * LABEL_HEIGHT;
      name = value ? s_spreadvalue_name : s_spreadlabel_name;
      text = value ? s_defaultvalue_text : s_spreadlabel_text;
      break;
   case 4:
      y1 = MARGIN_TOP + 6 * GAP + 3 * BUTTON_HEIGHT + 3 * LABEL_HEIGHT;
      name = value ? s_swapvalue_name : s_swaplabel_name;
      text = value ? s_defaultvalue_text : s_swaplabel_text;
      break;
   case 5:
      y1 = MARGIN_TOP + 7 * GAP + 3 * BUTTON_HEIGHT + 4 * LABEL_HEIGHT;
      name = value ? s_timevalue_name : s_timelabel_name;
      text = value ? s_defaultvalue_text : s_timelabel_text;
      break;
   default:
      y1 = 0;
      break;
   }

   if (!label.Create(0, name, 0, x1, y1, 0, 0))
      return false;
   if (!label.Text(text))
      return false;
   if (!label.FontSize(10))
      return false;
   if (!Add(label))
      return false;

   return true;
}
 

Guys,

I found solution - there is a bug in MQL4 library for CAppDialog. Highlighted line is missing. When added, everything works fine.

bool CAppDialog::CreateCommon(const long chart,const string name,const int subwin)
  {
//--- save parameters
   m_chart_id    =chart;
   m_name        =name;
   m_subwin      =subwin;
   m_program_name=name;
   m_deinit_reason=WRONG_VALUE;
//--- get unique ID
   m_instance_id=CreateInstanceId();
//--- initialize chart object
   m_chart.Attach(chart);
//--- determine type of program
   m_program_type=(ENUM_PROGRAM_TYPE)MQLInfoInteger(MQL_PROGRAM_TYPE);
//--- specify object and mouse events
   if(!m_chart.EventObjectCreate() || !m_chart.EventObjectDelete() || !m_chart.EventMouseMove())
     {
      Print("CAppDialog: object events specify error");
      m_chart.Detach();
      return(false);
     }
//--- get subwindow offset
   SubwinOff();
//--- succeed
   return(true);
  }

Thank you for all your replys. Problem is solved ;)

Reason: