//+------------------------------------------------------------------+
//|                                            inv_has_quick.mq4     |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "public domain"
#property link      "http://www.forexfactory.com/showthread.php?t=325018"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 5
#property indicator_buffers 6
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_color3 Lime
#property indicator_color4 Red
#property indicator_color5 Lime
#property indicator_color6 Red

extern string c0 = "This indicator works for historical data (99.9% accurate). It doesnt repaint bars. It does all the filtering but news.";
extern string c1 = "Your broker offset from UTC time (aka GMT offset).";
extern string c2 = "Tracking DST and adjusting this UTC shift is your responsibility!";
extern int    BrokerOffsetFromUTC = 1;
extern string c3 = "Trading hours in UTC.";
extern int    BeginTradingHour = 7;
extern int    EndTradingHour = 17;

extern string c4 = "Filter by RmUp and RmDn";
extern bool   RmFilter = true;
extern double RoomLimitRatio = 0.2;

extern string c5 = "Filter by ATR. Questionable idea.";
extern bool   ATRFilter = true;
extern int    ATR1_Prd = 5;
extern int    ATR2_Prd = 25;

extern string c6 = "Filter by pivot levels.";
extern bool   PivotFilter = true;
extern string c7 = "Minimum distance to nearest pivot in the direction of the proposed trade (five-digit broker will be auto-detected)";
extern int    MinPivotDistance = 10;
extern string c8 = "Filter by round price levels (very similar to pivot levels filter)";
extern bool   RoundNumberFilter = true;

extern string c9 = "Other stuff.";
extern bool   AlertOn = false;
extern bool   DeleteAllArrows = true;
extern bool   ExcludeSundayData = true;
extern int    MaxBars = 1000;
extern int    ArrowSpacing = 20;


datetime PrevTime;

int BarWidth = 0;
color BarColorUp = Lime;
color BarColorDown = Red;
color TextColor = White;


double buf3_up[];
double buf3_down[];
double buf2_up[];
double buf2_down[];
double buf1_up[];
double buf1_down[];

double PrevHaOpen1, PrevHaOpen2, PrevHaOpen3, PrevHaClose1, PrevHaClose2, PrevHaClose3, PrevHaHigh1, PrevHaHigh2, PrevHaHigh3, PrevHaLow1, PrevHaLow2, PrevHaLow3;
double HaOpen1, HaOpen2, HaOpen3, HaClose1, HaClose2, HaClose3, HaHigh1, HaHigh2, HaHigh3, HaLow1, HaLow2, HaLow3;
int i2, i3, prev_i2, prev_i3;

string shortname = "";

int ArrSize = 110;
int UniqueNum = 2282;
int Period1, Period2, Period3;

color TradeableColor = Lime;
color UntradeableColor = Red;
color OtherColor = Yellow;
int DisplayPosition = 0;

double point;
int LastBars0 = 0;
int DisplayCorner;
int x,y;

bool OkToTrade;

double yHigh, yLow, tOpen, yClose;
int yStart;
double p, q;
double r1, r2, r3, r4, r5, s1, s2, s3, s4, s5;


int init()
{
  Comment(" ");

  PrevTime = 0;
  
  BeginTradingHour += BrokerOffsetFromUTC;
  EndTradingHour += BrokerOffsetFromUTC;
  
  SetIndexStyle(0, DRAW_ARROW, 0, BarWidth,BarColorUp);
  SetIndexArrow(0, ArrSize);
  SetIndexBuffer(0, buf1_up);
  SetIndexEmptyValue(0, 0.0);
  SetIndexStyle(1, DRAW_ARROW, 0, BarWidth, BarColorDown);
  SetIndexArrow(1, ArrSize);
  SetIndexBuffer(1, buf1_down);
  SetIndexEmptyValue(1, 0.0);
  SetIndexStyle(2, DRAW_ARROW, 0, BarWidth, BarColorUp);
  SetIndexArrow(2, ArrSize);
  SetIndexBuffer(2, buf2_up);
  SetIndexEmptyValue(2, 0.0);
  SetIndexStyle(3, DRAW_ARROW, 0, BarWidth, BarColorDown);
  SetIndexArrow(3, ArrSize);
  SetIndexBuffer(3, buf2_down);
  SetIndexEmptyValue(3, 0.0);
  SetIndexStyle(4, DRAW_ARROW, 0, BarWidth, BarColorUp);
  SetIndexArrow(4, ArrSize);
  SetIndexBuffer(4, buf3_up);
  SetIndexEmptyValue(4, 0.0);
  SetIndexStyle(5, DRAW_ARROW, 0, BarWidth, BarColorDown);
  SetIndexArrow(5, ArrSize);
  SetIndexBuffer(5, buf3_down);
  SetIndexEmptyValue(5, 0.0);
  
  SetIndexLabel(0, NULL);
  SetIndexLabel(1, NULL);
  SetIndexLabel(2, NULL);
  SetIndexLabel(3, NULL);
  SetIndexLabel(4, NULL);
  SetIndexLabel(5, NULL);
   
  IndicatorDigits(0);
   
  getPeriod();
   
  shortname = "inv_has_quick";
  IndicatorShortName(shortname);
   
  point = Point;
  if (Digits % 2 == 1) point *= 10;
   
  if (DisplayPosition == 0) {DisplayCorner = 0; x = 0; y = 0;} else {DisplayCorner = 2; x = -215; y = 1;}
  if (ATRFilter)
  {
    ObjectCreate("xADR1", OBJ_LABEL, 0, 0, 1);
    ObjectSet("xADR1", OBJPROP_CORNER, DisplayCorner);
    ObjectSet("xADR1", OBJPROP_XDISTANCE, 255 + x);
    ObjectSet("xADR1", OBJPROP_YDISTANCE, y);
  }
  
  if (RmFilter)
  { 
    ObjectCreate("xADR2", OBJ_LABEL, 0, 0, 1);
    ObjectSet("xADR2", OBJPROP_CORNER, DisplayCorner);
    ObjectSet("xADR2", OBJPROP_XDISTANCE, 540 + x);
    ObjectSet("xADR2", OBJPROP_YDISTANCE, y);
   
    ObjectCreate("xADR3", OBJ_LABEL, 0, 0, 1);
    ObjectSet("xADR3", OBJPROP_CORNER, DisplayCorner);
    ObjectSet("xADR3", OBJPROP_XDISTANCE, 360 + x);
    ObjectSet("xADR3", OBJPROP_YDISTANCE, y);
   
    ObjectCreate("xADR4", OBJ_LABEL, 0, 0, 1);
    ObjectSet("xADR4", OBJPROP_CORNER, DisplayCorner);
    ObjectSet("xADR4", OBJPROP_XDISTANCE, 405 + x);
    ObjectSet("xADR4", OBJPROP_YDISTANCE, y);
   
    ObjectCreate("xADR5", OBJ_LABEL, 0, 0, 1);
    ObjectSet("xADR5", OBJPROP_CORNER, DisplayCorner);
    ObjectSet("xADR5", OBJPROP_XDISTANCE, 450 + x);
    ObjectSet("xADR5", OBJPROP_YDISTANCE, y);
  }
   
  return(0);
}
   
int deinit()
{
   if (DeleteAllArrows) ObjectsDeleteAll(0, 22);
   
   if (ATRFilter) ObjectDelete("xADR1");
   if (RmFilter) {ObjectDelete("xADR2"); ObjectDelete("xADR3"); ObjectDelete("xADR4"); ObjectDelete("xADR5");}
   
   return(0);
}

int start()
{
  if (Period() > 15) return(-1);
  if (Bars <= MaxBars * 2) return(0);

  int i, j, start;
  int atr1, atr2;
  int n, rng;
  double s;
  int adr1, adr5, adr10, adr20, adr;
  int RmUp, RmDn, RmLmt;
  double dif;
  double gp;
  string txt, name;
  double tHigh, tLow;
  
  if (PrevTime == 0)
  {
    start = iBarShift(NULL, 0, iTime(NULL, Period3, iBarShift(NULL, Period3, Time[MaxBars * 2 - Period3 / Period1 - 2])));
    i = start; i2 = iBarShift(NULL, Period2, Time[i]); i3 = iBarShift(NULL, Period3, Time[i]);
    prev_i2 = i2; prev_i3 = i3;
    PrevHaOpen1 = Open[i]; PrevHaOpen2 = iOpen(NULL, Period2, i2); PrevHaOpen3 = iOpen(NULL, Period3, i3);
    PrevHaClose1 = Close[i]; PrevHaClose2 = iClose(NULL, Period2, i2); PrevHaClose3 = iClose(NULL, Period3, i3);
    i--;
    for(; i >= start - MaxBars; i--)
    {
      // MTF HA calculations
      // Period1 - can calculate straigh away
      PrevHaOpen1 = (PrevHaOpen1 + PrevHaClose1) * 0.5;
      PrevHaClose1 = (Open[i] + High[i] + Low[i] + Close[i]) * 0.25;
      // Period2 - carefully written code to avoid looking into the future (repainting)
      i2 = iBarShift(NULL, Period2, Time[i]);
      if (prev_i2 != i2)
      {
        prev_i2 = i2;
        PrevHaOpen2 = (PrevHaOpen2 + PrevHaClose2) * 0.5;
      }
      j = iBarShift(NULL, 0, iTime(NULL, Period2, i2));
      PrevHaClose2 = Open[j] + Close[i];
      PrevHaHigh2 = High[j]; PrevHaLow2 = Low[j];
      j--;
      for (; j >= i; j--)
      {
        PrevHaHigh2 = MathMax(PrevHaHigh2, High[j]);
        PrevHaLow2 = MathMin(PrevHaLow2, Low[j]);
      }
      PrevHaClose2 = (PrevHaClose2 + PrevHaHigh2 + PrevHaLow2) * 0.25;
      // Period3 - carefully written code to avoid looking into the future (repainting)
      i3 = iBarShift(NULL, Period3, Time[i]);
      if (prev_i3 != i3)
      {
        prev_i3 = i3;
        PrevHaOpen3 = (PrevHaOpen3 + PrevHaClose3) * 0.5;
      }
      j = iBarShift(NULL, 0, iTime(NULL, Period3, i3));
      PrevHaClose3 = Open[j] + Close[i];
      PrevHaHigh3 = High[j]; PrevHaLow3 = Low[j];
      j--;
      for (; j >= i; j--)
      {
        PrevHaHigh3 = MathMax(PrevHaHigh3, High[j]);
        PrevHaLow3 = MathMin(PrevHaLow3, Low[j]);
      }
      PrevHaClose3 = (PrevHaClose3 + PrevHaHigh3 + PrevHaLow3) * 0.25;
      // MTF HA finished    
    }
    PrevTime = Time[start - MaxBars];
    // now PrevHaOpen1,2,3 and PrevHaClose1,2,3 are calculated on bar with open time PrevTime
  }
  
  dif = Time[0] - Time[1];
  for(i = ObjectsTotal() - 1; i > -1; i--)
  {
     if (StringFind(ObjectName(i), "FF_" + UniqueNum + "_") >= 0)  ObjectDelete(ObjectName(i));
  }

  // period 1
  txt = tf2txt(Period1); gp = 1.5;
  name = "FF_" + UniqueNum + "_1_" + txt;
  ObjectCreate(name, OBJ_TEXT, WindowFind(shortname), iTime(NULL, 0, 0) + dif * 3, gp);
  ObjectSetText(name, txt, 8, "Arial", TextColor);
  // period 2
  txt = tf2txt(Period2); gp = 2.5;
  name = "FF_" + UniqueNum + "_2_" + txt;
  ObjectCreate(name, OBJ_TEXT, WindowFind(shortname), iTime(NULL, 0, 0) + dif * 3, gp);
  ObjectSetText(name, txt, 8, "Arial", TextColor);
  // period 3
  txt = tf2txt(Period3); gp = 3.5;
  name = "FF_" + UniqueNum + "_3_" + txt;
  ObjectCreate(name, OBJ_TEXT, WindowFind(shortname), iTime(NULL, 0, 0) + dif * 3, gp);
  ObjectSetText(name, txt, 8, "Arial", TextColor);

  // calculations (quite complicated becase we want to avoid higher-timeframe bar repainting issue)
  i = iBarShift(NULL, 0, PrevTime); i2 = iBarShift(NULL, Period2, Time[i]); i3 = iBarShift(NULL, Period3, Time[i]);
  prev_i2 = i2; prev_i3 = i3;
  HaOpen1 = PrevHaOpen1; HaOpen2 = PrevHaOpen2; HaOpen3 = PrevHaOpen3; HaClose1 = PrevHaClose1; HaClose2 = PrevHaClose2; HaClose3 = PrevHaClose3;
  i--;
  for(; i >= 0; i--)
  {
    PrevTime = Time[i + 1]; PrevHaOpen1 = HaOpen1; PrevHaOpen2 = HaOpen2; PrevHaOpen3 = HaOpen3; PrevHaClose1 = HaClose1; PrevHaClose2 = HaClose2; PrevHaClose3 = HaClose3;    
    if ((TimeHour(Time[i]) >= BeginTradingHour) && (TimeHour(Time[i]) < EndTradingHour)) {OkToTrade = true;} else {OkToTrade = false;}

    // MTF HA calculations
    // Period1 - can calculate straight away
    HaOpen1 = (HaOpen1 + HaClose1) * 0.5;
    HaClose1 = (Open[i] + High[i] + Low[i] + Close[i]) * 0.25;
    HaHigh1 = MathMax(High[i], MathMax(HaOpen1, HaClose1));
    HaLow1 = MathMin(Low[i], MathMin(HaOpen1, HaClose1));
    buf1_up[i] = 1 * (HaOpen1 < HaClose1); buf1_down[i] = 1 * (HaOpen1 > HaClose1);
    // Period2 - carefully written code to avoid looking into the future (repainting)
    i2 = iBarShift(NULL, Period2, Time[i]);
    if (prev_i2 != i2)
    {
      prev_i2 = i2;
      HaOpen2 = (HaOpen2 + HaClose2) * 0.5;
    }
    j = iBarShift(NULL, 0, iTime(NULL, Period2, i2));
    HaClose2 = Open[j] + Close[i];
    HaHigh2 = High[j]; HaLow2 = Low[j];
    j--;
    for (; j >= i; j--)
    {
      HaHigh2 = MathMax(HaHigh2, High[j]);
      HaLow2 = MathMin(HaLow2, Low[j]);
    }
    HaClose2 = (HaClose2 + HaHigh2 + HaLow2) * 0.25;
    HaHigh2 = MathMax(HaHigh2, MathMax(HaOpen2, HaClose2));
    HaLow2 = MathMin(HaLow2, MathMin(HaOpen2, HaClose2));
    buf2_up[i] = 2 * (HaOpen2 < HaClose2); buf2_down[i] = 2 * (HaOpen2 > HaClose2);
    // Period3 - carefully written code to avoid looking into the future (repainting)
    i3 = iBarShift(NULL, Period3, Time[i]);
    if (prev_i3 != i3)
    {
      prev_i3 = i3;
      HaOpen3 = (HaOpen3 + HaClose3) * 0.5;
    }
    j = iBarShift(NULL, 0, iTime(NULL, Period3, i3));
    HaClose3 = Open[j] + Close[i];
    HaHigh3 = High[j]; HaLow3 = Low[j];
    j--;
    for (; j >= i; j--)
    {
      HaHigh3 = MathMax(HaHigh3, High[j]);
      HaLow3 = MathMin(HaLow3, Low[j]);
    }
    HaClose3 = (HaClose3 + HaHigh3 + HaLow3) * 0.25;
    HaHigh3 = MathMax(HaHigh3, MathMax(HaOpen3, HaClose3));
    HaLow3 = MathMin(HaLow3, MathMin(HaOpen3, HaClose3));
    buf3_up[i] = 3 * (HaOpen3 < HaClose3); buf3_down[i] = 3 * (HaOpen3 > HaClose3);    
    // MTF HA finished    
    // again, now PrevHaOpen1,2,3 and PrevHaClose1,2,3 are calculated on bar with open time PrevTime
    
    string text; color Color;
    int StartOfDay = iBarShift(NULL, 0, iTime(NULL, PERIOD_D1, iBarShift(NULL, PERIOD_D1, Time[i]))) - BrokerOffsetFromUTC * (PERIOD_H1 / Period1);
    if (StartOfDay < i) StartOfDay = iBarShift(NULL, 0, iTime(NULL, PERIOD_D1, iBarShift(NULL, PERIOD_D1, Time[i]) + 1)) - BrokerOffsetFromUTC * (PERIOD_H1 / Period1);

    if (ATRFilter)
    {
      // ATR
      atr1 = MathRound(iATR(NULL, 0, ATR1_Prd, i + 1) / point);
      atr2 = MathRound(iATR(NULL, 0, ATR2_Prd, i + 1) / point);
      text = "ATR" + ATR1_Prd + "=" + atr1 + "  ATR" + ATR2_Prd + "=" + atr2;
      if (atr1 > atr2) 
        Color = TradeableColor; 
      else 
        Color = UntradeableColor; 
      ObjectSetText("xADR1", text, 8, "Arial Narrow", Color);
    }
    
    if (RmFilter)
    {
      // ADR
      n = 1;
      s = 0.0;
      for (rng = 1; rng <= 20; rng++)
      {
        while (ExcludeSundayData && (TimeDayOfWeek(iTime(NULL, PERIOD_D1, i + n)) == 0)) n++;
        s = s + (iHigh(NULL, PERIOD_D1, i + n) - iLow(NULL, PERIOD_D1, i + n)) / point;
        if (rng == 1) adr1 = MathRound(s);
        if (rng == 5) adr5 = MathRound(s / 5);
        if (rng == 10) adr10 = MathRound(s / 10);
        if (rng == 20) adr20 = MathRound(s / 20);
        n++;
      }
      adr = MathRound((adr1 + adr5 + adr10 + adr20) / 4);
      text= "ADR=" + adr + "  1d=" + adr1 + "  5d=" + adr5 + "  10d=" + adr10 + "  20d=" + adr20;
      ObjectSetText("xADR2", text, 8, "Arial Narrow", OtherColor);

      // Range and Room
      tHigh = High[StartOfDay]; tLow = Low[StartOfDay];
      for (j = StartOfDay - 1; j >= i; j--)
      {
        tHigh = MathMax(tHigh, High[j]);
        tLow = MathMin(tLow, Low[j]);
      }
      RmUp = MathRound(adr - (Close[i] - tLow) / point);
      RmDn = MathRound(adr - (tHigh - Close[i]) / point);
      RmLmt = MathRound(adr * RoomLimitRatio);

      text = "RmUp:" + RmUp;
      if (RmUp > RmLmt) {Color = TradeableColor;} else {Color = UntradeableColor;}
      ObjectSetText("xADR3", text, 8, "Arial Narrow", Color);
      
      text = "RmDn:" + RmDn;
      if (RmDn > RmLmt) {Color = TradeableColor;} else {Color = UntradeableColor;}
      ObjectSetText("xADR4", text, 8, "Arial Narrow", Color);
      
      text = "Todays range=" + DoubleToStr(((tHigh - tLow) / point), 0);
      ObjectSetText("xADR5", text, 8, "Arial Narrow", OtherColor);
    }
    
    if (PivotFilter)
    {
      // pivots
      tOpen = Open[StartOfDay];
      yStart = iBarShift(NULL, PERIOD_D1, Time[i]) + 1;
      if (TimeDayOfWeek(iTime(NULL, PERIOD_D1, yStart)) == 0) yStart++;
      yStart = iBarShift(NULL, 0, iTime(NULL, PERIOD_D1, yStart)) - BrokerOffsetFromUTC * (PERIOD_H1 / Period1);
      yHigh = High[yStart]; yLow = Low[yStart];
      for (j = 1; j < PERIOD_D1 / Period1; j++)
      {
        yHigh = MathMax(yHigh, High[yStart - j]);
        yLow = MathMin(yLow, Low[yStart - j]);
      }
      yClose = Close[yStart - PERIOD_D1 / Period1 - 1];
      p = (yHigh + yLow + yClose) / 3;
      q = yHigh - yLow;
      r1 = p + (q * 0.382); 
      r2 = p + (q * 0.618);
      r3 = p + q;
      r4 = p + (q * 1.618);
      r5 = p + (q * 2.618);
      s1 = p - (q * 0.382);
      s2 = p - (q * 0.618);
      s3 = p - q;
      s4 = p - (q * 1.618);
      s5 = p - (q * 2.618);
    }

    // finally!    
    if (OkToTrade) mDrawArrows(i, RmUp, RmDn, RmLmt, atr1, atr2);
  }
     
  return(0); 
} 
  
void getPeriod()
{
  switch(Period()) 
  {
    case 1: 
      Period1 = 1; Period2 = 5; Period3 = 15;
      break;
    case 5: 
      Period1 = 5; Period2 = 15; Period3 = 60;
      break;
    case 15: 
      Period1 = 15; Period2 = 60; Period3 = 240;
      break;
  }
}

string tf2txt(int tf)
{
   if (tf == PERIOD_M1)
      return("M1");
   if (tf == PERIOD_M5)    
      return("M5");
   if (tf == PERIOD_M15)   
      return("M15");
   if (tf == PERIOD_H1)    
      return("H1");
   if (tf == PERIOD_H4)    
      return("H4");
   
   return("??");
}

void mDrawArrows(int j, int RmUp, int RmDn, int RmLmt, int atr1, int atr2)
{
  int mLineDown      = buf1_down[j] + buf2_down[j] + buf3_down[j];
  int PriormLineDown = buf1_down[j + 1] + buf2_down[j + 1] + buf3_down[j + 1];
  int mLineUp        = buf1_up[j] + buf2_up[j] + buf3_up[j];
  int PriormLineUp   = buf1_up[j + 1] + buf2_up[j + 1] + buf3_up[j + 1];

  if (mLineDown == 6 && PriormLineDown != 6 && ((!RmFilter) || (RmDn > RmLmt)) && ((!ATRFilter) || (atr1 > atr2)))
  {
    if (((!PivotFilter) ||
       (
       (
       ((HaLow1 < r5) || (HaLow1 - r5 > point * MinPivotDistance))
       && ((HaLow1 < r4) || (HaLow1 - r4 > point * MinPivotDistance))
       && ((HaLow1 < r3) || (HaLow1 - r3 > point * MinPivotDistance))
       && ((HaLow1 < r2) || (HaLow1 - r2 > point * MinPivotDistance))
       && ((HaLow1 < r1) || (HaLow1 - r1 > point * MinPivotDistance))
       && ((HaLow1 < p) || (HaLow1 - p > point * MinPivotDistance))
       && ((HaLow1 < tOpen) || (HaLow1 - tOpen > point * MinPivotDistance))
       && ((HaLow1 < yClose) || (HaLow1 - yClose > point * MinPivotDistance))
       && ((HaLow1 < s1) || (HaLow1 - s1 > point * MinPivotDistance))
       && ((HaLow1 < s2) || (HaLow1 - s2 > point * MinPivotDistance))
       && ((HaLow1 < s3) || (HaLow1 - s3 > point * MinPivotDistance))
       && ((HaLow1 < s4) || (HaLow1 - s4 > point * MinPivotDistance))
       && ((HaLow1 < s5) || (HaLow1 - s5 > point * MinPivotDistance))
       )
       &&
       (
       ((HaClose1 < r5) || (HaClose1 - r5 > point * MinPivotDistance))
       && ((HaClose1 < r4) || (HaClose1 - r4 > point * MinPivotDistance))
       && ((HaClose1 < r3) || (HaClose1 - r3 > point * MinPivotDistance))
       && ((HaClose1 < r2) || (HaClose1 - r2 > point * MinPivotDistance))
       && ((HaClose1 < r1) || (HaClose1 - r1 > point * MinPivotDistance))
       && ((HaClose1 < p) || (HaClose1 - p > point * MinPivotDistance))
       && ((HaClose1 < tOpen) || (HaClose1 - tOpen > point * MinPivotDistance))
       && ((HaClose1 < yClose) || (HaClose1 - yClose > point * MinPivotDistance))
       && ((HaClose1 < s1) || (HaClose1 - s1 > point * MinPivotDistance))
       && ((HaClose1 < s2) || (HaClose1 - s2 > point * MinPivotDistance))
       && ((HaClose1 < s3) || (HaClose1 - s3 > point * MinPivotDistance))
       && ((HaClose1 < s4) || (HaClose1 - s4 > point * MinPivotDistance))
       && ((HaClose1 < s5) || (HaClose1 - s5 > point * MinPivotDistance))
       )
       )
       )
       && ((!RoundNumberFilter) || (HaClose1 - MathFloor(HaLow1 / point / 100) * 100 * point > point * MinPivotDistance)))
    {
      ObjectCreate("mSell" + Time[j], OBJ_ARROW, 0, 0, 0, 0, 0);    //Draw Down arrow
      ObjectSet("mSell" + Time[j], OBJPROP_PRICE1, High[j] + (ArrowSpacing + 10) * point);
      ObjectSet("mSell" + Time[j], OBJPROP_TIME1, Time[j]);
      ObjectSet("mSell" + Time[j], OBJPROP_COLOR, Red);
      ObjectSet("mSell" + Time[j], OBJPROP_WIDTH, 2);
      ObjectSet("mSell" + Time[j], OBJPROP_ARROWCODE, 234);
      if (AlertOn) Alert("#MTF ", Symbol(), " Down");
    }
  }
  else if (mLineUp == 6 && PriormLineUp != 6 && ((!RmFilter) || (RmUp > RmLmt)) && ((!ATRFilter) || (atr1 > atr2)))
  {
    if (((!PivotFilter) || (
       (
       ((HaHigh1 > r5) || (r5 - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > r4) || (r4 - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > r3) || (r3 - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > r2) || (r2 - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > r1) || (r1 - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > p) || (p - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > tOpen) || (tOpen - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > yClose) || (yClose - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > s1) || (s1 - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > s2) || (s2 - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > s3) || (s3 - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > s4) || (s4 - HaHigh1 > point * MinPivotDistance))
       && ((HaHigh1 > s5) || (s5 - HaHigh1 > point * MinPivotDistance))
       )
       &&
       (
       ((HaClose1 > r5) || (r5 - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > r4) || (r4 - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > r3) || (r3 - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > r2) || (r2 - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > r1) || (r1 - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > p) || (p - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > tOpen) || (tOpen - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > yClose) || (yClose - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > s1) || (s1 - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > s2) || (s2 - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > s3) || (s3 - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > s4) || (s4 - HaClose1 > point * MinPivotDistance))
       && ((HaClose1 > s5) || (s5 - HaClose1 > point * MinPivotDistance))
       )
       )
       )
       && ((!RoundNumberFilter) || (MathCeil(HaHigh1 / point / 100) * 100 * point - HaHigh1 > point * MinPivotDistance)))
    {
      ObjectCreate("mBuy" + Time[j], OBJ_ARROW, 0, 0, 0, 0, 0);    //Draw Up arrow
      ObjectSet("mBuy" + Time[j], OBJPROP_PRICE1, Low[j] - ArrowSpacing * point);
      ObjectSet("mBuy" + Time[j], OBJPROP_TIME1, Time[j]);
      ObjectSet("mBuy" + Time[j], OBJPROP_COLOR, Lime);
      ObjectSet("mBuy" + Time[j], OBJPROP_WIDTH, 2);
      ObjectSet("mBuy" + Time[j], OBJPROP_ARROWCODE, 233);
      if (AlertOn) Alert("#MTF ", Symbol(), " Up");
    }
  }
  WindowRedraw();
  return(0);
}

