How to open new order right after the trailing stop is closed on same direction. - page 2

 
William Roeder #:
  1. You can not use any Trade Functions until you first select an order.
  2. You can't open orders at any price, only at the Bid or Ask.
Thanks William
 
Lorentzos Roussos #:

Try this , the comments have explainers . 

Hello Lorentzos,

Your EA works very well, and it is advance for my level.

I would like to write simple EA trailing stop loss and open new order when order is closed. I wrote one it works well for trailing stop loss but it does not open new order. Can you help me to check why? I think when the condition Orderstotal ()==0 then we can open the new order?

#property copyright "VNCIS"
#property strict

extern double ProfitTarget=200;    // pips to start to trailing stoploss
extern double trailingstep=10;    // Step 
extern bool   UseSound       = True;  
extern string NameFileSound  = "expert.wav"; 
extern int trend=0; // 0= trend down, 1=trend up


bool init=true;

double pBid, pAsk, pp;
int lastordertype;



datetime timeprev=0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{


//+------------------------------------------------------------------+
//| Expert start function for Chart Specific                                           |
//+------------------------------------------------------------------+
 
   CleanChart(); // clean chart
   
   Opentrade();  // Call function trailing stop loss
   
   
  
//Close/Open
   if(timeprev==Time[0])
    return(0);
   timeprev=Time[0];
    
return(0);


}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Function to Open Buy Order when one Buy Order closes

void Opentrade() // trailing stop loss functions
{
   
   int trade;
   int trades=OrdersTotal();
   
   
for(trade=0;trade<trades;trade++) 

 {
      
      OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
    
      if(OrderSymbol()!=Symbol())
       continue; // If the pair of the order is equal to the pair where the EA is running and continue
          
      
      
      pp = MarketInfo(OrderSymbol(), MODE_POINT); 
      pBid = MarketInfo(OrderSymbol(), MODE_BID);
      pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
      
     
                 
      //Long
      
      if(OrderType()==OP_BUY)
      {  
         
            if (pBid>=OrderOpenPrice()+pp*ProfitTarget)
                  {
                       OrderModify(OrderTicket(), OrderOpenPrice(),pBid-pp*trailingstep, OrderTakeProfit(), CLR_NONE);
                       if (trades==0)
                       {
                       OrderSend(Symbol(),OP_BUY,OrderLots(),Ask,0,NULL,NULL,"VNCIS",301278,0,Green);
                       }
                  }                 
      }        
             
        //Short
       if(OrderType()==OP_SELL)      
       {   
    
          if (pAsk<=(OrderOpenPrice()-pp*ProfitTarget)) 
                  {
                     OrderModify(OrderTicket(), OrderOpenPrice(),pAsk+pp*trailingstep, OrderTakeProfit(), CLR_NONE);
                     if (trades==0)
                       {
                       OrderSend(Symbol(),OP_BUY,OrderLots(),Bid,0,NULL,NULL,"VNCIS",301278,0,Green);
                       }  
                  }
      }//Short
 
      
}//for
} 

  
void CleanChart()
{
   string sComment   = "";
   string sp         = "****************************\n";
   string NL         = "\n";

   sComment = sComment + NL;
   sComment = sComment + NL;
   sComment = sComment + NL;
   sComment = sComment + NL;
   sComment = sComment + NL;
   sComment = sComment + NL;
   sComment = sComment + NL;
   Comment(sComment);
}         
 
vncis #:

Hello Lorentzos,

Your EA works very well, and it is advance for my level.

I would like to write simple EA trailing stop loss and open new order when order is closed. I wrote one it works well for trailing stop loss but it does not open new order. Can you help me to check why? I think when the condition Orderstotal ()==0 then we can open the new order?

You can simplify it but :

You must open only one order per symbol and keep its ticket 

int lastordertype;
int lastticket=-1;

You must retain the ticket of that order in memory 

lastticket=OrderSend(Symbol(),OP_BUY,OrderLots(),Ask,0,NULL,NULL,"VNCIS",301278,0,Green);

Here you are looping inside open orders so , if your order closes you are not accessing the loop at all

OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

And here this is unlikely , world peace is more possible statistically than the code reaching this point . You are modifying the trail stop and checking if trades are 0 , if trades are zero you have not entered the loop , and , if you have a succesful modification of the trail stop it means you are far from the price by at least trail distance .

            if (pBid>=OrderOpenPrice()+pp*ProfitTarget)
                  {
                       OrderModify(OrderTicket(), OrderOpenPrice(),pBid-pp*trailingstep, OrderTakeProfit(), CLR_NONE);
                       if (trades==0)
                       {
                       OrderSend(Symbol(),OP_BUY,OrderLots(),Ask,0,NULL,NULL,"VNCIS",301278,0,Green);
                       }
                  }  

And you are sending buy on both occasions 

Here is a "simple" version , you should not leave so many things unchecked though 

#property copyright "VNCIS"
#property strict

extern double ProfitTarget=200;    // points to start to trailing stoploss
extern double trailingstep=10;    // Step 
extern bool   UseSound       = True;  
extern string NameFileSound  = "expert.wav"; 
extern int trend=0; // 0= trend down, 1=trend up



double pBid, pAsk, pp;
ENUM_ORDER_TYPE lastordertype;
int lastticket=-1;


datetime timeprev=0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  timeprev=0;
  return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void OnTick()
{
 
   CleanChart(); // clean chart
   ManageTrade();  // Call function trailing stop loss

}

void ManageTrade() // trailing stop loss functions
{
//if ticket valid 
if(lastticket!=-1)
  {
      RefreshRates();
      pp = _Point;
      pBid = Bid;
      pAsk = Ask;
  //check if order was selected 
    if(OrderSelect(lastticket,SELECT_BY_TICKET))
    {      
    //if order is still open 
      if(OrderCloseTime()==0)
      {        
      //Long      
      if(OrderType()==OP_BUY)
      {  
         
            if (pBid>=OrderOpenPrice()+pp*ProfitTarget)
                  {
                  bool modi=OrderModify(OrderTicket(), OrderOpenPrice(),pBid-pp*trailingstep, OrderTakeProfit(), CLR_NONE);
                  }                 
      }        
             
        //Short
       if(OrderType()==OP_SELL)      
       {   
    
          if (pAsk<=(OrderOpenPrice()-pp*ProfitTarget)) 
                  {
                  bool modi=OrderModify(OrderTicket(), OrderOpenPrice(),pAsk+pp*trailingstep, OrderTakeProfit(), CLR_NONE);
                  }
      }//Short
      }//if order is still open ends here
      else{//if order is not open anymore 
      //get the direction
        lastordertype=(ENUM_ORDER_TYPE)OrderType();
      //get the lots
        double lots=OrderLots();
      //reset your ticket 
        lastticket=-1;
      //open new order based on previous direction
        //i assume the opposite 
          if(lastordertype==OP_BUY){
          lastticket=OrderSend(_Symbol,OP_SELL,lots,Bid,1000,NULL,NULL,"VNCIS",301278,0,clrRed);
          }
          else if(lastordertype==OP_SELL){
          lastticket=OrderSend(_Symbol,OP_BUY,lots,Ask,1000,NULL,NULL,"VNCIS",301278,0,clrGreen);
          }
      }//if order is not open anymore ends here
   }//if order was selected ends here
  }
  //if ticket valid ends here
} 

  
void CleanChart()
{
   string sComment   = "";
   string sp         = "****************************\n";
   string NL         = "\n";

   sComment = sComment + NL;
   sComment = sComment + NL;
   sComment = sComment + NL;
   sComment = sComment + NL;
   sComment = sComment + NL;
   sComment = sComment + NL;
   sComment = sComment + NL;
   Comment(sComment);
}         
Also if you manually close the order or a margin call occurs or take profit the order will be reopened , the "right after trail stop hits" is not maintained 
 
Lorentzos Roussos #:

You can simplify it but :

You must open only one order per symbol and keep its ticket 

You must retain the ticket of that order in memory 

Here you are looping inside open orders so , if your order closes you are not accessing the loop at all

And here this is unlikely , world peace is more possible statistically than the code reaching this point . You are modifying the trail stop and checking if trades are 0 , if trades are zero you have not entered the loop , and , if you have a succesful modification of the trail stop it means you are far from the price by at least trail distance .

And you are sending buy on both occasions 

Here is a "simple" version , you should not leave so many things unchecked though 

Also if you manually close the order or a margin call occurs or take profit the order will be reopened , the "right after trail stop hits" is not maintained 

Thank very much for your detail explanations. 

I tried your EA, it does not trailing stop loss, I checked through the codes, I did not find anything wrong except for slippage you set 1000, I set 0, but it still does not work. 

Can you have check again?

Many thanks

 
vncis #:

Thank very much for your detail explanations. 

I tried your EA, it does not trailing stop loss, I checked through the codes, I did not find anything wrong except for slippage you set 1000, I set 0, but it still does not work. 

Can you have check again?

Many thanks

It does not have a lastticket to trail , i assumed you had a mechanism that you would plug in to start the trades ,and get the last ticket too .

The slippage is not an issue . 1000 is more likely than 0 in real accounts .

You can start an order on init to test in tester or you can have it search for the manual order and get it's ticket.

Reason: