Can you have an EA that closes positions only?

 

I am thinking about a scenario where you may have opened a position manually and price spikes to a point where statistically speaking, it would be a good exit

Say at the upper Bollinger Band for example (something other than a simple points or pips target)

At the moment I don't have the skills to fully automate my strategy (working hard to fix that) so have to open positions manually and this often leaves me in a position where I cant react quickly enough or simply miss the closing signal I would like to exit with.

An EA that can be pre programmed to close "all open position" when the programmed logic is satisfied, I would think, could solve this interim problem (until system is fully automated)

If the answer is yes and someone could point me to any basic example, I could try and adapt with my fledgling coding kills

thanks in advance

Dave

 

P

This example includes code to close all Buys or all Sells at certain signals

https://www.mql5.com/en/code/8714

Good Luck

-BB-

 
int start()
{
      if(OrdersTotal() > 0){
      int n, slippage= 'some #' ;
      bool Long_Exit_Criteria=false, Short_Exit_Criteria=false;
      
      if( * define long exit criteria here * )  Long_Exit_Criteria=true;

      if( * define short exit criteria here * ) Short_Exit_Criteria=true;

      if(Long_Exit_Criteria)   //--- Check exit criteria for Long position, close all buy positions
      {
         for(n=0;n<OrdersTotal();n++){
            OrderSelect(n,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()==OP_BUY)
               OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Lime);}
      }
      if(Short_Exit_Criteria)   //--- Check exit criteria for Short position, close all sell positions
      {
         for(n=0;n<OrdersTotal();n++){
            OrderSelect(n,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()==OP_SELL)
               OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Red);}
      }}
      return(0);
}
 

For your learning pleasure heres some simple code to open market orders:

extern double Lots = 0.1;
extern double SL = 0;
extern double TP = 0;
extern int slippage = 3;
extern int magic_num = 123;

int start()
{
   static int ticket;
   bool Long_Entry_Criteria=false, Short_Enrty_Criteria=false;

   if( * define long entry criteria here * )  Long_Entry_Criteria=true;

   if( * define short entry criteria here * ) Short_Entry_Criteria=true;

   OrderSelect(ticket,SELECT_BY_TICKET);   //--- select most recently opened order
   
   if(Long_Entry_Criteria && OrderOpenTime()!=Time[0])
   {
      if(SL > 0) SL = NormalizeDouble(Ask - SL*Point,Digits);
      if(TP > 0) TP = NormalizeDouble(Ask + TP*Point,Digits);
      ticket = OrderSend(Symbol(),OP_BUY,Lots,Price,slippage,SL,TP,"BUY",magic_num,0,Lime);
      if(ticket < 0) Print("Error opening buy order ",GetLastError());
   }
   if(Short_Entry_Criteria && OrderOpenTime()!=Time[0])
   {
      if(SL > 0) SL = NormalizeDouble(Bid + SL*Point,Digits);
      if(TP > 0) TP = NormalizeDouble(Bid - TP*Point,Digits);
      ticket = OrderSend(Symbol(),OP_SELL,Lots,Price,slippage,SL,TP,"SELL",magic_num,0,Red);
      if(ticket < 0) Print("Error opening sell order ",GetLastError());
   }
   return(0);
}

...I just threw it together without testing it so if you have issues let me know. Should be good tho.
 
supertrade:

For your learning pleasure heres some simple code to open market orders:



thank you, very much appreciated!!!!
 
    for(n=0;n<OrdersTotal();n++){
            OrderSelect(n,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()==OP_BUY)
               OrderClose(OrderTi...
You MUST count down in the presence of multiple orders (multiple charts) when closing/deleting
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol() ){              // and my pair.
 
I was assuming trading on one chart only for the example but yes, when you have multiple charts open/EA's running, you need to be more rigorous.
Reason: