How to compare first and last trade, close when they are in profit?

 

How can I compare and close first and last order when they in profit? Meaning when EA opens 5 buy orders, compare 1st and 5th orders, close when they are in profit, likewise compare 4th and 2nd order, close when they are profit. Instead of closing all together, how can i close in this way? Can you please guide me how to do these?

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

//|                                               CloseOnAverage.mq4 |

//|                        Copyright 2021, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2021, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict



extern int Choose_stragegy=1; // 1-hedging 2-trend

input int             InpMaxTrades = 10;         // Max number of trades

input double          InpTradeGap = 0.005;       // Distance between trades

//input ENUM_ORDER_TYPE InpType = ORDER_TYPE_BUY;  // Order type;//

input double          InpMinProfit = 1.00;       // Profit point

input int             InpMagicNumber = 1111;     // Magic number

input double          Multiplier = 2;       

input string          InpTradeComment = __FILE__; // Trade comment

input double          InpVolume = 0.01;          // Volume per order

double pips;



//double multi= (InpVolume*Multiplier);



struct STradeSum {

   int buycount;

   int sellcount;

   double buyprofit;

   double sellprofit;

   double buytrailPrice;

   double selltrailPrice;

};



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

//| Expert initialization function                                   |

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



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



void OnTick() 

{

 

   STradeSum  sum;

   GetSum(sum);

    

   if (sum.buyprofit>InpMinProfit) 

   { // target reached

      CloseAll(ORDER_TYPE_BUY);

   } 

   

   else if (sum.sellprofit>InpMinProfit) 

   { // target reached

      CloseAll(ORDER_TYPE_SELL);

   }

   

   else if (sum.buycount==0) 

   {  // no buy trades

      OpenTrade(ORDER_TYPE_BUY);

   } 

   

   else if (sum.sellcount==0)

   {  // no sell trades

      OpenTrade(ORDER_TYPE_SELL);

   } 

   

   else if ((sum.buycount<InpMaxTrades))

   {

      

      if ( OrderType()==ORDER_TYPE_BUY && SymbolInfoDouble(Symbol(), SYMBOL_ASK)<=(sum.buytrailPrice-InpTradeGap)) 

      {   // Far enough below

         OpenmultiTrade(ORDER_TYPE_BUY);

      } 

      else if ((sum.sellcount<InpMaxTrades))

      {

         if ( OrderType() ==ORDER_TYPE_SELL && SymbolInfoDouble(Symbol(), SYMBOL_BID)>=(sum.selltrailPrice+InpTradeGap)) 

            {   // Far enough above

              OpenmultiTrade(ORDER_TYPE_SELL);

             }

      }

    }

    

   //else  if (sum.sellcount<InpMaxTrades)     

}



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



void  OpenTrade(ENUM_ORDER_TYPE InpType) {

 

   double   price =  (InpType==ORDER_TYPE_BUY) ?

                        SymbolInfoDouble(Symbol(), SYMBOL_ASK) :

                        SymbolInfoDouble(Symbol(), SYMBOL_BID);

   OrderSend(Symbol(), InpType, InpVolume, price, 0, 0, 0, InpTradeComment, InpMagicNumber);

    

}



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



void  OpenmultiTrade(ENUM_ORDER_TYPE InppType) {

 

   double   price =  (InppType==ORDER_TYPE_BUY) ?

                        SymbolInfoDouble(Symbol(), SYMBOL_ASK) :

                        SymbolInfoDouble(Symbol(), SYMBOL_BID);

   double multi = (InpVolume*Multiplier);

   OrderSend(Symbol(), InppType, multi, price, 0, 0, 0, InpTradeComment, InpMagicNumber);

}



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



void  CloseAll(ENUM_ORDER_TYPE orderType) {

 

   int   count    =  OrdersTotal();

 

   for (int i = count-1; i>=0; i--) {

      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

         if (  OrderSymbol()==Symbol()

               && OrderMagicNumber()==InpMagicNumber

               && OrderType()==orderType

               ) {

            OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0);

         }

      }

   }

 

}

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



void  GetSum(STradeSum &sum) {

 

   sum.buycount      =  0;

   sum.buyprofit     =  0.0;

   sum.buytrailPrice =  0.0;

   sum.sellcount      =  0;

   sum.sellprofit     =  0.0;

   sum.selltrailPrice =  0.0;

    

   int   count    =  OrdersTotal();

 

   for (int i = count-1; i>=0; i--) 

   {

      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 

      {

      //Buy orders

         if (OrderSymbol()==Symbol() && OrderMagicNumber()==InpMagicNumber && OrderType()== ORDER_TYPE_BUY)

                {

                  sum.buycount++;

                  sum.buyprofit  += OrderProfit()+OrderSwap()+OrderCommission();

                  if (sum.buytrailPrice==0 || OrderOpenPrice()<sum.buytrailPrice)

                     {

                        sum.buytrailPrice =  OrderOpenPrice();

                     }

                 }

          //sellorders

          if (OrderSymbol()==Symbol()&& OrderMagicNumber()==InpMagicNumber && OrderType()==ORDER_TYPE_SELL)

                { 

                  sum.sellcount++;

                  sum.sellprofit  += OrderProfit()+OrderSwap()+OrderCommission();

                  if (sum.selltrailPrice==0 || OrderOpenPrice()>sum.selltrailPrice)

                     {

                        sum.selltrailPrice =  OrderOpenPrice();

                     }

                }

            

        } 

     }

          

   return;

    

}



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

 

please edit your post -

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button


 
Sergey Golubev #:

please edit your post -

Thanks for the response. I edited it. Can you please help me with this?

 
Naresh kumar #:

Thanks for the response. I edited it. Can you please help me with this?

HI Naresh,

Did you find any solution?

Robie

 
Naresh kumar: How can I compare and close first and last order when they in profit? ? Can you please guide me how to do these?
Naresh kumar #: Can you please help me with this?
  1. void  OpenTrade(ENUM_ORDER_TYPE InpType) {
    
      double   price =  (InppType==ORDER_TYPE_BUY) ?

    MT4 has no such types OrderSend - Trade Functions - MQL4 Reference

  2. Create an OrderSelect loop. Find the last order, keep going and find the first order, compare. What's the problem?

  3. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help (2017)