Download MetaTrader 5

MT4 programming question for oriented objects

To add comments, please log in or register
Albert Poulis
648
Albert Poulis  

Can some one please help me with a problem i have with constructing buttons that will change the time frames or symbols.

I have read all of the forums unless i have missed some thing and by all means if you can help me i would be so happy .

the high lighted area is where my problem is please i would appreciate your help thank you.



here is my problem:


   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="btn")

     {

      int timeframes=cco.Timeframes();

      if(!(timeframes&OBJ_PERIOD_H1))

        {

         cco.Timeframes(timeframes|OBJ_PERIOD_H1);

        }


     }


//+------------------------------------------------------------------+

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
whroeder1
17111
whroeder1  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section (bottom of the Root page?)
  3.       int timeframes=cco.Timeframes();
          if(!(timeframes&OBJ_PERIOD_H1)){
             cco.Timeframes(timeframes|OBJ_PERIOD_H1);
    You never attached an object to the chart object, so the cco calls are meaningless.
  4. If you want to change timeframes just do it.
    Not compiled, not tested.
    ENUM_TIMEFRAMES TF[]={
       PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1,
       PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1
      };
    ENUM_TIMEFRAMES next_tf(void){
       int i=0;
       while(TF[i] < _Period) ++i;
       int next = (i+1) % ArraySize(TF);
       return TF[next];
    }
    ENUM_TIMEFRAMES prev_tf(void){
       int i=0;
       while(TF[i] < _Period) ++i;
       int prev = (i-1 + ArraySize(TF)) % ArraySize(TF);
       return TF[prev];
    }
    :
    if(id==CHARTEVENT_OBJECT_CLICK && sparam=="btn") ChartSetSymbolPeriod(0, _Symbol, next_tf() );
    Not compiled, not tested.
honest_knave
Moderator
2342
honest_knave  
Albert Poulis:

Can some one please help me with a problem i have with constructing buttons that will change the time frames or symbols.

I have read all of the forums unless i have missed some thing and by all means if you can help me i would be so happy .

the high lighted area is where my problem is please i would appreciate your help thank you.



here is my problem:

      int timeframes=cco.Timeframes();

      if(!(timeframes&OBJ_PERIOD_H1))

        {

         cco.Timeframes(timeframes|OBJ_PERIOD_H1);


You would use bitwise operations on an object's timeframe visibility, but not for the chart timeframe. As @whroeder1 has suggested, you can use ChartSetSymbolPeriod() instead.

Also, be careful about how you create btn2. If you set y2 < y1 it won't register the click:

//+------------------------------------------------------------------+
//|                                                  Al's_Window.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <ChartObjects\ChartObject.mqh> 
#include <Controls/Dialog.mqh>
#include <Controls/Button.mqh>

//---

CAppDialog dlog;
CButton btn,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8;
CChartObject cco;

//+------------------------------------------------------------------+
//| External Inputs                                                                 |
//+------------------------------------------------------------------+

ENUM_TIMEFRAMES TF[]=
  {
   PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1,
   PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1
  };

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int OnInit()
  {
   dlog.Create(0,"Easy Time & Symbol Trade Picker",0,0,0,300,200);
//---
   btn.Create(0,"btn",0,5,1,40,15);
   btn.ColorBackground(clrLime);
   btn.ColorBorder(clrBlack);
   btn.Font("Arial");
   btn.Text("M1");
//---
   btn1.Create(0,"btn1",0,5,15,40,30);
   btn1.Alignment(0,5,20,0,0);
   btn1.ColorBackground(clrLime);
   btn1.ColorBorder(clrBlack);
   btn1.Font("Arial");
   btn1.Text("M5");
//---
//   btn2.Create(0,"btn2",0,5,46,40,31);
//   btn1.Alignment(0,5,20,0,0);
   btn2.Create(0,"btn2",0,5,30,40,45);
   btn2.Alignment(0,5,20,0,0);
   btn2.ColorBackground(clrLime);
   btn2.ColorBorder(clrBlack);
   btn2.Font("Arial");
   btn2.Text("M15");
//---
   dlog.Add(btn);
   dlog.Add(btn1);
   dlog.Add(btn2);
   dlog.Run();
//--- create timer
   EventSetTimer(60);
//---
   return(INIT_SUCCEEDED);
  }

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

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }

//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
  }

//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   dlog.OnEvent(id,lparam,dparam,sparam);
   btn.OnEvent(id,lparam,dparam,sparam);
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if     (sparam=="btn"  && _Period!=PERIOD_M1)  ChartSetSymbolPeriod(0,_Symbol,PERIOD_M1);
      else if(sparam=="btn1" && _Period!=PERIOD_M5)  ChartSetSymbolPeriod(0,_Symbol,PERIOD_M5);
      else if(sparam=="btn2" && _Period!=PERIOD_M15) ChartSetSymbolPeriod(0,_Symbol,PERIOD_M15);
     }
  }
//+------------------------------------------------------------------+
Albert Poulis
648
Albert Poulis  
whroeder1:
  1. Please edit your post.
    For large amounts of code, attach it

  2. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section (bottom of the Root page?)
  3. You never attached an object to the chart object, so the cco calls are meaningless.
  4. If you want to change timeframes just do it.
    Not compiled, not tested.
    Not compiled, not tested.

To be honest i just wanted to get it over and done with so i thought posting it any where i can might do the trick and thank god you were around lol...thank you whroeder1 you have no idea what this means to me mate..
Albert Poulis
648
Albert Poulis  
honest_knave:

You would use bitwise operations on an object's timeframe visibility, but not for the chart timeframe. As @whroeder1 has suggested, you can use ChartSetSymbolPeriod() instead.

Also, be careful about how you create btn2. If you set y2 < y1 it won't register the click:


thank you so much honest_knave i am very happy with the feed back would you believe i was head butting the table trying to fix it lol.
honest_knave
Moderator
2342
honest_knave  

Opinion on the forum is generally divided about OOP... unless you have a pre-existing knowledge of OOP, for simple projects you may find it easier to stick with procedural to avoid any   moments.

Although it wasn't the problem here, I have found issues with the standard libraries that can be very frustrating to resolve.

Good luck!

Albert Poulis
648
Albert Poulis  
honest_knave:

Opinion on the forum is generally divided about OOP... unless you have a pre-existing knowledge of OOP, for simple projects you may find it easier to stick with procedural to avoid any   moments.

Although it wasn't the problem here, I have found issues with the standard libraries that can be very frustrating to resolve.

Good luck!


Thank you again honest_knave lol and yes your right frustration is what i went through lol.
To add comments, please log in or register