CustomRatesReplace

Sostituisce completamente lo storico dei prezzi del simbolo personalizzato entro l'intervallo di tempo specificato con i dati dell' array di tipo MqlRates.

int  CustomRatesReplace(
   const string     symbol,             // nome del simbolo
   datetime         from,               // data d'inizio
   datetime         to,                 // data di fine
   const MqlRates&  rates[],            // array per i dati da applicare al simbolo personalizzato
   uint             count=WHOLE_ARRAY   // numero degli elementi dell'array rates[] da utilizzare
   );

Parametri

symbol

[in]  Nome simbolo personalizzato.

from

[In] Orario della prima barra nella cronologia dei prezzi entro l'intervallo specificato da aggiornare.

to

[in] Orario dell'ultima barra nello storico dei prezzi entro l'intervallo specificato da aggiornare.

rates[]

[in] Array dei dati storici di tipo MqlRates per M1.

Valore Restituito

Numero di barre aggiornate o -1 in caso di errore.

Nota

Se la barra dall'array rates[] va oltre l'intervallo specificato, viene ignorata. Se una tale barra è già presente nello storico dei prezzi e rientra nell'intervallo specificato, viene sostituita. Tutte le altre barre dell'attuale storico dei prezzi al di fuori dell'intervallo specificato restano invariate. L'array dati rates[] dovrebbe essere corretto per quanto riguarda i prezzi OHLC, mentre i tempi di apertura della barra dovrebbero corrispondere al timeframe M1.

 

Esempio:

//+------------------------------------------------------------------+
//|                                           CustomRatesReplace.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#define   CUSTOM_SYMBOL_NAME     Symbol()+".C"     // nome del simbolo personalizzato
#define   CUSTOM_SYMBOL_PATH     "Forex"           // nome del gruppo in cui un simbolo è stato creato
#define   CUSTOM_SYMBOL_ORIGIN   Symbol()          // nome di un simbolo su cui è basata la personalizzazione 
 
#define   DATARATES_COUNT        4                 // numero di barre inviate al journal
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- ottenere il codice di errore quando si crea un simbolo personalizzato
   int create=CreateCustomSymbol(CUSTOM_SYMBOL_NAMECUSTOM_SYMBOL_PATHCUSTOM_SYMBOL_ORIGIN);
   
//---- se il codice di errore non è 0 (creazione del simbolo riuscita) e non è 5304 (il simbolo è già stato creato) - lasciare
   if(create!=0 && create!=5304)
      return;
 
//--- ottenere il numero di barre standard del simbolo
   int bars=Bars(CUSTOM_SYMBOL_ORIGINPERIOD_M1);
      
//--- ottenere i dati di tutte le barre del simbolo standard con timeframe ad un minuto nell'array MqlRates
   MqlRates rates[]={};
   ResetLastError();
   if(CopyRates(CUSTOM_SYMBOL_ORIGINPERIOD_M10barsrates)!=bars)
     {
      PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d"CUSTOM_SYMBOL_ORIGINbarsGetLastError());
      return;
     }
 
//--- impostare i dati copiati alla cronologia dei minuti del simbolo personalizzato
   ResetLastError();
   if(CustomRatesUpdate(CUSTOM_SYMBOL_NAMErates)<0)
     {
      PrintFormat("CustomRatesUpdate(%s) failed. Error %d"CUSTOM_SYMBOL_NAMEGetLastError());
      return;
     }
     
//--- dopo aver aggiornato i dati storici, ottenere il numero di barre del simbolo personalizzato
   bars=Bars(CUSTOM_SYMBOL_NAMEPERIOD_M1);
   
//--- ottenere i dati di tutte le barre del simbolo personalizzato su timeframe ad un minuto nell'array MqlRates
   ResetLastError();
   if(CopyRates(CUSTOM_SYMBOL_NAMEPERIOD_M10barsrates)!=bars)
     {
      PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d"CUSTOM_SYMBOL_NAMEbarsGetLastError());
      return;
     }
 
//--- stampare le ultime barre DATARATES_COUNT della cronologia dei minuti del simbolo personalizzato nel journal
   int digits=(int)SymbolInfoInteger(CUSTOM_SYMBOL_NAMESYMBOL_DIGITS);
   PrintFormat("Last %d bars of the custom symbol's minute history:"DATARATES_COUNT);
   ArrayPrint(ratesdigitsNULLbars-DATARATES_COUNTDATARATES_COUNT);
   
//---- modificare i dati delle due penultime barre nella cronologia dei minuti del simbolo personalizzato
   datetime time_fromrates[bars-3].time;
   datetime time_to  = rates[bars-2].time;
   
//--- rendere tutti i prezzi delle due penultime barre pari ai prezzi di apertura di queste barre nell'array 'rates'
   rates[bars-3].high=rates[bars-3].open;
   rates[bars-3].low=rates[bars-3].open;
   rates[bars-3].close=rates[bars-3].open;
   
   rates[bars-2].high=rates[bars-2].open;
   rates[bars-2].low=rates[bars-2].open;
   rates[bars-2].close=rates[bars-2].open;
   
//--- sostituire le barre esistenti con i dati dell'array 'rates' modificato
   ResetLastError();
   int replaced=CustomRatesUpdate(CUSTOM_SYMBOL_NAMErates);
   if(replaced<0)
     {
      PrintFormat("CustomRatesUpdate(%s) failed. Error %d"CUSTOM_SYMBOL_NAMEGetLastError());
      return;
     }
     
//--- dopo aver modificato le due barre di dati storici, ottenere nuovamente il numero di barre del simbolo personalizzato
   bars=Bars(CUSTOM_SYMBOL_NAMEPERIOD_M1);
   
//--- ottenere nuovamente i dati di tutte le barre del simbolo personalizzato con timeframe ad un minuto
   ResetLastError();
   if(CopyRates(CUSTOM_SYMBOL_NAMEPERIOD_M10barsrates)!=bars)
     {
      PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d"CUSTOM_SYMBOL_NAMEbarsGetLastError());
      return;
     }
 
//--- stampare le ultime barre DATARATES_COUNT della cronologia dei minuti aggiornata del simbolo personalizzato nel journal
   PrintFormat("\nLast %d bars after applying CustomRatesUpdate() with %d replaced bars:"DATARATES_COUNTreplaced);
   ArrayPrint(ratesdigitsNULLbars-DATARATES_COUNTDATARATES_COUNT);
     
//--- visualizzare un suggerimento sui tasti di terminazione dello script nel commento del grafico
   Comment(StringFormat("Press 'Esc' to exit or 'Del' to delete the '%s' symbol and exit"CUSTOM_SYMBOL_NAME));
//--- attendere che i tasti Esc o Del vengano premuti per uscire da un ciclo infinito
   while(!IsStopped() && TerminalInfoInteger(TERMINAL_KEYSTATE_ESCAPE)==0)
     {
      Sleep(16);
      //--- quando si preme Del, eliminare il simbolo personalizzato creato e i relativi dati
      if(TerminalInfoInteger(TERMINAL_KEYSTATE_DELETE)<0)
        {
         //--- eliminare i dati della barra
         int deleted=CustomRatesDelete(CUSTOM_SYMBOL_NAME0LONG_MAX);
         if(deleted>0)
            PrintFormat("%d history bars of the custom symbol '%s' were successfully deleted"deletedCUSTOM_SYMBOL_NAME);
         
         //--- cancellare i dati dei tick
         deleted=CustomTicksDelete(CUSTOM_SYMBOL_NAME0LONG_MAX);
         if(deleted>0)
            PrintFormat("%d history ticks of the custom symbol '%s' were successfully deleted"deletedCUSTOM_SYMBOL_NAME);
         
         //--- eliminare il simbolo
         if(DeleteCustomSymbol(CUSTOM_SYMBOL_NAME))
            PrintFormat("Custom symbol '%s' deleted successfully"CUSTOM_SYMBOL_NAME);
         break;
        }
     }
//--- pulire il grafico prima di uscire
   Comment("");
   /*
   risultato:
   Last 4 bars of the custom symbol's minute history:
                    [time]  [open]  [high]   [low] [close] [tick_volume] [spread] [real_volume]
   [0] 2024.07.29 13:37:00 1.08394 1.08396 1.08388 1.08390            16        1             0
   [1] 2024.07.29 13:38:00 1.08389 1.08400 1.08389 1.08398            35        1             0
   [2] 2024.07.29 13:39:00 1.08398 1.08410 1.08394 1.08410            29        1             0
   [3] 2024.07.29 13:40:00 1.08409 1.08414 1.08408 1.08414            14        1             0
   
   Last 4 bars after applying CustomRatesUpdate() with 250820 replaced bars:
                    [time]  [open]  [high]   [low] [close] [tick_volume] [spread] [real_volume]
   [0] 2024.07.29 13:37:00 1.08394 1.08396 1.08388 1.08390            16        1             0
   [1] 2024.07.29 13:38:00 1.08389 1.08389 1.08389 1.08389            35        1             0
   [2] 2024.07.29 13:39:00 1.08398 1.08398 1.08398 1.08398            29        1             0
   [3] 2024.07.29 13:40:00 1.08409 1.08414 1.08408 1.08414            14        1             0
   */
  }
//+----------------------------------------------------------------------------------------+
//| Creare un simbolo personalizzato, restituire un codice di errore |
//+----------------------------------------------------------------------------------------+
int CreateCustomSymbol(const string symbol_nameconst string symbol_pathconst string symbol_origin=NULL)
  {
//--- definire il nome di un simbolo su cui è basata la personalizzazione
   string origin=(symbol_origin==NULL ? Symbol() : symbol_origin);
   
//--- se non è stato possibile creare un simbolo personalizzato e questo non corrisponde all'errore 5304, segnalarlo nel journal
   ResetLastError();
   int error=0;
   if(!CustomSymbolCreate(symbol_namesymbol_pathorigin))
     {
      error=GetLastError();
      if(error!=5304)
         PrintFormat("CustomSymbolCreate(%s, %s, %s) failed. Error %d"symbol_namesymbol_pathoriginerror);
     }
//--- successo
   return(error);
  }
//+------------------------------------------------------------------+
//| Rimuovere un simbolo personalizzato |
//+------------------------------------------------------------------+
bool DeleteCustomSymbol(const string symbol_name)
  {
//--- nascondere il simbolo dalla finestra Market Watch
   ResetLastError();
   if(!SymbolSelect(symbol_namefalse))
     {
      PrintFormat("SymbolSelect(%s, false) failed. Error %d"GetLastError());
      return(false);
     }
      
//--- se non è stato possibile eliminare un simbolo personalizzato, segnalarlo nel journal e restituire 'false'
   ResetLastError();
   if(!CustomSymbolDelete(symbol_name))
     {
      PrintFormat("CustomSymbolDelete(%s) failed. Error %d"symbol_nameGetLastError());
      return(false);
     }
//--- successo
   return(true);
  }

 

Guarda anche

CustomRatesDelete, CustomRatesUpdate, CopyRates