Download MetaTrader 5
To add comments, please log in or register
Join MQL5 Cloud Network. Improve your rating!
Keith Watford
Moderator
8027
Keith Watford 2014.03.07 13:11 

This is part of my code

//|                                                           GumRai |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "GumRai"
#property link      "http://www.metaquotes.net"

enum ENUM_MA_TIMEFRAME 
  {
   M5,
   M15,
   M30,
   H1,
   H4,
   Daily,
   Weekly,
   Current_Chart
  };  

//--- input parameters

input ENUM_APPLIED_PRICE  MA_applied_price = 0;
input ENUM_MA_METHOD  MA_Method = MODE_SMA;
input ENUM_MA_TIMEFRAME  MA_Chart_Timeframe = Current_Chart;
input int        MA_Use_Period = 200;
input color Colour=Red;


#include <stdlib.mqh >
#include <WinUser32.mqh>               // Needed to MessageBox

 
 ENUM_TIMEFRAMES  MA_Timeframe;
 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
 
 switch(MA_Chart_Timeframe)
   {
   case M5:
      MA_Timeframe = PERIOD_M5;
      break;
   case M15:
      MA_Timeframe = PERIOD_M15;
      break;
   case M30:
      MA_Timeframe = PERIOD_M30;
      break;
   case H1:
      MA_Timeframe = PERIOD_H1;
      break;
   case H4:
      MA_Timeframe = PERIOD_H4;
      break;
   case Daily:
      MA_Timeframe = PERIOD_D1;
      break;
   case Weekly:
      MA_Timeframe = PERIOD_W1;
      break;
   case Current_Chart:
      MA_Timeframe = PERIOD_CURRENT;
      break;
      
int y=20;
for(int x=1;x<=3;x++)
{
ObjectCreate("Message"+x, OBJ_LABEL, 0, 0, 0);// Creating obj.
   ObjectSet("Message"+x, OBJPROP_CORNER, 1);    // Reference corner
   ObjectSet("Message"+x, OBJPROP_XDISTANCE, 10);// X coordinate
   ObjectSet("Message"+x, OBJPROP_YDISTANCE, y);// Y coordinate
   ObjectSetText("Message"+x,".",8,"Arial",CLR_NONE);  // Empty Labels
   y=y+15;
 }
  
      
   }
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   for(int x=1;x<=3;x++)
   {
   ObjectDelete("Message"+x) ;   
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
 
  double ma_value = iMA(Symbol(),MA_Timeframe,MA_Use_Period,0,MA_Method,MA_applied_price,0);
  ObjectSetText("Message1","MA Value = "+DoubleToStr(ma_value,Digits),8,"Arial",Colour);
  ObjectSetText("Message2","Timeframe = "+MA_Timeframe,8,"Arial",Colour);
  ObjectSetText("Message3","Applied price = "+MA_applied_price,8,"Arial",Colour);
    
//----
   return(0);
  }
//+------------------------------------------------------------------+

Everything works fine in the inputs, the drop down lists work perfectly

The problem I have is when setting the text to the labels

  double ma_value = iMA(Symbol(),MA_Timeframe,MA_Use_Period,0,MA_Method,MA_applied_price,0);
  ObjectSetText("Message1","MA Value = "+DoubleToStr(ma_value,Digits),8,"Arial",Colour);
  ObjectSetText("Message2","Timeframe = "+MA_Timeframe,8,"Arial",Colour);
  ObjectSetText("Message3","Applied price = "+MA_applied_price,8,"Arial",Colour);

If the Ma_Timeframe is PERIOD_CURRENT it will just print as 0

If MA_applied_price is close, it will print as 0

Although I am happy enough if I could get MA_applied_price to print as "Close"

I would like Ma_Timeframe to print as Current_Chart

Or if Ma_Timeframe ==PERIOD_H1, to print as "H1" instead of 60

Is there a simple way to do this?

Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2014.03.07 14:04  
GumRai:

This is part of my code

Everything works fine in the inputs, the drop down lists work perfectly

The problem I have is when setting the text to the labels

If the Ma_Timeframe is PERIOD_CURRENT it will just print as 0

PERIOD_CURRENT is 0 . . . from here: https://docs.mql4.com/constants/chartconstants/enum_timeframes

I'm not experienced with these ENUMs yet . . but shouldn't this . . .

   case Current_Chart:
      MA_Timeframe = PERIOD_CURRENT;
      break;

. . . be this instead ?

   case Current_Chart:
      MA_Timeframe = Period();
      break;
qjol
3243
qjol 2014.03.07 14:25  

GumRai:

Or if Ma_Timeframe ==PERIOD_H1, to print as "H1" instead of 60

Is there a simple way to do this?


no because PERIOD_H1 is a int & "H1" is a string

u cant convert int PERIOD_H1 to be a string "H1"

of cource u can do it but not like u do it

if (Period() == 60)
string H1 = "H1";
Ian Venner
2583
Ian Venner 2014.03.07 15:40  

It wont return the name of the identifier, it is the same thing as calling a variable, it just returns its value. You know you can give the drop down list "friendly" names too like

enum veg
{
Car, //carrots
tom, //tomatoes
cab, //cabbage
cal, //cauliflour
pot //potatoes
};

input veg vegatable = carrots

ydrol
595
ydrol 2014.03.07 15:50  
GumRai:

Or if Ma_Timeframe ==PERIOD_H1, to print as "H1" instead of 60. Is there a simple way to do this?

How about EnumToString() ..

StringReplace(EnumToString(Ma_Timeframe),"PERIOD_","")
Ian Venner
2583
Ian Venner 2014.03.07 15:58  

EnumToString() !! i didnt know that existed

Keith Watford
Moderator
8027
Keith Watford 2014.03.07 20:38  
ydrol:

How about EnumToString() ..



SDC:

EnumToString() !! i didnt know that existed


Thankyou so much ydrol

I guess that when I was looking down the index in the documentation, my eyes were conditioned by all the Enums being in capital letters, so I totally missed that.

whroeder1
13656
whroeder1 2014.03.07 21:07  
SDC: EnumToString() !! i didnt know that existed
GumRai: so I totally missed that.
Why I created Alphabetic Index of MQL4 Functions (600+) - MQL4 forum
Ian Venner
2583
Ian Venner 2014.03.08 03:49  
Thanks, I hadn't seen that post before, very much appreciated.
/
To add comments, please log in or register