ONLY CANDLE / BAR - How to catalog or segregate Candle? - PLEASE CORRECT ME! - page 4

 

I have a feeling we lose a little bit control over the code....

 

        if ( SignalBULL62Executed) CheckForClose_BULL6();       // I don't know it is the right place to close

No, this is for sure not the right place.

 

   int    result;
   if(Bid==Close[1]+3*Point)  
     {
      result=OrderSend(Symbol(),OP_BUY,0.61,Ask,0,Close[1]-25*Point,0,"",MAGICMA2,0,Red);
      return;
     }
   if (result == -1)
      {
      int e = GetLastError();
      Print(e);
      }   
  } 

This 'error handling' does not make anything better. Check it again.

 

bool Fun_New_Bar()                              // bool             

   {                                
   static datetime New_Time=0;      
   New_Bar=false;                   
   if(New_Time!=Time[0])           
      {
      New_Time=Time[0];                
      New_Bar=true;                    
      }
   }

This is not created for fun! It should be used to reset the executed flags on a new bar.

 

if(Bid==Close[1]+3*Point)  

What have we learned about comparing doubles and whether this makes any sense here?

 

So you have decided to delete your post I am referencing to... Well done, that's very helpful !

 

Sorry but yesterday was problem in internet (mql4.com stop working). I was try correct something and ...  collapse

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
#define MAGICMA1  2000        // blue starategy
#define MAGICMA2  2001        // red strategy

//+------------------------------------------------------------------+
//| Check NEW BAR                                |
//+------------------------------------------------------------------+
bool New_Bar = false;

bool Fun_New_Bar()                              // bool             

   {                                
   static datetime New_Time=0;      
   New_Bar=false;                   
   if(New_Time!=Time[0])           
      {
      New_Time=Time[0];                
      New_Bar=true;                    
      }
   }

//+------------------------------------------------------------------+
//| Calculate open positions    HOW MEANY OF THIS is open   |
//+------------------------------------------------------------------+

//--- FOR BLUE---------------------------------------------------------
int OpenOrders_BULL4(string symbol)
  {
   int buys=0;

   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; 
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA1)    // magic ma 1 ( is ==)
        {
         if(OrderType()==OP_BUY) buys++;
        }
     }
   return(buys);
  }
  
//--- FOR RED--------------------------------------------------------------
int OpenOrders_BULL6(string symbol)
  {
   int buys=0;

   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; 
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA2)    // magic ma 2 ( is ==)
        {
         if(OrderType()==OP_BUY) buys++;
        }
     }
   return(buys);
  }



//+------------------------------------------------------------------+
//| Condition candle                                                 |
//+------------------------------------------------------------------+
bool BULL4()
   {
   if(Close[1]-Open[1]>=4*Point && Close[1]-Open[1]<=5*Point)
   return(true);
   } 

bool BULL6()
   {
   if(Close[1]-Open[1]>=6*Point && Close[1]-Open[1]<=7*Point)
   return(true);
   }
      
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
//--------------------------- blue bull4 magicma1 
void OpenBULL41()
  {
   int    result;
   if(Bid==Close[1]+3*Point)  
     {
      result=OrderSend(Symbol(),OP_BUY,0.41,Ask,0,Close[1]-25*Point,0,"",MAGICMA1,0,Blue);
      return(true);
     }
    if (result == -1)
      {
      int e = GetLastError();
      Print(e);
      }      
  }
  
void OpenBULL42()
  {
   int    result;
   
   for(int i=1;i<=OrdersTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; 
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA1)    // magic ma 1 ( is ==)
        {
         if(Ask>OrderOpenPrice()+2*Point)  
            {
             result=OrderSend(Symbol(),OP_BUY,0.42,Ask,0,Close[1]-25*Point,0,"",MAGICMA1,0,Blue);
             return;
            }
          if (result == -1)
             {
             int e = GetLastError();
             Print(e);
             }    
        }
     }
  }  
//--------------------------- red bull6 magicma2 
void OpenBULL61()
  {
   int    result;
   if(Bid==Close[1]+3*Point)  
     {
      result=OrderSend(Symbol(),OP_BUY,0.61,Ask,0,Close[1]-25*Point,0,"",MAGICMA2,0,Red);
      return;
     }
   if (result == -1)
      {
      int e = GetLastError();
      Print(e);
      }   
  }
  
void OpenBULL62()
  {
   int    result;
   if(Ask>OrderOpenPrice()+2*Point)  
     {
      result=OrderSend(Symbol(),OP_BUY,0.62,Ask,0,Close[1]-25*Point,0,"",MAGICMA2,0,Red);
      return;
     }
    if (result == -1)
      {
      int e = GetLastError();
      Print(e);
      }     
  }

void OpenBULL63()
  {
   int    result;
   if(Ask>OrderOpenPrice()+2*Point)  
     {
      result=OrderSend(Symbol(),OP_BUY,0.63,Ask,0,Close[1]-25*Point,0,"",MAGICMA2,0,Red);
      return;
     }
    if (result == -1)
      {
      int e = GetLastError();
      Print(e);
      }    
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+

void CheckForClose_BULL4()
{

   if(OrderOpenPrice()+4*Point< Ask)
   for (int i = OrdersTotal()-1; i >= 0;i--)          // add: -1
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if (OrderSymbol() == Symbol()&& OrderMagicNumber()==MAGICMA1) 
      {
         bool ticket = true;
         if (OrderType() == OP_BUY) ticket = OrderClose(OrderTicket(),OrderLots(),Bid,1,Blue);

      }
   }
} 

void CheckForClose_BULL6()
{

   if(OrderOpenPrice()+8*Point< Ask)
   for (int i = OrdersTotal()-1; i >= 0;i--)
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if (OrderSymbol() == Symbol()&& OrderMagicNumber()==MAGICMA2) 
      {
         bool ticket = true;
         if (OrderType() == OP_BUY) ticket = OrderClose(OrderTicket(),OrderLots(),Bid,1,Red);

      }
   }
}  
  
//+------------------------------------------------------------------+
//| flag                               |
//+------------------------------------------------------------------+ 
Fun_New_Bar();
if (New_Bar==false);
bool SignalBULL41Executed=false;
bool SignalBULL42Executed=false;
bool SignalBULL61Executed=false;
bool SignalBULL62Executed=false;
bool SignalBULL63Executed=false;

if(Fun_New_Bar())
{
   SignalBULL41Executed=true;
   return(SignalBULL41Executed);
}


  
  
//+------------------------------------------------------------------+
//| Start function                                 |
//+------------------------------------------------------------------+
void start()
  {
// ----------------bull4
if(BULL4())             
  if(!SignalBULL41Executed && OpenOrders_BULL4(Symbol())==0)
  {
    OpenBULL41();//do open buy position
    SignalBULL41Executed=true;
    if ( SignalBULL41Executed) CheckForClose_BULL4();       // I don't know it is the right place to close
  }
  
  
  if(!SignalBULL42Executed && OpenOrders_BULL4(Symbol())==1)
  {
    OpenBULL42();//do open buy position
    SignalBULL42Executed=true;
        if ( SignalBULL42Executed) CheckForClose_BULL4();       // I don't know it is the right place to close
  }
  
// ----------------bull6  
if(BULL6())             
  if(!SignalBULL61Executed && OpenOrders_BULL6(Symbol())==0)
  {
    OpenBULL61();//do open buy position
    SignalBULL61Executed=true;
        if ( SignalBULL61Executed) CheckForClose_BULL6();       // I don't know it is the right place to close
  }
  
  
  if(!SignalBULL62Executed && OpenOrders_BULL6(Symbol())==1)
  {
    OpenBULL62();//do open buy position
    SignalBULL62Executed=true;
        if ( SignalBULL62Executed) CheckForClose_BULL6();       // I don't know it is the right place to close
  }
  
  
  if(!SignalBULL63Executed && OpenOrders_BULL6(Symbol())==2)
  {
    OpenBULL63();//do open buy position
    SignalBULL42Executed=true;
        if ( SignalBULL62Executed) CheckForClose_BULL6();       // I don't know it is the right place to close
  }   

  }
//+------------------------------------------------------------------+
 

It is not easy to help you. Why won't you try to implement only one strategy for now, but with a proper, Open(), Close(), Count(), NewBar(), Signal() function.

The below block is outside of the start function. I am not sure this is wanted.

Fun_New_Bar();
if (New_Bar==false);               // <-- ???
bool SignalBULL41Executed=false;   // <-- This could be correct, but I am not sure you know why and
bool SignalBULL42Executed=false;   //     its not more an accident that you initalize it here.
bool SignalBULL61Executed=false;
bool SignalBULL62Executed=false;
bool SignalBULL63Executed=false;

if(Fun_New_Bar())                  // <-- Your Fun_New_Bar() function doesn't return anything,
{                                  //     you set the global New_Bar variable with the function. 
   SignalBULL41Executed=true;      // <-- You should set it to false on a new bar.    
   return(SignalBULL41Executed);   // <-- ???
}
 

I think the same as You, small steps. Let's take just one option BULL4
Sometimes looking like Ea to my on codebase

https://www.mql5.com/en/code/9156 - interesting

I understand these (but not feel), i'ts a flag and I understand its purpose

 What dou you think? about small steps

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
#define MAGICMA1  2000        // blue starategy


//+------------------------------------------------------------------+
//| Check NEW BAR                                |
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| Calculate open positions    HOW MEANY OF THIS is open   |
//+------------------------------------------------------------------+

int OpenOrders_BULL4(string symbol)
  {
   int buys=0;

   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; 
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA1)   
        {
         if(OrderType()==OP_BUY) buys++;
        }
     }
   return(buys);
  }
  
//+------------------------------------------------------------------+
//| Condition candle                                                 |
//+------------------------------------------------------------------+
bool BULL4()
   {
   if(Close[1]-Open[1]>=4*Point && Close[1]-Open[1]<=5*Point)
      {
         return(true);
      } 
   else
      {
         return(false);
      } 
   }
//+------------------------------------------------------------------+
//| Condition send buy                                                 |
//+------------------------------------------------------------------+
 
bool BULL4send()
   {
   if( Bid==Close[1]+3*Point)
      {
         return(true);
      } 
    else
      {
        return(false);
      }
   }
 
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+

bool OpenBULL41()
  {
      int    result;

      result=OrderSend(Symbol(),OP_BUY,0.41,Ask,0,Close[1]-25*Point,0,"",MAGICMA1,0,Blue);
      if(result>0)
           {
            if(OrderSelect(result,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
      else
         {
            Print("Error opening BUY order : ",GetLastError()); 
            return(false);
         }
      return(true);       
   }
   
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+

  
//+------------------------------------------------------------------+
//| Start function                                 |
//+------------------------------------------------------------------+
void start()
  {
// ----------------bull4
   if(BULL4() && BULL4send() && OpenOrders_BULL4(Symbol())==0)             

     {
       OpenBULL41();//do open buy position
     }
  }
//+------------------------------------------------------------------+
 

Yes, small steps, so the errors are manageable...
Do you want to highlight something specific from the codebase EA?

I wont see this again - I will not help you anymore if you further ignore the correction steps, I - or someone else - has adviced to do:

if( Bid==Close[1]+3*Point)

 

Slippage is 0? You'll probably get an error. But the fixed error handling will catch it :-)

result=OrderSend(Symbol(),OP_BUY,0.41,Ask,0,Close[1]-25*Point,0,"",MAGICMA1,0,Blue);

 

This is not wrong, but not ideal and in a worst case it does not what you want inside the loop. Best, don't even start using loops from 0 to OrdersTotal. Read this: Loops and Closing or Deleting Orders

for(int i=0;i<OrdersTotal();i++)

 

What is your brokers spread and how many digits do you have for the symbol you want to trade? I am not sure you use Point correctly (Read on the first page of your thread). 

 
kronin:

Yes, small steps, so the errors are manageable...
Do you want to highlight something specific from the codebase EA?

I think no, nothing to light, I understand what I wrote.

I wont see this again - I will not help you anymore if you further ignore the correction steps, I - or someone else - has adviced to do:

Yes.  I know what do NormalizeDouble, I thing that I understand ( if I have: euro/usd = 1.2390022129.....09.... And we need: 1,29893 end! not infinity ) but I cant write these, could be these?

 if (      NormalizeDouble  (  abc(),0  )      )

bool abc()

{

         bool z,y,z;

x=Bid;

y=Close[1]

z=x-y+3*Point

return(z);

or ? 

 

If ( NormalizeDouble ((Close[1]-Bid) +3*point),0 ) ;

 

Slippage is 0? You'll probably get an error. But the fixed error handling will catch it :-)

I can have +1 no problem result=OrderSend(Symbol(),OP_BUY,0.41,Ask,  ,Close[1]-25*Point,0,"",MAGICMA1,0,Blue);

error will handling, good, we see what will be after
 

This is not wrong, but not ideal and in a worst case it does not what you want inside the loop. Best, don't even start using loops from 0 to OrdersTotal. Read this: Loops and Closing or Deleting Orders

VERY GOOD ARTICLE !  I sholud read these at begining :)

 

What is your brokers spread and how many digits do you have for the symbol you want to trade? I am not sure you use Point correctly (Read on the first page of your thread). 

I remember these.  I have Admiral Market Bid=1,29000 Ask = 1,29001. These is a problem I know,, but I do not have resolution at these moment I not found the good example.

 

I'm writing code...

 

 
Wodzuuu:

Yes.  I know what do NormalizeDouble, I thing that I understand ( if I have: euro/usd = 1.2390022129.....09.... And we need: 1,29893 end! not infinity ) but I cant write these, could be these?

 
if (      NormalizeDouble  (  abc(),0  )      )

bool abc()

{


You can't NormalizeDouble() a bool,  the clue is in the function name and in the documentation too . . .  

double NormalizeDouble(double valueint digits)

 
RaptorUK:

You can't NormalizeDouble() a bool,  the clue is in the function name and in the documentation too . . .  

It's a pitty we cannot normalize true to be very true :-(

 

Wodzuuu:
Yes. I know what do NormalizeDouble, I thing that I understand ( if I have: euro/usd = 1.2390022129.....09.... And we need: 1,29893 end! not infinity ) but I cant write these, could be these?

No, you don't have to normalize Bid or Close[1]. What I meant is, you should just not compare it for equality.

Imagine: 
Close[1] = 1.31933
New_Bar
Tick1, Bid = 1.31935
Tick2, Bid = 1.31934 
Tick3, Bid = 1.31937
Tick4, Bid = 1.31937
Tick5, Bid = 1.31942 


Your code does look for a Tick where Bid is 1.31936  

if( Bid==Close[1]+3*Point)

You'll miss your signal. This would not happen, if you use > . 

 

Wodzuuu:
I can have +1 no problem result=OrderSend(Symbol(),OP_BUY,0.41,Ask, 1 ,Close[1]-25*Point,0,"",MAGICMA1,0,Blue);

This doesn't make it much better. All your calculation in Points should be Pips from my understanding. However, I have no experience with ECN brokers, so it might work.

 

Wodzuuu:

This is not wrong, but not ideal and in a worst case it does not what you want inside the loop. Best, don't even start using loops from 0 to OrdersTotal. Read this: Loops and Closing or Deleting Orders

VERY GOOD ARTICLE ! I sholud read these at begining :)

Agree and there are many very good articles more, written by the same author. Also the book is not a bad place to start.

 

Wodzuuu:

I have Admiral Market Bid=1,29000 Ask = 1,29001. These is a problem I know,, but I do not have resolution at these moment I not found the good example.

On your brokers website is stated, that for EURUSD, the minimal spread is 0,1. The average spread is 0,5. <-- This is meant in pips! So 0,1 is 1 Point. 0,5 is 5 Points.
So in your case, one Pip is 10*Point. The spread will confuse all your mini calculation where you add only 3 points to a price.
Search for 'adjust for 5 digit broker' and you should find a lot of examples.

 

HI :)

 

If this code was bad because I was passing signals, it has improved. I thought that the problem is entirely in a different place, not in the sign of equality '=='.........    the '>=' is better idea of course

if( Bid==Close[1]+3*Point)
if( Bid>=Close[1]+MyPips*3)

 Question 1. Do I have use MyPips in candle, in these function? bool BULL4()

 

DIGIT problem.

I found these 

https://www.mql5.com/en/forum/140097    by WHRoeder

https://www.mql5.com/en/forum/123736  by WHRoeder

What is Digit ? in https://docs.mql4.com/predefined/variables/digits  and  https://docs.mql4.com/convert/doubletostr

Because you are the expert I should have said something, it might be important.
Now I use the MT4 platform and I see 4 digits after the decimal point (Bid-Ask = 0.0001 usualy). At the moment we programmed in MQL4 let it be.
In MT5, I see 5 digits after the decimal point. threre is differs betwen MQL4 and MQL5. At the moment this spreed does not matter to me. Important to our EA worked well.
I do not want at this time to talk about it (unless necessary)

 

 

AND I WROTE PROGRAM for me is good :) how about You?

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
#define MAGICMA1  2000        // blue starategy

//+------------------------------------------------------------------+
//| Check NEW BAR                                |
//+------------------------------------------------------------------+

// nothing yet, one moment

//+------------------------------------------------------------------+
//| These are adjusted for 5/4 digit brokers                           |
//+------------------------------------------------------------------+

double  MyPips;        // slippage and others

int init()
  {
    if (Digits == 5)    // Adjust for five (5) 
    {                
         MyPips=Point*10; 
    } 
    else                // Adjust for four (4, and ..)
    {
         MyPips=Point*1;  
    }
  }
 
//+------------------------------------------------------------------+
//| Calculate open positions    HOW MEANY OF THIS is open   |
//+------------------------------------------------------------------+

int OpenOrders_BULL4(string symbol)
  {
   int buys=0;

   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; 
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA1)   
        {
         if(OrderType()==OP_BUY) buys++;
        }
     }
   return(buys);
  }
  
//+------------------------------------------------------------------+
//| Condition candle                                                 |
//+------------------------------------------------------------------+
bool BULL4()
   {
   if(Close[1]-Open[1]>=4*Point && Close[1]-Open[1]<=5*Point)
      {
         return(true);
      } 
   else
      {
         return(false);
      } 
   }

//+------------------------------------------------------------------+
//| Condition send buy                                                 |
//+------------------------------------------------------------------+
 
bool BULL4send()
   {
   if( Bid>=Close[1]+MyPips*3)
      {
         return(true);
      } 
    else
      {
        return(false);
      }
   }
 
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+

bool OpenBULL41()
  {
      int    result;

      result=OrderSend(Symbol(),OP_BUY,0.41,Ask,MyPips,Close[1]-25*MyPips,0,"",MAGICMA1,0,Blue);
      if(result>0)
           {
            if(OrderSelect(result,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
      else
         {
            Print("Error opening BUY order : ",GetLastError()); 
            return(false);
         }
      return(true);       
   }
   
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+

bool CheckForCloseBULL4()
{
   int i;
   for(i=OrdersTotal()-1;i>=0;i--)
      if( ! OrderSelect(i, SELECT_BY_POS, MODE_TRADES) ) continue;
         if( OrderMagicNumber() == MAGICMA1 && OrderSymbol() == Symbol()  && OrderType() == OP_BUY )
            if(OrderOpenPrice()+8*MyPips < Ask) return(true);
            
   else return(false);
}

//+------------------------------------------------------------------+
//|  close                               |
//+------------------------------------------------------------------+

void CloseBULL4()
{
   int i;   
   for(i=OrdersTotal()-1;i>=0;i--)
      {
      if( ! OrderSelect(i, SELECT_BY_POS, MODE_TRADES) ) continue;  
   
      if( OrderMagicNumber() == MAGICMA1 && OrderSymbol() == Symbol()  && OrderType() == OP_BUY )  
   
         if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), MyPips*1,Black ) )               
            Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  
      } 
}

//+------------------------------------------------------------------+
//| Start function                                 |
//+------------------------------------------------------------------+
void start()
  {
// ----------------bull4
   if(BULL4() && BULL4send() && OpenOrders_BULL4(Symbol())==0)             

     {
       OpenBULL41();//do open buy position
     }
     
   if(CheckForCloseBULL4()==true && OpenOrders_BULL4(Symbol())==1) CloseBULL4();   
  }
//+------------------------------------------------------------------+

 

 

 

 

 

Reason: