Comment créer un tableau de nombres magiques ? - page 4

 
Jon,
1) On vérifie que le premier OrderSend() a réussi en testant que la valeur de retour est supérieure à zéro avant de tenter de passer la deuxième commande.
supérieure à zéro avant de tenter de passer la deuxième commande.
2) Pour identifier et traiter les paires de commandes, vous pourriez baser votre code sur ce qui suit si les paires de commandes
sont composées de différents types (toutefois, l'algorythme devrait être modifié si le regroupement est supérieur à 2 ou si les commandes sont de types différents).
était supérieur à 2 ou si les ordres d'un groupe étaient tous du même type) :
L'exemple de programme "ReportsTrader.mq4" qui suit place des ordres straddle à proximité de l'émission de rapports ou de nouvelles.
rapports ou d'événements d'actualité.
Un ordre straddle est composé de deux ordres en attente au-dessus et au-dessous du prix du marché. Lorsqu'un
Lorsqu'un ordre en attente atteint le prix du marché, il est transformé en ordre de marché et l'autre ordre en attente est transformé en ordre de vente.

La commande doit être supprimée. Les paires de commandes nécessitent donc un numéro de séquence pour les suivre.

//+------------------------------------------------------------------+
//|                                                ReportsTrader.mq4 |
//|                                          Copyright © 2010, sxTed |
//|                                            MailTo: sxTed@gmx.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, sxTed"
#property link      "MailTo: sxTed@gmx.com"

//+------------------------------------------------------------------+
//| input parameters:                                                |
//+-----------0---+----1----+----2----+----3]------------------------+
extern int            ExpertID = 2;
extern int            StopLoss = 20;
extern int          TakeProfit = 60;
extern int    DistanceToMarket = 15;
extern double             Lots = 0.05;
extern bool         AlertError = true;

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void start() {
   static string Report[4][4] = {"2010.12.30 05:00", "USDJPY", "JPY Purchasing Manager Index"    , "0",
                                 "2010.12.30 09:00", "EURUSD", "EUR Italian Producer Price Index", "0",
                                 "2010.12.30 13:30", "USDJPY", "USD Initial Jobless Claims"      , "0",
                                 "2010.12.31 00:30", "AUDUSD", "AUD Private Sector Credit"       , "0"
                                };
   int    Position[10][7], iReports=ArrayRange(Report,0), i, k, iPairs, iPipsFactor;
   int    iOrders=OrdersTotal(), iPositions=ArrayRange(Position,0), iSequenceNumber;
   double dTS, dSL, pp;
   string sSequenceNumber;

   //----------------------------------------------------------------- triage of orders   
   if (iOrders > iPositions) iPositions=ArrayResize(Position,iOrders)/iOrders;
   ArrayInitialize(Position,0);
   for (i=0; i<iOrders; i++) {
      if (!OrderSelect(i,SELECT_BY_POS))   continue;
      if (OrderMagicNumber() != ExpertID) continue;
      iSequenceNumber = StrToInteger(OrderComment());
      for (k=0; k<iPositions; k++) {
         if (Position[k][6] == iSequenceNumber) {
            Position[k][OrderType()] = OrderTicket();
            break;
         }   
         if (Position[k][6] == 0) {
            Position[k][6]           = iSequenceNumber;
            Position[k][OrderType()] = OrderTicket();
            iPairs++;
            break;
         }   
      }      
   }
   //----------------------------------------------------------------- maintain straddle orders   
   for (i=0; i<iPairs; i++) {
      if (Position[i][OP_BUY] > 0) {
         if (OrderSelect(Position[i][OP_BUY], SELECT_BY_TICKET)) {
            if ((MarketInfo(OrderSymbol(),MODE_DIGITS) == 5) || (MarketInfo(OrderSymbol(),MODE_DIGITS) == 3)) iPipsFactor = 10;
            else iPipsFactor = 1;
            dTS=StopLoss*iPipsFactor*MarketInfo(OrderSymbol(),MODE_POINT);
            dSL=OrderStopLoss()+dTS*MathFloor((MarketInfo(OrderSymbol(),MODE_BID)-dTS-OrderStopLoss())/dTS);
            if (MarketInfo(OrderSymbol(),MODE_BID)-dSL >= dTS && dSL > OrderStopLoss()) {
               if (!OrderModify(OrderTicket(),OrderOpenPrice(),dSL,OrderTakeProfit(),0)) return(Error("OrderModify"));
            }
         }
      }
      if (Position[i][OP_SELL] > 0) {
         if (OrderSelect(Position[i][OP_SELL], SELECT_BY_TICKET)) {
            if ((MarketInfo(OrderSymbol(),MODE_DIGITS) == 5) || (MarketInfo(OrderSymbol(),MODE_DIGITS) == 3)) iPipsFactor = 10;
            else iPipsFactor = 1;
            dTS=StopLoss*iPipsFactor*MarketInfo(OrderSymbol(),MODE_POINT);
            dSL=OrderStopLoss()-dTS*MathFloor((OrderStopLoss()-dTS-MarketInfo(OrderSymbol(),MODE_ASK))/dTS);
            if (dSL-MarketInfo(OrderSymbol(),MODE_ASK) >= dTS && dSL < OrderStopLoss()) {
               if (!OrderModify(OrderTicket(),OrderOpenPrice(),dSL,OrderTakeProfit(),0)) return(Error("OrderModify"));
            }
         }
      }
      if (Position[i][OP_BUYSTOP] > 0 && Position[i][OP_SELLSTOP] == 0) {
         if (!OrderDelete(Position[i][OP_BUYSTOP])) return(Error("OrderDelete ticket "+Position[i][OP_BUYSTOP]));
      }
      if (Position[i][OP_BUYSTOP] == 0 && Position[i][OP_SELLSTOP] > 0) {
         if (!OrderDelete(Position[i][OP_SELLSTOP])) return(Error("OrderDelete ticket "+Position[i][OP_SELLSTOP]));
      }
   }
   //----------------------------------------------------------------- place new straddle orders for Report
   for (i=0; i<iReports; i++) {
      if (Report[i][3] == "0") {
         if ((TimeGMT() >= StrToTime(Report[i][0])-PERIOD_M5*60) && (TimeGMT() <= StrToTime(Report[i][0]))) {
            Report[i][3] = "done";
            sSequenceNumber=DoubleToStr(SequenceNumber(),0);
            if ((MarketInfo(Report[i][1],MODE_DIGITS) == 5) || (MarketInfo(Report[i][1],MODE_DIGITS) == 3)) iPipsFactor = 10;
            else iPipsFactor = 1;
            pp=MarketInfo(Report[i][1],MODE_ASK)+DistanceToMarket*iPipsFactor*MarketInfo(Report[i][1],MODE_POINT);
            if (OrderSend(Report[i][1],OP_BUYSTOP,Lots,pp,3,pp-StopLoss*iPipsFactor*MarketInfo(Report[i][1],MODE_POINT),pp+TakeProfit*iPipsFactor*MarketInfo(Report[i][1],MODE_POINT),sSequenceNumber,ExpertID,0,Green) == -1) return(Error("OrderSend OP_BUYSTOP"));
            RefreshRates();
            pp=MarketInfo(Report[i][1],MODE_BID)-DistanceToMarket*iPipsFactor*MarketInfo(Report[i][1],MODE_POINT);
            if (OrderSend(Report[i][1],OP_SELLSTOP,Lots,pp,3,pp+StopLoss*iPipsFactor*MarketInfo(Report[i][1],MODE_POINT),pp-TakeProfit*iPipsFactor*MarketInfo(Report[i][1],MODE_POINT),sSequenceNumber,ExpertID,0,Red) == -1) return(Error("OrderSend OP_SELLSTOP"));
         }
      }
   }
}

int Error(string sErrorMessage) {
   int iErrorNumber=GetLastError();
   Print(sErrorMessage," error ",iErrorNumber);
   if (AlertError) Alert(sErrorMessage," error ",iErrorNumber);
   return(-1);
}

//+------------------------------------------------------------------+
//| Function..: SequenceNumber                                       |
//| Purpose...: Generate a sequential number.                        |
//| Returns...: dSeqNum - next sequence number.                      |
//| Notes.....: MT4 keeps the value of the global variable at the    |
//|             client terminal for 4 weeks since the last access.   |
//|             Use SequenceNumber() to generate a unique identity   |
//|             for each order (and passed via parameter <magic>     |
//|             number, or converted to a string and passed via the  |
//|             parameter <comment> to the OrderSend() function) as  |
//|             the trade servers of some brokers do modify the      |
//|             ticket number of a pending order when it changes to  |
//|             a market order.                                      |
//|             The same sequence number could, for example, be used |
//|             to identify the two positions of a straddle order.   |
//|             ******************************************************
//|             * If the expert has to close partial lots, then MT4  *
//|             * retains in the new order the contents of the       *
//|             * OrderMagicNumber() but loses OrderComment().       *
//|             ******************************************************
//| Sample....: string sNumber=DoubleToStr(SequenceNumber(),0);      |
//|             if(OrderSend("EURUSD",OP_BUY,1,Ask,3,Ask-25*Point,   |
//|                          Ask+25*Point,sNumber,16384,0,Green) > 0)|
//|                OrderSend("EURUSD",OP_BUY,1,Ask,3,Ask-25*Point,   |
//|                          Ask+65*Point,sNumber,16384,0,Green);    |
//+------------------------------------------------------------------+
double SequenceNumber() {
   double dSeqNum=1, d;
   string sName="SequenceNumber";

   while (GlobalVariableCheck("Semaphore")) d+=0;
   GlobalVariableSet("Semaphore",1);
   if (GlobalVariableCheck(sName)) dSeqNum=GlobalVariableGet(sName)+1;
   GlobalVariableSet(sName,dSeqNum);
   GlobalVariableDel("Semaphore");
   return(dSeqNum);
}

//+------------------------------------------------------------------+
//| Function..: TimeGMT                                              |
//| Thank you.: Slawa ref. http://www.metatrader4.com/forum/2435     |
//| Purpose...: Retrieve the current system date and time expressed  |
//|             in GMT (UTC) time.                                   |
//| Returns...: tGMT.                                                |
//+------------------------------------------------------------------+
#import "Kernel32.dll"
  void GetSystemTime(int& TimeArray[]);
#import

datetime TimeGMT() {
  int TimeArray[4];
  GetSystemTime(TimeArray);
  int YY=TimeArray[0]&0x0000FFFF;
  int MM=TimeArray[0]>>16;
  int DD=TimeArray[1]>>16;
  int hh=TimeArray[2]&0x0000FFFF;
  int mm=TimeArray[2]>>16;
  int ss=TimeArray[3]&0x0000FFFF;
  return(StrToTime(StringConcatenate(YY,".",MM,".",DD," ",hh,":",mm,":",ss)));
}
//+------------------------------------------------------------------+
Dossiers :
 
Merci ! Wow, il faudra du temps pour lire et comprendre votre code...
 

Je ne sais pas si vous avez déjà résolu ce problème Chee Chua, mais j'ai été très près de pouvoir faire la même chose.

Mon système crée des niveaux de prix (basés sur mes entrées au départ) qui sont ensuite censés être permanents, de sorte que si le prix repasse par un niveau de prix et qu'un ordre n'existe pas, un ordre est placé. Ces niveaux de prix sont créés lorsque chaque ordre en attente est créé en utilisant une boucle for. Le magicnumber est ensuite dérivé du pricelevel.

Vous pouvez voir mon code ici :

https://www.mql5.com/en/forum/306224


Mon seul problème est que parfois mon numéro est inférieur d'un chiffre au niveau de prix de la transaction, ce que je ne peux pas résoudre.

Anyone want to take a crack at this?
Anyone want to take a crack at this?
  • 2019.03.11
  • www.mql5.com
Hi All, Anyone want to take a crack at this? This code will ask for inputs, so in this case the following were entered 2019.03.11 11:56:18.393 2016...