Download MetaTrader 5
To add comments, please log in or register
Have you discovered something really important? Write an article and receive 200 USD!
Uncl3B0B
29
Uncl3B0B 2016.04.11 10:10 

How does it do to the command entered, it is not yet entry and that did not enter when it is entered? So that each command entered only once.

 

if (((C1 -60*Point) > O1) && ((H1 -100*Point) > L1))  
      {ticket=OrderSend (Symbol(),OP_BUYSTOP,Lots,H1+25*Point+Spread,0,H1+25*Point+Spread-SL, H1+25*Point+Spread+PT_1,51, MN1, ex,Blue);
       ticket=OrderSend (Symbol(),OP_BUYSTOP,Lots,H1+25*Point+Spread,0,H1+25*Point+Spread-SL, H1+25*Point+Spread+PT_2,52, MN2, ex,Blue);
       ticket=OrderSend (Symbol(),OP_BUYSTOP,Lots,H1+25*Point+Spread,0,H1+25*Point+Spread-SL2,H1+25*Point+Spread+PT_3,53, MN3, ex,Blue);} 

 

 When add restrictions, so one command unnecessarily blocking all others:

 for(p = 0; p < OrdersTotal(); p++) {if(OrderSelect(p, SELECT_BY_POS, MODE_TRADES) && (OrderMagicNumber()==MN1)) {return(0);}}
 for(p = 0; p < OrdersTotal(); p++) {if(OrderSelect(p, SELECT_BY_POS, MODE_TRADES) && (OrderMagicNumber()==MN2)) {return(0);}}
 for(p = 0; p < OrdersTotal(); p++) {if(OrderSelect(p, SELECT_BY_POS, MODE_TRADES) && (OrderMagicNumber()==MN3)) {return(0);}}

 

 What specify the condition to command with a specific Magic Number was the always only one?

THX 

Fernando Carreiro
1438
Fernando Carreiro 2016.04.11 10:32  

@Uncl3B0B: Sorry, but it is very difficult to understand your query. Your English is very difficult to understand.

It seems that you want a way to only select one of the orders for just one magic number but it is unclear why your code would be written for 3 magic numbers in the first place.

Can you perhaps please try again, maybe have someone help you out in explaining it in a more understandable English please?

Or maybe, someone here from the Czech Republic could open a dialog with you in your native language, via private messages, in order to help you out.

Uncl3B0B
29
Uncl3B0B 2016.04.11 11:23  

Yes, my english is too bad - sorry. I try to send it again.

 

 I send order / I enter the command 

if (((C1 -60*Point) > O1) && ((H1 -100*Point) > L1))  
      {ticket=OrderSend (Symbol(),OP_BUYSTOP,Lots,H1+25*Point+Spread,0,H1+25*Point+Spread-SL, H1+25*Point+Spread+PT_1,51, MN1, ex,Blue);}

 

 The command will be entered again and again. Prevent so that before command I give restrictions:

for(p = 0; p < OrdersTotal(); p++) {if(OrderSelect(p, SELECT_BY_POS, MODE_TRADES) && (OrderMagicNumber()==MN1)) {return(0);}}

This works. But until moments before want more independent commands.

 When I want to send more commands to be independent of each other, so the restrictions blocking all

How to make restrictions that the individual orders were independent of each other?

Fernando Carreiro
1438
Fernando Carreiro 2016.04.11 23:19  

This should give you an idea of a possible solution to you problem. Some variables and functions are just place-holders for you own code and logic, namely "MyMagicNumber", "BuySignal", "SellSignal", "PlaceBuyOrder" and "PlaceSellOrder".

// Check Open Orders and Count Them
int OpenBuyOrderCount = 0, OpenSellOrderCount = 0;
for( int i = OrdersTotal() - 1; i >= 0; i-- )
{
   if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
   {
      if( ( OrderMagicNumber() == MyMagicNumber ) && ( OrderSymbol() == _Symbol ) )
      {
         switch( OrderType() )
         {
            case OP_BUY:
               OpenBuyOrderCount++;
               break;
               
            case OP_SELL:
               OpenSellOrderCount++;
               break;
         }
      }      
   }
}

// Check Signal and Place Order if None exist in the same direction
if( BuySignal  && ( OpenBuyOrderCount  < 1 ) ) PlaceBuyOrder();
if( SellSignal && ( OpenSellOrderCount < 1 ) ) PlaceSellOrder();
Uncl3B0B
29
Uncl3B0B 2016.04.12 09:34  

This is my homemade creation. Everything works - only restriction blocks all commands, not just one.

One running order, not allow other order activated - I need activated order while other order runnig ...

extern double Lots = 0.1;                
extern double Stoploss_1 = 35;
extern double Stoploss_2 = 45;         
extern double Profit_target_1 = 120;     
extern double Profit_target_2 = 200;
extern double Profit_target_3 = 350;
extern double ShiftBE1 = 35;  
extern double ShiftBE2 = 55;

int start()  {  
//+------------------------------------------------------------------+
//| Definition of variables                                          |
//+------------------------------------------------------------------+
   int D = DayOfWeek();                         
   int m = TimeMinute(TimeCurrent());           
   int h = TimeHour(TimeCurrent());             
   int s = TimeSeconds(TimeCurrent());
   int    MN1 = 991; 
   int    MN2 = 992;  
   int    MN3 = 993;
   int    buy  = 0,sell = 0, ticket, p, modify,result;                    
 
   double SL   = Stoploss_1*Point; 
   double SL2  = Stoploss_2*Point;   
   double PT_1 = Profit_target_1*Point; 
   double PT_2 = Profit_target_2*Point; 
   double PT_3 = Profit_target_3*Point; 
   
   double BE1 = ShiftBE1*Point; 
   double BE2 = ShiftBE2*Point;
                       
   double H1 = iHigh (NULL,PERIOD_M5,1);        
   double L1 = iLow  (NULL,PERIOD_M5,1);        
  
   double O1 = iOpen (NULL,PERIOD_M5,1);         
   double C1 = iClose(NULL,PERIOD_M5,1);
      
   double Spread = 10*Point;                     
   double ex = iTime(Symbol(),PERIOD_M1,0)+ 1489; 
//+------------------------------------------------------------------+
//| Shift to BE                                                      |
//+------------------------------------------------------------------+ 
 for(p = 0; p < OrdersTotal(); p++) {if (OrderSelect(p,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_BUY && Ask >= OrderOpenPrice()+ BE1
     && (OrderMagicNumber()==MN1 || OrderMagicNumber()==MN2 || OrderMagicNumber()==MN3 ) && OrderStopLoss()<OrderOpenPrice()) 
   { modify=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, Orange);}}
 for(p = 0; p < OrdersTotal(); p++) {if (OrderSelect(p,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_SELL && Bid <= OrderOpenPrice() - BE1
     && (OrderMagicNumber()==MN1 || OrderMagicNumber()==MN2 || OrderMagicNumber()==MN3 ) && OrderStopLoss()>OrderOpenPrice()) 
   { modify=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, Orange );}}

 for(p = 0; p < OrdersTotal(); p++) {if (OrderSelect(p,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_BUY && Ask >= OrderOpenPrice()+ BE2
     && (OrderMagicNumber()==MN1 || OrderMagicNumber()==MN2 || OrderMagicNumber()==MN3)  && OrderStopLoss()<OrderOpenPrice()+10*Point ) 
   { modify=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()+10*Point, OrderTakeProfit(), 0, Orange);}}
 for(p = 0; p < OrdersTotal(); p++) {if (OrderSelect(p,SELECT_BY_POS,MODE_TRADES) && OrderType()==OP_SELL && Bid <= OrderOpenPrice() - BE2
     && (OrderMagicNumber()==MN1 || OrderMagicNumber()==MN2 || OrderMagicNumber()==MN3 ) && OrderStopLoss()>OrderOpenPrice()-10*Point) 
   { modify=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()-10*Point, OrderTakeProfit(), 0, Orange );}}

//+------------------------------------------------------------------+   
//| Trailing Stop                                                    |   
//+------------------------------------------------------------------+  
for(p = 0; p < OrdersTotal(); p++) {if (OrderSelect(p,SELECT_BY_POS,MODE_TRADES)==true && OrderType()==OP_BUY &&  
      (OrderMagicNumber()==MN1 ) && Ask-OrderOpenPrice()> 70*Point && (Ask-70*Point) > OrderStopLoss()) 
     { modify=OrderModify(OrderTicket(), OrderOpenPrice(), Ask-70*Point, OrderTakeProfit(), 0, Green);}}
for(p = 0; p < OrdersTotal(); p++) {if (OrderSelect(p,SELECT_BY_POS,MODE_TRADES)==true && OrderType()==OP_SELL && 
      (OrderMagicNumber()==MN1  ) && OrderOpenPrice()- Bid > 70*Point && (Bid+70*Point) < OrderStopLoss())  
     { modify=OrderModify(OrderTicket(), OrderOpenPrice(),Bid+70*Point, OrderTakeProfit(), 0, Green);}}  
     
for(p = 0; p < OrdersTotal(); p++) {if (OrderSelect(p,SELECT_BY_POS,MODE_TRADES)==true && OrderType()==OP_BUY &&  
      ( OrderMagicNumber()==MN2 || OrderMagicNumber()==MN3) && Ask-OrderOpenPrice()> 160*Point && (Ask-160*Point) > OrderStopLoss()) 
     { modify=OrderModify(OrderTicket(), OrderOpenPrice(), Ask-160*Point, OrderTakeProfit(), 0, Green);}}
for(p = 0; p < OrdersTotal(); p++) {if (OrderSelect(p,SELECT_BY_POS,MODE_TRADES)==true && OrderType()==OP_SELL && 
      (OrderMagicNumber()==MN2 || OrderMagicNumber()==MN3 ) && OrderOpenPrice()- Bid > 160*Point && (Bid+160*Point) < OrderStopLoss())  
     { modify=OrderModify(OrderTicket(), OrderOpenPrice(),Bid+160*Point, OrderTakeProfit(), 0, Green);}} 

//+------------------------------------------------------------------+
//| Close awaiting instructions on activation opposite positions     |
//+------------------------------------------------------------------+
for (p = 0; p < OrdersTotal(); p++) {if (OrderSelect(p, SELECT_BY_POS, MODE_TRADES )&& OrderMagicNumber()==MN1) {if (OrderSymbol() == Symbol()) {if (OrderType() == OP_BUY ) buy++; if (OrderType() == OP_SELL) sell++;    } } }
   if (buy > 0) {for (p = 0; p < OrdersTotal(); p++){if (OrderSelect (p, SELECT_BY_POS, MODE_TRADES )&& OrderMagicNumber()==MN1)  {if (OrderSymbol() == Symbol()) {if  (OrderType() == OP_SELLSTOP) result =  OrderDelete(OrderTicket(),Yellow); } } } }
   if (sell > 0) {for (p = 0; p < OrdersTotal(); p++) {if (OrderSelect (p, SELECT_BY_POS, MODE_TRADES )&& OrderMagicNumber()==MN1)  {if (OrderSymbol() == Symbol()) {if (OrderType() == OP_BUYSTOP) result =  OrderDelete(OrderTicket(),Yellow); } } } }  
 for (p = 0; p < OrdersTotal(); p++) {if (OrderSelect(p, SELECT_BY_POS, MODE_TRADES )&& OrderMagicNumber()==MN2) {if (OrderSymbol() == Symbol()) {if (OrderType() == OP_BUY ) buy++; if (OrderType() == OP_SELL) sell++;    } } }
   if (buy > 0) {for (p = 0; p < OrdersTotal(); p++){if (OrderSelect (p, SELECT_BY_POS, MODE_TRADES )&& OrderMagicNumber()==MN2)  {if (OrderSymbol() == Symbol()) {if  (OrderType() == OP_SELLSTOP) result =  OrderDelete(OrderTicket(),Yellow); } } } }
   if (sell > 0) {for (p = 0; p < OrdersTotal(); p++) {if (OrderSelect (p, SELECT_BY_POS, MODE_TRADES )&& OrderMagicNumber()==MN2)  {if (OrderSymbol() == Symbol()) {if (OrderType() == OP_BUYSTOP) result =  OrderDelete(OrderTicket(),Yellow); } } } }  
 for (p = 0; p < OrdersTotal(); p++) {if (OrderSelect(p, SELECT_BY_POS, MODE_TRADES )&& OrderMagicNumber()==MN3) {if (OrderSymbol() == Symbol()) {if (OrderType() == OP_BUY ) buy++; if (OrderType() == OP_SELL) sell++;    } } }
   if (buy > 0) {for (p = 0; p < OrdersTotal(); p++){if (OrderSelect (p, SELECT_BY_POS, MODE_TRADES )&& OrderMagicNumber()==MN3)  {if (OrderSymbol() == Symbol()) {if  (OrderType() == OP_SELLSTOP) result =  OrderDelete(OrderTicket(),Yellow); } } } }
   if (sell > 0) {for (p = 0; p < OrdersTotal(); p++) {if (OrderSelect (p, SELECT_BY_POS, MODE_TRADES )&& OrderMagicNumber()==MN3)  {if (OrderSymbol() == Symbol()) {if (OrderType() == OP_BUYSTOP) result =  OrderDelete(OrderTicket(),Yellow); } } } }  
 
//+------------------------------------------------------------------+
//| Expiration/Deleted                                               |
//+------------------------------------------------------------------+     
 if (TimeCurrent() - OrderOpenTime()>=4.5*60)for(p = 0; p < OrdersTotal(); p++) {if (OrderSelect (p, SELECT_BY_POS, MODE_TRADES )&& (OrderMagicNumber()==MN1 || OrderMagicNumber()==MN2 ))
     {if (OrderType() == OP_BUYSTOP || OP_SELLSTOP) result =  OrderDelete(OrderTicket(),Yellow);} }
 if (TimeCurrent() - OrderOpenTime()>=5*60) for(p = 0; p < OrdersTotal(); p++) {if (OrderSelect (p, SELECT_BY_POS, MODE_TRADES )&& (OrderMagicNumber()==MN3 ))
     {if (OrderType() == OP_BUYSTOP || OP_SELLSTOP) result =  OrderDelete(OrderTicket(),Yellow);} }    
//+------------------------------------------------------------------+
//| Restrictions                                                     |
//+------------------------------------------------------------------+  
 for(p = 0; p < OrdersTotal(); p++) {if(OrderSelect(p, SELECT_BY_POS, MODE_TRADES) && (OrderMagicNumber()==MN1)) {return(0);}}    // restriction blocks all commands, not just one.
 for(p = 0; p < OrdersTotal(); p++) {if(OrderSelect(p, SELECT_BY_POS, MODE_TRADES) && (OrderMagicNumber()==MN2)) {return(0);}}    // restriction blocks all commands, not just one.
 for(p = 0; p < OrdersTotal(); p++) {if(OrderSelect(p, SELECT_BY_POS, MODE_TRADES) && (OrderMagicNumber()==MN3)) {return(0);}}    // restriction blocks all commands, not just one.
 
//+------------------------------------------------------------------+
//| Buy                                                              |    
//+------------------------------------------------------------------+    
  if (((C1 -60*Point) > O1 && (H1 -100*Point) > L1)  && s > 0 && s < 10 && D >= 1 && h >= 9 &&  ((m==0)||(m==5)||(m==10)||(m==15)||(m==20)||(m==25)||(m==30)||(m==35)||(m==40)||(m==45)||(m==50)||(m==55)))  
      {ticket=OrderSend (Symbol(),OP_BUYSTOP,Lots,H1+25*Point+Spread,0,H1+25*Point+Spread-SL, H1+25*Point+Spread+PT_1,51, MN1, ex,Blue);
       ticket=OrderSend (Symbol(),OP_BUYSTOP,Lots,H1+25*Point+Spread,0,H1+25*Point+Spread-SL, H1+25*Point+Spread+PT_2,52, MN2, ex,Blue);
       ticket=OrderSend (Symbol(),OP_BUYSTOP,Lots,H1+25*Point+Spread,0,H1+25*Point+Spread-SL2,H1+25*Point+Spread+PT_3,53, MN3, ex,Blue); } 
  
//+------------------------------------------------------------------+
//|  Sell                                                            |    
//+------------------------------------------------------------------+ 
  if (((C1 +60*Point) < O1 && (L1 +100*Point) < H1)  && s > 0 && s < 10 && D >= 1 && h >= 9 &&  ((m==0)||(m==5)||(m==10)||(m==15)||(m==20)||(m==25)||(m==30)||(m==35)||(m==40)||(m==45)||(m==50)||(m==55)))
      {ticket=OrderSend (Symbol(),OP_SELLSTOP, Lots,L1-25*Point, 0,L1-25*Point+SL, L1-25*Point-PT_1,51, MN1, ex,Red); 
       ticket=OrderSend (Symbol(),OP_SELLSTOP, Lots,L1-25*Point, 0,L1-25*Point+SL, L1-25*Point-PT_2,52, MN2, ex,Red);
       ticket=OrderSend (Symbol(),OP_SELLSTOP, Lots,L1-25*Point, 0,L1-25*Point+SL2,L1-25*Point-PT_3,53, MN3, ex,Red); } 
   
   return(0);}
//+------------------------------------------------------------------+

  
Fernando Carreiro
1438
Fernando Carreiro 2016.04.12 11:59  

The whole thing is just TOO wrong! You have WAY TOO MANY loops over the Orders and your so called "Restriction" blocks are absolutely useless.

Have a look at my example code to get some orientation. You can probably do everything with a single loop over the orders.

Your code seems to want to align things to the 5Min period, so just attached it to a M5 chart and code it to work with the current period instead. There is no need to hard code time-frames in this case.

Also, you will seriously benefit from some code structure and some array usage too.

Right now, I am quite busy, but later today, I will try to post a total rewrite of your code, but done properly in order to help you out, so hang in there.

Fernando Carreiro
1438
Fernando Carreiro 2016.04.12 15:21  

Now that I am back, I took some time to really analyse your code. My conclusion, is that there is just too many fundamental problems with it and too many illogical constructs.

So, instead of trying to write an EA to handle 3 different types of order configurations, with Stop-Loss, Take-Profit, Break-Even, Trailing-Stop, specialized  timing conditions, etc. - why don't you first tackle a simple single order EA.

Code a simple EA, that just has Take-Profit and Stop-Loss and nothing else with just a single order. If you have difficulty getting it to work, post it here for some help, until it is works correctly.

Then slowly build on it, adding the parts you need, until you reach the stage you want to achieve. At each stage, as you run into problems, come here with your questions.

At the moment, your current code has just too much functionality that is just not correctly done, and trying to explain every part, is going to be overwhelming for you as well as anyone explaining it to you.

Sorry if this seems disheartening, but lets start small and slowly build up to it.

Uncl3B0B
29
Uncl3B0B 2016.04.13 11:42  
Thanks for your help .... but because of the language barrier and because MQL not know, we would not get very far. MQL I "taught" to one web site, which use similar commands which I posted here - and I understand that. I know why did not my limit, but I do not know how to fix it. I try to use your design - a style applied but I do not understand, so hopefully it setting up.
/
To add comments, please log in or register