Struggling with my first attempt at coding

To add comments, please log in or register
Phil
29
Phil  
Hi, I've just started learning to code mql4 over the last couple weeks and have gotten stuck with the first stage of the custom indicator & EA that I'm trying to code.

The first thing that I'm trying to get this to do is to create 2 vertical lines for each week, one on Monday 6.00, and one Friday 21.00. I want these lines to appear through from the beginning of the chart, to the current week.

This is what I have so far. When I attach it to the chart at the moment, nothing at all appears on the chart.

Any help & suggestions of changes I need to make would be most appreciated.

Best

Phil

//+------------------------------------------------------------------+
//|                                 Flat Line Strategy Indicator.mq4 |
//|                                                          Philsko |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Philsko"
#property link      ""

#property indicator_chart_window
//--- input parameters
extern int       StartDay=1;
extern int       EndDay=5;
extern int       StartTime=6;
extern int       EndTime=21;
extern color     StartColour=DarkOrange;
extern color     EndColour=OrangeRed;
extern color     FlatLineColour=Gold;

string           StartLine;
string           EndLine;
string           FlatLine;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   //---- Check for possible errors
   if(counted_bars<0) return(-1);   
//---- Last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   
   int pos=Bars-counted_bars-1;
   
      //main calculation loop
      while(pos>=0)
         {
            if( TimeHour(Time[pos])==StartTime && TimeDayOfWeek(Time[pos])==StartDay )
            {
               ObjectCreate("StartLine",OBJ_VLINE,0,TimeHour(Time[pos]),0);
               ObjectSet("StartLine", OBJPROP_STYLE, STYLE_DASHDOT);
               ObjectSet("StartLine", OBJPROP_COLOR, StartColour);
            }
            if( TimeHour(Time[pos])==EndTime && TimeDayOfWeek(Time[pos])==EndDay )
            {
               ObjectCreate("EndLine",OBJ_VLINE,0,TimeHour(Time[pos]),0);
               ObjectSet("EndLine", OBJPROP_STYLE, STYLE_DASHDOT);
               ObjectSet("EndLine", OBJPROP_COLOR, EndColour);
            }
            pos--;
         }
    
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
Simon Gniadkowski
17661
Simon Gniadkowski  
Philsko:
Hi, I've just started learning to code mql4 over the last couple weeks and have gotten stuck with the first stage of the custom indicator & EA that I'm trying to code.

The first thing that I'm trying to get this to do is to create 2 vertical lines for each week, one on Monday 6.00, and one Friday 21.00. I want these lines to appear through from the beginning of the chart, to the current week.

This is what I have so far. When I attach it to the chart at the moment, nothing at all appears on the chart.

Any help & suggestions of changes I need to make would be most appreciated.

The most obvious thing to notice . . .  when you create your first set of lines they then get re-created (and that will cause an error) at the next week . . . .  if you want many lines then they need to have different names,  you can't have more than one line named  StartLine,  you could just increment an integer variable and use it with StartLine,  so StartLine0, StartLine1,  etc.

The reason you aren't seeing any lines is because your Time1  values ( TimeHour(Time[pos]) ) aren't time values at all,  look at the documentation for TimeHour() . . . it returns an int not a date time,  it can return a value between 0 and 23 ,  why aren't you just using Time[pos]  ?

Phil
29
Phil  
RaptorUK:

The most obvious thing to notice . . .  when you create your first set of lines they then get re-created (and that will cause an error) at the next week . . . .  if you want many lines then they need to have different names,  you can't have more than one line named  StartLine,  you could just increment an integer variable and use it with StartLine,  so StartLine0, StartLine1,  etc.

The reason you aren't seeing any lines is because your Time1  values ( TimeHour(Time[pos]) ) aren't time values at all,  look at the documentation for TimeHour() . . . it returns an int not a date time,  it can return a value between 0 and 23 ,  why aren't you just using Time[pos]  ?



Hi Raptor, thanks for your reply. I did wonder if the naming of the lines would be an issue. Will try to do something along the lines of what you've suggested with that in the morning!


On the Time1 issue, I see what you mean, I think. Would this be more what I need? I'm still not getting any lines but maybe that's because of the naming problem.


Thanks again for your help.

while(pos>=0)
         {
            if( TimeHour(Time[pos])==StartTime && TimeDayOfWeek(Time[pos])==StartDay )
            {
               ObjectCreate("StartLine",OBJ_VLINE,0,Time[pos],0);
               ObjectSet("StartLine", OBJPROP_STYLE, STYLE_DASHDOT);
               ObjectSet("StartLine", OBJPROP_COLOR, StartColour);
            }
            if( TimeHour(Time[pos])==EndTime && TimeDayOfWeek(Time[pos])==EndDay )
            {
               ObjectCreate("EndLine",OBJ_VLINE,0,Time[pos],0);
               ObjectSet("EndLine", OBJPROP_STYLE, STYLE_DASHDOT);
               ObjectSet("EndLine", OBJPROP_COLOR, EndColour);
            }
            pos--;
         }
Simon Gniadkowski
17661
Simon Gniadkowski  
Philsko:


Hi Raptor, thanks for your reply. I did wonder if the naming of the lines would be an issue. Will try to do something along the lines of what you've suggested with that in the morning!

On the Time1 issue, I see what you mean, I think. Would this be more what I need? I'm still not getting any lines but maybe that's because of the naming problem.


Even without fixing the names you should get 2 lines . . .  right click on yor chart and check the Objects,  see if your lines are there, if they are find where they have been placed,  that may give you some clues to what is wrong.
Simon Gniadkowski
17661
Simon Gniadkowski  
Add an index number to your line names and your code works,  at least, it does on my chart.
Phil
29
Phil  

I looked for the two lines in the object list as you suggested and they were there, but they were back in 1970.

I've been trying to add an index number to my lines this morning but can't seem to get it right. I've gone through a few ideas and currently have this.

It seems to be getting there as I can now see the two lines. They're in odd places though, the first one is where I wanted it to start, the first Monday of the chart, but the second one is a few months late. Again, maybe this will correct itself once the names are sorted.

Best

#property indicator_chart_window
//--- input parameters
extern int       StartDay=1;
extern int       EndDay=5;
extern int       StartTime=6;
extern int       EndTime=21;
extern color     StartColour=DarkOrange;
extern color     EndColour=OrangeRed;
extern color     FlatLineColour=Gold;

string           StartLineName="StartLine";
string           EndLineName="EndLine";
string           FlatLine;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   //---- Check for possible errors
   if(counted_bars<0) return(-1);   
//---- Last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   
   int pos=Bars-counted_bars-1;
   
      //main calculation loop

      while(pos>=0)
         {
            int LineNum=0;
            string StartName=StartLineName+LineNum;
            string EndName=EndLineName+LineNum;
   
            if( TimeHour(Time[pos])==StartTime && TimeDayOfWeek(Time[pos])==StartDay )
            {
               ObjectCreate(StartName,OBJ_VLINE,0,Time[pos],0);
               ObjectSet(StartName, OBJPROP_STYLE, STYLE_DASHDOT);
               ObjectSet(StartName, OBJPROP_COLOR, StartColour);
            }
            if( TimeHour(Time[pos])==EndTime && TimeDayOfWeek(Time[pos])==EndDay )
            {
               ObjectCreate(EndName,OBJ_VLINE,0,Time[pos],0);
               ObjectSet(EndName, OBJPROP_STYLE, STYLE_DASHDOT);
               ObjectSet(EndName, OBJPROP_COLOR, EndColour);
            }
            LineNum++;
            pos--;
         }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
Simon Gniadkowski
17661
Simon Gniadkowski  
Philsko:

I looked for the two lines in the object list as you suggested and they were there, but they were back in 1970.

I've been trying to add an index number to my lines this morning but can't seem to get it right. I've gone through a few ideas and currently have this.

It seems to be getting there as I can now see the two lines. They're in odd places though, the first one is where I wanted it to start, the first Monday of the chart, but the second one is a few months late. Again, maybe this will correct itself once the names are sorted.

Best

This is your problem . . .

int LineNum=0;

 for each bar you are initializing LineNum to 0,  move it to the beginning of your start() function and make it a static declaration.  (click the link and scroll down a little)

Phil
29
Phil  

Quality mate, working perfectly! Thank you very much for your speedy help.

Legend!

William Roeder
18310
William Roeder  

Using a counter, you can get multiple lines per bar on a chart refresh.

Instead time is unique and the code simpler.

ObjectCreate(StartName+Time[pos],OBJ_VLINE,0,Time[pos],0);
Phil
29
Phil  

Ahh yeah nice one, that is simpler. Two new tricks learnt! :-)

Cheers

To add comments, please log in or register