RoundLevels_v2

Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
RoundLevels_v2
//+------------------------------------------------------------------+
//|                                                  RoundLevels.mq4 |
//|                                                  by Captain Nemo |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 0

extern bool ShowMidLevels=true;
extern bool ShowMidLevels_W1=false;
extern bool ShowMidLevels_MN1=false;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   DeleteLevels();
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   DeleteLevels();
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeleteLevels()
  {
   int obj_total=ObjectsTotal();
   string name;
   for(int i=obj_total;i>=0;i--)
     {
      name=ObjectName(i);
      if(StringFind(name,"RoundLevel_")!=-1) ObjectDelete(name);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int highestBar=iHighest(NULL,0,WHOLE_ARRAY,0);
   int lowestBar=iLowest(NULL,0,WHOLE_ARRAY,0);

   double lowestPrice=Low[lowestBar];
   double highestPrice=High[highestBar];

   double numOfDigits;

   double x=MathCeil(highestPrice);
   if(x/100000>=1) {Alert("Prices >= then 100000 are not supported!");return(0);}

   if(x/10000>=1) numOfDigits=5;
   else if(x/1000>=1) numOfDigits=4;
   else if(x/100>=1) numOfDigits=3;
   else if(x/10>=1) numOfDigits=2;
   else if(x>=1) numOfDigits=1;
   else if(x*10>=1) numOfDigits=-1;
   else {Alert("Prices lower then 0.1 are not supported!");return(0);}

   double bottomLine=0,topLine=0,step=0;
   switch((int)numOfDigits)
     {
      case 5:
         bottomLine=(MathFloor(lowestPrice/1000)*1000);
         topLine=(MathCeil(highestPrice/1000)*1000);
         step=1000;
         break;

      case 4:
         bottomLine=(MathFloor(lowestPrice/100)*100);
         topLine=(MathCeil(highestPrice/100)*100);
         step=100;
         break;

      case 3:
         bottomLine=(MathFloor(lowestPrice/10)*10);
         topLine=(MathCeil(highestPrice/10)*10);
         step=10;
         break;

      case 2:
         bottomLine=MathFloor(lowestPrice);
         topLine=MathCeil(highestPrice);
         step=1;
         break;

      case 1:
         bottomLine=(MathFloor(lowestPrice*10)/10);
         topLine=(MathCeil(highestPrice*10)/10);
         step=0.1;
         break;

      case -1:
         bottomLine=(MathFloor(lowestPrice*100)/100);
         topLine=(MathCeil(highestPrice*100)/100);
         step=0.01;
         break;
     }

   for(double p=bottomLine;p<topLine+step;p=p+step)
     {
      // round levels
      string objName="RoundLevel_"+(string)NormalizeDouble(p,Digits);
      if(ObjectFind(objName)!=-1)
        {
         ObjectSet(objName,OBJPROP_PRICE1,p);
        }
      else
        {
         ObjectCreate(objName,OBJ_HLINE,0,0,p);
         ObjectSet(objName,OBJPROP_STYLE,STYLE_DOT);

         if(MathMod(NormalizeDouble(p,Digits),step*5)==0)
            ObjectSet(objName,OBJPROP_COLOR,Crimson);
         else
            ObjectSet(objName,OBJPROP_COLOR,Teal);
        }

      // mid levels
      if((ShowMidLevels_MN1==true && Period()==PERIOD_MN1) || (ShowMidLevels_W1==true && Period()==PERIOD_W1) || (ShowMidLevels==true && Period()<=PERIOD_D1))
        {
         objName="RoundLevel_"+DoubleToStr(p+step/2,5);
         if(ObjectFind(objName)!=-1)
           {
            ObjectSet(objName,OBJPROP_PRICE1,p+step/2);
           }
         else if(p+step/2<topLine)
           {
            ObjectCreate(objName,OBJ_HLINE,0,0,p+step/2);
            ObjectSet(objName,OBJPROP_STYLE,STYLE_DOT);
            ObjectSet(objName,OBJPROP_COLOR,SeaGreen);
           }
        }
     }

   return(0);
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---