Download MetaTrader 5

Problem extending Market Session Open lines over the weekend

To add comments, please log in or register
Carla
16
Carla  

Hello,

I am novice coder and trying to modify market session indicator (by Adam Jowett).

My intended modification was to draw market session open line for 48hrs. But having problem if Open is Friday lines and 48hrs includes weekend.

below is what I wrote - if Friday, extend line for 72hrs. but this code affects all weekdays.

if (TimeDayOfWeek(currentTime)==5)sessionLengthb = Period() * 60 * ((60 / Period()) * 72);


Your advice is greatly appreciated.

Carla



for reference below is other part of codes

int init()
{
   sessionLength = Period() * 60 * ((60 / Period()) * 48);//length of extention - 48hours
   sessionLengthb = Period() * 60 * ((60 / Period()) * 24);
   
   sydneyHour = getHour(sydney);
   tokyoHour = getHour(tokyo);
   newyorkHour = getHour(newyork);
   londonHour = getHour(london);
   
   sydneyMinute = getMinute(sydney);
   tokyoMinute = getMinute(tokyo);
   londonMinute = getMinute(london);
   nyMinute = getMinute(newyork);
   
   if (sydneyHour > -1)
   {
      sydneyHour = sydneyHour + gmtServerOffset;
      if (sydneyHour > 23) sydneyHour = sydneyHour - 24;
      if (sydneyHour < 0) sydneyHour = sydneyHour + 24;
   }
   
   if (tokyoHour > -1)
   {
      tokyoHour = tokyoHour + gmtServerOffset;
      if (tokyoHour > 23) tokyoHour = tokyoHour - 24;
      if (tokyoHour < 0) tokyoHour = tokyoHour + 24;
   }
   
   if (londonHour > -1)
   {
      londonHour = londonHour + gmtServerOffset;
      if (londonHour > 23) londonHour = londonHour - 24;
      if (londonHour < 0) londonHour = londonHour + 24;
   }
   
   if (newyorkHour > -1)
   {
      newyorkHour = newyorkHour + gmtServerOffset;
      if (newyorkHour > 23) newyorkHour = newyorkHour - 24;
      if (newyorkHour < 0) newyorkHour = newyorkHour + 24;
   }
   
   return(0);
}


int start()
{
   if (Bars <= 10) return(0);
   
   if (Period() > maxTimeframe) {
      clearScreen(prefix, true, NULL);
      Print("Market Sessions will only show on " + maxTimeframe + " or lower");
      return(0);
   }

   int counted = IndicatorCounted();
   if (counted < 0) return(-1);
   if (counted > 0) counted--;
   
   int i = Bars - counted;
   int timeRange = 0;
   double rangeHigh = 0;
   double rangeLow = 0;
   
   int days = 0;
   int sessions = 0;
    
   while (i > 0)
   {
      int currentTime = Time[i];
      int dayNumber = TimeDayOfWeek(currentTime) + 1;
      string offsetLabel = (gmtServerOffset >= 0) ? "+" + gmtServerOffset : gmtServerOffset;
      
      string sydneyLabel = "Sydney open  [" + sydney + " " + offsetLabel + "]";
      string tokyoLabel = "Tokyo  [" + tokyo + " " + offsetLabel + "]";
      string londonLabel = "London  [" + london + " " + offsetLabel + "]";
      string newYorkLabel = "New York  [" + newyork + " " + offsetLabel + "]";
      
      if (TimeHour(currentTime) == sydneyHour && TimeMinute(currentTime) == sydneyMinute)
      {
         ObjectDelete(prefix + "future_line" + lastSydneySession);
         
         lastSydneySession = currentTime;
        if (TimeDayOfWeek(currentTime)==5)sessionLengthb = Period() * 60 * ((60 / Period()) * 72);
       //  drawVerticalLine(prefix + "sydneyTimeOpen"+i, currentTime, 1, sydneyColour, lineStyle, true, sydneyLabel);
         if (showOpenPrice) {drawTrendLine(prefix +"sydneyopen" + i, currentTime, currentTime + sessionLengthb, Open[i], Open[i], openwidth, sydneyColour, lineStyle, true, false, DoubleToStr(Open[i], Digits));}
                
      if (showOpenPrice) {drawTrendLine(prefix +"zydneyopen" + i, currentTime, currentTime + sessionLengthb, Open[i], Open[i], openwidth, sydneyColour,lineStyle, true, false, DoubleToStr(Open[i], Digits));
          drawTrendLine(prefix +"zydneyopenb" + i, currentTime, currentTime + sessionLength, Open[i], Open[i], 0, sydneyColour,lineStyle, true, false, DoubleToStr(Open[i], Digits));}
                              
 
        if (showPreviousRange) {
            timeRange = iBarShift(NULL, 0, lastNewYorkSession) - iBarShift(NULL, 0, currentTime) - 1;
            rangeHigh = High[iHighest(NULL, 0, MODE_HIGH, timeRange, i + 1)];
            rangeLow = Low[iLowest(NULL, 0, MODE_LOW, timeRange, i + 1)];
            
            drawTrendLine(prefix + "nyRangeHigh" + i, currentTime, currentTime + sessionLengthb, rangeHigh, rangeHigh, 1,newYorkColour, rangestyle, true, false, "");
            drawTrendLine(prefix + "nyRangeLow" + i, currentTime, currentTime + sessionLengthb, rangeLow, rangeLow, 1,newYorkColour, rangestyle, true, false, ""); 
               
     
       }
      } 
      else if (TimeHour(currentTime) == tokyoHour && TimeMinute(currentTime) == tokyoMinute)
      {
         ObjectDelete(prefix + "future_line" + lastTokyoSession);
         lastTokyoSession = currentTime;

       //  drawVerticalLine(prefix + "line"+i, currentTime, 1, tokyoColour, lineStyle, true, tokyoLabel);
         if (showOpenPrice) drawTrendLine(prefix +"tokyoopen" + i, currentTime, currentTime + sessionLength, Open[i], Open[i], 1, tokyoColour, lineStyle, true, false, DoubleToStr(Open[i], Digits));
      }
      else if (TimeHour(currentTime) == londonHour && TimeMinute(currentTime) == londonMinute)
      {
         ObjectDelete(prefix + "future_line" + lastLondonSession);
         lastLondonSession = currentTime;
         
        // drawVerticalLine(prefix + "line"+i, currentTime, 1, londonColour, lineStyle, true, londonLabel);
         if (showOpenPrice) {drawTrendLine(prefix +"londonoopen" + i, currentTime, currentTime + sessionLengthb, Open[i], Open[i], openwidth, londonColour,lineStyle, true, false, DoubleToStr(Open[i], Digits));
          drawTrendLine(prefix +"londonoopenb" + i, currentTime, currentTime + sessionLength, Open[i], Open[i], 0, londonColour,lineStyle, true, false, DoubleToStr(Open[i], Digits));}
                                    
   
         
         if (showPreviousRange) {
            timeRange = iBarShift(NULL, 0, lastSydneySession) - iBarShift(NULL, 0, currentTime) - 1;
            rangeHigh = High[iHighest(NULL, 0, MODE_HIGH, timeRange, i + 1)];
            rangeLow = Low[iLowest(NULL, 0, MODE_LOW, timeRange, i + 1)];
            
            drawTrendLine(prefix + "asianRangeHigh" + i, currentTime, currentTime + sessionLengthb, rangeHigh, rangeHigh, 1, sydneyColour, rangestyle, true, false, "");
            drawTrendLine(prefix + "asianRangeLow" + i, currentTime, currentTime + sessionLengthb, rangeLow,rangeLow, 1,sydneyColour, rangestyle, true, false, ""); 
         }
      }
      else if (TimeHour(currentTime) == newyorkHour && TimeMinute(currentTime) == nyMinute)
      {
         ObjectDelete(prefix + "future_line" + lastNewYorkSession);
         lastNewYorkSession = currentTime;
 
        // drawVerticalLine(prefix + "line"+i, currentTime, 1, newYorkColour, lineStyle, true, newYorkLabel);
         if (showOpenPrice) {drawTrendLine(prefix +"newyorkopen" + i, currentTime, currentTime + sessionLengthb, Open[i], Open[i], openwidth, newYorkColour,lineStyle, true, false, DoubleToStr(Open[i], Digits));
          drawTrendLine(prefix +"newyorkopenb" + i, currentTime, currentTime + sessionLength, Open[i], Open[i], 0, newYorkColour,lineStyle, true, false, DoubleToStr(Open[i], Digits));}
        
         if (showPreviousRange) {
            timeRange = iBarShift(NULL, 0, lastLondonSession) - iBarShift(NULL, 0, currentTime) - 1;
            rangeHigh = High[iHighest(NULL, 0, MODE_HIGH, timeRange, i + 1)];
            rangeLow = Low[iLowest(NULL, 0, MODE_LOW, timeRange, i + 1)];
            
            drawTrendLine(prefix + "ukRangeHigh" + i, currentTime, currentTime + sessionLengthb, rangeHigh, rangeHigh, 1, londonColour, rangestyle, true, false, "");
            drawTrendLine(prefix + "ukRangeLow" + i, currentTime, currentTime + sessionLengthb, rangeLow, rangeLow, 1, londonColour, rangestyle, true, false, ""); 
         }
      }
         
      if (i == 1) {
         if (showFutureSessions) {
            int project = (TimeDayOfWeek(lastNewYorkSession) == 5 || TimeDayOfWeek(lastSydneySession) == 5) ? oneDay * 3 : oneDay;
            
            drawFutureSession(lastSydneySession + project, sydneyLabel);
            drawFutureSession(lastTokyoSession + project, tokyoLabel);
            drawFutureSession(lastLondonSession + project, londonLabel);
            drawFutureSession(lastNewYorkSession + project, newYorkLabel);   
         }
      }  
      
      i--;
   }
   
   return(0);
}

int getHour (string timeString) {
   string result[];
   string needle = ":";
   string u_needle = StringGetCharacter(needle,0);
   int k = StringSplit(timeString, u_needle, result);
   
   if (k > 0) return result[0];
   
   return -1;
}

int getMinute (string timeString) {
   string result[];
   string needle = ":";
   string u_needle = StringGetCharacter(needle,0);
   int k = StringSplit(timeString, u_needle, result);
   
   if (k > 0) {
      if (StringLen(result[1]) > 0 ) {
         return result[1];
      }
   }
   
   return 0;
}

void OnChartEvent(const int CHARTEVENT_MOUSE_MOVE, const long& lparam, const double& dparam, const string& sparam)
{
   int i;
   string name = "";
   
   double priceMin = ChartGetDouble(currentChart, CHART_PRICE_MIN);

   for (i = ObjectsTotal(); i >= 0; i--) 
   {
      name = ObjectName(i);
      if (StringFind(name, "_time_label", 0) > -1) 
      {
         ObjectSet(name, OBJPROP_PRICE1, priceMin);
      } 
   }
}
To add comments, please log in or register