Chiudere solo le compravendite redditizie, all'obiettivo di profitto - pagina 3

 
af1:

Hi GumRai, thanks for your time. I've already tried with your changes, but orders keep closing at 1. In other words, profitable orders are not waiting to reach 25.

Anche usando la linea di codice precedentemente fornita da DeVries e Raptor?

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

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

    if(ClosePendingOnly) ClosePendingOrdersOnly();

Speriamo che tu non abbia cambiato questa linea, la stavi considerando

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

Se l'hai cambiato a 25.00, allora avrai tutti i trade profittevoli di $1+ chiusi

 
GumRai:

Anche usando la linea di codice precedentemente fornita da DeVries e Raptor?

Speriamo che tu non abbia cambiato questa linea, la stavi considerando

Se l'hai cambiato a 25.00, allora avrai tutti i trade profittevoli di $1+ chiusi


Ho provato tutti i suggerimenti, ma ancora non si chiude bene.

 
af1:


Ho provato tutti i suggerimenti, ma ancora non si chiude bene.


Forse puoi mostrare il codice completo che stai usando al momento
 
GumRai:

Forse puoi mostrare il codice completo che stai usando al momento

//|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//|              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 = true; // 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 && ProfitTarget == 0.0) CloseAllinProfit();
    
    if(BuyProfit+SellProfit >= ProfitTarget && !CloseProfitableTradesOnly) CloseAll();
    
    if(CloseProfitableTradesOnly && BuyProfit+SellProfit >= ProfitTarget) CloseAllinProfit(); 

    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()

 


 

Si prega di fare riferimento al mio post

https://forum.mql4.com/56959/page2#822980

Non state sommando solo i trade redditizi. Stai calcolando il profitto o la perdita netta

 
GumRai:

Si prega di fare riferimento al mio post

https://forum.mql4.com/56959/page2#822980

Non stai sommando solo i trade redditizi. Stai calcolando il profitto o la perdita netta



GumRai, ho fatto le modifiche al codice suggerite da Raptor e da te:

https://www.mql5.com/en/forum/146091
https://www.mql5.com/en/forum/146091/page2#822980

E ora sembra chiudere bene. Grazie per questo!


Solo un'altra cosa. Se voglio calcolare solo le compravendite redditizie, o solo le compravendite redditizie, invece di calcolare tutte le compravendite redditizie, cosa devo cambiare?

Sto pensando a questo cambiamento per calcolare solo gli acquisti, ma non sono sicuro. È corretto?

da questo:

if(OrderType()==OP_SELL) 
         {
            OrdersSELL++;
            SellLots += OrderLots(); 
            ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            if(ThisTradeProfit>0)
            BuyProfit += ThisTradeProfit;
         }

a questo:

//if(OrderType()==OP_SELL) 
         {
            //OrdersSELL++;
            //SellLots += OrderLots(); 
            //ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            //if(ThisTradeProfit>0)
            //BuyProfit += ThisTradeProfit;
         }
 

Credo che si potrebbero aggiungere 2 bool esterni ai parametri di input, chiamati qualcosa come "BuyTradesOnly" e "SellTradesOnly" entrambi impostati inizialmente su false

poi

if(OrderType()==OP_SELL && BuyTradesOnly == false)
         {
            OrdersSELL++;
            SellLots += OrderLots(); 
            ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            if(ThisTradeProfit>0)
            BuyProfit += ThisTradeProfit;
         }
 
GumRai:

Credo che si potrebbero aggiungere 2 bool esterni ai parametri di input, chiamati qualcosa come "BuyTradesOnly" e "SellTradesOnly" entrambi impostati inizialmente su false

poi


Non ho aggiunto i 2 extern bool, ho solo cambiato questo per chiudere "solo gli ordini di acquisto"

for(int i=0;i<OrdersTotal();i++)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
         if(OrderType()==OP_BUY)  
         {
            OrdersBUY++;
            BuyLots += OrderLots();
            double ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            if(ThisTradeProfit>0)
            BuyProfit += ThisTradeProfit;
         }

e cambiare questo per chiudere "solo ordini di vendita"

for(int i=0;i<OrdersTotal();i++)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
         if(OrderType()==OP_SELL)  
         {
            OrdersSELL++;
            SellLots += OrderLots();
            double ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            if(ThisTradeProfit>0)
            BuyProfit += ThisTradeProfit;
         }

Così ora ho due EA. Uno per chiudere gli acquisti e uno per chiudere le vendite. Per me va bene.


Ho testato su entrambi gli account, demo e reale. Sulla demo funziona bene, ma sul conto reale, una volta raggiunto l'obiettivo di profitto, inizierà a chiudere le posizioni, ma poi si fermerà chiudendo le posizioni quando gli ordini rimanenti scendono sotto l'obiettivo di profitto. Questo succede se il prezzo sta cambiando al momento della chiusura. Quindi tende a lasciare aperti gli ordini redditizi, invece di chiuderli tutti (ordini redditizi).

Ho letto questo post di RaptorUK https://www.mql5.com/en/forum/139654. È qualcosa del genere, ma non so davvero quale potrebbe essere la soluzione migliore per questo problema.


 
af1:


Ho letto questo post da RaptorUK https://www.mql5.com/en/forum/139654. È qualcosa del genere, ma non so davvero quale potrebbe essere la soluzione migliore per questo problema.

La soluzione è data nel thread. . . ecco perché l'ho creato. Conto alla rovescia nel ciclo non in alto.
 
RaptorUK:
La soluzione è data nel thread...è per questo che l'ho creato. Conto alla rovescia nel ciclo non in alto.


Ok Raptor, proverò a fare quel ciclo, ma prima di farlo, cosa succede se cambio solo

Da questo:
}
      Sleep(1000);
   }
A questo:
}
      Sleep(5000);
      RefreshRates();
      continue;
   }


Questo potrebbe fare il lavoro?