Duplicate orders being sent?

 

I am currently forwarding testing my EA and I've discovered an issue. My coding seems to be having a conflict when it is trying to place multiple orders.

Some times it will place two orders on the same market (double the risk) - Most of the time it won't be doing this though. I swear I thought I had it correctly written?

int start()
 {
   if(IsNewCandle())
      {
      CheckForMaTrade();//signals, deletions and candle trails only need checked on a new candle.
      }

   if(OpenOrdersThisPair(Symbol())>0)
      {
      if(UseMoveToBreakEven)MoveToBreakEven();//Move to b/e, normal trail and MA_trail need to trail on each tick
      if(Use_MA_Trail)MA_Trail();
      }
   
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

for(int c=OrdersTotal()-1; c>=0; c--)
  {  
    if(!OrderSelect(OrderTicket(),SELECT_BY_TICKET, MODE_TRADES))continue;
      if(OrderMagicNumber()==MagicNumber)
         if(OrderSymbol() == Symbol())           
         {
            if(OpenOrdersThisPair(Symbol())>0 && OrderSymbol()==Symbol() && OrderType()==OP_SELL)
               {
               CloseHalfOrder1(); 
               }
   
            if(OpenOrdersThisPair(Symbol())>0 && OrderSymbol()==Symbol() && OrderType()==OP_BUY)
               {
               CloseHalfOrder(); 
               }   
         }
   }

}  


//+-------------------------------------------------------------------------------------+
//| Order Buy Function                                                                  |
//+-------------------------------------------------------------------------------------+   

//Place a pending buystop if no orders exists.. pending or otherwise.
if(direction==0)
{ 
      double btp=buy_takeprofit_price;
      double Min_Lot = MarketInfo(Symbol(),MODE_MINLOT);
      double Lot_Step = MarketInfo(Symbol(),MODE_LOTSTEP);
      double BuyLotSize =(RiskedAmount/(pips_to_bsl/pips))/10;
      double Lots = NormalizeDouble(BuyLotSize,2);
      LotSize = MathFloor(Lots/Lot_Step)*Lot_Step;

      static double Stored_BuyPrice;

      if(OpenOrdersThisPair(Symbol())==0)
         {
         int BuyTicketOrder= OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,BuyStopPrice,btp,NULL,MagicNumber,0,Green);
         } 

     for(int b=OrdersTotal()-1; b>=0; b--)
        {
        if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
          if(OrderType()==OP_BUYSTOP) 
            if(OrderMagicNumber()==MagicNumber)
               if(OrderSymbol() == Symbol())
                {  
                   if(BuyStopPrice - OrderStopLoss() > Point / 2. )
                        {
                        Stored_BuyPrice = OrderOpenPrice();
                        DeleteOrder = OrderDelete(OrderTicket());
                        if(DeleteOrder != TRUE) Print("Buy Delete Order Failed = ",GetLastError());
                        }

                   if(OpenOrdersThisPair(Symbol())==0 && DeleteOrder==True && H1_Buy_Touch == "H1 Buy Touch")// If there are no open orders = place a new order.
                        {
                        int NewBuyOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,Stored_BuyPrice,3,BuyStopPrice,btp,NULL,MagicNumber,0,Green);
                        }   
                 }      
         }
}   // end of  if(direction==0)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 


//+---------------------------------------------------------------------------------------+
//|Order Sell Function                                                                    |
//+---------------------------------------------------------------------------------------+   

if(direction==1)
{//--Sell--//
      double stp=sell_takeprofit_price;
      double MinLot = MarketInfo(Symbol(),MODE_MINLOT);
      double LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
      double SellLotSize =(RiskedAmount/(pips_to_ssl/pips))/10;
      double lots = NormalizeDouble(SellLotSize,2);
      LotSize = MathFloor(lots/LotStep)*LotStep;
      
      static double Stored_SellPrice; // When the order is deleted because the OrderOpenPrice() > MA, it will store the OpenPrice here for use on a new order.

      if(OpenOrdersThisPair(Symbol())==0)
         {
         int SellTicketOrder=OrderSend(Symbol(),OP_SELLSTOP,LotSize,sellPrice,3,SellStopPrice,stp,NULL,MagicNumber,0,Red);

         }

      for(int o=OrdersTotal()-1; o>=0; o--)   
       {
        if(OrderSelect(o,SELECT_BY_POS,MODE_TRADES))
         if(OrderType()==OP_SELLSTOP)
           if(OrderMagicNumber()==MagicNumber)
             if(OrderSymbol() == Symbol())
                    
                {
                   if(OrderStopLoss() - BuyStopPrice > Point / 2.) 
                        {
                        Stored_SellPrice = OrderOpenPrice(); // this price was stored from the last Order before it was Deleted.
                        DeleteOrder=OrderDelete(OrderTicket());
                        if(DeleteOrder!=TRUE)Print("Sell Delete Order Failed = ",GetLastError());
                        } 

                   if(OpenOrdersThisPair(Symbol())==0 && DeleteOrder == True && H1_Sell_Touch == "H1 Sell Touch")
                        {
                        int NewSellOrder = OrderSend(Symbol(),OP_SELLSTOP,LotSize,Stored_SellPrice,3,SellStopPrice,stp,NULL,MagicNumber,0,Green);
                        }    
                 }
        } 
    }
}
 
DomGilberto:

I am currently forwarding testing my EA and I've discovered an issue. My coding seems to be having a conflict when it is trying to place multiple orders.

Some times it will place two orders on the same market (double the risk) - Most of the time it won't be doing this though. I swear I thought I had it correctly written?

What are Function return values ? How do I use them ?
 
Sorry, I should have said that I have removed all the return values to compact the code. It is not telling me much... I guess I was asking to see if anyone can see a fundamental flaw in the way I have written this coding, relative to how it knows that the current market in question has one order open on it already....?
 
for(int c=OrdersTotal()-1; c>=0; c--)
  {  
    if(!OrderSelect(OrderTicket(),SELECT_BY_TICKET, MODE_TRADES))continue;
You can NOT call OrderTicket() until you do a successful OrderSelect()
 
WHRoeder:
You can NOT call OrderTicket() until you do a successful OrderSelect()

if(OrderSelect(OrderTicket(),SELECT_BY_TICKET, MODE_TRADES)

I assume you mean changing it to that^ ?

After all, the second parameter of OrderSelect says I can put "OrderTicket()" depending on the second parameter, "SELECT_BY_TICKET"?

Thanks for your post :)

 
OrderTicket() is meaningless because you have not called OrderSelect() first
Your code
if(OrderSelect(OrderTicket(),SELECT_BY_TICKET, MODE_TRADES)
Same as
int tckt = OrderTicket();
if(OrderSelect(tckt,SELECT_BY_TICKET, MODE_TRADES)
Equivalent
int tckt = 0;                                   // Bogus value.
if(OrderSelect(0,SELECT_BY_TICKET, MODE_TRADES) // Always fails.


The second parameter of OrderSelect() does NOT say you can put "OrderTicket()" it says you can put a ticket number. OrderTicket() is NOT a ticket number UNTIL you do a successful select.

 

WHRoeder:

The second parameter of OrderSelect() does NOT say you can put "OrderTicket()" it says you can put a ticket number. OrderTicket() is NOT a ticket number UNTIL you do a successful select.

Right. I thought I was selecting an order with OrderSelect()? I was under the impression that it would return true based upon selecting an order and in doing so, requiring a ticket number or index to verify it is true? How would I successfully select the order then?

Thanks WHRoeder!
 
DomGilberto:
Sorry, I should have said that I have removed all the return values to compact the code. It is not telling me much... I guess I was asking to see if anyone can see a fundamental flaw in the way I have written this coding, relative to how it knows that the current market in question has one order open on it already....?
It will not tell you much until you need it to when something goes wrong . . . then you won't have it in there to tell you anything and it will be too late to get the information in a live environment. You still ignore good advice . . .
 
RaptorUK:
It will not tell you much until you need it to when something goes wrong . . . then you won't have it in there to tell you anything and it will be too late to get the information in a live environment. You still ignore good advice . . .


Yea you're right. I am not ignoring good advice mate, I am listening to everything!

I do have everything printing out. Just wanting to understand why most of the time these trades (demo forward test) they're working, and other pairs are being duplicated. I think it may have something to do with what WHRoeder is saying in regards to OrderSelect...
 
I'm confused how you successfully use "OrderSelect()" before you can successfully use OrderTicket? I thought OrderTicket was an integral parameter to OrderSelect :s?
 

The OrderTicket page is very clear, no ? Look at OrderSelect() use here Loops and Closing or Deleting Orders

Reason: