Download MetaTrader 5

Resetting an indicator with a bool variable

To add comments, please log in or register
simoncs
324
simoncs  

Hi

I am trying to turn part of an indicator into an EA.

At the moment i just want to see the the basic logic working before i add to it, but i can't seem to get the switch correct to reset the indicator. I just want to see the date/time printed for each bullish pin or bearish pin.

I was trying to create a bool switch to reset the indicator each time a new one appeared, but I can't seem to get the logic correct. I have tried adding it as an else statement as well, and added print statements to see what is happening at each point. I am having difficulty in coding the logic to achieve this.

If i add the switch inside the curly brackets then i just get repeat messages every new bar, if I add it outside then i can get print messages for the bearish bar or the bullish bar depending on the order of the code, but i can't seem t get it to alternate between the two.

Then of course there could be a scenario where there may be more than one bullish signal consecutively, so I am not sure that using the switch as I have intended is the best way to do this either.

 

Once I have this basic logic in place i then want to be able to reset the indicator after x many bars. eg the signal could be valid for upto 5 bars afterwards, but still reference the bar that was a pinbar - I guess i need to then add a for loop i to achieve this? 

 

Any  advice welcome. 

 

    
Fun_New_Bar(); 

     
 ///////    Trade Entry  //---
 
 

 
if (BullishPinBar()==true)

   {
    Print("BullishPinBar ",BullishBarNo,",", TimeToString(BullishBarTime,TIME_DATE),","
    ,TimeToString(BullishBarTime,TIME_MINUTES),
     "Bull236 = ",DoubleToStr(BullFib236,Digits),"Bull618 = ",DoubleToStr(BullFib618,Digits));
      
   }
   BearishPin=false; 

if (BearishPinBar()==true)
     
   {
    Print("BearishPinBar ", BearishBarNo,",", TimeToString(BearishBarTime,TIME_DATE),","
    ,TimeToString(BearishBarTime,TIME_MINUTES),
    "Bear236 = ",DoubleToStr(BearFib236,Digits),"Bear618 = ",DoubleToStr(BearFib618,Digits));
    
   }
   BullishPin=false;

                                          
// 
Comment(
"NeedBarsCounted =",NeedBarsCounted, "\n"
,"LastBars =",LastBars, "\n"
,"NoseLength = ",NoseLength, "\n"
,"BullishPin = ",BullishPinBar(), "\n"
,"BearishPin = ",BearishPinBar(), "\n"
, "ResetBar = ",ResetBar, "\n"


);

   return(0);
  }
 
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//
//                                  FUNCTION SECTION             
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 
 
 
  
  
 
bool BearishPinBar ()
{

if (New_Bar==true)
{
 

   if (LastBars == Bars) return(0);
   NeedBarsCounted = Bars - LastBars;
   LastBars = Bars;
   if (NeedBarsCounted == Bars) NeedBarsCounted--;

   for (int i = NeedBarsCounted; i >= 1; i--)
   {
      // Won't have Left Eye for the left-most bar
      if (i == Bars - 1) continue;
      
      // Left Eye and Nose bars's paramaters
      NoseLength = High[i] - Low[i];
      if (NoseLength == 0) NoseLength = Point;
      LeftEyeLength = High[i + 1] - Low[i + 1];
      if (LeftEyeLength == 0) LeftEyeLength = Point;
      NoseBody = MathAbs(Open[i] - Close[i]);
      if (NoseBody == 0) NoseBody = Point;
      LeftEyeBody = MathAbs(Open[i + 1] - Close[i + 1]);
      if (LeftEyeBody == 0) LeftEyeBody = Point;
      BearFib236 = NormalizeDouble(Low[i] + (NoseLength *0.236),Digits);
      BearFib618 = NormalizeDouble(Low[i] + (NoseLength *0.618),Digits);
      BearTailLow = High[i];
      

// Bearish Pinbar
if (High[i] - High[i + 1] >= NoseLength * NoseProtruding) // Nose protrusion
{
if (NoseBody / NoseLength <= MaxNoseBodySize) // Nose body to candle length ratio
{
if (1 - (High[i] - MathMax(Open[i], Close[i])) / NoseLength < NoseBodyPosition) 
// Nose body position in bottom part of the bar
{
if ((!LeftEyeOppositeDirection) || (Close[i + 1] > Open[i + 1])) // Left Eye bullish if required
{
if ((!NoseSameDirection) || (Close[i] < Open[i])) // Nose bearish if required
{
if (LeftEyeBody / LeftEyeLength  >= LeftEyeMinBodySize) // Left eye body to candle length ratio
{
if ((MathMax(Open[i], Close[i]) <= High[i + 1]) && (MathMin(Open[i], Close[i]) >= Low[i + 1])) 
// Nose body inside Left Eye bar
{
if (NoseBody / LeftEyeBody <= NoseBodyToLeftEyeBody) // Nose body to Left Eye body ratio
{
if (NoseLength / LeftEyeLength >= NoseLengthToLeftEyeLength) // Nose length to Left Eye length ratio
{
if (Low[i] - Low[i + 1] >= LeftEyeLength * LeftEyeDepth)  // Left Eye low is low enough
{
   if ((!NoseBodyInsideLeftEyeBody) || ((MathMax(Open[i], Close[i]) <= MathMax(Open[i + 1], Close[i + 1])) 
      && (MathMin(Open[i], Close[i]) >= MathMin(Open[i + 1], Close[i + 1])))) 
      // Nose body inside Left Eye body if required
   {
      Down[i] = High[i] + 5 * Point + NoseLength / 5;
      if (i == 1)  BearishPin=true; BearishBarNo = iBarShift(Symbol(),ExtPinBarTF,Time[1]);
      BearishBarTime = iTime(Symbol(),ExtPinBarTF,i);
      
   }
}
}
}
}
}
}
}
}
}
}
}
}      
return(BearishPin);       
}

bool BullishPinBar()      
{  

if (New_Bar==true)
{

  if (LastBars == Bars) return(0);
   NeedBarsCounted = Bars - LastBars;
   LastBars = Bars;
   if (NeedBarsCounted == Bars) NeedBarsCounted--;

   for (int i = NeedBarsCounted; i >= 1; i--)
   {
      // Won't have Left Eye for the left-most bar
      if (i == Bars - 1) continue;
      
      // Left Eye and Nose bars's paramaters
      NoseLength = High[i] - Low[i];
      if (NoseLength == 0) NoseLength = Point;
      LeftEyeLength = High[i + 1] - Low[i + 1];
      if (LeftEyeLength == 0) LeftEyeLength = Point;
      NoseBody = MathAbs(Open[i] - Close[i]);
      if (NoseBody == 0) NoseBody = Point;
      LeftEyeBody = MathAbs(Open[i + 1] - Close[i + 1]);
      if (LeftEyeBody == 0) LeftEyeBody = Point; 
       BullFib236 = NormalizeDouble(High[i] - (NoseLength *0.236),Digits);
       BullFib618 = NormalizeDouble(High[i] - (NoseLength *0.618),Digits);
       BullTailLow = Low[i];  
      
// Bullish Pinbar
if (Low[i + 1] - Low[i] >= NoseLength * NoseProtruding) // Nose protrusion
{
if (NoseBody / NoseLength <= MaxNoseBodySize) // Nose body to candle length ratio
{
if (1 - (MathMin(Open[i], Close[i]) - Low[i]) / NoseLength < NoseBodyPosition) 
// Nose body position in top part of the bar
{
if ((!LeftEyeOppositeDirection) || (Close[i + 1] < Open[i + 1])) // Left Eye bearish if required
{
if ((!NoseSameDirection) || (Close[i] > Open[i])) // Nose bullish if required
{
if (LeftEyeBody / LeftEyeLength >= LeftEyeMinBodySize) // Left eye body to candle length ratio
{
if ((MathMax(Open[i], Close[i]) <= High[i + 1]) && (MathMin(Open[i], Close[i]) >= Low[i + 1])) 
// Nose body inside Left Eye bar
{
if (NoseBody / LeftEyeBody <= NoseBodyToLeftEyeBody) // Nose body to Left Eye body ratio
{
if (NoseLength / LeftEyeLength >= NoseLengthToLeftEyeLength) // Nose length to Left Eye length ratio
{
if (High[i + 1] - High[i] >= LeftEyeLength * LeftEyeDepth) // Left Eye high is high enough
{
if ((!NoseBodyInsideLeftEyeBody) || ((MathMax(Open[i], Close[i]) <= MathMax(Open[i + 1], Close[i + 1])) 
   && (MathMin(Open[i], Close[i]) >= MathMin(Open[i + 1], Close[i + 1])))) // Nose body inside Left Eye body if required
{
   Up[i] = Low[i] - 5 * Point - NoseLength / 5;
   if (i == 1)   BullishPin=true; BullishBarNo = iBarShift(Symbol(),ExtPinBarTF,Time[1]);
   BullishBarTime = iTime(Symbol(),ExtPinBarTF,i);
}
}
}
}
}
}
}
}
}
}
}
}
}
      
return(BullishPin);         
}


void Fun_New_Bar()                              // Funct. detecting ..
  {                                             // .. a new bar
   static datetime New_Time=0;                  // Time of the current bar
   New_Bar=false;                               // No new bar
   if(New_Time!=Time[0])                        // Compare time
     {
      New_Time=Time[0];                         // Now time is so
      New_Bar=true;                             // A new bar detected
     }
  }

Files:
Keith Watford
Moderator
10569
Keith Watford  

Sorry, but your post is way too wide for my screen and I won't keep scrolling back and forth in order to read it.

If the lines in your code were shorter, it wouldn't be so difficult to read 

whroeder1
15577
whroeder1  
simoncs: I am trying to turn part of an indicator into an EA.
  1. Are your books one column but two feet wide? No because that is unreadable. They are 6 inches, sometimes two columns, so you can read it easily. So should be your code. I'm not going to go scrolling back and forth trying to read it. Edit the post with formatted code and you might get additional help.
  2. Don't do that. Just get the value of the indicator. Detailed explanation of iCustom - MQL4 forum
simoncs
324
simoncs  
WHRoeder:
  1. Are your books one column but two feet wide? No because that is unreadable. They are 6 inches, sometimes two columns, so you can read it easily. So should be your code. I'm not going to go scrolling back and forth trying to read it. Edit the post with formatted code and you might get additional help.
  2. Don't do that. Just get the value of the indicator. Detailed explanation of iCustom - MQL4 forum


sure, i have amended above code. I had used the inbuilt mql4 editor styler function to try to make it easier to read.

I had thought about using icustom, asi have used this before. However i was just concerned with the computational overhead. In the indicator there are drawing functions which populate all the relevant historical bars, alert features etc that i don't need. So i was concerned that these would impact optimisation run time, and so thought i would be better off just stripping out the relevant bits that i needed. Is that not the case?

whroeder1
15577
whroeder1  
simoncs: So i was concerned that these would impact optimisation run time, and so thought i would be better off just stripping out the relevant bits that i needed. Is that not the case?

  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Coded right, the indicator calculates all bars on initial load, and after that just updates bar zero each tick.
To add comments, please log in or register