is it posible to open 2 different Orders at the same signal?

 

Hi all pro coders,

Im quite new to OrderSend function in mql ... I want to know that is it posible to open 2 different Orders at the same tick when there is a new signal?

in fact, I want to open 2 different Orders with its own MagicNumbers at the same tick when there is a new signal generated ... then i will apply 2 different exit strategies later.

Do I have to use RefreshRates() before calling OrderSend func for the second Order?

is there any problem with the simple peice of code below ? ... could you guys please shed me some light :)

have a good day & thank you so much!

#property strict

int     magic1       = 333333;
int     magic2       = 999999;
double  lot1         = 0.02;
double  lot2         = 0.01;
int MaxWaiting_sec   = 30;
//---
bool isNewPeriod(int period)
{
   static datetime dtTime = 0;
   if( Time[period] != dtTime)
      {dtTime = Time[period];return(true);}
   else                     {return(false);}
}
//---
void _IsTradeAllowed(int maxWaiting_Sec = 30)
{   
     int StartWaitingTime;
         // check whether the trade context is free
         if(!IsTradeAllowed())
          {
           StartWaitingTime = (int)GetTickCount();
           // infinite loop
           while(true)
             {
               if(IsStopped())                                                      {Print("The expert was stopped by the user!");return;}                
               if((int)GetTickCount() - StartWaitingTime > maxWaiting_Sec * 1000)   {Print("The standby limit (" + (string)maxWaiting_Sec + " sec) exceeded!");return;}
               // if the trade context has become free,
               if(IsTradeAllowed())                                                 {Print("Trade context is free!");break;}
               Sleep(100);
             }
          }// finally trade is allowed :)
          else                                                                      {Print("Trade context is free!");}
}
//---
void OnTick()
{
double ask;

if (!isNewPeriod(15)) return;
// Trading criteria
 double  MA1 = iMA(NULL,15,10,0,MODE_LWMA,PRICE_TYPICAL,0);
 double  MA2 = iMA(NULL,15,50,0,MODE_LWMA,PRICE_TYPICAL,0); 

// Opening BUY orders
    if (MA1 < MA2)
    {
      //Opening BUY1
         _IsTradeAllowed(MaxWaiting_sec);                                        // check whether the trade context is free
         RefreshRates();                                                         // Refresh rates
         ask = MarketInfo(Symbol(),MODE_ASK);
         int TicketBUY1 = OrderSend(Symbol(),OP_BUY,lot1,ask,3,0,0,NULL,magic1); //Opening Buy1
         if (TicketBUY1 < 0) {Alert("Error opening BUY1 ", GetLastError());}              
         else                {Alert("Successfully opened order BUY1 ",TicketBUY1);}
         
      //Opening BUY2
         _IsTradeAllowed(MaxWaiting_sec);                                        // check whether the trade context is free                           
         RefreshRates();                                                         // Refresh rates
         ask = MarketInfo(Symbol(),MODE_ASK);
         int TicketBUY2 = OrderSend(Symbol(),OP_BUY,lot2,ask,3,0,0,NULL,magic2); //Opening Buy2
         if (TicketBUY2 < 0) {Alert("Error opening BUY2 ", GetLastError());}
         else                {Alert("Successfully opened order BUY2 ",TicketBUY2);} 
    }
} // end

 
aphong:


of course it is possible.

just some normal trading operations.

however, you may have to do the coding work by yourself or you can try :

https://www.mql5.com/en/job

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • www.mql5.com
Hello, I need you to develop a "Next 2 Min Candle Prediction Indicator" by using the provided Candle_Direction.ex4 Indicator. (attached) Alternatively if you have better indication, it's up to your choice. Since MT4 doesn't provide 2 min chart, you may use consecutive 1 min chart for 2 minutes in a row. That means every 2 minutes next candle's...
 
tickfenix:

of course it is possible.

just some normal trading operations.

however, you may have to do the coding work by yourself or you can try :

https://www.mql5.com/en/job

Thanks  tickfenix so much

I just update a simple peice of code to better describe my thought ... is it the right way to do the task? 

I just read some of the Docs of OrderSend func & im confused a little bit! LOL

... if It s not Could you shed me some light on "some normal trading operations" ... I think i can code simple stuff pretty well :)


 

Do I have to use RefreshRates() before calling OrderSend func for the second Order?

Most brokers today accept several trading requests sent in a short period of time, say, one second.

However, because of the transaction mechanism on the broker's server, you will only get a deal at the "best possible" price.

It's quite normal that the price specified in the OrderSend() function is not the exact price to be executed.

The price you actually get depends on the latest quote on the server side at that time.

So, if your requirement is to get the deal done first, rather than the best price, you can simply set a looser "slippage" range and send the order request at the current Ask/Bid.

In other words, there is no need to use RefreshRates().


Documentation on MQL5: Trade Functions / OrderSend
Documentation on MQL5: Trade Functions / OrderSend
  • www.mql5.com
[in,out]  Pointer to a structure of MqlTradeResult type describing the result of trade operation in case of a successful completion (if true is returned). parameter are filled out correctly. If there are no errors, the server accepts the order for further processing. If the order is successfully accepted by the trade server, the OrderSend...
 
aphong:


And, I don't like that _IsTradeAllowed() in your code.

This is simpler:

   if ( IsConnected() && IsTradeAllowed() && !IsTradeContextBusy() ) 
   { 
      //go! 
   }


Also, there's a problem in your code: how do you avoid infinite open positions?

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
tickfenix:

And, I don't like that _IsTradeAllowed() in your code.

1. This is simpler...

2. Also, there's a problem in your code: how do you avoid infinite open positions?...

1. "This is simpler... " yep You re right, I just read some latest Infor on TradeContext!  & Its a big help for me indeed, hey i just modified my code a bit, check it out please :)

2. "...how do you avoid infinite open positions? " Uhmm, I thought If I use this code line /if (!isNewPeriod(15)) return;/  then It should only open maximum one new order per signal generated  if there is a new M15 candle, isnt it? 

Thank you so much for your recent help ... I ve leant alot!
have a good day! :)

#property strict

int     magic1       = 333333;
int     magic2       = 999999;
double  lot1         = 0.02;
double  lot2         = 0.01;
int MaxWaiting_sec   = 10;
//---
bool isNewPeriod(int period)
{
   static datetime dtTime = 0;
   if( Time[period] != dtTime)
      {dtTime = Time[period];return(true);}
   else                     {return(false);}
}
//---
bool _CheckTradeContextBusy(int maxWaiting_Sec = 10)
{   
       bool check = true;
       int StartWaitingTime;
       // check whether the trade context is free
       if (IsTradeContextBusy()) 
          {  
             while(IsTradeContextBusy())
                {
                 StartWaitingTime = (int)GetTickCount();
                     if(IsStopped())                                                      {Print("The expert was stopped by the user!");check=false; break;}                
                     if((int)GetTickCount() - StartWaitingTime > maxWaiting_Sec * 1000)   {Print("The standby limit (" + (string)maxWaiting_Sec + " sec) exceeded!");check=false;break;}
                     // if the trade context has become free,
                     if(!IsTradeContextBusy())                                            {Print("Trade context is free!");check=true;break;}
                     Sleep(10);     
                }
             // if we make it here & (check == true) then TradeContext is not busy anymore!
             if(!IsTradeContextBusy())                                                    {Print("Trade context is free!");check=true;}   
          }
       else {Print("Trade context is free!"); check=true;}
 //---                                                                                 
return (check);
}
//---
void OnTick()
{
double ask;

if (!isNewPeriod(15)) return;
// Trading criteria
 double  MA1 = iMA(NULL,15,10,0,MODE_LWMA,PRICE_TYPICAL,0);
 double  MA2 = iMA(NULL,15,50,0,MODE_LWMA,PRICE_TYPICAL,0); 

// Opening BUY orders
    if (MA1 < MA2)
    {
      //Opening BUY1
         if (IsConnected()&&IsTradeAllowed()&&_CheckTradeContextBusy(MaxWaiting_sec)){    // check whether the trade context is free
            RefreshRates();                                                               // Refresh rates
            ask = MarketInfo(Symbol(),MODE_ASK);
            int TicketBUY1 = OrderSend(Symbol(),OP_BUY,lot1,ask,3,0,0,NULL,magic1);       //Opening Buy1
            if (TicketBUY1 < 0) {Alert("Error opening BUY1 ", GetLastError());}              
            else                {Alert("Successfully opened order BUY1 ",TicketBUY1);}
         }
         
      //Opening BUY2
         if (IsConnected()&&IsTradeAllowed()&&_CheckTradeContextBusy(MaxWaiting_sec)){    // check whether the trade context is free                          
            RefreshRates();                                                               // Refresh rates
            ask = MarketInfo(Symbol(),MODE_ASK);
            int TicketBUY2 = OrderSend(Symbol(),OP_BUY,lot2,ask,3,0,0,NULL,magic2);       //Opening Buy2
            if (TicketBUY2 < 0) {Alert("Error opening BUY2 ", GetLastError());}
            else                {Alert("Successfully opened order BUY2 ",TicketBUY2);}
         } 
    }
} // end
Reason: