Download MetaTrader 5

Object multiple timeframes visibility as external parameter / input (OBJ_PERIOD_X|...)

To add comments, please log in or register
Chistabo
157
Chistabo  

Hello!

 

I have a script, which plots data from .csv, as vertical lines with (or without) text. At the script start, I would like to be able to select timeframes that those vertical lines (and text) should be plotted on. Like:

 

input int PlotOnTimeframes = 60, 240, 1440; 

 

//+------------------------------------------------------------------+
//|                                    PlotVerticalLineScriptChi.mq4 |
//|                                       Copyright © 2011, Chistabo |
//|                                               chistabo@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2014 by Chistabo"
#property link      "chistabo@gmail.com"
#property script_show_inputs
#property description "LineStyle:";
#property description "Solid-0 | dash=1 | dot=2 | dash-dot=3 | dash-dot-dot=4";
//********************************************
#property description "Colors:";
#property description "Yellow=Sy/Sun | White=Ch/Moon | Red=Ma/Mar | Green=Bu/Mer | DarkOrange=Gu/Jup | Violet=Sk/Ven | DodgerBlue=Sa/Sa ";
#property description "Plotted data will have object name 'VLine NAME...', so you can find and delete those easily.";
   input string FileName = "PlotDataChiAspects.csv";
   input string DataName = "Ch Sa";
        input color LineColour = Blue;
        input int LineStyle = 3;
        input color TextColour = White;
        input bool ShowText = false;
        input int PlotOnTimeframes = 60, 240, 1440; // <- this input to select timeframes to plot vertical line & text on
// TO-DO: timeframes input      
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int OnStart()
   {
//----
   ResetLastError();
        string EDate, Description;
        int TimeIndex;
        double Buffer;
        int handle;
   handle = FileOpen (FileName, FILE_CSV|FILE_READ, ',');
   if (handle < 1)
      {
      PrintFormat ("File %s not found, last error is %d", FileName, GetLastError());
      return (false);
      }
   else PrintFormat ("Plotting %s file...", FileName);   
        while (!FileIsEnding (handle))
      {
                EDate = FileReadString (handle);
                Description = FileReadString (handle);
                if (EDate == "") continue;
                datetime DDate = StrToTime (EDate);
//**********************************************************************************************                
                SetObj ("VLine " + DataName + " - " + EDate + " - " + Description, OBJ_VLINE, StrToTime (EDate), 0, 1, LineColour);
                
      TimeIndex = 0;
      for (int j = 0; j < Bars; j++)
      {
         if (Time [j] >= DDate && Time [j+1] <= DDate) TimeIndex = j;
         if (TimeIndex != 0) break;
      }
//**********************************************************************************************
      if (ShowText)
         {
         SetTxtObj ("VLine " + DataName + " - " + EDate + Description, OBJ_TEXT, StrToTime (EDate),
         Low [TimeIndex] - Buffer, Description, 90, TextColour);
         }
        }
        FileClose (handle);
        PrintFormat("File %s plotted!", FileName);
//--------------
//   PrintFormat("Description = " + Description);
//--------------
   return (0);
  }
//+------------------------------------------------------------------+
int SetTxtObj (string Name, int ObjType, datetime Time1, double Price1, string ObjText, double Angle, color LColor) 
   {
   if (ObjectFind (Name) < 0)
      {
//object not found create new one
      if (ObjType == OBJ_TEXT)
         {
         ObjectCreate (Name, OBJ_TEXT, 0, Time1, Price1);
         ObjectSetText (Name, ObjText, 8, "Arial", LColor);
         ObjectSet (Name, OBJPROP_ANGLE, Angle);
         ObjectSet (Name, OBJPROP_BACK, 1); 
         ObjectSet (Name, OBJPROP_TIMEFRAMES, OBJ_PERIOD_H4 | OBJ_PERIOD_D1 | OBJ_PERIOD_H1); // <- recode
         }   
      }
   else
      {
      if (ObjectType (Name) == OBJ_TEXT)
         {
         ObjectSet (Name, OBJPROP_TIME1, Time1);
         ObjectSet (Name, OBJPROP_PRICE1, Price1);
         ObjectSetText (Name, ObjText, 8, "Arial", LColor);
         ObjectSet (Name, OBJPROP_ANGLE, Angle);
         ObjectSet (Name, OBJPROP_BACK, 1);
         ObjectSet (Name, OBJPROP_TIMEFRAMES, OBJ_PERIOD_H4 | OBJ_PERIOD_D1 | OBJ_PERIOD_H1); // <- recode
         }
      }
   return(0);
   }
