stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

MQL Empty4 CODE FOR PROFIT CHECK BEFORE CLOSE
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4678
Page 2 of 2
Author:  keydcuk [ Fri Apr 08, 2016 11:50 am ]
Post subject:  MQL Empty4 CODE FOR PROFIT CHECK BEFORE CLOSE

pascalx » Fri Apr 08, 2016 11:14 am wrote:

Code: Select all

#define INVALID_TICKET_ID -1

bool GetRealOrderProfit(double& profit, int ticketId)
{
   if (ticketId != INVALID_TICKET_ID)
   {
      if (OrderSelect(ticketId, SELECT_BY_TICKET))
      {
         profit = OrderProfit() + OrderSwap() + OrderCommission();
         return true;
      }
   }
   return false;
}

void MyFunction()
{
   int myTicketId;
   // ...

   double myProfit;
   if (GetRealOrderProfit(myProfit, myTicketId) && myProfit > 0.0)
   {
       // my order is profitable, yay !!
   }
}
Something like this might do it. It might be missing spread, but you can easily correct the math if it is wrong. And you may need to work with MagicNumber to safely select the order.
Thanks for your reply. Where exactly should I paste this code to give it a try? I am not a coder. :)
Author:  jlcgarcia [ Sun Apr 17, 2016 2:58 pm ]
Post subject:  MQL Empty4 CODE FOR PROFIT CHECK BEFORE CLOSE

Hi,


I am using a very similar subroutine for one of my multipair EAs and here there is the code:

Code: Select all

//declarations
extern bool    ProfitAsPercentageOfBalance   = true;
extern double  PercentageProfit              = 1;

//Start()



   if(ProfitAsPercentageOfBalance)
     {
      ProfitPercentage=AccountBalance() *(PercentageProfit/100);
     if( OpenProfitPartial()>ProfitPercentage)  {Print(OpenProfitPartial());closeOrdersF(OP_SELL);closeOrdersF(OP_BUY);Sleep(60000);}


     }



  
    
     

double OpenProfitPartial()
  {
   double Profit=0.0;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(/*OrderSymbol() != Symbol() ||*/ OrderMagicNumber()!=magic) continue;
      //    if(OrderType() != OrdType) continue;
      if((OrderProfit()+OrderSwap()+OrderCommission())<0)continue;
      Profit+=(OrderProfit()+OrderSwap()+OrderCommission());
     }
   return(Profit);
  }

int closeOrdersF(int type)
  {
   int  ticket,ordertype,total1=OrdersTotal();
   double priceClose;
   bool oc,od;

   if(total1==0) {Print("Nothing to close"); return(0);}
   total1--;
   for(int x=total1; x>=0; x--)
     
     {
      if(OrderSelect(x,SELECT_BY_POS))
        {
         ordertype= OrderType();
         if(type != ordertype  /*|| OrderSymbol() != Symbol()*/||OrderMagicNumber()!=magic) continue;

         ticket=OrderTicket();

         if ((OrderProfit()+OrderCommission()+OrderCommission())<0) continue;
       
         switch(ordertype)
           {
            case 0:
               priceClose=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),Digits);
               Print("Closing order ¹ ",x," ",ticket);
               oc=OrderClose(ticket,OrderLots(),priceClose,3,Yellow);
               break;
            case 1:
               priceClose=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),Digits);
                Print("Closing order ¹ ",x," ",ticket);
               oc=OrderClose(ticket,OrderLots(),priceClose,3,Yellow);
               break;

            default:
                Print("Delete order ¹ ",x," ",ticket);
               od=OrderDelete(ticket);
               break;
           }
        }
     }
   return(0);
  }

I am sure that this code can be simplified, but it works for me.

Regards

José Luis
All times are UTC Page 2 of 2