//+------------------------------------------------------------------+
//| 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_NAME, CUSTOM_SYMBOL_PATH, CUSTOM_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_ORIGIN, PERIOD_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_ORIGIN, PERIOD_M1, 0, bars, rates)!=bars)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_ORIGIN, bars, GetLastError());
return;
}
//--- impostare i dati copiati alla cronologia dei minuti del simbolo personalizzato
ResetLastError();
if(CustomRatesUpdate(CUSTOM_SYMBOL_NAME, rates)<0)
{
PrintFormat("CustomRatesUpdate(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError());
return;
}
//--- dopo aver aggiornato i dati storici, ottenere il numero di barre del simbolo personalizzato
bars=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1);
//--- ottenere i dati di tutte le barre del simbolo personalizzato su timeframe ad un minuto nell'array MqlRates
ResetLastError();
if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars, rates)!=bars)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars, GetLastError());
return;
}
//--- stampare le ultime barre DATARATES_COUNT della cronologia dei minuti del simbolo personalizzato nel journal
int digits=(int)SymbolInfoInteger(CUSTOM_SYMBOL_NAME, SYMBOL_DIGITS);
PrintFormat("Last %d bars of the custom symbol's minute history:", DATARATES_COUNT);
ArrayPrint(rates, digits, NULL, bars-DATARATES_COUNT, DATARATES_COUNT);
//---- modificare i dati delle due penultime barre nella cronologia dei minuti del simbolo personalizzato
datetime time_from= rates[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_NAME, rates);
if(replaced<0)
{
PrintFormat("CustomRatesUpdate(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError());
return;
}
//--- dopo aver modificato le due barre di dati storici, ottenere nuovamente il numero di barre del simbolo personalizzato
bars=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1);
//--- ottenere nuovamente i dati di tutte le barre del simbolo personalizzato con timeframe ad un minuto
ResetLastError();
if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars, rates)!=bars)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars, GetLastError());
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_COUNT, replaced);
ArrayPrint(rates, digits, NULL, bars-DATARATES_COUNT, DATARATES_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_NAME, 0, LONG_MAX);
if(deleted>0)
PrintFormat("%d history bars of the custom symbol '%s' were successfully deleted", deleted, CUSTOM_SYMBOL_NAME);
//--- cancellare i dati dei tick
deleted=CustomTicksDelete(CUSTOM_SYMBOL_NAME, 0, LONG_MAX);
if(deleted>0)
PrintFormat("%d history ticks of the custom symbol '%s' were successfully deleted", deleted, CUSTOM_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_name, const string symbol_path, const 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_name, symbol_path, origin))
{
error=GetLastError();
if(error!=5304)
PrintFormat("CustomSymbolCreate(%s, %s, %s) failed. Error %d", symbol_name, symbol_path, origin, error);
}
//--- successo
return(error);
}
//+------------------------------------------------------------------+
//| Rimuovere un simbolo personalizzato |
//+------------------------------------------------------------------+
bool DeleteCustomSymbol(const string symbol_name)
{
//--- nascondere il simbolo dalla finestra Market Watch
ResetLastError();
if(!SymbolSelect(symbol_name, false))
{
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_name, GetLastError());
return(false);
}
//--- successo
return(true);
}
|