Open only 1 Sellstop and 1 Buystop maximum 2 Orders

 

Hello everyone,

i want to open only 1 SELLSTOP  and 1 BUYSTOP  Pending Order. i am using this below script. when i start EA it work greate and open 1 BUYSTOP and 1 SELLSTOP.

But My problem is some time EA Open or make Duplicate orders. 

Mean's When i close Sellstop order it open again sellstop order because i have already buystop opened order. if i close buystop order then EA Open again BUYstop.

if i close again sellstop then EA OPen BUYSTOP but i have already BUYSTOP and if i close BUYSTOP then it open SELLSTOP order again. this is my problem .

after all i don't want duplicate orders and want only 1 sell 1 buy order and maximum 2 orders. 

Please Help me i am trying to solve this problem myself but i need your help.

Thanks 

 

 

//-------------------OPEN SELL ORDER-----------------//
   if (OrderType()==OP_BUYSTOP && OrdersTotal()==1 && OrdersTotal()<2 && OrdersTotal()!=2)
       {
       int ticket = OrderSend(Symbol(),OP_SELLSTOP,lotsize,price,0,0,0,"comments",786,0,Blue);
       }
     //-----------------END SELL ORDER CODE//
     //>>
     //----------------OPEN BUY ORDER--------//
     if ( OrdersTotal()!=2 || OrdersTotal()>2 && OrdersTotal()<2)
       {
       int ticket = OrderSend(Symbol(),OP_BUYSTOP,Lotsize,price,0,0,0,"comments",786,0,Blue);
       }
     //----------------END BUY ORDER CODE----//
 

OrderType() can not be used unless you do an OrderSelect() first.

https://docs.mql4.com/trading/ordertype 

 
razamughal67:

Hello everyone,

i want to open only 1 SELLSTOP  and 1 BUYSTOP  Pending Order. i am using this below script. when i start EA it work greate and open 1 BUYSTOP and 1 SELLSTOP.

But My problem is some time EA Open or make Duplicate orders. 

Mean's When i close Sellstop order it open again sellstop order because i have already buystop opened order. if i close buystop order then EA Open again BUYstop.

if i close again sellstop then EA OPen BUYSTOP but i have already BUYSTOP and if i close BUYSTOP then it open SELLSTOP order again. this is my problem .

after all i don't want duplicate orders and want only 1 sell 1 buy order and maximum 2 orders. 

Please Help me i am trying to solve this problem myself but i need your help.

Thanks 

 

 

//+------------------------------------------------------------------+
//|                                                  Gypsy-Hedge.mq4 |
//+------------------------------------------------------------------+    
//-------------------------------------------------------------------------------------------------
//Global Script SetUp
//-------------------------------------------------------------------------------------------------
string GlobalInputs="Global Script Inputs";//Info
int    Script_Slippage=0;//Slippage
string Script_Comment="Comment Here";//Place Comment Here
int    Script_MagicNumber=0001;//Magic Number
color  Script_ArrowColor=clrNONE;//Arrow Color
//-------------------------------------------------------------------------------------------------
//Average True Range for StopLoss and TakeProfit
//-------------------------------------------------------------------------------------------------
string ATRInfo="Use Average True Range for SL and TP - True or False";//Info
bool   Use_ATRStops=true;
double ATR_LotSize=0.01;//Lotsize
int    ATR_Slippage=0;//Slippage
string ATR_Indicator="";//Average True Range Indicator
int    ATR_Period=20;//ATR Period
int    ATR_SL_Multiplier=2;//ATR StopLoss Multiplier
int    ATR_TP_Multiplier=5;//ATR TakeProfit Multiplier
int    ATR_BufferShift=1;//ATR Buffer Shift
//-------------------------------------------------------------------------------------------------
//Risk Management
//-------------------------------------------------------------------------------------------------
string RiskInfo="Use Risk Management to set LotSize - True or False";//Info
bool   Use_RiskManagement_LotSize=false;
double Risk_Percent=2;//Percentage of Account Balance to Risk
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+      
int start()
  {
   double Script_Long=0,Script_LongSL=0,Script_LongTP=0,
   Script_Short=0,Script_ShortSL=0,Script_ShortTP=0;
//-------------------------------------------------------------------------------------------------
//Average True Range Indicator
   double ATR=iATR(Symbol(),0,ATR_Period,ATR_BufferShift);

   double atrHedgeLong=NormalizeDouble(Ask+ATR,Digits);
   double atrHedgeShort=NormalizeDouble(Bid-ATR,Digits);
//-------------------------------------------------------------------------------------------------
   if(Use_ATRStops==true)
     {
      Script_LongSL=Ask-NormalizeDouble(ATR*ATR_SL_Multiplier,Digits);
      Script_LongTP=Ask+NormalizeDouble(ATR*ATR_TP_Multiplier,Digits);
      Script_ShortSL=Bid+NormalizeDouble(ATR*ATR_SL_Multiplier,Digits);
      Script_ShortTP=Bid-NormalizeDouble(ATR*ATR_TP_Multiplier,Digits);

      if(Use_RiskManagement_LotSize==true)
        {
         Script_Long=OrderSend(Symbol(),OP_BUYSTOP,RMLotSize(),atrHedgeLong,
                               Script_Slippage,Script_LongSL,Script_LongTP,Script_Comment,
                               Script_MagicNumber,Script_ArrowColor);
         Script_Short=OrderSend(Symbol(),OP_SELLSTOP,RMLotSize(),atrHedgeShort,
                                Script_Slippage,Script_ShortSL,Script_ShortTP,Script_Comment,
                                Script_MagicNumber,Script_ArrowColor);
         Alert("Hedge Order ..Error  : ",GetLastError());
        }
      if(Use_RiskManagement_LotSize==false)
        {
         Script_Long=OrderSend(Symbol(),OP_BUYSTOP,ATR_LotSize,atrHedgeLong,
                               Script_Slippage,Script_LongSL,Script_LongTP,Script_Comment,
                               Script_MagicNumber,Script_ArrowColor);
         Script_Short=OrderSend(Symbol(),OP_SELLSTOP,ATR_LotSize,atrHedgeShort,
                                Script_Slippage,Script_ShortSL,Script_ShortTP,Script_Comment,
                                Script_MagicNumber,Script_ArrowColor);
         Alert("Hedge Order ..Error  : ",GetLastError());
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double RMLotSize()
  {
/*
//-------------------------------------------------------------------------------------------------
//Risk Management
//-------------------------------------------------------------------------------------------------
extern bool   Use_RiskManagement_LotSize=false;//Use Risk Management to set LotSize - True or False
extern double Risk_Percent=2;//Percentage of Account Balance to Risk
extern string Risk_break="";//=======================================
*/
//current account balance
   double acctbal=0.00;
   if(AccountBalance()>0){acctbal=AccountBalance();}

//risk percentage
   double riskpercentage=0.00;
   if(Risk_Percent>0){riskpercentage=Risk_Percent/100;}

//amount of capitol to risk based on percentage
   double riskcapitol=0.00;
   if(acctbal>0){riskcapitol=acctbal*riskpercentage;}

//lotsize based on risk capitol
   double lotsize=0.00;
   if(riskcapitol>0){lotsize=riskcapitol*0.01;}

//if lotsize is more than is permitted change the lotsize to the maximum  permitted
   if(lotsize>MarketInfo(Symbol(),MODE_MAXLOT)) {lotsize=MarketInfo(Symbol(),MODE_MAXLOT);}

//if lotsize is less than is permitted change the lotsize to the minimum  permitted  
   if(lotsize<MarketInfo(Symbol(),MODE_MINLOT)) {lotsize=MarketInfo(Symbol(),MODE_MINLOT);}
   return(lotsize);
  }
//+------------------------------------------------------------------+
 
Jimdandy:

OrderType() can not be used unless you do an OrderSelect() first.

https://docs.mql4.com/trading/ordertype 


Thanks Jimdandy,

You mean's first we need to select order number then we can use OrdersType() in my if statement.

 when i have 2 orders and i use 

Alert("You Have ",OrdersTotal(), " Orders");//Get All Open Order in Alert Message

 Alert message show correct numbers of orders but when i modify the order then it show Ordernumber 3 modify.

So then i thought

OrdersTotal()==1 && OrdersTotal()<2 && OrdersTotal()!=2

 is not work properly because after modify order we have the numbers of orders 3. 

Reason: