Download MetaTrader 5

EA to open a order everytime the conditions are fulfilled

To add comments, please log in or register
marve7ou_marc
21
marve7ou_marc  

Hey there,


I programmed an EA that is working all in all pretty good. But there is one problem, it only open one short and one long order and then wait until these are closed to open the next orders. But I want this EA to open an order every time my conditions are fulfilled. The broker allows that but I can't figure out where the problem is. Can someone hint me to a solution for that issue?

thank you in advance

 //Open Buy Order
      if(BuySignalshort == true)
         {
            while(LongOrdershort<=4)
               {
                  
                  LongOrdershort = OrderSend(Symbol(),OP_BUY,longTradeLots,Ask,MySlippage,0,0,"MaxingLong",MagicNumber,0,Green);
                     if(LongOrdershort<0)
                     {
                        Print("OrderSend failed with error'",GetLastError());
                     }
                     else
                     {
                        Print("OrderSend places successfully");
                     }
               }
         }
      //Open Sell Order
      if (SellSignalshort == true)
         {
            while(ShortOrdershort<=4)
               {
                  
                  ShortOrdershort = OrderSend(Symbol(),OP_SELL,shortTradeLots,Bid,MySlippage,0,0,"MaxingShort",MagicNumber,0,Red);
                     if(ShortOrdershort<0)
                     {
                        Print("OrderSend failed with error'",GetLastError());
                     }
                     else
                     {
                        Print("OrderSend places successfully");
                     }
               }
         }

      
      //TakeProfit BuyOrdershort
      if(OrderSelect(LongOrdershort,SELECT_BY_TICKET)==true)
         {
            if(OrderCloseTime()==0 && OrderTakeProfit()==0)
            {       
               double TakeProfit= NormalizeDouble(longTakeProfit,Digits);
               bool OrderChange = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,0,Orange);
            }
         }
         
       //TakeProfit SellOrdershort
      if(OrderSelect(ShortOrdershort,SELECT_BY_TICKET)==true)
         {
            if(OrderCloseTime()==0 && OrderTakeProfit()==0)
            {
               double TakeProfit= NormalizeDouble(OrderOpenPrice()-(StdDevshort*2),Digits);
               bool OrderChange = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,0,Orange);
            }
         }
      

      
      //SL Delay
      //Sleep(24000);
         
      //StopLoss BuyOrdershort
      if(OrderSelect(LongOrdershort,SELECT_BY_TICKET)==true)
         {
            if(OrderCloseTime()==0 && OrderStopLoss()==0)
            {
               double StopLoss= NormalizeDouble((MAshort - (StdDevshort*shortSLFactor)),Digits);
               bool OrderChange = OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,OrderTakeProfit(),0,Yellow);
            }
         }
         
      //StopLoss SellOrdershort
      if(OrderSelect(ShortOrdershort,SELECT_BY_TICKET)==true)
         {
            if(OrderCloseTime()==0 && OrderStopLoss()==0)
            {
               double StopLoss= NormalizeDouble(MAshort +(StdDevshort*shortSLFactor),Digits);
               bool OrderChange = OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,OrderTakeProfit(),0,Yellow);
            }
         }
         

      
         
      //Set ticketnumber to 0 after closing
      if(OrderSelect(LongOrdershort,SELECT_BY_TICKET)==true)
         {
            if(OrderTicket()>0 && OrderCloseTime()>0) LongOrdershort=0;
         }
         
      if(OrderSelect(ShortOrdershort,SELECT_BY_TICKET)==true)
         {
            if(OrderTicket()>0 && OrderCloseTime()>0) ShortOrdershort=0;
         }
  }
  
  
  // Get My Points   
double MyPoint()
{
   double CalcPoint = 0;
   
   if(_Digits == 2 || _Digits == 3) CalcPoint = 0.01;
   else if(_Digits == 4 || _Digits == 5) CalcPoint = 0.0001;
   
   return(CalcPoint);
}


// Get My Slippage
int MySlippage()
{
   int CalcSlippage = 0;
   
   if(_Digits == 2 || _Digits == 4) CalcSlippage = Slippage;
   else if(_Digits == 3 || _Digits == 5) CalcSlippage = Slippage * 10;
   
   return(CalcSlippage);
}
Drazen Penic
2729
Drazen Penic  

LongOrdershort and ShortOrdershort will get ticket number after opening first successful long and short orders.

Ticket numbers are integers assigned on the server and they are ordinal numbers of the orders on the server. They are assigned in a sequence for all active accounts on the particular server and/or account type at the broker.

So, they are way bigger than 4 and you can't use them as a number of active orders on your MT4 instance.


But, if you run this EA in the tester, orders will get ticket numbers starting with 1 because those ticket numbers are generated on your computer.

marve7ou_marc
21
marve7ou_marc  
Drazen Penic:

LongOrdershort and ShortOrdershort will get ticket number after opening first successful long and short orders.

Ticket numbers are integers assigned on the server and they are ordinal numbers of the orders on the server. They are assigned in a sequence for all active accounts on the particular server and/or account type at the broker.

So, they are way bigger than 4 and you can't use them as a number of active orders on your MT4 instance.


But, if you run this EA in the tester, orders will get ticket numbers starting with 1 because those ticket numbers are generated on your computer.


Thank you Drazen for your answer, can you tell me how to get this right?

Drazen Penic
2729
Drazen Penic  

Add two new variables and use them to count the number of successfully opened orders. 



int longOrdersCount = 0;
int shortOrdersCount = 0;
...


 //Open Buy Order
      if(BuySignalshort == true)
         {
            while(longOrdersCount<=4)
               {
                  
                  LongOrdershort = OrderSend(Symbol(),OP_BUY,longTradeLots,Ask,MySlippage,0,0,"MaxingLong",MagicNumber,0,Green);
                     if(LongOrdershort<0)
                     {
                        Print("OrderSend failed with error'",GetLastError());
                     }
                     else
                     {
                        Print("OrderSend places successfully");
                        longOrdersCount++; 
                     }
               }
         }

...

marve7ou_marc
21
marve7ou_marc  
Drazen Penic:

Add two new variables and use them to count the number of successfully opened orders. 




this works to open as much orders as I defined, but it will open all orders at the same time. How can I define that it opens one order per bar if the condition is fulfilled and the next on a new bar if the condition is fulfilled again? on the other hand, the order modify function will only modify the first order that is sent, how can I select every order to set TP and SL?

Thank you very much for your help!!!

Drazen Penic
2729
Drazen Penic  
marve7ou_marc:

this works to open as much orders as I defined, but it will open all orders at the same time. How can I define that it opens one order per bar if the condition is fulfilled and the next on a new bar if the condition is fulfilled again? on the other hand, the order modify function will only modify the first order that is sent, how can I select every order to set TP and SL?

Thank you very much for your help!!!


One order per bar:

  • Record time of the last order in a variable or loop through the active orders and find the time of the last order
  • Compare with the start time of the current bar
  • Be careful to compensate for the eventual timeframe changes and other EA restarts

For the TP and SL

  • create a separate money management function(s) that will calculate SL and TP for buy and sell orders respectively
  • call money management functions to obtain appropriate SL and TP values before OrderSend() 

To add comments, please log in or register