open order after TP or SL

 

Hello, I have tried without success to get an order opened after my current trade stop loss or take profit has been hit. Here are my requirements. I currently enter a trade where my ea sets the stop loss and take profit. I wait for the market to hit either my stop loss or my take profit and when this happens I should enter the market with a new market order. However, the new market order should be as follows; If last trade was a OP_BUY and hit target, the new market order should be OP_BUY. If last trade was a OP_BUY and hit stop, the new market order should be OP_SELL. If last trade was a OP_SELL and hit target, the new market order should be OP_SELL. If last trade was a OP_SELL and hit stop, the new market order should be OP_BUY. Any order that hits a stop ea should enter market order with double lots.

My ea currently sets the sl and tp, so I need code that will execute the new order per above criteria.

Thanks for any help. I don't expect you guys to do the whole thing, just send me in the right direction. I am a .net programmer trying to learn MQL4.

Thanks again.

 
RobertD517:

Hello, I have tried without success to get an order opened after my current trade stop loss or take profit has been hit. Here are my requirements. I currently enter a trade where my ea sets the stop loss and take profit. I wait for the market to hit either my stop loss or my take profit and when this happens I should enter the market with a new market order. However, the new market order should be as follows; If last trade was a OP_BUY and hit target, the new market order should be OP_BUY. If last trade was a OP_BUY and hit stop, the new market order should be OP_SELL. If last trade was a OP_SELL and hit target, the new market order should be OP_SELL. If last trade was a OP_SELL and hit stop, the new market order should be OP_BUY. Any order that hits a stop ea should enter market order with double lots.

My ea currently sets the sl and tp, so I need code that will execute the new order per above criteria.

Thanks for any help. I don't expect you guys to do the whole thing, just send me in the right direction. I am a .net programmer trying to learn MQL4.

Thanks again.


What do you think yourself ? Aren't you the one that learn the code then make the code you need yourself.....

Here is my help and now do the code....... If you place the trade place directly at that time BUYSTOP and SELLSTOP how simple will that be ?

 
RobertD517:

Thanks for any help. I don't expect you guys to do the whole thing, just send me in the right direction. I am a .net programmer trying to learn MQL4.

Thanks again.

There are many ways of doing this . . . it's a little difficult to give good advice without seeing your code.

You can https://docs.mql4.com/trading/OrderSelect the last trade that was closed via SL or TP, check it's https://docs.mql4.com/trading/OrderType and https://docs.mql4.com/trading/OrderProfit to see if it was closed at TP or SL, then use this info to determine the next Order type

 

OK RaptorUK, I want to thank you for your prompt reply. I have tried to incorporate the above code blocks as you have suggested. And although it seems that what I have done should work, it is quite obvious that my logic structure is incorrect, because when my stop or target is hit it just closes the position, but fails to enter the new position.

Hope you can help.

Thanks, RobertD517.

My current code is as follows...

#property copyright ""
#property link ""

//---- input parameters
extern double Lots=0.2;
extern double TakeProfitPips=60;
extern double StopLossPips=60;
extern double magic.number=0;
int Factor, Digt, cnt, PL, total, bt, st, pos;
double TPp, SLp, BSp, SSp, LotsD, LotsX, lotsResult;

int iOrders;
int i;
bool plResult;
bool otResult;
bool closeResult;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init(){}

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit(){}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if(Close[0]>10) {Factor=1000; Digt=3;} else
if(Close[0]<10) {Factor=100000; Digt=5;}


if(OrdersTotal()!=0)
{
for(cnt=0; cnt<OrdersTotal(); cnt++)
{
OrderSelect(cnt,SELECT_BY_POS);
//if(total==-1 )


//--------------Take Profit--------------------------------

if(OrderTakeProfit()==0 && TakeProfitPips !=0)
{
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{TPp = OrderOpenPrice()+TakeProfitPips/Factor;}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{TPp = OrderOpenPrice()-TakeProfitPips/Factor;}

} else TPp = OrderTakeProfit();



//--------------Stop Loss--------------------------------

if(OrderStopLoss()==0 && StopLossPips !=0)
{
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{SLp = OrderOpenPrice()-StopLossPips/Factor;}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{SLp = OrderOpenPrice()+StopLossPips/Factor;}
} else SLp = OrderStopLoss();

//---------------Modify Order--------------------------
if (OrderType()==OP_BUY || OrderType()==OP_SELL)
OrderModify(OrderTicket(),OrderOpenPrice(),SLp,TPp,0);

//-----------------------------------------------------
{
int ticket;
double point;





LastTradeProfit(bool closeResult, plResult, order_type; double lotsResult;);

if(closeResult==true && plResult==true && order_type==true)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,0,0,0,0,"some comment",16384,0,Green);

if(ticket<=0) Print("Error = ",GetLastError());
else { Print("ticket = ",ticket); break; }
//---- 5 seconds wait
Sleep(5000);
}
//while(true)
if(closeResult==true && plResult==false && order_type==true)

{
LotsD=Lots;
ticket=OrderSend(Symbol(),OP_SELL,LotsD,0,0,0,0,"some comment",16384,0,Green);

if(ticket<=0) Print("Error = ",GetLastError());
else { Print("ticket = ",ticket); break; }
//---- 5 seconds wait
Sleep(5000);
}


if(closeResult==true && plResult==true && order_type==false)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,0,0,0,0,"some comment",16384,0,Green);

if(ticket<=0) Print("Error = ",GetLastError());
else { Print("ticket = ",ticket); break; }
//---- 5 seconds wait
Sleep(5000);
}
//while(true)
if(closeResult==true && plResult==false && order_type==false)

{
LotsD=Lots;
ticket=OrderSend(Symbol(),OP_BUY,LotsD,0,0,0,0,"some comment",16384,0,Green);

if(ticket<=0) Print("Error = ",GetLastError());
else { Print("ticket = ",ticket); break; }
//---- 5 seconds wait
Sleep(5000);
}

//----
return(0);
}
} // for cnt
}//if OrdersTotal
return(0);
}// Start()

int LastTradeProfit()
{
iOrders = OrdersHistoryTotal()-1;
for (i = iOrders; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if (OrderSymbol() == Symbol())
{
if ((TimeDayOfYear(OrderOpenTime()) == DayOfYear()) && (TimeYear(OrderOpenTime()) == Year()))
{
if (OrderProfit() >= 0)
plResult = true;
else
plResult = false;
{
if(OrderSelect(i, SELECT_BY_POS)==true)
{
int order_type;
order_type=OP_BUY;
// ...
}
else
Print("OrderSelect() returned error - ",GetLastError());

if(OrderSelect(i,SELECT_BY_POS)==true)
Print("lots for the order i ",OrderLots());
if(OrderLots()>0)
{
lotsResult=OrderLots();
}
else
Print("OrderSelect returned error of ",GetLastError());

if(OrderSelect(i,SELECT_BY_POS)==true)
Print("Close price for the order ",i," = ",OrderClosePrice());
if(OrderClosePrice()>0)
{
closeResult=true;
}
else
closeResult=false;
Print("OrderSelect failed error code is",GetLastError());
}
}
}
}
}
return(0);



//+------------------------------------------------------------------+
 

Please use this to post code . . . it makes it easier to read.

 
OK

	          
 
#property copyright ""
#property link ""

//---- input parameters
extern double Lots=0.2;
extern double TakeProfitPips=200;
extern double StopLossPips=200;
extern double magic.number=0;
int Faktor, Digt, cnt, PL, total, bt, st, pos;
double TPp, SLp, BSp, SSp, LotsD, LotsX, lotsResult;

int iOrders;
int i;
bool plResult;
bool otResult;
bool closeResult;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init(){}

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit(){}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if(Close[0]>10) {Faktor=1000; Digt=3;} else
if(Close[0]<10) {Faktor=100000; Digt=5;}


if(OrdersTotal()!=0)
{
for(cnt=0; cnt<OrdersTotal(); cnt++)
{
OrderSelect(cnt,SELECT_BY_POS);
//if(total==-1 )


//--------------Take Profit--------------------------------

if(OrderTakeProfit()==0 && TakeProfitPips !=0)
{
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{TPp = OrderOpenPrice()+TakeProfitPips/Faktor;}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{TPp = OrderOpenPrice()-TakeProfitPips/Faktor;}

} else TPp = OrderTakeProfit();



//--------------Stop Loss--------------------------------

if(OrderStopLoss()==0 && StopLossPips !=0)
{
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{SLp = OrderOpenPrice()-StopLossPips/Faktor;}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{SLp = OrderOpenPrice()+StopLossPips/Faktor;}
} else SLp = OrderStopLoss();

//---------------Modify Order--------------------------
if (OrderType()==OP_BUY || OrderType()==OP_SELL)
OrderModify(OrderTicket(),OrderOpenPrice(),SLp,TPp,0);

//-----------------------------------------------------
{
int ticket;
double point;





LastTradeProfit(bool closeResult, plResult, order_type; double lotsResult;);

if(closeResult==true && plResult==true && order_type==true)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,0,0,0,0,"some comment",16384,0,Green);

if(ticket<=0) Print("Error = ",GetLastError());
else { Print("ticket = ",ticket); break; }
//---- 5 seconds wait
Sleep(5000);
}
//while(true)
if(closeResult==true && plResult==false && order_type==true)

{
LotsD=Lots;
ticket=OrderSend(Symbol(),OP_SELL,LotsD,0,0,0,0,"some comment",16384,0,Green);

if(ticket<=0) Print("Error = ",GetLastError());
else { Print("ticket = ",ticket); break; }
//---- 5 seconds wait
Sleep(5000);
}


if(closeResult==true && plResult==true && order_type==false)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,0,0,0,0,"some comment",16384,0,Green);

if(ticket<=0) Print("Error = ",GetLastError());
else { Print("ticket = ",ticket); break; }
//---- 5 seconds wait
Sleep(5000);
}
//while(true)
if(closeResult==true && plResult==false && order_type==false)

{
LotsD=Lots;
ticket=OrderSend(Symbol(),OP_BUY,LotsD,0,0,0,0,"some comment",16384,0,Green);

if(ticket<=0) Print("Error = ",GetLastError());
else { Print("ticket = ",ticket); break; }
//---- 5 seconds wait
Sleep(5000);
}

//----
return(0);
}
} // for cnt
}//if OrdersTotal
return(0);
}// Start()

int LastTradeProfit()
{
iOrders = OrdersHistoryTotal()-1;
for (i = iOrders; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if (OrderSymbol() == Symbol())
{
if ((TimeDayOfYear(OrderOpenTime()) == DayOfYear()) && (TimeYear(OrderOpenTime()) == Year()))
{
if (OrderProfit() >= 0)
plResult = true;
else
plResult = false;
{
if(OrderSelect(i, SELECT_BY_POS)==true)
{
int order_type;
order_type=OP_BUY;
// ...
}
else
Print("OrderSelect() returned error - ",GetLastError());

if(OrderSelect(i,SELECT_BY_POS)==true)
Print("lots for the order i ",OrderLots());
if(OrderLots()>0)
{
lotsResult=OrderLots();
}
else
Print("OrderSelect returned error of ",GetLastError());

if(OrderSelect(i,SELECT_BY_POS)==true)
Print("Close price for the order ",i," = ",OrderClosePrice());
if(OrderClosePrice()>0)
{
closeResult=true;
}
else
closeResult=false;
Print("OrderSelect failed error code is",GetLastError());
}
}
}
}
}
return(0);



//+------------------------------------------------------------------+
 
Click the SRC button, paste your code into the box, Click Insert bottom right . . . . or attach your code as a file if it's too big for the box.
 
#property copyright ""
#property link      ""

//---- input parameters
extern double    Lots=0.2;
extern double        TakeProfitPips=200;
extern double        StopLossPips=200;
extern double        magic.number=0;
      int         Faktor, Digt, cnt, PL, total, bt, st, pos;
   double         TPp, SLp, BSp, SSp, LotsD, LotsX, lotsResult;
   
   int iOrders;
   int i;
   bool plResult;
   bool otResult;
   bool closeResult;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init(){}

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit(){}

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
      if(Close[0]>10)  {Faktor=1000; Digt=3;}  else
      if(Close[0]<10)  {Faktor=100000; Digt=5;}
      
      
      if(OrdersTotal()!=0)
         {
         for(cnt=0; cnt<OrdersTotal(); cnt++)
            {
            OrderSelect(cnt,SELECT_BY_POS);
//if(total==-1   )
     

//--------------Take Profit--------------------------------

            if(OrderTakeProfit()==0 && TakeProfitPips !=0)
               {
                  if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
                     {TPp = OrderOpenPrice()+TakeProfitPips/Faktor;}
                  if(OrderType()==OP_SELL && OrderSymbol()==Symbol())                     
                     {TPp = OrderOpenPrice()-TakeProfitPips/Faktor;}
                     
               } else TPp = OrderTakeProfit();
               
               
               
//--------------Stop Loss--------------------------------

            if(OrderStopLoss()==0 && StopLossPips !=0)
               {
                  if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
                     {SLp = OrderOpenPrice()-StopLossPips/Faktor;}
                  if(OrderType()==OP_SELL && OrderSymbol()==Symbol())                     
                     {SLp = OrderOpenPrice()+StopLossPips/Faktor;}
               } else SLp = OrderStopLoss();

//---------------Modify Order--------------------------
            if (OrderType()==OP_BUY || OrderType()==OP_SELL)   
            OrderModify(OrderTicket(),OrderOpenPrice(),SLp,TPp,0);
                           
//-----------------------------------------------------
  {
   int    ticket;
   double point;

   

   
   
   LastTradeProfit(bool closeResult, plResult, order_type; double lotsResult;);
   
   if(closeResult==true && plResult==true && order_type==true)
     {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,0,0,0,0,"some comment",16384,0,Green);
      
      if(ticket<=0) Print("Error = ",GetLastError());
      else { Print("ticket = ",ticket); break; }
      //---- 5 seconds wait
      Sleep(5000);
     }
     //while(true)
     if(closeResult==true && plResult==false && order_type==true)
   
     {
      LotsD=Lots;
      ticket=OrderSend(Symbol(),OP_SELL,LotsD,0,0,0,0,"some comment",16384,0,Green);
      
      if(ticket<=0) Print("Error = ",GetLastError());
      else { Print("ticket = ",ticket); break; }
      //---- 5 seconds wait
      Sleep(5000);
     }
      
  
   if(closeResult==true && plResult==true && order_type==false)
     {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,0,0,0,0,"some comment",16384,0,Green);
      
      if(ticket<=0) Print("Error = ",GetLastError());
      else { Print("ticket = ",ticket); break; }
      //---- 5 seconds wait
      Sleep(5000);
     }
     //while(true)
   if(closeResult==true && plResult==false && order_type==false)
   
     {
      LotsD=Lots;
      ticket=OrderSend(Symbol(),OP_BUY,LotsD,0,0,0,0,"some comment",16384,0,Green);
      
      if(ticket<=0) Print("Error = ",GetLastError());
      else { Print("ticket = ",ticket); break; }
      //---- 5 seconds wait
      Sleep(5000);
     }
  
//----
   return(0);
  }
            } // for cnt
         }//if OrdersTotal
  return(0);
  }// Start()
  
  int LastTradeProfit()
 {
    iOrders = OrdersHistoryTotal()-1;
    for (i = iOrders; i>=0; i--)
 {
    OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
    if (OrderSymbol() == Symbol())
     {  
      if ((TimeDayOfYear(OrderOpenTime()) == DayOfYear()) && (TimeYear(OrderOpenTime()) == Year()))
       {
        if (OrderProfit() >= 0)
         plResult = true;
        else
          plResult = false;
       {
         if(OrderSelect(i, SELECT_BY_POS)==true)
            {
               int order_type;
               order_type=OP_BUY;
               // ...
            }
         else
            Print("OrderSelect() returned error - ",GetLastError());
          
            if(OrderSelect(i,SELECT_BY_POS)==true)
               Print("lots for the order i ",OrderLots());
                  if(OrderLots()>0)
                     {
                        lotsResult=OrderLots();
                      }  
            else
               Print("OrderSelect returned error of ",GetLastError());
               
                  if(OrderSelect(i,SELECT_BY_POS)==true)
                     Print("Close price for the order ",i," = ",OrderClosePrice());
                        if(OrderClosePrice()>0)
                     {
                        closeResult=true;
                      } 
                  else
                        closeResult=false;
                     Print("OrderSelect failed error code is",GetLastError());    
         }             
       }
     }
   }
}
  return(0);

   
 
//+------------------------------------------------------------------+
 

Here you are calling the function . . .

  LastTradeProfit(bool closeResult, plResult, order_type; double lotsResult;);

here you are defining the Function . . .

int LastTradeProfit()

The call and the definition have to match up, you define the variables that you want to pass in the definition and then pass them in the call . . .

 
      if(Close[0]>10)  {Faktor=1000; Digt=3;}  else
      if(Close[0]<10)  {Faktor=100000; Digt=5;}
mr robert can u explain what r u trying to achive with Close[0] > or < 10 & how is this thing related to digt 3 or 5