Help! - Script to draw a vertical line on every daily outside bar. Why is this not working?

 

Hi I am trying to draw a vertical line on every outside bar on the daily timeframe. At the moment I'm very close, but suffering from glitches where it draws the lines everywhere, many not on outside bars.


Please advise. Thank you! 


//+------------------------------------------------------------------+
//|                                           Outside Bar Finder.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#include <stdlib.mqh>
static int BarTime;
   

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()

   {
   return(INIT_SUCCEEDED);
   }
   
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
   {
   for(int i=MathMax(Bars-2-IndicatorCounted(),1);i>=0;i--)
   {
      double hi = iHigh(NULL, PERIOD_D1,i);
      double lo = iLow(NULL, PERIOD_D1,i);
      double clos = iClose(NULL, PERIOD_D1,i);      
      
      

      
      double prevclos=iClose(NULL, PERIOD_D1,i+1);
      double prevhi=iHigh(NULL, PERIOD_D1,i+1);
      double prevlo=iLow(NULL, PERIOD_D1,i+1);
      
   if((hi>prevhi&&lo<prevlo&&clos<prevlo&&clos<prevclos)||(hi>prevhi&&lo<prevlo&&clos>prevhi&&clos>prevclos))
      {
      BarTime = Time[i];
          ObjectCreate(0, "Outside Bar" + Time[i], OBJ_VLINE, 0, Time[i], 0);
          ObjectSetInteger(0, "Outside Bar" + Time[i], OBJPROP_COLOR, clrRed);
          ObjectSetInteger(0, "Outside Bar" + Time[i], OBJPROP_BACK, true);
          ObjectSetInteger(0, "Outside Bar" + Time[i],OBJPROP_TIMEFRAMES,OBJ_PERIOD_D1);
      }
   }
   return(rates_total);
}
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019.05.06)
              Messages Editor

  2. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016.05.11)

  3. Change the style to histogram.

  4. In MT4, buffers and the predefined arrays are all ordered AsSeries. There is a difference between the arrays passed to OnCalculate (e.g. low[]) and the MT4 predefined variables (e.g. Low[].) The passed arrays have no default direction, just like MT5.

    To determine the indexing direction of time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[], call ArrayGetAsSeries(). In order not to depend on default values, you should unconditionally call the ArraySetAsSeries() function for those arrays, which are expected to work with.predefined variables (e.g. Low[].) The passed arrays have no default direction, just like MT5.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference

 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019.05.06)
              Messages Editor

  2. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016.05.11)

  3. Change the style to histogram.

  4. In MT4, buffers and the predefined arrays are all ordered AsSeries. There is a difference between the arrays passed to OnCalculate (e.g. low[]) and the MT4 predefined variables (e.g. Low[].) The passed arrays have no default direction, just like MT5.

Thank you very much mate. I have adapted the code to the above edited one and I will try to understand what you've posted. Unfortunately at the moment it is still having the same problem as before where it goes haywire in the recent history and draws lines everywhere. Please help me! Thank you! 

Files:
OB.JPG  260 kb
 
  1. Now you've changed your original post, thus invalidating subsequent posts.
  2.    for(int i=MathMax(Bars-2-IndicatorCounted(),1);i>=0;i--)
       {
          double hi = iHigh(NULL, PERIOD_D1,i);

    You are mixing apples and oranges.

  3. How do you expect to show an outside D1 candle on lower timeframes?
 
William Roeder:
  1. Now you've changed your original post, thus invalidating subsequent posts.
  2. You are mixing apples and oranges.

  3. How do you expect to show an outside D1 candle on lower timeframes?

Hi I'm sorry but I just don't have the coding knowledge to understand what you're hinting at. Thank you very much for the reply regardless, I really appreciate it. I'll try and work it out. I only want the vertical line to appear on the D1 only, and only if that D1 is an outside bar.