What am I missing??

 

I have written a small EA and have this strange thing happening where the same code that I use for detecting a Buy transaction that stops more Buy transactions from happening in that loop, does not work with the Sell transaction, therefore making it go in a loop and repeat Selll transactions until all funds are depleted.

I am attaching the simple code. Can somebody help me to see what I am doing wrong?

Count = 0;
         
             for (Count = OrdersTotal()-1; Count >= 0; Count--)
                {
                       if (OrderSelect(Count, SELECT_BY_POS)
                       && OrderMagicNumber() == MagicNumber
                       && OrderSymbol() == Symbol()
                       && OrderCloseTime() == 0)

                       
                       {
                          if (OrderType()== OP_BUY)  BuyTrade = true;
                          else BuyTrade = false;
                       }
                    }
                    
 RefreshRates();                    
   
 if (Ask > HighValue + (OpeningPips * UsePoint) && BuyTrade == false)
 
   {
   
      /*double BuyStopLoss = Bid - (StopLoss * UsePoint);*/
       
       double BuyStopLoss = LowValue - (OpeningPips * UsePoint);

      if (ProfitTakeFunc == true) double BuyTakeProfit = Ask + (ProfitTarget * UsePoint);
      if (ProfitTakeFunc == false) BuyTakeProfit = 0;
   
      if(IsTradeContextBusy()) Sleep(10);

            BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,BuyStopLoss,BuyTakeProfit,"Morning System",MagicNumber,0,Blue);
            
           if(BuyTicket == -1)
               {
                  ErrorCode = GetLastError();
                  ErrDesc = ErrorDescription(ErrorCode);

                  ErrAlert = StringConcatenate("Open Buy Order - Error ",ErrorCode,": ",ErrDesc);
                  Alert(ErrAlert);

                  ErrLog = StringConcatenate("Ask: ",Ask," Lots: ",LotSize," Stop: ",BuyStopLoss," Profit: ",BuyTakeProfit);
                  Print(ErrLog);
               }
            
                        }
                         
Count = 0;
         
             for (Count = OrdersTotal()-1; Count >= 0; Count--)
                {
                       if (OrderSelect(Count, SELECT_BY_POS)
                       && OrderMagicNumber() == MagicNumber
                       && OrderSymbol() == Symbol()
                       && OrderCloseTime() == 0)

                       
                       {
                          if (OrderType()== OP_SELL) SellTrade = true;
                          else SellTrade = false;
                       }
                    }

        RefreshRates();         
        
        if (Bid < LowValue - (OpeningPips * UsePoint) && SellTrade == false)
               {
               
             
               /*double SellStopLoss = Ask + (StopLoss * UsePoint);*/
               
               double SellStopLoss = HighValue + (OpeningPips * UsePoint);
               
               if (ProfitTakeFunc == true) double SellTakeProfit = Bid - (ProfitTarget * UsePoint);
          if (ProfitTakeFunc == false) SellTakeProfit = 0; 
          
           if(IsTradeContextBusy()) Sleep(10);
           SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,UseSlippage,SellStopLoss,SellTakeProfit,"SR System",MagicNumber,0,Red);
           
          // Error handling
             if(SellTicket == -1)
               {
                ErrorCode = GetLastError();
                ErrDesc = ErrorDescription(ErrorCode);

                ErrAlert = StringConcatenate("Open Sell Order - Error ",ErrorCode,": ",ErrDesc);
                Alert(ErrAlert);

                ErrLog = StringConcatenate("Bid: ",Bid," Lots: ",LotSize," Stop: ",SellStopLoss," Profit: ",SellTakeProfit);
                Print(ErrLog);
               }
          }             

//Adjust trailing stops
           
     if (TrailingStopFunc == true)
           {
                          if(BuyMarketCount(Symbol(),MagicNumber) > 0 && TrailingStop > 0)
                                 {
                                        BuyTrailingStop(Symbol(),TrailingStop,BuyMinimumProfit,MagicNumber);
                                 }
                                  
                          if(SellMarketCount(Symbol(),MagicNumber) > 0 && TrailingStop > 0)
                                 {
                                        SellTrailingStop(Symbol(),TrailingStop,SellMinimumProfit,MagicNumber);
                                 }
                           }
                            
   return(0);
  }
 
ernest02:

I have written a small EA and have this strange thing happening where the same code that I use for detecting a Buy transaction that stops more Buy transactions from happening in that loop, does not work with the Sell transaction, therefore making it go in a loop and repeat Selll transactions until all funds are depleted.

I am attaching the simple code. Can somebody help me to see what I am doing wrong?

Please make life easier for those that you want to help you,  sort out your indenting so your code can easily be read by others . . . .  also add comments to explain what you are doing and why . . .

Count = 0;

for(Count=OrdersTotal()-1; Count>=0; Count--)
   {
   if(OrderSelect(Count,SELECT_BY_POS)
   && OrderMagicNumber()==MagicNumber
   && OrderSymbol()==Symbol()
   && OrderCloseTime()==0)
      {
      if(OrderType()== OP_BUY)  BuyTrade = true;
      else BuyTrade = false;
      }
   }

RefreshRates();
if(Ask>HighValue+(OpeningPips*UsePoint) && BuyTrade==false)
   {
   /*double BuyStopLoss = Bid - (StopLoss * UsePoint);*/

   double BuyStopLoss=LowValue -(OpeningPips*UsePoint);

   if(ProfitTakeFunc == true) double BuyTakeProfit = Ask + (ProfitTarget * UsePoint);
   if(ProfitTakeFunc == false) BuyTakeProfit = 0;

   if(IsTradeContextBusy()) Sleep(10);

   BuyTicket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,BuyStopLoss,BuyTakeProfit,"Morning System",MagicNumber,0,Blue);

   if(BuyTicket==-1)
      {
      ErrorCode=GetLastError();
      ErrDesc=ErrorDescription(ErrorCode);

      ErrAlert=StringConcatenate("Open Buy Order - Error ",ErrorCode,": ",ErrDesc);
      Alert(ErrAlert);

      ErrLog=StringConcatenate("Ask: ",Ask," Lots: ",LotSize," Stop: ",BuyStopLoss," Profit: ",BuyTakeProfit);
      Print(ErrLog);
      }
   }

Count=0;

for(Count=OrdersTotal()-1; Count>=0; Count--)
   {
   if(OrderSelect(Count,SELECT_BY_POS)
   && OrderMagicNumber()==MagicNumber
   && OrderSymbol()==Symbol()
   && OrderCloseTime()==0)
      {
      if(OrderType()== OP_SELL) SellTrade= true;
      else SellTrade= false;
      }
   }

RefreshRates();
if(Bid<LowValue -(OpeningPips*UsePoint) && SellTrade==false)
   {
   /*double SellStopLoss = Ask + (StopLoss * UsePoint);*/

   double SellStopLoss=HighValue+(OpeningPips*UsePoint);

   if(ProfitTakeFunc==true) double SellTakeProfit=Bid -(ProfitTarget*UsePoint);
   if(ProfitTakeFunc==false) SellTakeProfit=0;

   if(IsTradeContextBusy()) Sleep(10);
   SellTicket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,UseSlippage,SellStopLoss,SellTakeProfit,"SR System",MagicNumber,0,Red);

   // Error handling
   if(SellTicket==-1)
      {
      ErrorCode=GetLastError();
      ErrDesc=ErrorDescription(ErrorCode);

      ErrAlert=StringConcatenate("Open Sell Order - Error ",ErrorCode,": ",ErrDesc);
      Alert(ErrAlert);

      ErrLog=StringConcatenate("Bid: ",Bid," Lots: ",LotSize," Stop: ",SellStopLoss," Profit: ",SellTakeProfit);
      Print(ErrLog);
      }
   }

//Adjust trailing stops

if(TrailingStopFunc==true)
   {
   if(BuyMarketCount(Symbol(),MagicNumber)>0 && TrailingStop>0)
      {
      BuyTrailingStop(Symbol(),TrailingStop,BuyMinimumProfit,MagicNumber);
      }

   if(SellMarketCount(Symbol(),MagicNumber)>0 && TrailingStop>0)
      {
      SellTrailingStop(Symbol(),TrailingStop,SellMinimumProfit,MagicNumber);
      }
   }

return(0);
}
 
RaptorUK:

Please make life easier for those that you want to help you,  sort out your indenting so your code can easily be read by others . . . .  also add comments to explain what you are doing and why . . .

The problem lies within your loops,  both of them,  the Buy works by chance not design . . .

for(Count=OrdersTotal()-1; Count>=0; Count--)
   {
   if(OrderSelect(Count,SELECT_BY_POS)
   && OrderMagicNumber()==MagicNumber
   && OrderSymbol()==Symbol()
   && OrderCloseTime()==0)
      {
      if(OrderType()== OP_BUY)  BuyTrade = true;
      else BuyTrade = false;
      }
   }

Imagine you have 5 trades in the pool, 0, 1, 2, 3, 4   4 is a Sell, 3 a Buy, 2 a Sell, 1 a Buy, 0 a Buy

When you loop ends at position 0  BuyTrade is true 

 

Now consider this situation:

Imagine you have 5 trades in the pool, 0, 1, 2, 3, 4   4 is a Sell, 3 a Buy, 2 a Sell, 1 a Buy, 0 a Sell

When you loop ends at position 0  BuyTrade is false even though you have open Buy trades . . .

Do this instead . . .

BuyTrade = false;  //  set the bool to false as an initial condition

for(Count=OrdersTotal()-1; Count>=0; Count--)
   {
   if(OrderSelect(Count,SELECT_BY_POS)
   && OrderMagicNumber()==MagicNumber
   && OrderSymbol()==Symbol()
   && OrderCloseTime()==0)
      {
      if(OrderType()== OP_BUY)  BuyTrade = true;   //  if any of the trades are OP_BUY set the bool to true
      }
   }
 

RaptorUK,

Thank you very much for your analysis of my code and the recommend solution. It is greatly appreciated!

Not sure why my indenting is not acceptable? Is there a standard way of indenting that i do not follow? Please guide me.

In future I will certainly add comments to make it easier to understand my code.

Thank you again!


Ernest.

 
BuyTrade = false;  //  set the bool to false as an initial condition
SellTrade = false;
for(Count=OrdersTotal()-1; Count>=0; Count--)
   {
   if(OrderSelect(Count,SELECT_BY_POS)
   && OrderMagicNumber()==MagicNumber
   && OrderSymbol()==Symbol()
   && OrderCloseTime()==0)
      {
      if(OrderType()== OP_BUY)  BuyTrade = true;   //  if any of the trades ar OP_BUY set the bool to true
      if(OrderType()== OP_SELL)  SellTrade = true;   //  if any of the trades ar OP_SELL set the bool to true
      }
   }

and with this way you have checked it for both conditions in one time

if you find a trade from  OrdersTotal() then it will have OrderCloseTime()==0  because it is still open

you only need it if you search  OrdersHistoryTotal( )

I prefer to write this function as  ......  

BuyTrade = false;     //  set the bool to false as an initial condition
SellTrade = false;
for(Count=OrdersTotal()-1; Count>=0; Count--)
   {
   if(OrderSelect(Count,SELECT_BY_POS,MODE_TRADES)==false)        break;
   if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
   //---- check order type
   if(OrderType()==OP_BUY)
       { BuyTrade = true ;} 
   if(OrderType()==OP_SELL)
       { SellTrade = true ;}
   //....other ordertypes you have to select
   }
 
ernest02:

RaptorUK,

Thank you very much for your analysis of my code and the recommend solution. It is greatly appreciated!

Not sure why my indenting is not acceptable? Is there a standard way of indenting that i do not follow? Please guide me.

There are many ways that people choose to indent code,  I have my preference but I would not suggest that everyone follows my choice,  but I would say that whatever choice you make you need to be consistent,  an example:

 if (Ask > HighValue + (OpeningPips * UsePoint) && BuyTrade == false)
 
   {
   
      /*double BuyStopLoss = Bid - (StopLoss * UsePoint);*/
       
       double BuyStopLoss = LowValue - (OpeningPips * UsePoint);

      if (ProfitTakeFunc == true) double BuyTakeProfit = Ask + (ProfitTarget * UsePoint);
      if (ProfitTakeFunc == false) BuyTakeProfit = 0;
   
      if(IsTradeContextBusy()) Sleep(10);

            BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,BuyStopLoss,BuyTakeProfit,"Morning System",MagicNumber,0,Blue);  // why is this line indented ?
            
           if(BuyTicket == -1)
               {
                  ErrorCode = GetLastError();
                  ErrDesc = ErrorDescription(ErrorCode);

                  ErrAlert = StringConcatenate("Open Buy Order - Error ",ErrorCode,": ",ErrDesc);
                  Alert(ErrAlert);

                  ErrLog = StringConcatenate("Ask: ",Ask," Lots: ",LotSize," Stop: ",BuyStopLoss," Profit: ",BuyTakeProfit);
                  Print(ErrLog);
               }
            
                        }   //  where is the matching opening brace ? indenting helps to see where it is
 
deVries:
and with this way you have checked it for both conditions in one time

Thank you very much deVries. I actually had it as one loop to check for both conditions, but when I experienced the described problem I split them to see if it will make a difference.

Once again - thank you very much!


Ernest.

 
deVries:

and with this way you have checked it for both conditions in one time

if you find a trade from  OrdersTotal() then it will have OrderCloseTime()==0  because it is still open

you only need it if you search  OrdersHistoryTotal( )

I prefer to write this function as  ......  

 

so i can do also

BuyTrade = false;     //  set the bool to false as an initial condition
SellTrade = false;
for(Count=OrdersTotal()-1; Count>=0; Count--)
   {
   if(OrderSelect(Count,SELECT_BY_POS,MODE_TRADES)==false)        break;
   if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
   //---- check order type
   if(OrderType()==OP_BUY)
       { 
       BuyTrade = true ;
       if (TrailingStopFunc == true && TrailingStop>0)
            {
            //code modify directly if condition are there
            }
       } 
   if(OrderType()==OP_SELL)
       { 
       SellTrade = true ;
       if (TrailingStopFunc == true && TrailingStop>0)
            {
            //code modify directly if condition are there
            }
       }
   //....other ordertypes you have to select
   }

 this way you can delete part

 

//Adjust trailing stops

if(TrailingStopFunc==true)
   {
   if(BuyMarketCount(Symbol(),MagicNumber)>0 && TrailingStop>0)
      {
      BuyTrailingStop(Symbol(),TrailingStop,BuyMinimumProfit,MagicNumber);
      }

   if(SellMarketCount(Symbol(),MagicNumber)>0 && TrailingStop>0)
      {
      SellTrailingStop(Symbol(),TrailingStop,SellMinimumProfit,MagicNumber);
      }
   }