Problème avec le montant total des ordres ouverts - page 2

 

Oui, vous devez resélectionner l'ordre.

   int buy_ticket=0;
   total=0;
   for(i = OrdersTotal()-1; i >= 0 ; i--)
   if ( OrderSelect(i, SELECT_BY_POS)                
    &&  OrderMagicNumber()  == magicNumber            
    &&  OrderSymbol()       == Symbol())
    {             
      total++;
      if(OrderType()==OP_BUY)
         buy_ticket=OrderTicket();
    }
    
   if(buy_ticket>0 && OrderSelect(buy_ticket,SELECT_BY_TICKET))
     {
     //Do stuff
     }

Avec le code ci-dessus, vous créez une nouvelle variable pour stocker le numéro de ticket, puis vous pouvez sélectionner la transaction par son numéro de ticket.

 
GumRai:

Oui, vous devez resélectionner l'ordre.

Avec le code ci-dessus, vous créez une nouvelle variable pour stocker le numéro de ticket, puis vous pouvez sélectionner la transaction par son numéro de ticket.

Awesome !!!!!! Merci beaucoup pour cela. Après de nombreuses semaines, tout fonctionne maintenant. J'ai beaucoup appris. Merci à tous !!!
 
Trader3000:
Awesome !!!!!! Merci beaucoup pour cela. Après de nombreuses semaines, tout fonctionne maintenant. J'ai beaucoup appris. Merci à tous !!!

Désolé mais j'ai parlé trop vite. Le Trailingstop ne se déclenche que parfois, donc je pense que le code est encore cassé quelque part. Voici l'EA complet. Quelqu'un peut-il vérifier et me faire savoir si vous trouvez une erreur. Merci.

#define  magicNumber  12345

// Kicks in when position reaches at least TrailingStop pips of profit.

extern string Label_Trailingstart = "Pip threshold to activate Trailing stop";
extern double TrailingStart = 10;
extern string Label_Trailingstop = "Pips trailing behind";
extern double TrailingStop = 5;

//Set it to some value above 0 to activate Hedge
extern double Hedge = 10;
extern double Multiplier = 3;

extern string Label_StopLoss = "Set StopLoss of original trade";
extern double StopLoss = 11;

extern string Label_Stoploss1 = "Set Stoploss of Hedge trade";
extern string Label_Stoploss2 = "Value should be less than 'Hedge'";
extern string Label_Stoploss3 = "otherwise a second Hedge trade will open";
extern double Stoploss = 9;

extern string Label_Percentage = "Lotsize percentage of Equity";
extern double Percentage = 1;
extern double Lotsize = 0.01;

int i, result, total;
double stoplevel;

int init()
{
   stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10;  // get broker's stoplevel
   if(StopLoss<=stoplevel) StopLoss=stoplevel;     // we compare our StopLoss with
   if(Stoploss<=stoplevel) Stoploss=stoplevel;     // stoplevel and adjust it when error occured
   if(TrailingStop<=stoplevel) TrailingStart=stoplevel+TrailingStop;  // we compared our TakeProfit
                                                       // as we compared our StopLoss
   if ((StopLoss || Stoploss || TrailingStop) < stoplevel)
   {
   MessageBox("Please note: If your inputs for StopLoss, Stoploss"+
              "\nand/or TrailingStop are below the minimum levels"+
              "\nrequired by your broker, they will automatically"+ 
              "\nbe increased to "+StringConcatenate(stoplevel)); 
   }                                                             
   return(0);
}
 
int deinit()
{
   return(0);
}
 
int start()
{ 
   //double Lots = NormalizeDouble(AccountEquity()*Percentage*Lotsize/100, 2);
   
  int buy_ticket=0;
  int sell_ticket=0;
   total=0;
   for(i = OrdersTotal()-1; i >= 0 ; i--)
   if ( OrderSelect(i, SELECT_BY_POS)                
    &&  OrderMagicNumber()  == magicNumber            
    &&  OrderSymbol()       == Symbol())
    {             
      total++;
      if(OrderType()==OP_BUY)
         buy_ticket=OrderTicket();
      if(OrderType()==OP_SELL)
         sell_ticket=OrderTicket();   
    }
          if(total==0 && Close[1]>Close[2])
           {
              result=OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3, Ask - StopLoss*Point*10, 0, "Original", magicNumber, 0, Blue);
              Print("Error setting Original order: ",GetLastError());     
           }
           
     if (buy_ticket>0 && OrderSelect(buy_ticket,SELECT_BY_TICKET) && OrderType() == OP_BUY) 
       {
        if (Bid - OrderOpenPrice() > NormalizeDouble(TrailingStart *Point*10,Digits))
         {
           if (OrderStopLoss() < Bid - NormalizeDouble(TrailingStop *Point*10,Digits))
           {
              if (OrderModify(OrderTicket(), OrderOpenPrice(), Bid - NormalizeDouble(TrailingStop *Point*10,Digits),
              OrderTakeProfit(), Blue)) 
              Print("Error setting Buy trailing stop: ",GetLastError()); 
           }
         }
            else if (OrderOpenPrice() > Bid + NormalizeDouble(Hedge*Point*10,Digits))
            if(total == 1)
            {
            result=OrderSend(Symbol(), OP_SELL, NormalizeDouble(OrderLots() * Multiplier, 2), Bid, 3,
            Bid + Stoploss*Point*10, 0, "Hedge", magicNumber, 0, Blue);
            Print("Error setting Sell Hedge: ", GetLastError());
            }
       }
     else if (sell_ticket>0 && OrderSelect(buy_ticket,SELECT_BY_TICKET) && OrderType() == OP_SELL)
       {
        if (OrderOpenPrice() - Ask > NormalizeDouble(TrailingStart *Point*10,Digits)) 
         {
          if (OrderStopLoss() > Ask + NormalizeDouble(TrailingStop *Point*10,Digits))
           {
              if (OrderModify(OrderTicket(), OrderOpenPrice(), Ask + NormalizeDouble(TrailingStop *Point*10,Digits),
              OrderTakeProfit(), Red))
              Print("Error setting Sell trailing stop: ",GetLastError()); 
           }
              }
                 else if (OrderOpenPrice() < Ask - NormalizeDouble(Hedge*Point*10,Digits))
            if(total == 1)
            { 
            result=OrderSend(Symbol(), OP_BUY, NormalizeDouble(OrderLots() * Multiplier, 2), Ask, 3,
            Ask - Stoploss*Point*10, 0, "Hedge", magicNumber, 0, Red);
            Print("Error setting Buy Hedge: ", GetLastError());
            }  
            }
    return(0);
}
 
  if (buy_ticket>0 && OrderSelect(buy_ticket,SELECT_BY_TICKET) && OrderType() == OP_BUY) 
       {
        //Code
       }
     else if (sell_ticket>0 && OrderSelect(buy_ticket,SELECT_BY_TICKET) && OrderType() == OP_SELL)
       {
        //If there is an open buy order code here will not be executed
        //The else is not necessary
       }

,

 
GumRai:

,

Merci beaucoup de l'avoir signalé. J'ai corrigé l'erreur et cela fonctionne maintenant. J'ai une autre question. J'essaie de faire apparaître la boîte de message lorsque mon stoploss est inférieur au STOP_LEVEL du courtier, mais elle s'affiche même lorsqu'il est supérieur. J'ai essayé de le mettre dans la section "start" plutôt que dans la section "init", mais cela ne fonctionne pas non plus. Quelqu'un pourrait-il jeter un coup d'œil et me dire ce qui ne va pas ? Merci.

int init()
{
   stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10;  // get broker's stoplevel
   if(StopLoss<=stoplevel) StopLoss=stoplevel;     // we compare our StopLoss with
   if(Stoploss<=stoplevel) Stoploss=stoplevel;     // stoplevel and adjust it when error occured
   if(TrailingStop<=stoplevel) TrailingStart=stoplevel+TrailingStop;  // we compared our TakeProfit
                                                       // as we compared our StopLoss
   if ((StopLoss || Stoploss || TrailingStop) < stoplevel)
   {
   MessageBox("Please note: If your inputs for StopLoss, Stoploss"+
              "\nand/or TrailingStop are below the minimum levels"+
              "\nrequired by your broker, they will automatically"+ 
              "\nbe increased to "+StringConcatenate(stoplevel)); 
   }                                                             
   return(0);
}
 
   if ((StopLoss || Stoploss || TrailingStop) < stoplevel)

ça n'a pas de sens

tu veux dire ?

   if ((StopLoss  < stoplevel || TrailingStop) < stoplevel)
 
GumRai:

ça n'a pas de sens

tu veux dire ?

Merci encore une fois, cela a réglé le problème :)
 

Bonjour à tous, j'ai de nouveau rencontré un problème. C'est très étrange et je ne comprends pas pourquoi cela se produit. Comme je l'ai mentionné plus haut, l'EA a bien fonctionné, mais maintenant je veux simplement passer de ceci

result=OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3, Ask - StopLoss*Point*10, 0, "Original", magicNumber, 0, Blue);

à

result=OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3, 0, 0, "Original", magicNumber, 0, Blue);

Je ne touche à rien d'autre, mais après ce simple changement, ni le Trailing stop ni le hedge ne se déclenchent. C'est bizarre. Le code complet est ci-dessus.

 
Comment peut-on traîner un arrêt quand on n'en a pas.
 

Merci pour votre réponse, mais je ne comprends pas. Le code pour le Trailing stop est ici

{
        if (Bid - OrderOpenPrice() > NormalizeDouble(TrailingStart *Point*10,Digits))
         {
           if (OrderStopLoss() < Bid - NormalizeDouble(TrailingStop *Point*10,Digits))
           {
              if (OrderModify(OrderTicket(), OrderOpenPrice(), Bid - NormalizeDouble(TrailingStop *Point*10,Digits),
              OrderTakeProfit(), Blue)) 

Si j'ai un Stoploss, l'EA fonctionne et le Trailingstop et la couverture se déclenchent, mais si je change le Stoploss à 0, rien ne fonctionne. Je ne vois pas comment et pourquoi le Stoploss aurait une influence sur le Trailingstop et la couverture ?

Raison: