Download MetaTrader 5

Change Time frame

To add comments, please log in or register
GrumpyDuckMan
328
GrumpyDuckMan  

Hello all,

I have been trying to change a charts timeframe in my EA, but I haven't been able to get it to work.

  string we ="GOLD";
  ObjectSetInteger(0,we,OBJPROP_TIMEFRAMES,OBJ_PERIOD_M15); 

The code complied with no errors or, warnings.  I need some help to make this work properly.

whroeder1
15902
whroeder1  
GrumpyDuckMan: t I haven't been able to get it to work.
  1. Your code tries to change which timeframe an object is visible on.
  2. If you want to change the chart, do it: ChartSetSymbolPeriod
GrumpyDuckMan
328
GrumpyDuckMan  
bool ChartSetSymbolPeriod(long Chart_ID=NULL,string ChartName="GOLD",ENUM_TIMEFRAMES TimePeriod =PERIOD_M15);

Is this correct?  I can't tell because the program compiles with no errors. If it is correct how do I get the chart time to change.

GrumpyDuckMan
328
GrumpyDuckMan  

Hello all,

In my efforts to try and understand what I am trying to do, I have tried to comment my code.

/* bool  ChartSetSymbolPeriod( 
   long             chart_id,     // Chart ID 
   string           symbol,       // Symbol name 
   ENUM_TIMEFRAMES  period        // Period);
*/

//   Chart ID. 0 means the current chart.
bool ChartSetSymbolPeriod(long Chart_ID =0,
/*   Chart symbol. NULL value means the Current
     Chart symbol (Expert Advisor is attached to)
*/ 
     string ChartName=NULL,
/*   Chart period (timeframe). Can be one of the ENUM_TIMEFRAMES values. 0 means the current chart period.
     Returns true if the command has been added to chart queue, otherwise false.
     To get error details use the GetLastError() function. 
*/                            
     ENUM_TIMEFRAMES ChartTime = PERIOD_M15);
/*   The symbol/period change leads to the re-initialization of the Expert Advisor, attached to a chart.
     Re-initialization is not performed on offline charts, they're only refreshed 
     (the same as when clicking Refresh in the terminal).
*/

I can't identify my problem at the moment .

Re-initialization is not performed on offline charts, they're only refreshed 
     (the same as when clicking Refresh in the terminal).

If the server is offline does that mean I can't change Time Frames?

Sorry I am trying to understand the working bits of MetaQuotes MQL4

whroeder1
15902
whroeder1  
GrumpyDuckMan: Is this correct?  I can't tell because the program compiles with no errors.
GrumpyDuckMan: Sorry I am trying to understand the working bits of MetaQuotes MQL4

  1. Stop lying to yourself (and us.) That code does not compile. That code is the prototype of the function I already gave you.
    Call the function.
    learn to code it, or pay (Freelance) someone to code it. We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
Index of /_MT4_Tutorials
  • www.forexmt4.com
Over 50 tradable currency pairs No commission or hidden costs Guaranteed Stop Losses Mobile Trading Full 24-hour multi lingual customer support and technical assistance A unique 24-hour customer trading room Daily market analysis Mini accounts available Open an account Today!
GrumpyDuckMan
328
GrumpyDuckMan  
whroeder1:

  1. Stop lying to yourself (and us.) That code does not compile. That code is the prototype of the function I already gave you.
    Call the function.
    learn to code it, or pay (Freelance) someone to code it. We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
  2. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section (bottom of the Root page?)

Hello whroeder1,

First of all...I'm not sure if your trying to attack me or help!  Please except my apologise for accidently posting my question in the wrong area.

After your first reply I looked up ChartSetSymbolPeriod in the MQL4 reference manual. Thank you for that helpful bit of information.

I then tried to use ChartSetSymbolPeriod to change the time frame on the chart.

#property copyright " "
#property link      " "
#property version   "1.00"
#property strict
#include <stdlib.mqh>
#include <WinUser32.mqh>

/* bool  ChartSetSymbolPeriod( 
   long             chart_id,     // Chart ID 
   string           symbol,       // Symbol name 
   ENUM_TIMEFRAMES  period        // Period);
*/

//   Chart ID. 0 means the current chart.
bool ChartSetSymbolPeriod(long Chart_ID =0,
/*   Chart symbol. NULL value means the Current
     Chart symbol (Expert Advisor is attached to)
*/ 
     string ChartName=NULL,
/*   Chart period (timeframe). Can be one of the ENUM_TIMEFRAMES values. 0 means the current chart period.
     Returns true if the command has been added to chart queue, otherwise false.
     To get error details use the GetLastError() function. 
*/                            
     ENUM_TIMEFRAMES ChartTime = PERIOD_M15);
/*   The symbol/period change leads to the re-initialization of the Expert Advisor, attached to a chart.
     Re-initialization is not performed on offline charts, they're only refreshed 
     (the same as when clicking Refresh in the terminal).
*/
int OnInit()
{

}


The code compiled in MetaEditor 4. I just need help in applying it. Can you please help me.

Edit: Thank you for the link

Index of /_MT4_Tutorials
  • www.forexmt4.com
Over 50 tradable currency pairs No commission or hidden costs Guaranteed Stop Losses Mobile Trading Full 24-hour multi lingual customer support and technical assistance A unique 24-hour customer trading room Daily market analysis Mini accounts available Open an account Today!
Keith Watford
Moderator
10652
Keith Watford  
GrumpyDuckMan:


The code compiled in MetaEditor 4. I just need help in applying it. Can you please help me.
The code that you have posted will not compile, why do you insist that it does?
GrumpyDuckMan
328
GrumpyDuckMan  
Complete code.
#property copyright " "
#property link      " "
#property version   "1.00"
#property strict
#include <stdlib.mqh>
#include <WinUser32.mqh>

input int stoploss,takeprofit;
input int Slippage=3;
input int MagicNumber=0,num_of_trades=1;
input double lot=0.01;

enum symbols {GOLD,USDMXN,EURMXN,  
CADJPY, EURNOK, GBPCHF, NZDUSD, USDNOK,};
input symbols symbol;

long   x_distance,y_distance;
int    Diff_Y_Butt1,Diff_Y_Butt2,Diff_Y_Butt3,slip,i,j,a,b,c,d,e;
double res,PipPoints,b_s,a_s,points,Spread,CurrBid,CurrAsk,CurrPoints;

/* bool  ChartSetSymbolPeriod( 
   long             chart_id,     // Chart ID 
   string           symbol,       // Symbol name 
   ENUM_TIMEFRAMES  period        // Period);
*/

//   Chart ID. 0 means the current chart.
bool ChartSetSymbolPeriod(long Chart_ID =0,
/*   Chart symbol. NULL value means the Current
     Chart symbol (Expert Advisor is attached to)
*/ 
     string ChartName=NULL,
/*   Chart period (timeframe). Can be one of the ENUM_TIMEFRAMES values. 0 means the current chart period.
     Returns true if the command has been added to chart queue, otherwise false.
     To get error details use the GetLastError() function. 
*/                            
     ENUM_TIMEFRAMES ChartTime = PERIOD_M15);
/*   The symbol/period change leads to the re-initialization of the Expert Advisor, attached to a chart.
     Re-initialization is not performed on offline charts, they're only refreshed 
     (the same as when clicking Refresh in the terminal).
*/
int OnInit()
  {
 Testing();
  if(Digits==3 || Digits==5)
     {
     PipPoints=Point*10;
     slip=Slippage*10;
     }
  else
     {
     PipPoints=Point;
     slip=Slippage;
     }
  ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true);
  //Basic button settings
  string           InpName="TradeButton";       // Button name
  ENUM_BASE_CORNER InpCorner=CORNER_LEFT_UPPER; // Chart corner for anchoring
  string           InpFont="Arial";             // Font
  int              InpFontSize=8;               // Font size
  color            InpColor=clrBlack;           // Text color
  color            InpBackColor=C'236,233,216'; // Background color
  color            InpBorderColor=clrNONE;      // Border color
  bool             InpState=false;              // Pressed/Released
  bool             InpBack=false;               // Background object
  bool             InpSelection=false;           // Highlight to move
  bool             InpHidden=true;              // Hidden in the object list
  long             InpZOrder=0;                 // Priority for mouse click

   if(!ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0,x_distance))
     {
     }
   if(!ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y_distance))
     {
     }

   int x=(int)(x_distance*0.85);
   int tby=(int)(y_distance*0.1);
   int x_size=100;
   int y_size=25;
   int bty=(int)(y_distance*0);     
   if(!ButtonCreate(0,"NewButton",0,x,7,x_size,y_size,InpCorner,"Order",
   InpFont,InpFontSize,InpColor,InpBackColor,InpBorderColor,InpState,InpBack,
   InpSelection,InpHidden,InpZOrder))
     {
     }
   if(!ButtonCreate(0,"TradeButton",0,x,tby,x_size,y_size,InpCorner,"Place Trades",
   InpFont,InpFontSize,InpColor,InpBackColor,InpBorderColor,InpState,InpBack,
   InpSelection,InpHidden,InpZOrder))
     {
     }
   int cay=(int)(y_distance*0.2);
   if(!ButtonCreate(0,"CloseAll",0,x,cay,x_size,y_size,InpCorner,"Close All",
   InpFont,InpFontSize,InpColor,InpBackColor,InpBorderColor,InpState,InpBack,
   InpSelection,InpHidden,InpZOrder))
     {
     }
   int y=(int)(y_distance*0.3);
   if(!ButtonCreate(0,"Move",0,x,y,30,10,InpCorner,"Move",InpFont,6,
      InpColor,InpBackColor,InpBorderColor,InpState,InpBack,InpSelection,
      InpHidden,InpZOrder))
     {
     }
   Diff_Y_Butt1=y-tby;
   Diff_Y_Butt2=y-cay;
   Diff_Y_Butt3=y-bty;
  
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  { 
  }
//+------------------------------------------------------------------+
//                 CurrPoints  = MarketInfo("GOLD",MODE_POINT);
//+------------------------------------------------------------------+
void Testing()
{

              CurrBid     = MarketInfo("GOLD",MODE_BID);
              CurrAsk     = MarketInfo("GOLD",MODE_ASK); 
          
   double CurrentADX      = iADX("GOLD",5,14,PRICE_OPEN,MODE_MAIN,0);
   double READminus       = iADX("GOLD",5,14,PRICE_OPEN,MODE_MINUSDI,0);  
   double READplus        = iADX("GOLD",5,14,PRICE_OPEN,MODE_PLUSDI,0);
   
   double A_CurrentADX    = iADX("GOLD",5,14,PRICE_CLOSE,MODE_MAIN,0);
   double A_READminus     = iADX("GOLD",5,14,PRICE_CLOSE,MODE_MINUSDI,0);  
   double A_READplus      = iADX("GOLD",5,14,PRICE_CLOSE,MODE_PLUSDI,0);
/*  double  iMACD( 
   string       symbol,           // symbol 
   int          timeframe,        // timeframe 
   int          fast_ema_period,  // Fast EMA period 
   int          slow_ema_period,  // Slow EMA period 
   int          signal_period,    // Signal line period 
   int          applied_price,    // applied price 
   int          mode,             // line index 
   int          shift             // shift 
   ); 
*/
   double CurrentSignal   = iMACD("GOLD",0,12,26,9,PRICE_OPEN,MODE_MAIN,0);
   double CurrentValue    = iMACD("GOLD",0,12,26,9,PRICE_OPEN,MODE_SIGNAL,0);      
   double A_CurrentSignal = iMACD("GOLD",0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   double A_CurrentValue  = iMACD("GOLD",0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);  
   
   double A_CurrentiMA    = iMA("GOLD",15,5,5,MODE_SMA,PRICE_CLOSE,0);
   double CurrentiMA      = iMA("GOLD",15,5,5,MODE_SMA,PRICE_OPEN,0);
     
 //  string       symbol,           // symbol 
 //  int          timeframe,        // timeframe 
 //  int          ma_period,        // MA averaging period 
 //  int          ma_shift,         // MA shift 
 //  int          ma_method,        // averaging method 
 //  int          applied_price,    // applied price 
 //  int          shift             // shift 
 //  ); 

// ModifyOrder
// SwapOrder
// CheckOrder
// StopOrder
// StartOrder                          //
    } 
//-------------------------------------------------------------------
//-------------------------------------------------------------------

void buy()
{  string sym = EnumToString(symbol);
   string Curr = sym;
   double s_l=stoploss;
   double t_p=takeprofit;

   double ask    = SymbolInfoDouble(sym,SYMBOL_ASK),
          bid    = SymbolInfoDouble(sym,SYMBOL_BID),
          pnt    = SymbolInfoDouble(sym,SYMBOL_POINT);
   int    digits = (int)SymbolInfoInteger(sym,SYMBOL_DIGITS);
   int ticket= OrderSend(Curr,OP_BUY,lot,bid,slip,s_l,t_p,"EA b Order",MagicNumber,0,clrBlue);
  if(ticket<0)  
   { 
   Print("Debug",GetLastError()); 
   } 
 else 
   Print("Order successfully"); 
 
   } 
 //+------------------------------------------------------------------+
//+------------------------------------------------------------------+  
void sell()
{
  string sym = EnumToString(symbol);
   string Curr = sym;
   double s_l=stoploss;
   double t_p=takeprofit;
   
   // you'll also need to deal with broker suffix / prefix
   double ask    = SymbolInfoDouble(sym,SYMBOL_ASK),
          bid    = SymbolInfoDouble(sym,SYMBOL_BID),
          pnt    = SymbolInfoDouble(sym,SYMBOL_POINT);
   int    digits = (int)SymbolInfoInteger(sym,SYMBOL_DIGITS);
   int ticket= OrderSend(Curr,OP_SELL,lot,bid,slip,s_l,t_p,"EA Order",MagicNumber,0,clrBlue);
  if(ticket<0)  
   { 
   Print("Debug",GetLastError()); 
   } 
 else 
   Print("Order successfully"); 
   }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void CloseAllTrades()
 {
 

 }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam=="TradeButton"){
         string txt;
         txt="Yes to open BUY,or No to open SELL";
           
         int btn=MessageBox(txt,"CONFIRM OPEN NEW TRADES", MB_YESNOCANCEL);
         if(btn ==IDYES)buy();
         if (btn ==IDNO)sell();
           else (btn =IDCANCEL);
         ObjectSetInteger(0,"TradeButton",OBJPROP_STATE,false);
         }
      if(sparam=="CloseAll")
         {
         string txt="Close All trades";
         int btn=MessageBox(txt,"CONFIRM CLOSE ALL", MB_YESNO);
         if(btn ==IDYES)
            CloseAllTrades();
         ObjectSetInteger(0,"CloseAll",OBJPROP_STATE,false);
         }
      }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+     
    if(id==CHARTEVENT_MOUSE_MOVE)
     {
      if(ObjectGetInteger(0,"Move",OBJPROP_STATE))
        {
         int cw=(int)ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0);
         int newx=(int)(lparam)-15; 
         int newy=(int)dparam-5; 
         ObjectSet("Move",OBJPROP_XDISTANCE,newx);
         ObjectSet("Move",OBJPROP_YDISTANCE,newy);
         ObjectSet("TradeButton",OBJPROP_XDISTANCE,newx);
         ObjectSet("TradeButton",OBJPROP_YDISTANCE,newy-Diff_Y_Butt1);
         ObjectSet("CloseAll",OBJPROP_XDISTANCE,newx);
         ObjectSet("CloseAll",OBJPROP_YDISTANCE,newy-Diff_Y_Butt2);
         ObjectSet("NewButton",OBJPROP_XDISTANCE,newx);
         ObjectSet("NewButton",OBJPROP_YDISTANCE,newy-Diff_Y_Butt3);
         ChartRedraw();
        }
     }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
