Come creare una matrice di numeri magici? - pagina 4

 
Jon,
1) Il primo OrderSend() viene controllato che abbia avuto successo, verificando che il valore di ritorno sia maggiore di zero
maggiore di zero prima di tentare di piazzare il secondo ordine.
2) Per identificare ed elaborare le coppie di ordini potresti basare il tuo codice su quanto segue se le coppie di ordini
fossero composte da tipi diversi (tuttavia l'algoritmo dovrebbe essere modificato se il raggruppamento
fosse più di 2 o se gli ordini di un gruppo fossero tutti dello stesso tipo):
Il programma di esempio "ReportsTrader.mq4" che segue piazza ordini straddle in prossimità dell'emissione di
rapporti o notizie.
Un ordine straddle è composto da due ordini pendenti sopra e sotto il prezzo di mercato. Quando un
Quando un ordine pendente raggiunge il prezzo di mercato viene trasformato in un ordine a mercato e l'altro pendente

l'ordine deve essere cancellato. Quindi le coppie di ordini richiedono un numero di sequenza per rintracciarli.

//+------------------------------------------------------------------+
//|                                                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)));
}
//+------------------------------------------------------------------+
File:
 
Grazie! Wow, ci vorrà del tempo per leggere e capire il tuo codice...
 

Non so se hai mai risolto questo problema Chee Chua, ma sono arrivato molto vicino a poter fare la stessa cosa.

Il mio sistema crea dei livelli di prezzo (basati sui miei input all'inizio) che dovrebbero poi essere permanenti, in modo che se il prezzo rivisita un livello di prezzo e un ordine non esiste, un ordine viene piazzato. Questi livelli di prezzo vengono creati quando ogni ordine in sospeso viene creato utilizzando un ciclo for. Il magicnumber è poi derivato dal pricelevel.

Potete vedere il mio codice qui:

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


Il mio unico problema è che a volte il mio numero esce 1 cifra meno del Pricelevel del trade, cosa che non riesco a risolvere.

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...