What is wrong in my code? Getting error 138 - page 2

 
Marco vd Heijden:

It can not be the TradeCloser() function because it is nowhere called.

Are you sure you have correctly selected the order you want to close ?

For example you can not call OrderSymbol() or OrderType() when you don't have an order selected .

Look closely, OrderClose() is in the TradeCloser() function multiple times unless you mean something else.
 
Marco vd Heijden:

It can not be the TradeCloser() function because it is nowhere called.

Are you sure you have correctly selected the order you want to close ?

For example you can not call OrderSymbol() or OrderType() when you don't have an order selected .

hmmm I thought so,


Take a look at this please:

void TradeCloser(){ //start

            int total = OrdersTotal();
            MqlTick tick;
            SymbolInfoTick(_Symbol,tick);
            if (total > 0)
            
            {
            for(int cnt=0;cnt<total;cnt++)
            {
               if(OrderSelect(cnt,SELECT_BY_POS))
               {
            
                     if ((Next_BUY_Order_Comment == "M1") && (OrderType() == OP_BUY)) // compare the commenting
               {
                  // do what you want.
                 // OrderCloseBy(Close1B,Close2B, CLR_NONE);
                  OrderClose(Close1B(),Order0_Lot,tick.bid,2,Red);

               }
                     if ((Next_BUY_Order_Comment == "M2") && (OrderType() == OP_BUY)) // compare the commenting
               {
                  // do what you want.
                  OrderClose(Close2B(),Order1_Lot,tick.bid,2,Red);
               }
                     if ((Next_BUY_Order_Comment == "M3") && (OrderType() == OP_BUY)) // compare the commenting
               {
                  // do what you want.
                  OrderClose(Close3B(),Order2_Lot,tick.bid,2,Red);
               }
                     if ((Next_BUY_Order_Comment == "M4") && (OrderType() == OP_BUY)) // compare the commenting
               {
                  // do what you want.
                  //myOrderModifyRel(Close6B(), 3 * myPoint, 0);
                 // OrderModify(Close5B(), OrderOpenPrice(), OrderOpenPrice()-sala, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
                  //OrderModify(Close5B(), OrderOpenPrice(), 3 * myPoint, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
                  OrderClose(Close4B(),Order3_Lot,tick.bid,2,Red);
               }
               
                     if ((Next_BUY_Order_Comment == "M5") && (OrderType() == OP_BUY)) // compare the commenting
               {
                  // do what you want.
                  
                  OrderClose(Close5B(),Order4_Lot,tick.bid,2,Red);
               }
               
               if ((Next_SELL_Order_Comment == "M1") && (OrderType() == OP_SELL)) // compare the commenting
               {
                  // do what you want.

                  OrderClose(Close1S(),Order0_Lot,tick.ask,2,Blue);

               }
                     if ((Next_SELL_Order_Comment == "M2") && (OrderType() == OP_SELL)) // compare the commenting
               {
                  // do what you want.
                  OrderClose(Close2S(),Order1_Lot,tick.ask,2,Blue);
               }
                     if ((Next_SELL_Order_Comment == "M3") && (OrderType() == OP_SELL)) // compare the commenting
               {
                  // do what you want.
                  OrderClose(Close3S(),Order2_Lot,tick.ask,2,Blue);
               }
                     if ((Next_SELL_Order_Comment == "M4") && (OrderType() == OP_SELL)) // compare the commenting
               {
                  // do what you want.
                  //myOrderModifyRel(Close6S(), 3 * myPoint, 0);
                  //OrderModify(Close5S(), OrderOpenPrice(), 3 * myPoint, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
                 // OrderModify(Close5S(), OrderOpenPrice(), OrderOpenPrice()+sala, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
                  OrderClose(Close4S(),Order3_Lot,tick.ask,2,Blue);
               }
               
                     if ((Next_SELL_Order_Comment == "M5") && (OrderType() == OP_SELL)) // compare the commenting
               {
                  // do what you want.
                  
                  OrderClose(Close5S(),Order4_Lot,tick.ask,2,Blue);
               }
            
         }
      }
   }



//end
}
I originally had the orderselect function in my code but took it out because it would return the wrong ticket for some reason. When I took it out, it selects the right ticket but error 138 hits me. Any ideas as to how I should approach this problem buddy?
 

Again.

Let's try a different approach.

Point me to the line where TradeCloser() is called.

 
Marco vd Heijden:

Again.

Let's try a different approach.

Point me to the line where TradeCloser() is called.

OnTimer()


right before I open the next trade in the sequence
 
So I fixed the problem by removing the ordertype etc, however, now I have a problem where it selects buy and sell trades together. My updated function is below. How can I make it differentiate if the trade is buy or sell? Any ideas?

void TradeCloser()
  { //start

   RefreshRates();

   if((Next_BUY_Order_Comment=="M1")) // compare the commenting
     {
      // do what you want.
      RefreshRates();
      OrderClose(Close1B(),Order0_Lot,Bid,2,Red);

     }
   if((Next_BUY_Order_Comment=="M2")) // compare the commenting
     {
      // do what you want.
      RefreshRates();
      OrderClose(Close2B(),Order1_Lot,Bid,2,Red);
     }
   if((Next_BUY_Order_Comment=="M3")) // compare the commenting
     {
      // do what you want.
      RefreshRates();
      OrderClose(Close3B(),Order2_Lot,Bid,2,Red);
     }
   if((Next_BUY_Order_Comment=="M4")) // compare the commenting
     {
      RefreshRates();
      OrderClose(Close4B(),Order3_Lot,Bid,2,Red);
     }

   if((Next_BUY_Order_Comment=="M5")) // compare the commenting
     {
      // do what you want.
      RefreshRates();
      OrderClose(Close5B(),Order4_Lot,Bid,2,Red);
     }

   if((Next_SELL_Order_Comment=="M1")) // compare the commenting
     {
      // do what you want.
      RefreshRates();

      OrderClose(Close1S(),Order0_Lot,Ask,2,Blue);

     }
   if((Next_SELL_Order_Comment=="M2")) // compare the commenting
     {
      // do what you want.
      RefreshRates();
      OrderClose(Close2S(),Order1_Lot,Ask,2,Blue);
     }
   if((Next_SELL_Order_Comment=="M3")) // compare the commenting
     {
      // do what you want.
      RefreshRates();
      OrderClose(Close3S(),Order2_Lot,Ask,2,Blue);
     }
   if((Next_SELL_Order_Comment=="M4")) // compare the commenting
     {
      RefreshRates();
      OrderClose(Close4S(),Order3_Lot,Ask,2,Blue);
     }

   if((Next_SELL_Order_Comment=="M5")) // compare the commenting
     {
      // do what you want.
      RefreshRates();
      OrderClose(Close5S(),Order4_Lot,Ask,2,Blue);
     }



//end
  }
 
void TradeCloser()
  { //start

   RefreshRates();
   int total=OrdersTotal();

   if(total>0)

     {
      for(int cnt=0;cnt<total;cnt++)
        {
         if(OrderSelect(cnt,SELECT_BY_POS))
           {
            if(OrderType()==OP_BUY)
              {
               if((Next_BUY_Order_Comment=="M1")) // compare the commenting
                 {
                  // do what you want.
                  RefreshRates();
                  OrderClose(Close1B(),Order0_Lot,Bid,2,Red);

                 }
               if((Next_BUY_Order_Comment=="M2")) // compare the commenting
                 {
                  // do what you want.
                  RefreshRates();
                  OrderClose(Close2B(),Order1_Lot,Bid,2,Red);
                 }
               if((Next_BUY_Order_Comment=="M3")) // compare the commenting
                 {
                  // do what you want.
                  RefreshRates();
                  OrderClose(Close3B(),Order2_Lot,Bid,2,Red);
                 }
               if((Next_BUY_Order_Comment=="M4")) // compare the commenting
                 {
                  RefreshRates();
                  OrderClose(Close4B(),Order3_Lot,Bid,2,Red);
                 }

               if((Next_BUY_Order_Comment=="M5")) // compare the commenting
                 {
                  // do what you want.
                  RefreshRates();
                  OrderClose(Close5B(),Order4_Lot,Bid,2,Red);
                 }
              }
           }
        }
     }

   if(total>0)

     {
      for(int cnt=0;cnt<total;cnt++)
        {
         if(OrderSelect(cnt,SELECT_BY_POS))
           {
            if(OrderType()==OP_SELL)
              {
               if((Next_SELL_Order_Comment=="M1")) // compare the commenting
                 {
                  // do what you want.
                  RefreshRates();

                  OrderClose(Close1S(),Order0_Lot,Ask,2,Blue);

                 }
               if((Next_SELL_Order_Comment=="M2")) // compare the commenting
                 {
                  // do what you want.
                  RefreshRates();
                  OrderClose(Close2S(),Order1_Lot,Ask,2,Blue);
                 }
               if((Next_SELL_Order_Comment=="M3")) // compare the commenting
                 {
                  // do what you want.
                  RefreshRates();
                  OrderClose(Close3S(),Order2_Lot,Ask,2,Blue);
                 }
               if((Next_SELL_Order_Comment=="M4")) // compare the commenting
                 {
                  RefreshRates();
                  OrderClose(Close4S(),Order3_Lot,Ask,2,Blue);
                 }

               if((Next_SELL_Order_Comment=="M5")) // compare the commenting
                 {
                  // do what you want.
                  RefreshRates();
                  OrderClose(Close5S(),Order4_Lot,Ask,2,Blue);
                 }
              }
           }
        }
     }


//end
  }
I just tried this but it does the same thing as the code above. Any help here guys?
 

So this would be the THIRD time.

Where is the call to TradeCloser() ?

I won't ask a fourth time.
 
Ok so I thought about this and realized that all I need is one function that can check if a trade moved negative 10 pips from the pool of current open trades and then return if it is a buy or sell trade. Then I can add it into the TradeCloser() function. Can anybody help with this? I'm currently racking my brains over this conceptual problem.
 
Marco vd Heijden:

So this would be the THIRD time.

Where is the call to TradeCloser() ?

I won't ask a fourth time.
This will be my second time saying this. It is called in OnTimer() right before I open the next trade in the sequence
 
We can not help you.
Reason: