Download MetaTrader 5

Button of trend dirrection, need help

To add comments, please log in or register
Copy the deals of successful traders. Subscribe to signals!
rokas balsys
11
rokas balsys 2016.01.02 14:14 
Hello traders, I am trying to create one helpfull indicator, but I got one problem. I added 5 buttons with 5 labels, with different Timeframes, when I press any button the label changes and  writes that trend is UP, DOWT or something else. But I have a problem, when I change timeframe my all labels goes back to default, I cant find how to fix it, can anyone help me?

 
string buttonID="Button";
string buttonID2="Button2";
string buttonID3="Button3";
string buttonID4="Button4";
string buttonID5="Button5";
string labelID="Info";
string labelID2="Info2";
string labelID3="Info3";
string labelID4="Info4";
string labelID5="Info5";
int broadcastEventID=5000;
bool PrevSelected = false;
bool PrevSelected2 = false;
bool PrevSelected3 = false;
bool PrevSelected4 = false;
bool PrevSelected5 = false;
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
//void OnStart()
  {
   int height=ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0);
   
//--- Create a button to send custom events
   ObjectCreate(0,buttonID,OBJ_BUTTON,0,10,570);
   ObjectSetInteger(0,buttonID,OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,buttonID,OBJPROP_BGCOLOR,clrGray);
   ObjectSetInteger(0,buttonID,OBJPROP_XDISTANCE,5);
   ObjectSetInteger(0,buttonID,OBJPROP_YDISTANCE,height-25);
   ObjectSetInteger(0,buttonID,OBJPROP_XSIZE,30);
   ObjectSetInteger(0,buttonID,OBJPROP_YSIZE,18);
   //ObjectSetString(0,buttonID,OBJPROP_BORDER_COLOR,clrWhite);
   ObjectSetString(0,buttonID,OBJPROP_FONT,"Arial");
   ObjectSetString(0,buttonID,OBJPROP_TEXT,"MN");
   ObjectSetInteger(0,buttonID,OBJPROP_FONTSIZE,9);
   ObjectSetInteger(0,buttonID,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,buttonID,OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,buttonID,OBJPROP_BACK,true);
   ObjectSetInteger(0,buttonID,OBJPROP_CORNER,4);

   ObjectCreate(0,buttonID2,OBJ_BUTTON,0,10,570);
   ObjectSetInteger(0,buttonID2,OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,buttonID2,OBJPROP_BGCOLOR,clrGray);
   ObjectSetInteger(0,buttonID2,OBJPROP_XDISTANCE,5);
   ObjectSetInteger(0,buttonID2,OBJPROP_YDISTANCE,height-45);
   ObjectSetInteger(0,buttonID2,OBJPROP_XSIZE,30);
   ObjectSetInteger(0,buttonID2,OBJPROP_YSIZE,18);
   //ObjectSetString(0,buttonID2,OBJPROP_BORDER_COLOR,clrWhite);
   ObjectSetString(0,buttonID2,OBJPROP_FONT,"Arial");
   ObjectSetString(0,buttonID2,OBJPROP_TEXT,"W1");
   ObjectSetInteger(0,buttonID2,OBJPROP_FONTSIZE,9);
   ObjectSetInteger(0,buttonID2,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,buttonID2,OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,buttonID2,OBJPROP_BACK,true);
   ObjectSetInteger(0,buttonID2,OBJPROP_CORNER,4);
   
   ObjectCreate(0,buttonID3,OBJ_BUTTON,0,10,570);
   ObjectSetInteger(0,buttonID3,OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,buttonID3,OBJPROP_BGCOLOR,clrGray);
   ObjectSetInteger(0,buttonID3,OBJPROP_XDISTANCE,5);
   ObjectSetInteger(0,buttonID3,OBJPROP_YDISTANCE,height-65);
   ObjectSetInteger(0,buttonID3,OBJPROP_XSIZE,30);
   ObjectSetInteger(0,buttonID3,OBJPROP_YSIZE,18);
   //ObjectSetString(0,buttonID3,OBJPROP_BORDER_COLOR,clrWhite);
   ObjectSetString(0,buttonID3,OBJPROP_FONT,"Arial");
   ObjectSetString(0,buttonID3,OBJPROP_TEXT,"D1");
   ObjectSetInteger(0,buttonID3,OBJPROP_FONTSIZE,9);
   ObjectSetInteger(0,buttonID3,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,buttonID3,OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,buttonID3,OBJPROP_BACK,true);
   ObjectSetInteger(0,buttonID3,OBJPROP_CORNER,4);
   
   ObjectCreate(0,buttonID4,OBJ_BUTTON,0,10,570);
   ObjectSetInteger(0,buttonID4,OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,buttonID4,OBJPROP_BGCOLOR,clrGray);
   ObjectSetInteger(0,buttonID4,OBJPROP_XDISTANCE,5);
   ObjectSetInteger(0,buttonID4,OBJPROP_YDISTANCE,height-85);
   ObjectSetInteger(0,buttonID4,OBJPROP_XSIZE,30);
   ObjectSetInteger(0,buttonID4,OBJPROP_YSIZE,18);
   //ObjectSetString(0,buttonID4,OBJPROP_BORDER_COLOR,clrWhite);
   ObjectSetString(0,buttonID4,OBJPROP_FONT,"Arial");
   ObjectSetString(0,buttonID4,OBJPROP_TEXT,"H4");
   ObjectSetInteger(0,buttonID4,OBJPROP_FONTSIZE,9);
   ObjectSetInteger(0,buttonID4,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,buttonID4,OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,buttonID4,OBJPROP_BACK,true);
   ObjectSetInteger(0,buttonID4,OBJPROP_CORNER,4);
   
   ObjectCreate(0,buttonID5,OBJ_BUTTON,0,10,570);
   ObjectSetInteger(0,buttonID5,OBJPROP_COLOR,clrBlack);
   ObjectSetInteger(0,buttonID5,OBJPROP_BGCOLOR,clrGray);
   ObjectSetInteger(0,buttonID5,OBJPROP_XDISTANCE,5);
   ObjectSetInteger(0,buttonID5,OBJPROP_YDISTANCE,height-105);
   ObjectSetInteger(0,buttonID5,OBJPROP_XSIZE,30);
   ObjectSetInteger(0,buttonID5,OBJPROP_YSIZE,18);
   //ObjectSetString(0,buttonID5,OBJPROP_BORDER_COLOR,clrWhite);
   ObjectSetString(0,buttonID5,OBJPROP_FONT,"Arial");
   ObjectSetString(0,buttonID5,OBJPROP_TEXT,"H1");
   ObjectSetInteger(0,buttonID5,OBJPROP_FONTSIZE,9);
   ObjectSetInteger(0,buttonID5,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,buttonID5,OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,buttonID5,OBJPROP_BACK,true);
   ObjectSetInteger(0,buttonID5,OBJPROP_CORNER,4);
 
//--- Create a label for displaying information
   ObjectCreate(0,labelID,OBJ_LABEL,0,100,100);
   ObjectSetInteger(0,labelID,OBJPROP_COLOR,clrRed);
   ObjectSetInteger(0,labelID,OBJPROP_XDISTANCE,40);
   ObjectSetInteger(0,labelID,OBJPROP_YDISTANCE,height-25);
   ObjectSetString(0,labelID,OBJPROP_FONT,"Trebuchet MS");
   ObjectSetString(0,labelID,OBJPROP_TEXT,"---");
   ObjectSetInteger(0,labelID,OBJPROP_FONTSIZE,9);
   ObjectSetInteger(0,labelID,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,labelID,OBJPROP_HIDDEN,true);

//--- Create a label for displaying information 2
   ObjectCreate(0,labelID2,OBJ_LABEL,0,100,100);
   ObjectSetInteger(0,labelID2,OBJPROP_COLOR,clrRed);
   ObjectSetInteger(0,labelID2,OBJPROP_XDISTANCE,40);
   ObjectSetInteger(0,labelID2,OBJPROP_YDISTANCE,height-45);
   ObjectSetString(0,labelID2,OBJPROP_FONT,"Trebuchet MS");
   ObjectSetString(0,labelID2,OBJPROP_TEXT,"---");
   ObjectSetInteger(0,labelID2,OBJPROP_FONTSIZE,9);
   ObjectSetInteger(0,labelID2,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,labelID2,OBJPROP_HIDDEN,true); 
   
//--- Create a label for displaying information 2
   ObjectCreate(0,labelID3,OBJ_LABEL,0,100,100);
   ObjectSetInteger(0,labelID3,OBJPROP_COLOR,clrRed);
   ObjectSetInteger(0,labelID3,OBJPROP_XDISTANCE,40);
   ObjectSetInteger(0,labelID3,OBJPROP_YDISTANCE,height-65);
   ObjectSetString(0,labelID3,OBJPROP_FONT,"Trebuchet MS");
   ObjectSetString(0,labelID3,OBJPROP_TEXT,"---");
   ObjectSetInteger(0,labelID3,OBJPROP_FONTSIZE,9);
   ObjectSetInteger(0,labelID3,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,labelID3,OBJPROP_HIDDEN,true); 
   
//--- Create a label for displaying information 2
   ObjectCreate(0,labelID4,OBJ_LABEL,0,100,100);
   ObjectSetInteger(0,labelID4,OBJPROP_COLOR,clrRed);
   ObjectSetInteger(0,labelID4,OBJPROP_XDISTANCE,40);
   ObjectSetInteger(0,labelID4,OBJPROP_YDISTANCE,height-85);
   ObjectSetString(0,labelID4,OBJPROP_FONT,"Trebuchet MS");
   ObjectSetString(0,labelID4,OBJPROP_TEXT,"---");
   ObjectSetInteger(0,labelID4,OBJPROP_FONTSIZE,9);
   ObjectSetInteger(0,labelID4,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,labelID4,OBJPROP_HIDDEN,true); 
   
//--- Create a label for displaying information 2
   ObjectCreate(0,labelID5,OBJ_LABEL,0,100,100);
   ObjectSetInteger(0,labelID5,OBJPROP_COLOR,clrRed);
   ObjectSetInteger(0,labelID5,OBJPROP_XDISTANCE,40);
   ObjectSetInteger(0,labelID5,OBJPROP_YDISTANCE,height-105);
   ObjectSetString(0,labelID5,OBJPROP_FONT,"Trebuchet MS");
   ObjectSetString(0,labelID5,OBJPROP_TEXT,"---");
   ObjectSetInteger(0,labelID5,OBJPROP_FONTSIZE,9);
   ObjectSetInteger(0,labelID5,OBJPROP_SELECTABLE,0);
   ObjectSetInteger(0,labelID5,OBJPROP_HIDDEN,true); 
//---
   return(INIT_SUCCEEDED);
   //Print("a=",a);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   ObjectDelete(0,buttonID);
   ObjectDelete(0,buttonID2);
   ObjectDelete(0,buttonID3);
   ObjectDelete(0,buttonID4);
   ObjectDelete(0,buttonID5);
   ObjectDelete(0,labelID);
   ObjectDelete(0,labelID2);
   ObjectDelete(0,labelID3);
   ObjectDelete(0,labelID4);
   ObjectDelete(0,labelID5);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
 
  }
//+------------------------------------------------------------------+

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
  int height2=ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0);
  ObjectSetInteger(0,buttonID,OBJPROP_YDISTANCE,height2-25);
  ObjectSetInteger(0,buttonID2,OBJPROP_YDISTANCE,height2-45);
  ObjectSetInteger(0,buttonID3,OBJPROP_YDISTANCE,height2-65);
  ObjectSetInteger(0,buttonID4,OBJPROP_YDISTANCE,height2-85);
  ObjectSetInteger(0,buttonID5,OBJPROP_YDISTANCE,height2-105);
  
  ObjectSetInteger(0,labelID,OBJPROP_YDISTANCE,height2-25);
  ObjectSetInteger(0,labelID2,OBJPROP_YDISTANCE,height2-45);
  ObjectSetInteger(0,labelID3,OBJPROP_YDISTANCE,height2-65);
  ObjectSetInteger(0,labelID4,OBJPROP_YDISTANCE,height2-85);
  ObjectSetInteger(0,labelID5,OBJPROP_YDISTANCE,height2-105);
  
//--- Check the event by pressing a mouse button
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      bool selected = ObjectGetInteger(0,buttonID,OBJPROP_STATE);
      bool selected2 = ObjectGetInteger(0,buttonID2,OBJPROP_STATE);
      bool selected3 = ObjectGetInteger(0,buttonID3,OBJPROP_STATE);
      bool selected4 = ObjectGetInteger(0,buttonID4,OBJPROP_STATE);
      bool selected5 = ObjectGetInteger(0,buttonID5,OBJPROP_STATE);
      
      
      if(selected != PrevSelected)
      {
         PrevSelected = selected;
         a++;
         if(a == 5){a=0;}
      }
      //Print("ID2  = ",i);
      
      if(a==1)
      {
         ObjectSetString(0,labelID,OBJPROP_TEXT,"UP");
      }
      else if(a==2)
      {
         ObjectSetString(0,labelID,OBJPROP_TEXT,"DOWN");
      }
      else if(a==3)
      {
         ObjectSetString(0,labelID,OBJPROP_TEXT,"CONS_UP");
      }
      else if(a==4)
      {
         ObjectSetString(0,labelID,OBJPROP_TEXT,"CONS_DOWN");
      }
      else if(a==0)
      {
         ObjectSetString(0,labelID,OBJPROP_TEXT,"---");
      }
      
      if(selected2 != PrevSelected2)
      {
         PrevSelected2 = selected2;
         b++;
         if(b == 5){b=0;}
      }
      if(b==1)
      {
         ObjectSetString(0,labelID2,OBJPROP_TEXT,"UP");
      }
      else if(b==2)
      {
         ObjectSetString(0,labelID2,OBJPROP_TEXT,"DOWN");
      }
      else if(b==3)
      {
         ObjectSetString(0,labelID2,OBJPROP_TEXT,"CONS_UP");
      }
      else if(b==4)
      {
         ObjectSetString(0,labelID2,OBJPROP_TEXT,"CONS_DOWN");
      }
      else if(b==0)
      {
         ObjectSetString(0,labelID2,OBJPROP_TEXT,"---");
      }
      
      if(selected3 != PrevSelected3)
      {
         PrevSelected3 = selected3;
         c++;
         if(c == 5){c=0;}
      }
      if(c==1)
      {
         ObjectSetString(0,labelID3,OBJPROP_TEXT,"UP");
      }
      else if(c==2)
      {
         ObjectSetString(0,labelID3,OBJPROP_TEXT,"DOWN");
      }
      else if(c==3)
      {
         ObjectSetString(0,labelID3,OBJPROP_TEXT,"CONS_UP");
      }
      else if(c==4)
      {
         ObjectSetString(0,labelID3,OBJPROP_TEXT,"CONS_DOWN");
      }
      else if(c==0)
      {
         ObjectSetString(0,labelID3,OBJPROP_TEXT,"---");
      }  
      
      
      if(selected4 != PrevSelected4)
      {
         PrevSelected4 = selected4;
         d++;
         if(d == 5){d=0;}
      }
      if(d==1)
      {
         ObjectSetString(0,labelID4,OBJPROP_TEXT,"UP");
      }
      else if(d==2)
      {
         ObjectSetString(0,labelID4,OBJPROP_TEXT,"DOWN");
      }
      else if(d==3)
      {
         ObjectSetString(0,labelID4,OBJPROP_TEXT,"CONS_UP");
      }
      else if(d==4)
      {
         ObjectSetString(0,labelID4,OBJPROP_TEXT,"CONS_DOWN");
      }
      else if(d==0)
      {
         ObjectSetString(0,labelID4,OBJPROP_TEXT,"---");
      }  
      
      if(selected5 != PrevSelected5)
      {
         PrevSelected5 = selected5;
         e++;
         if(e == 5){e=0;}
      }
      if(e==1)
      {
         ObjectSetString(0,labelID5,OBJPROP_TEXT,"UP");
      }
      else if(e==2)
      {
         ObjectSetString(0,labelID5,OBJPROP_TEXT,"DOWN");
      }
      else if(e==3)
      {
         ObjectSetString(0,labelID5,OBJPROP_TEXT,"CONS_UP");
      }
      else if(e==4)
      {
         ObjectSetString(0,labelID5,OBJPROP_TEXT,"CONS_DOWN");
      }
      else if(e==0)
      {
         ObjectSetString(0,labelID5,OBJPROP_TEXT,"---");
      }      
   }
}

Files:
button.mq4 13 kb
Vladimir Karputov
Moderator
46162
Vladimir Karputov 2016.01.05 11:32  

In case the symbol or timeframe of a chart, to which the Expert Advisor is attached, changes, Expert Advisors are not loaded or unloaded. In this case client terminal subsequently calls OnDeinit() handlers on the old symbol/timeframe and OnInit() on the new symbol/timeframe (if they are such), values of global variables and static variables are not reset. All events, which have been received for the Expert Advisor before the initialization is completed (OnInit() function) are skipped.

Thus the OnInit() event is raised. And in the OnInit () all items are created anew.

Stuart Browne
5818
Stuart Browne 2016.01.06 00:56  
Karputov Vladimir:

In case the symbol or timeframe of a chart, to which the Expert Advisor is attached, changes, Expert Advisors are not loaded or unloaded. In this case client terminal subsequently calls OnDeinit() handlers on the old symbol/timeframe and OnInit() on the new symbol/timeframe (if they are such), values of global variables and static variables are not reset. All events, which have been received for the Expert Advisor before the initialization is completed (OnInit() function) are skipped.

Thus the OnInit() event is raised. And in the OnInit () all items are created anew.

Store the state of the labels in global variables then (if they exist), load them in OnInit()
Alexey Volchanskiy
16683
Alexey Volchanskiy 2016.01.14 07:50  
Stuart Browne:
Store the state of the labels in global variables then (if they exist), load them in OnInit()

I use this code for resolve such problem

int LastDeinitReason = 0;

void OnDeinit(const int reason)
{
    LastDeinitReason = reason;
    if(reason==REASON_CHARTCHANGE || reason==REASON_TEMPLATE || reason==REASON_PARAMETERS)
        return;
    // code for real deinit 
}

int OnInit()
{
    if(LastDeinitReason==REASON_CHARTCHANGE || LastDeinitReason==REASON_TEMPLATE)
    {
        LastDeinitReason = 0;
        return(INIT_SUCCEEDED);
    }
    if(LastDeinitReason==REASON_PARAMETERS)
    {
        SetParameters();
        return(INIT_SUCCEEDED);                              
    }
    // code for start init
}


To add comments, please log in or register