bool ButtonCreate( 
   const long              chart_ID=0,               // chart's ID
   const string            name="Button",            // button name
   const int               sub_window=0,             // subwindow index
   const int               x=0,                      // X coordinate
   const int               y=0,                      // Y coordinate
   const int               width=50,                 // button width
   const int               height=18,                // button height
   const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
   const string            text="Button",            // text
   const string            font="Arial",             // font
   const int               font_size=10,             // font size
   const color             clr=clrBlack,             // text color
   const color             back_clr=C'236,233,216',  // background color
   const color             border_clr=clrNONE,       // border color
   const bool              state=false,              // pressed/released
   const bool              back=false,               // in the background
   const bool              selection=false,          // highlight to move
   const bool              hidden=true,              // hidden in the object list
   const long              z_order=0)                // priority for mouse click
  {
   if(!ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0))
     {
     return(false);
     }
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
   return(true);// successful execution
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void BuySellOnChange(){


}
GrumpyDuckMan
328
GrumpyDuckMan  

Still not working :(

// ',' - unexpected token       RunTest.mq4
// 'ENUM_TIMEFRAMES' - syntax error     RunTest.mq4

    how = ChartSetSymbolPeriod (0,gold,ENUM_TIMEFRAMES,PERIOD_M15);
  // bool how =  ChartSetSymbolPeriod(0,"GOLD",ENUM_TIMEFRAMES,PERIOD_M15);
GrumpyDuckMan
328
GrumpyDuckMan  

I think this might be better. I still can't get it to work.

bool SetTime(
     const long Chart_ID =0,
     const string ChartName="GOLD",
     ENUM_TIMEFRAMES ChartTime = PERIOD_M15);
   if(!ChartSetSymbolPeriod(Chart_ID,ChartName,ChartTime))
     {
     return(false);
     }
GrumpyDuckMan
328
GrumpyDuckMan  
// static string ChartTime,ChartName;
// static long Chart_ID;
        
if(!ChartSetSymbolPeriod(Chart_ID,
                        ChartName,
                        ChartTime))// implicit conversion from 'string' to 'number'
     {
     return(false);                // 'return'-'void' function returns a value
     }
12
To add comments, please log in or register