Download MetaTrader 5
To add comments, please log in or register
Do you know C++? MQL5 has much in common with it. Just try it yourself!
mar
1143
mar 2014.03.07 17:16 

Hi coders,

as mentioned in the headline, I developed a short script. Unfortunately it doesn't work at all. Any help is very appreciated!

int start()
  {
//----
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=MathMin(Bars-counted_bars,Bars-1);
   for(int i=limit; i>=0; i--)
   {
      datetime BarTime = iTime(NULL, PERIOD_D1, i);
      if (TimeDayOfWeek(BarTime) == 5)
      {
         if (!ObjectFind(Symbol()+BarTime)) ObjectCreate(Symbol()+BarTime, OBJ_VLINE, 0, BarTime,0);
      }
   }

//----
   return(0);
  }
Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2014.03.07 18:33  
mar:

Hi coders,

as mentioned in the headline, I developed a short script. Unfortunately it doesn't work at all. Any help is very appreciated!

What timeframe chart are you running it on ? your i in your loop will be relative to that timeframe . . . not D1
mar
1143
mar 2014.03.07 19:47  

Corrected it... Now it works. I had some errors in the header and I didn't know that IndicatorCounted() was always -1. Obviously it can't be used in a script.

int start()
  {
//----
   for(int i=1000; i>=0; i--)
   {
      datetime BarTime = iTime(NULL, PERIOD_D1, i);

      if (TimeDayOfWeek(BarTime) == 5)
      {
         ObjectCreate("OBJ"+BarTime, OBJ_VLINE, 0, BarTime,0);
      }
   }
//----
   return(0);
  }
mar
1143
mar 2014.03.07 20:10  

So, the last step is done. This script draws a trendline between Friday's close and Monday's Open. If it is a gap up, the line will be green and is the market gapping down it will be red.

Now I would like to go further. At the moment the trendlines are printed with absolute values and if the timeframe is changed, the trendlines stay according to the D1 timeframe. Would it be possible with an indicator that the trendlines will be adjusted when changing timeframes? When I change from D1 to H1 I want the trendline to be changed (or deleted and re-drawn) to the exact close of the last hourly candle to the very first open of Monday.

This is the script I have done so far:

int start()
  {
//----
   if (Period() != PERIOD_D1) return(0);
   ObjectsDeleteAll();
   for(int i=1000; i>=0; i--)
   {
      datetime BarTime1 = iTime(NULL, 0, i+1);
      datetime BarTime  = iTime(NULL, 0, i);
      if (TimeDayOfWeek(BarTime1) == 5)
      {
         double FridayClose = iClose(NULL, 0, i+1);
         double MondayOpen = iOpen(NULL, 0, i);
         color col = Red;
         if (MondayOpen > FridayClose) col = Lime;
         ObjectCreate("OBJ"+BarTime, OBJ_TREND, 0, BarTime1, FridayClose, BarTime, MondayOpen);
         ObjectSet("OBJ"+BarTime, OBJPROP_RAY, false);
         ObjectSet("OBJ"+BarTime, OBJPROP_COLOR, col);
         ObjectSet("OBJ"+BarTime, OBJPROP_WIDTH, 2);
      }
   }
//----
   return(0);
  }
Keith Watford
Moderator
7904
Keith Watford 2014.03.08 01:17  
mar:

So, the last step is done. This script draws a trendline between Friday's close and Monday's Open. If it is a gap up, the line will be green and is the market gapping down it will be red.

Now I would like to go further. At the moment the trendlines are printed with absolute values and if the timeframe is changed, the trendlines stay according to the D1 timeframe. Would it be possible with an indicator that the trendlines will be adjusted when changing timeframes? When I change from D1 to H1 I want the trendline to be changed (or deleted and re-drawn) to the exact close of the last hourly candle to the very first open of Monday.

This is the script I have done so far:


Take a look at this.

//if (Period() != PERIOD_D1) return(0);
   ObjectsDeleteAll();
   int days_to_go_back = 100;
   datetime d_start[];
   ArrayResize( d_start,days_to_go_back);
   for(int i=0;i<days_to_go_back;i++)
      {
      d_start[i] = iTime(NULL, PERIOD_D1, i);
      }
   int d_index = 1;
   while( d_index < days_to_go_back )
      {
      if (TimeDayOfWeek(d_start[d_index]) == 5)
         {
         double FridayClose = iClose(NULL, PERIOD_D1, d_index);
         double MondayOpen = iOpen(NULL, PERIOD_D1,d_index -1 );
         datetime BarTime1 = d_start[d_index -1]-1;
         datetime BarTime2 = d_start[d_index -1];
         
         color col = Red;
         if (MondayOpen > FridayClose) col = Lime;
         ObjectCreate("OBJ"+BarTime1, OBJ_TREND, 0, BarTime1, FridayClose, BarTime2, MondayOpen);
         ObjectSet("OBJ"+BarTime1, OBJPROP_RAY, false);
         ObjectSet("OBJ"+BarTime1, OBJPROP_COLOR, col);
         ObjectSet("OBJ"+BarTime1, OBJPROP_WIDTH, 2);
         
         }
      d_index++;
      }
      

You should really change your code to avoid using ObjectsDeleteAll()

mar
1143
mar 2014.03.08 09:49  

Hello GumRai,

thank you so much! This is really working great! At the moment I do not really know how you did it that the trendlines are always at the correct place in every timeframe but I will study it and learn from it!

Keith Watford
Moderator
7904
Keith Watford 2014.03.08 10:19  
mar:

Hello GumRai,

thank you so much! This is really working great! At the moment I do not really know how you did it that the trendlines are always at the correct place in every timeframe but I will study it and learn from it!


You're welcome.

Of course, it would still need some additional code

ie

days_to_go_back must be less than the bars on the D1 chart, or it will try to get values from an index that doesn't exist and that could cause a critical error.

Similarly, it may be an idea to add an additional line of code to prevent it from trying to place objects on bars that don't exist in the smaller time-frames. After all, the daily chart probably goes further back in history than the minute chart.

         datetime BarTime1 = d_start[d_index -1]-1;
         datetime BarTime2 = d_start[d_index -1];
         if(BarTime1 < Time[Bars-1] )     //if the time is earlier than the earliest bar on the current chart, stop the loop.
            break;
mar
1143
mar 2014.03.10 07:51  

Hi GumRai,

thank you very much for your additional explanation! That all makes sense to me. The idea of Time[Bars-1] is something really new. Of course I know Time and I know Bars for themselves but I never thought about using it together.. You see, I am still stuck in the beginner's state.. ;)

My next "project" should be a kind of dashboard where all pairs are shown with their weekend gap in pips and percentage. So you can have a quick look at all gaps. But after a visual backtest I am not really sure anymore having found an interesting way to trade the gaps... Mostly gaps are too small and sometimes they get filled, sometimes not. Doesn't seem to be a good trading idea. But anyway I learned a piece of coding...

/
To add comments, please log in or register