I want to Open 1 deal every 1 condition

 
In this code there are 2 buy conditions, but i just want to open 1 deal every one condition

to be the total deals are unlimited, but every buy condition is equal to just 1 deal 


extern int MagicNumber=10005;
extern double Lots =0.1;
extern double StopLoss=0;
extern double TakeProfit=20;
extern double PIPS=20;
int TrailingStop=0;
extern int Slippage=3;
input bool OpenMeBuy=true;
input bool OpenMeSell=false;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start()
{

  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;

  {
  
     int result=0;

if(OpenMeBuy == true
&& LastClosedInfo("Profit")==0
) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Test",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         if ( ! OrderSelect(result, SELECT_BY_TICKET)){return(0);}
         if ( ! OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green)){return(0);}
        }

        return(0);
     }
if(OpenMeSell == true
&& LastClosedInfo("Profit")==0
) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Test",MagicNumber,0,Red);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         if ( ! OrderSelect(result, SELECT_BY_TICKET)){return(0);}
         if ( ! OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green)){return(0);}
        }

        return(0);
     }
//-----------------------------------------------------------------------------------------------1
if(LastClosedInfo("ClosePrice")==LastClosedInfo("TP")
&& LastClosedInfo("Type")==0
&& LastClosedInfo("Lots")==Lots
) // Here is your open buy rule
     { 
        result=OrderSend(Symbol(),OP_BUYSTOP,Lots,LastClosedInfo("TP")+(PIPS*MyPoint),Slippage,0,0,"Test",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=LastClosedInfo("TP")+(PIPS*MyPoint)+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=LastClosedInfo("TP")+(PIPS*MyPoint)-StopLoss*MyPoint;
         if ( ! OrderSelect(result, SELECT_BY_TICKET)){return(0);}
         if ( ! OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green)){return(0);}
        }

        return(0);
     }

if(LastClosedInfo("ClosePrice")==LastClosedInfo("TP")
&& LastClosedInfo("Type")==1
&& LastClosedInfo("Lots")==Lots
) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELLSTOP,Lots,LastClosedInfo("TP")-(PIPS*MyPoint),Slippage,0,0,"Test",MagicNumber,0,Red);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=LastClosedInfo("TP")-(PIPS*MyPoint)-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=LastClosedInfo("TP")-(PIPS*MyPoint)+StopLoss*MyPoint;
         if ( ! OrderSelect(result, SELECT_BY_TICKET)){return(0);}
         if ( ! OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green)){return(0);}
        }

        return(0);
     }
//-----------------------------------------------------------------------------------------------2
}
  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      if ( ! OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)){return(0);}
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
              if(iHigh(_Symbol, PERIOD_CURRENT,1)>iHigh(_Symbol, PERIOD_CURRENT,1)
              
              )    //here is your close buy rule
              {
                   if ( ! OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red)){return(0);}
              }
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     if ( ! OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green)){return(0);}
                     return(0);
                    }
                 }
              }
           }
         else 
           {
              if(iHigh(_Symbol, PERIOD_CURRENT,1)>iHigh(_Symbol, PERIOD_CURRENT,1)
                
              )    // here is your close sell rule
                {
                   if ( ! OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red)){return(0);}
                }
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     if ( ! OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red)){return(0);}
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}
//======================================================================
double LastClosedInfo(string S, int type = -1)
{
for(int i = OrdersHistoryTotal() - 1; i >= 0; i--)
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
if(OrderSymbol() == Symbol()
&& OrderMagicNumber() == MagicNumber
&& (OrderType() == type || type == -1))
{
if(S == "Lots") return(OrderLots());
else if(S == "OpenPrice") return(OrderOpenPrice());
else if(S == "ClosePrice") return(OrderClosePrice());
else if(S == "OpenPrice") return(OrderOpenPrice());
else if(S == "TP") return(OrderTakeProfit());
else if(S == "SL") return(OrderStopLoss());
else if(S == "Type") return(OrderType());
else if(S == "Profit") return(OrderProfit());
else if(S == "OpenTime") return(OrderOpenTime());
else if(S == "CloseTime") return(OrderCloseTime());
else if(S == "Com") return(OrderCommission());
else if(S == "Swap") return(OrderSwap());

}
return(0);
}


I tried to use this code:

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     if ( ! OrderSelect(i,SELECT_BY_POS ,MODE_TRADES)){return(0);}
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}

But it dose not making what i want!



Any help!

 
Hello?
 
Is that impossible?
 
  1. Your code
    if(iHigh(_Symbol, PERIOD_CURRENT,1)>iHigh(_Symbol, PERIOD_CURRENT,1)
    Equivalent.
    When will that ever be true?
    double h=iHigh(_Symbol, PERIOD_CURRENT,1);
    if(h>h

  2. Ahmed Abd El Aziz: But it dose not making what i want!

    Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
William Roeder #:
  1. Your code
    Equivalent.
    When will that ever be true?

  2. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)


1- It is not important for me, but thanks anyway.

2- I guess you didn't understand me. If i get the last error or last price ..ETC, it will not help me in this case.


For example:

I want to tell the expert to:

- open 1 buy position if the close price > moving average 100

&& open another buy position if  the close price > moving average 200


if(Close[i+1]>MA100[i+1])
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Test",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         if ( ! OrderSelect(result, SELECT_BY_TICKET)){return(0);}
         if ( ! OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green)){return(0);}
        }

        return(0);
     }

if(Close[i+1]>MA200[i+1])
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Test",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         if ( ! OrderSelect(result, SELECT_BY_TICKET)){return(0);}
         if ( ! OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green)){return(0);}
        }

        return(0);
     }


In this case, the expert will open unlimited buy positions if one or more conditions are achieved!

The question is how to make the expert to open 1 position for each condition.


Now i think that i can do this by get ticket number but it is also inaccurate.

 
Ahmed Abd El Aziz #: 1- It is not important for me, but thanks anyway.
It is important. It is extremely important. Pay attention please and read it again.
 
Ahmed Abd El Aziz # :


1- It is not important for me, but thanks anyway.

2- I guess you didn't understand me. If i get the last error or last price ..ETC, it will not help me in this case.


For example:

I want to tell the expert to:

- open 1 buy position if the close price > moving average 100

&& open another buy position if  the close price > moving average 200



In this case, the expert will open unlimited buy positions if one or more conditions are achieved!

The question is how to make the expert to open 1 position for each condition.


Now i think that i can do this by get ticket number but it is also inaccurate.

if ( iHigh ( _Symbol , PERIOD_CURRENT , 0 )> iHigh ( _Symbol , PERIOD_CURRENT , 1 )
I guess you didn't notice the logic error in the code you asked the question! Maybe your code could be like above.

The solution to your problem may be to use two different magic numbers for two different transaction conditions and to impose a single transaction constraint for each magic no.


 
Fernando Carreiro #:
It is important. It is extremely important. Pay attention please and read it again.

This is intentional that H>H to avoid this condition or maybe change it later.


Ahmet Metin Yilmaz #:

I guess you didn't notice the logic error in the code you asked the question! Maybe your code could be like above.

The solution to your problem may be to use two different magic numbers for two different transaction conditions and to impose a single transaction constraint for each magic no.


Thanks i will try to use the magic number in this case.

Reason: