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

 
Wodzuuu:

For those who still do not understand my EA write a story: D hehehe


The event is candles - features various options for buying and selling or closing.

I understand more by looking at your code than reading what you have written . . .   ;-)
 

Instead of watching TV shows as your second function you should consider to read and understand the Book.   ;-)

What you are trying to achieve is straight forward and used in many strategies, but your code - and your stories - are confusing and partially wrong - at least the code.... not sure about the stories. 

//Pseudo code

if(NewBar) ResetSignals();                //Set all SignalxExecuted to false
if(SignalBuy1&&!SignalBuy1Executed&&OpenOrders()==1){
//do open buy position
  SignalBuy1Executed=true;                //Set Signal to exectued to prevent a reuse in the same bar
}

if(SignalBuy2&&!SignalBuy2Executed&&OpenOrders()==2){
//do open buy position
  SignalBuy2Executed=true;                //Set Signal to exectued to prevent a reuse in the same bar
}

SignalBuy1=CheckForOpenBull42();          //Check for Signal1 condition is true
SignalBuy2=CheckForOpenBull62();	  //Check for Signal2 condition is true	

if(OpenOrders()>0)CheckForClose();        //Check any order needs to be closed


//and so on.....

Why do you not try to implement only one signal for the moment and concentrate on coding for the needed functions? If you have good code for one signal it is probably easy to implement the other signals and a lot of functions are already there then.

Later you could use Arrays and other function to simplify it, but simplification of your code above should not be your biggest issue at this time.

 

Kronin Yes! you understand me :), many of the strategies in one EA.
I have to write 150 policies and they must walk at the same time on the same graph, and run all the strategies at once. When I run 150 EA at the same time my PC will explode - I think so.

After that, it's pointless because you can have only one strategy EA and many already inside.

Of course, the strategy can not interfere with each other out and when run one the second can run too, and I can have run strategie 1, and 3 and 5. and strategie (1,3,5) running at the same moment.

I will not write any more stories :)


My code is wrong - it is known, I tried to put the two strategies in a single EA, the code works well but the totality is not working well

Your code is very interesting, I will study it, please give me some time

 

Hi friends, after a long work in code and through information from Kronin code is structured and operates.
Code presents two strategies in a single EA


1. For a small candle purchase scenario is marked with blue arrow, are two options for BUY
2.  For the average candle  purchase scenario candle is marked with a red arrow, are three options for BUY


I have a situation too where 1 point = 1 pip is convenient (to solve the problem is for later)
Not use options if (NewBar) ResetSignals () because I think they are not the cause of the error

If you delete a function PART 2  in start()  blue one strategy will work , and it works fine
If you run the code PART1 and PART2 containing two launch strategies, but they will overlap. this is error

 

Could you take a look Kronin

 

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
#define MAGICMA  20050610

//+------------------------------------------------------------------+
//| Check NEW BAR     WORK AT BARS  M15                                 |
//+------------------------------------------------------------------+
/*

bool New_Bar = false;

bool Fun_New_Bar()                

   {                                
   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                    |
//+------------------------------------------------------------------+

int OpenOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; 
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY) buys++;
        }
     }
//---- return orders volume
   return(buys);
  }

//+------------------------------------------------------------------+
//| Calculate candle      These is condidion                         |
//+------------------------------------------------------------------+

// scenario 1 / EA 1  /  startegy 1 / blue

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

// scenario 2 / EA 2  /  startegy 2 / red

bool BULL6()
   {
   if(Close[1]-Open[1]>=6*Point && Close[1]-Open[1]<=7*Point)
   return(true);
   }
      
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+

// Check for open for scenario 1 / EA 1  /  startegy 1 
 
bool OpenBULL41()
  {
   int    res;
   if(Bid==Close[1]+3*Point)  
     {
      res=OrderSend(Symbol(),OP_BUY,0.41,Ask,0,Close[1]-25*Point,0,"",MAGICMA,0,Blue);
      return (true);
     }
   return (false);  
  }
  
//----------------------------------- 
bool OpenBULL42()
  {
   int    res;
   if(Ask>OrderOpenPrice()+4*Point)  
     {
      res=OrderSend(Symbol(),OP_BUY,0.42,Ask,0,Close[1]-25*Point,0,"",MAGICMA,0,Blue);
      return;
     }
  }  
  
// Check for open for scenario 2 / EA 2  /  startegy 2
  
void OpenBULL61()
  {
   int    res;
   if(Bid==Close[1]+4*Point)  
     {
      res=OrderSend(Symbol(),OP_BUY,0.61,Ask,0,Close[1]-25*Point,0,"",MAGICMA,0,Red);
      return;
     }
  }
//-----------------------------------  
 
void OpenBULL62()
  {
   int    res;
   if(Ask>OrderOpenPrice()+2*Point)  
     {
      res=OrderSend(Symbol(),OP_BUY,0.62,Ask,0,Close[1]-25*Point,0,"",MAGICMA,0,Red);
      return;
     }
  }
  
//----------------------------------- 
void OpenBULL63()
  {
   int    res;
   if(Ask>OrderOpenPrice()+2*Point)  
     {
      res=OrderSend(Symbol(),OP_BUY,0.63,Ask,0,Close[1]-25*Point,0,"",MAGICMA,0,Red);
      return;
     }
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+

void CheckForClose()
{
   RefreshRates();
   if(OrderOpenPrice()+4*Point< Ask)
   for (int i = OrdersTotal()-1; i >= 0;i--)       // I Have do -1 in "OrdersTotal()-1"
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if (OrderSymbol() == Symbol()) 
      {
         bool ticket = true;
         if (OrderType() == OP_BUY) ticket = OrderClose(OrderTicket(),OrderLots(),Bid,1,Black);

      }
   }
} 
/*
void CheckForClose2()
{
   RefreshRates();
   if(OrderOpenPrice()+6*Point< Ask)
   for (int i = OrdersTotal(); i >= 0;i--)
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if (OrderSymbol() == Symbol()) 
      {
         bool ticket = true;
         if (OrderType() == OP_BUY) ticket = OrderClose(OrderTicket(),OrderLots(),Bid,1,Black);

      }
   }
} 
*/
                                       
//+------------------------------------------------------------------+
//| Start function                                 |
//+------------------------------------------------------------------+
void start()
  {
      
//------------------                                                          PART1      
           bool SignalBULL41Executed=false;
            if(BULL4() && !SignalBULL41Executed && OpenOrders(Symbol())==0)
               {
               OpenBULL41();//do open buy position
               SignalBULL41Executed=true;
               }
               
            bool SignalBULL42Executed=false;
            if(!SignalBULL42Executed && OpenOrders(Symbol())==1)
               {
               OpenBULL42();//do open buy position
               SignalBULL42Executed=true;
               }  
               
            if(OpenOrders(Symbol())>0)CheckForClose();  
            
//------------------                                                          PART 2
            
            bool SignalBULL61Executed=false;
            if(BULL6() && !SignalBULL61Executed && OpenOrders(Symbol())==0)
               {
               OpenBULL61();//do open buy position
               SignalBULL61Executed=true;
               }
               
            bool SignalBULL62Executed=false;
            if(!SignalBULL62Executed && OpenOrders(Symbol())==1)
               {
               OpenBULL62();//do open buy position
               SignalBULL62Executed=true;
               }  
               
           bool SignalBULL63Executed=false;
            if(!SignalBULL63Executed && OpenOrders(Symbol())==2)
               {
               OpenBULL63();//do open buy position
               SignalBULL63Executed=true;
               }                
               
//            if(SignalBULL61Executed && OpenOrders(Symbol())>0)CheckForClose2();                    
            
  }
//+------------------------------------------------------------------+
 
Wodzuuu:

Not use options if (NewBar) ResetSignals () because I think they are not the cause of the error

The way that the function Fun_NewBar()  is coded won't allow you to do that . . .  you need to do it like this so that it is type bool and returns a bool value . . .

bool Fun_New_Bar()                
   {                                
   static datetime New_Time=0;      
                     
   if(New_Time!=Time[0])           
      {
      New_Time=Time[0];                
      return(true);       // <---- we have a new bar so the function returns true
      }

   return(false);
   }
 

Yes you're right returns bool and there is int (already fixed) I do not see before. Thank You
But this function I do not use,  it  is as a text

 I do not think this applies to functions that separates me two overlapping strategies. I do not see the relationship. Or maybe I'm wrong?

//Pseudo code

if(NewBar) ResetSignals();                //Set all SignalxExecuted to false
if(SignalBuy1&&!SignalBuy1Executed&&OpenOrders()==1){
//do open buy position
  SignalBuy1Executed=true;                //Set Signal to exectued to prevent a reuse in the same bar
}
 

Good to see you made some progress, but it is still a long way to go :-)

You need to fix what RaptorUK stated above. The reset of the Signals is important, otherwise the EA stopps quite soon with opening new trades.
Your implementation is wrong. If you reset the SignalExecuted with every tick, it has no sense. Maybe it does not open more trades, because you filter also the OpenOrders. But, as your strategy allows to have multiple orders open, I can imagine you also want an algorithm to close orders, so you need the SignalExecuted flag reset on a new bar (with your implementation it is useless and you don't need it).

If I get you correct, you won't execute BULL4 if BULL6 is also true. Or in other words, blue strategy only, if red strategy is not valid, or small candle only if average candle is not valid.

if(BULL4()&&!BULL6(){             // <--- only strategy BULL4 is valid, we are looking at a 'small' bar.
  if(!SignalBULL41Executed && OpenOrders(Symbol())==0){
    OpenBULL41();//do open buy position
    SignalBULL41Executed=true;
  }
  if(!SignalBULL42Executed && OpenOrders(Symbol())==1){
    OpenBULL42();//do open buy position
    SignalBULL42Executed=true;
}
 

Next steps:

// Check for open for scenario 1 / EA 1  /  startegy 1 
 
bool OpenBULL41()
  {
   int    res;
   if(Bid==Close[1]+3*Point)  					                       // <--- equal makes no sense here and you should not compare doubles for equality. 
                                                                                                  Read (again): https://www.mql5.com/en/forum/136997 
     {
      res=OrderSend(Symbol(),OP_BUY,0.41,Ask,0,Close[1]-25*Point,0,"",MAGICMA,0,Blue); // <--- what is your plan with res? You are aware, it does include the ticket 
                                                                                                  number or -1, aren't you?

      return (true);                                                                   // <--- Even if OrderSend() fails you return true.
     }
   return (false);  
  }
  
//----------------------------------- 
bool OpenBULL42()
  {
   int    res;
   if(Ask>OrderOpenPrice()+4*Point) 							// <--- OrderOpenPrice() from a somewhere selected order may or may not work here 
                                                                                                  (may because only one order is open), but it is not nice coded. 
     {
      res=OrderSend(Symbol(),OP_BUY,0.42,Ask,0,Close[1]-25*Point,0,"",MAGICMA,0,Blue);
      return;
     }
  }  

The CheckForClose() function has pretty much the same issues and additonally no filter for the magic number. Also here, OrderOpenPrice() selected from an order in the buy count could end in closing the wrong order (I am not sure it works actually).
Please also read again what Raptor wrote on the first page of this thread.

From your posts and the comments in the code, I have a feeling you want to know which order belongs to which strategy. To achieve that, you can define a second magic number for the second strategy. Then it is easier to close orders according to the strategy they were open 

 
kronin:

Next steps:

From your posts and the comments in the code, I have a feeling you want to know which order belongs to which strategy. To achieve that, you can define a second magic number for the second strategy. Then it is easier to close orders according to the strategy they were open 

Wouldn't it just be simpler to run 2 different strategies in 2 separate EAs,  each on it's own chart ?
 
RaptorUK:
Wouldn't it just be simpler to run 2 different strategies in 2 separate EAs,  each on it's own chart ?

Yes, for 2, 3, 4, 5... agreed. But according to the stories and this one : "I have to write 150 policies and they must walk at the same time on the same graph, and run all the strategies at once. When I run 150 EA at the same time my PC will explode - I think so."
I think he plans to run many strategies more in one EA....