//+------------------------------------------------------------------+
int SetObj (string Name, int ObjType, datetime Time1, double Price1, int Thickness, color LColor) 
   {
   if (ObjectFind (Name) < 0)
      {
//object not found create new one
      if (ObjType == OBJ_VLINE)
         {
         ObjectCreate (Name, OBJ_VLINE, 0, Time1, Price1);
         ObjectSet (Name, OBJPROP_WIDTH, Thickness);
         ObjectSet (Name, OBJPROP_COLOR, LColor);
         ObjectSet (Name, OBJPROP_STYLE, LineStyle);
         ObjectSet (Name, OBJPROP_BACK, 1);
         ObjectSet (Name, OBJPROP_TIMEFRAMES, OBJ_PERIOD_H4 | OBJ_PERIOD_D1 | OBJ_PERIOD_H1); // <- recode
      }   
   }
   else
      {
      if (ObjectType (Name) == OBJ_VLINE)
         {
         ObjectSet (Name, OBJPROP_TIME1, Time1);
         ObjectSet (Name, OBJPROP_PRICE1, Price1);
         ObjectSet (Name, OBJPROP_WIDTH, Thickness);
         ObjectSet (Name, OBJPROP_COLOR, LColor);         
         ObjectSet (Name, OBJPROP_BACK, 1);
         ObjectSet (Name, OBJPROP_TIMEFRAMES, OBJ_PERIOD_H4 | OBJ_PERIOD_D1 | OBJ_PERIOD_H1); // <- recode
         }
      }
   return(0);
   }

 csv is in this format:

1990.07.08 00:00, Ch 000 Sa

1990.07.21 00:00, Ch 180 Sa 

etc...

yyyy.mm.dd hh:mm, text

 

I tried replacing all instances of 'OBJ_PERIOD_H4 | OBJ_PERIOD_D1 | OBJ_PERIOD_H1' with 'PlotOnTimeframes', and it works, just, when I run the script and input window pops-up, the last input line reads 'PlotOnTimeframes - 112'?

Eeee, how do you get 112 from 60, 240, 1440?

 

Thank you for your help,

 

Best regards,

 

Simon 

Chistabo
157
Chistabo  

Eeee, how do you get 112 from 60, 240, 1440?

OK, I figured this out - it is decimal to hexadecimal conversion, so all I have to do is sum values of wanted timeframes and use that sum as input:

 

 

Hex

Decimal

 

OBJ_NO_PERIODS, EMPTY

-1

 

The object is not drawn in all timeframes

OBJ_PERIOD_M1

0x0001

1

The object is drawn in 1-minute chart

OBJ_PERIOD_M5

0x0002

2

The object is drawn in 5-minute chart

OBJ_PERIOD_M15

0x0004

4

The object is drawn in 15-minute chart

OBJ_PERIOD_M30

0x0008

8

The object is drawn in 30-minute chart

OBJ_PERIOD_H1

0x0010

16

The object is drawn in 1-hour chart

OBJ_PERIOD_H4

0x0020

32

The object is drawn in 4-hour chart

OBJ_PERIOD_D1

0x0040

64

The object is drawn in day charts

OBJ_PERIOD_W1

0x0080

128

The object is drawn in week charts

OBJ_PERIOD_MN1

0x0100

256

The object is drawn in month charts

OBJ_ALL_PERIODS

0x01ff

511

The object is drawn in all timeframes

 

Still, if any of you would be kind enough to tell how the code would look if I wanted to use timeframe-to-minutes scale (i.e. 60,15,1440), that would be nice.

 

Best regards,

 

Simon 

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI  

Just detect which timeframe and assign appropriately.

Just convert it, let say h1 = 60 minutes. There are many ways to do the logic.

Chistabo
157
Chistabo  
deysmacro:

Just detect which timeframe and assign appropriately.

Just convert it, let say h1 = 60 minutes. There are many ways to do the logic.


Thank you, but I am learning to program on the go, so some more specific answer would be appreciated.

Anyway, I was thinking and one solution would be a code that would:

- read the input line and sum all parameters that are there

- use that sum and insert it as PlotOnTimeframes value

 

That is:

input int PlotOnTimeframes = 60, 240, 1440;
...
read PlotOnTimeframes values and sum = 60 + 240 + 1440 = 1740
convert to hexadecimal and pass to PlotOnTimeframes values in the code

 Now I need to figure it out how to sum those values. Any hint would be welcome...

 

Best regards,

 

Simon 

whroeder1
15551
whroeder1  
Chistabo:
input int PlotOnTimeframes = 60, 240, 1440;

Now I need to figure it out how to sum those values. Any hint would be welcome...
OBJ_PERIOD_H1=16 OBJ_PERIOD_H4=32 OBJ_PERIOD_D1=64 16+32+64=112
To add comments, please log in or register