Cerrar sólo las operaciones rentables, en el objetivo de beneficio

 

Hola, estoy probando este ea y me parece muy útil para cerrar órdenes, pero en mi caso no quiero cerrar todas las órdenes abiertas, sólo quiero cerrar las órdenes rentables.

He cambiado el valor "CloseProfitableTradesOnly" de false a true, pero las órdenes siguen cerrándose a 1 usd. Y lo que busco es cerrar sólo las órdenes rentables cuando, en conjunto, llegan a 25 usd. Todas las órdenes cerradas por encima de 1 usd de beneficio.

Si tienen alguna sugerencia se los agradezco mucho. Gracias de antemano por su ayuda.

Aquí está el ea y el código:

//|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//|              Close 
//|   Last Updated 12-12-2006 10:00pm
//|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
#define     NL    "\n" 

extern int    ProfitTarget     = 25;             // closes all orders once Float hits this $ amount
extern bool   CloseAllNow      = false;          // closes all orders now
extern bool   CloseProfitableTradesOnly = false; // closes only profitable trades
extern double ProftableTradeAmount      = 1;     // Only trades above this amount close out
extern bool   ClosePendingOnly = false;          // closes pending orders only
extern bool   UseAlerts        = false;

//+-------------+
//| Custom init |
//|-------------+
int init()
  {

  }

//+----------------+
//| Custom DE-init |
//+----------------+
int deinit()
  {

  }

//+------------------------------------------------------------------------+
//| Closes everything
//+------------------------------------------------------------------------+
void CloseAll()
{
   int i;
   bool result = false;

   while(OrdersTotal()>0)
   {
      // Close open positions first to lock in profit/loss
      for(i=OrdersTotal()-1;i>=0;i--)
      {
         if(OrderSelect(i, SELECT_BY_POS)==false) continue;

         result = false;
         if ( OrderType() == OP_BUY)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 15, Red );
         if ( OrderType() == OP_SELL)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 15, Red );
         if (UseAlerts) PlaySound("alert.wav");
      }
      for(i=OrdersTotal()-1;i>=0;i--)
      {
         if(OrderSelect(i, SELECT_BY_POS)==false) continue;

         result = false;
         if ( OrderType()== OP_BUYSTOP)  result = OrderDelete( OrderTicket() );
         if ( OrderType()== OP_SELLSTOP)  result = OrderDelete( OrderTicket() );
         if ( OrderType()== OP_BUYLIMIT)  result = OrderDelete( OrderTicket() );
         if ( OrderType()== OP_SELLLIMIT)  result = OrderDelete( OrderTicket() );
         if (UseAlerts) PlaySound("alert.wav");
      }
      Sleep(1000);
   }
}
   
//+------------------------------------------------------------------------+
//| cancels all orders that are in profit
//+------------------------------------------------------------------------+
void CloseAllinProfit()
{
  for(int i=OrdersTotal()-1;i>=0;i--)
 {
    OrderSelect(i, SELECT_BY_POS);
    bool result = false;
        if ( OrderType() == OP_BUY && OrderProfit()+OrderSwap()>ProftableTradeAmount)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
        if ( OrderType() == OP_SELL && OrderProfit()+OrderSwap()>ProftableTradeAmount)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
        if (UseAlerts) PlaySound("alert.wav");
 }
  return; 
}

//+------------------------------------------------------------------------+
//| cancels all pending orders 
//+------------------------------------------------------------------------+
void ClosePendingOrdersOnly()
{
  for(int i=OrdersTotal()-1;i>=0;i--)
 {
    OrderSelect(i, SELECT_BY_POS);
    bool result = false;
        if ( OrderType()== OP_BUYSTOP)   result = OrderDelete( OrderTicket() );
        if ( OrderType()== OP_SELLSTOP)  result = OrderDelete( OrderTicket() );
  }
  return; 
  }

//+-----------+
//| Main      |
//+-----------+
int start()
  {
   int      OrdersBUY;
   int      OrdersSELL;
   double   BuyLots, SellLots, BuyProfit, SellProfit;

//+------------------------------------------------------------------+
//  Determine last order price                                       |
//-------------------------------------------------------------------+
      for(int i=0;i<OrdersTotal();i++)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
         if(OrderType()==OP_BUY)  
         {
            OrdersBUY++;
            BuyLots += OrderLots();
            BuyProfit += OrderProfit() + OrderCommission() + OrderSwap();
         }
         if(OrderType()==OP_SELL) 
         {
            OrdersSELL++;
            SellLots += OrderLots();
            SellProfit += OrderProfit() + OrderCommission() + OrderSwap();
         }
      }               
   
    if(CloseAllNow) CloseAll();
    
    if(CloseProfitableTradesOnly) CloseAllinProfit();
    
    if(BuyProfit+SellProfit >= ProfitTarget) CloseAll(); 

    if(ClosePendingOnly) ClosePendingOrdersOnly();
       
   
   Comment("                            Comments Last Update 12-12-2006 10:00pm", NL,
           "                            Buys    ", OrdersBUY, NL,
           "                            BuyLots        ", BuyLots, NL,
           "                            Sells    ", OrdersSELL, NL,
           "                            SellLots        ", SellLots, NL,
           "                            Balance ", AccountBalance(), NL,
           "                            Equity        ", AccountEquity(), NL,
           "                            Margin              ", AccountMargin(), NL,
           "                            MarginPercent        ", MathRound((AccountEquity()/AccountMargin())*100), NL,
           "                            Current Time is  ",TimeHour(CurTime()),":",TimeMinute(CurTime()),".",TimeSeconds(CurTime()));
 } // start()

 


Archivos adjuntos:
 
if((BuyProfit+SellProfit >= ProfitTarget)  && CloseProfitableTradesOnly) CloseAllinProfit();
 
af1:

Hola, estoy probando este ea y me parece muy útil para cerrar órdenes, pero en mi caso no quiero cerrar todas las órdenes abiertas, sólo quiero cerrar las órdenes rentables.

He cambiado el valor "CloseProfitableTradesOnly" de false a true, pero las órdenes siguen cerrándose a 1 usd. Y lo que busco es cerrar sólo las órdenes rentables cuando, en conjunto, llegan a 25 usd. Todas las órdenes cerradas por encima de 1 usd de beneficio.


Supongo que no escribió este código . . .

    if(CloseAllNow) CloseAll();
    
    if(CloseProfitableTradesOnly) CloseAllinProfit();
    
    if(BuyProfit+SellProfit >= ProfitTarget) CloseAll(); 

    if(ClosePendingOnly) ClosePendingOrdersOnly();

. . pero deberías ser capaz de leerlo.

Cambiaste el valor de "CloseProfitableTradesOnly" de false a true, por lo que si te fijas arriba se llama a CloseAllinProfit() . . . sin importar la ganancia total.

Pruebe este cambio:

    if(CloseAllNow) CloseAll();
    
    if(CloseProfitableTradesOnly && ProfitTarget == 0.0) CloseAllinProfit();
    
    if(BuyProfit+SellProfit >= ProfitTarget && !CloseProfitableTradesOnly) CloseAll(); 

    if(CloseProfitableTradesOnly && BuyProfit+SellProfit >= ProfitTarget) CloseAllinProfit();

    if(ClosePendingOnly) ClosePendingOrdersOnly();
 

Hola Raptor, gracias por tu respuesta.

Yo no escribí este código, sólo cambié el valor de entrada "CloseProfitableTradesOnly" de false a true, así:

Estoy probando con tus cambios pero aun asi el ea no cierra en el objetivo de ganancias, (en este caso 25 usd.)


¿Tengo que cambiar de 0.0 a 25.0 en el código también?

if(CloseProfitableTradesOnly && ProfitTarget == 0.0) CloseAllinProfit();
 
af1:

Hola Raptor, gracias por tu respuesta.

Yo no escribí este código, sólo cambié el valor de entrada "CloseProfitableTradesOnly" de false a true, así:

Estoy probando con tus cambios pero aun asi el ea no cierra en el objetivo de ganancias, (en este caso 25 usd.)


¿Tengo que cambiar de 0.0 a 25.0 en el código también?


No, esta línea debería hacer lo que usted quiere, ¿está seguro de que hizo TODOS los cambios?

    if(CloseProfitableTradesOnly && BuyProfit+SellProfit >= ProfitTarget) CloseAllinProfit();
 
He cambiado de esto
if(CloseAllNow) CloseAll();
    
if(CloseProfitableTradesOnly) CloseAllinProfit();
    
if(BuyProfit+SellProfit >= ProfitTarget) CloseAll(); 

if(ClosePendingOnly) ClosePendingOrdersOnly();

A esto:

if(CloseAllNow) CloseAll();
    
if(CloseProfitableTradesOnly && ProfitTarget == 0.0) CloseAllinProfit();
    
if(BuyProfit+SellProfit >= ProfitTarget && !CloseProfitableTradesOnly) CloseAll(); 

if(CloseProfitableTradesOnly && BuyProfit+SellProfit >= ProfitTarget) CloseAllinProfit();

if(ClosePendingOnly) ClosePendingOrdersOnly();


Y la entrada "CloseProfitableTradesOnly" de falso, a verdadero.


Pero no se cierra. ¿Qué otra cosa podría ser?

 
af1:
He cambiado de esto

A esto:


Y la entrada "CloseProfitableTradesOnly" de falso, a verdadero.


Pero no se cierra. ¿Qué otra cosa podría ser?

Tal vez su beneficio total es menos de 25.0
 
RaptorUK:
Tal vez su beneficio total sea inferior al 25,0


Si era menos de 25, entonces debe cerrar aún más rápido.

 
af1:


Si era menos de 25, entonces debe cerrar aún más rápido.

No, el beneficio tiene que ser mayor o igual a 25,0 antes de que se cierre...

if(CloseProfitableTradesOnly && BuyProfit+SellProfit >= ProfitTarget) CloseAllinProfit();
 
RaptorUK:

No, el beneficio tiene que ser mayor o igual a 25,0 para que se cierre. . .


Aquí hay un ejemplo de Raptor. Tengo 4 órdenes con 97,9 de beneficio. Así que si mi objetivo de beneficio es 25, y la condición es "CloseProfitableTradesOnly" verdadera, entonces estas 4 órdenes deberían cerrarse. Pero no se cierra ninguna orden.

 
af1:


Aquí hay un ejemplo de Raptor. Tengo 4 órdenes con 97.9 de beneficio. Así que si mi objetivo de ganancias es 25, y la condición es "CloseProfitableTradesOnly" true, entonces estas 4 órdenes deberían cerrarse. Pero no se cierra ninguna orden.

No, el beneficio total tiene que ser mayor que igual a 25.0, el suyo es -59.80
Razón de la queja: