//+------------------------------------------------------------------+
//| CustomRatesUpdate.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" // Name des nutzerdefinierten Symbols
#define CUSTOM_SYMBOL_PATH "Forex" // Name der Gruppe, in der das Symbol erstellt werden soll
#define CUSTOM_SYMBOL_ORIGIN Symbol() // Name des Symbols, das als Basis des nutzerdefinierten Symbols dienen soll
#define DATARATES_COUNT 4 // Anzahl der Balken, die an das Log gesendet werden soll
//+------------------------------------------------------------------+
//| Skript Programm Start Funktion |
//+------------------------------------------------------------------+
void OnStart()
{
//--- Abrufen des Fehlercodes beim Erstellen eines nutzerdefinierten Symbols
int create=CreateCustomSymbol(CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_PATH, CUSTOM_SYMBOL_ORIGIN);
//--- wenn der Fehlercode nicht 0 (erfolgreiche Symbolerstellung) und nicht 5304 (Symbol wurde bereits erstellt) ist - verlasse das Skript
if(create!=0 && create!=5304)
return;
//--- Anzahl der Balken des Standardsymbols abrufen und im Log drucken
int bars_origin=Bars(CUSTOM_SYMBOL_ORIGIN, PERIOD_M1);
PrintFormat("The symbol '%s' from which the custom '%s' was created has %d bars of minute history.", CUSTOM_SYMBOL_ORIGIN, CUSTOM_SYMBOL_NAME, bars_origin);
//--- Anzahl der nutzerdefinierten Symbolbalken abrufen und im Log ausdrucken
int bars_custom=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1);
PrintFormat("Custom symbol '%s' created from symbol '%s' has %d bars of minute history", CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_ORIGIN, bars_custom);
//--- Abrufen der Daten aller Balken des Standardsymbols des Zeitrahmens 1 Minute in das MqlRates-Array
MqlRates rates[]={};
ResetLastError();
if(CopyRates(CUSTOM_SYMBOL_ORIGIN, PERIOD_M1, 0, bars_origin, rates)!=bars_origin)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_ORIGIN, bars_origin, GetLastError());
return;
}
//--- Setzen die kopierten Daten auf den Minuten-Historie des nutzerdefinierten Symbols
ResetLastError();
int updated=CustomRatesUpdate(CUSTOM_SYMBOL_NAME, rates);
if(updated<0)
{
PrintFormat("CustomRatesUpdate(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError());
return;
}
//--- Anzahl der Balken des nutzerdefinierten Symbols nach dem Hinzufügen des Verlaufs abrufen und im Log drucken
bars_custom=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1);
PrintFormat("\nAfter CustomRatesUpdate(), the custom symbol '%s' has %d bars of minute history", CUSTOM_SYMBOL_NAME, bars_custom);
//--- die Daten aller Balken des nutzerdefinierten Symbols des Zeitrahmens 1 Minute in das MqlRates-Array abrufen
ResetLastError();
if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars_custom, rates)!=bars_custom)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars_custom, GetLastError());
return;
}
//--- die letzten vier Balken der M1-Historie des nutzerdefinierten Symbols im Log drucken
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_custom-DATARATES_COUNT, DATARATES_COUNT);
//--- Ersetzen der Daten im MqlRates-Array durch den Umkehrwert der Daten von SymbolName.
for(int i=0; i<bars_custom; i++)
{
rates[i].open =(rates[i].open !=0 ? 1.0 / rates[i].open : rates[i].open);
rates[i].high =(rates[i].high !=0 ? 1.0 / rates[i].high : rates[i].high);
rates[i].low =(rates[i].low !=0 ? 1.0 / rates[i].low : rates[i].low);
rates[i].close =(rates[i].close!=0 ? 1.0 / rates[i].close : rates[i].close);
}
//--- Zuweisen der geänderten Daten der Minutenhistorie des nutzerdefinierten Symbols
ResetLastError();
updated=CustomRatesUpdate(CUSTOM_SYMBOL_NAME, rates);
if(updated<0)
{
PrintFormat("CustomRatesUpdate(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError());
return;
}
//--- Daten aller Balken des Minuten-Zeitrahmens des nutzerdefinierten Symbols erneut in das MqlRates-Array abrufen
ResetLastError();
if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars_custom, rates)!=bars_custom)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars_custom, GetLastError());
return;
}
//--- die letzten vier Balken des aktualisierten Minutenhistorie des nutzerdefinierten Symbols im Log ausdrucken
Print("\nLast %d bars after changing the custom symbol calculation formula:", DATARATES_COUNT);
ArrayPrint(rates, digits, NULL, bars_custom-DATARATES_COUNT, DATARATES_COUNT);
//--- einen Hinweis zu den Tasten zur Beendigung des Skripts im Kommentar des Charts anzeigen
Comment(StringFormat("Press 'Esc' to exit or 'Del' to delete the '%s' symbol and exit", CUSTOM_SYMBOL_NAME));
//--- warten, bis die Tasten „Esc“ oder „Entf“ gedrückt werden, um die Endlosschleife zu verlassen
while(!IsStopped() && TerminalInfoInteger(TERMINAL_KEYSTATE_ESCAPE)==0)
{
Sleep(16);
//--- beim Drücken von Entf das erstellte nutzerdefinierte Symbol und seine Daten löschen
if(TerminalInfoInteger(TERMINAL_KEYSTATE_DELETE)<0)
{
//--- Balkendaten löschen
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);
//--- Tickdaten löschen
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);
//--- Symbol löschen
if(DeleteCustomSymbol(CUSTOM_SYMBOL_NAME))
PrintFormat("Custom symbol '%s' deleted successfully", CUSTOM_SYMBOL_NAME);
break;
}
}
//--- Chart vor dem Ende löschen
Comment("");
/*
Ergebnis:
The symbol 'EURUSD' from which the custom 'EURUSD.C' was created has 250488 bars of minute history.
Custom symbol 'EURUSD.C' created from symbol 'EURUSD' has 0 bars of minute history
After CustomRatesUpdate(), the custom symbol 'EURUSD.C' has 250488 bars of minute history
Last 4 bars of the custom symbol's minute history:
[time] [open] [high] [low] [close] [tick_volume] [spread] [real_volume]
[0] 2024.06.18 11:14:00 1.07235 1.07239 1.07232 1.07239 24 0 0
[1] 2024.06.18 11:15:00 1.07238 1.07239 1.07232 1.07235 44 0 0
[2] 2024.06.18 11:16:00 1.07234 1.07238 1.07227 1.07234 37 0 0
[3] 2024.06.18 11:17:00 1.07234 1.07234 1.07217 1.07225 41 0 0
Last 4 bars after changing the custom symbol calculation formula:
[time] [open] [high] [low] [close] [tick_volume] [spread] [real_volume]
[0] 2024.06.18 11:14:00 0.93253 0.93250 0.93256 0.93250 24 0 0
[1] 2024.06.18 11:15:00 0.93251 0.93250 0.93256 0.93253 44 0 0
[2] 2024.06.18 11:16:00 0.93254 0.93251 0.93260 0.93254 37 0 0
[3] 2024.06.18 11:17:00 0.93254 0.93254 0.93269 0.93262 41 0 0
*/
}
//+------------------------------------------------------------------+
//| Nutzerdefiniertes Symbol erstellen, Fehlercode zurückgeben |
//+------------------------------------------------------------------+
int CreateCustomSymbol(const string symbol_name, const string symbol_path, const string symbol_origin=NULL)
{
//--- Definition des Namens eines Symbols, auf dem ein nutzerdefiniertes Symbol basieren soll.
string origin=(symbol_origin==NULL ? Symbol() : symbol_origin);
//--- Wenn das Erstellen eines nutzerdefinierten Symbols fehlgeschlagen ist und nicht der Fehler 5304 aufgetreten ist, wird es im Log gemeldet.
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);
}
//--- Erfolg
return(error);
}
//+------------------------------------------------------------------+
//| Nutzerdefiniertes Symbol entfernen |
//+------------------------------------------------------------------+
bool DeleteCustomSymbol(const string symbol_name)
{
//--- das Symbol aus dem Fenster der Marktübersicht ausblenden
ResetLastError();
if(!SymbolSelect(symbol_name, false))
{
PrintFormat("SymbolSelect(%s, false) failed. Error %d", GetLastError());
return(false);
}
//--- Wenn das Löschen eines nutzerdefinierten Symbols fehlgeschlagen ist, wird das im Log gemeldet und „false“ zurückgegeben.
ResetLastError();
if(!CustomSymbolDelete(symbol_name))
{
PrintFormat("CustomSymbolDelete(%s) failed. Error %d", symbol_name, GetLastError());
return(false);
}
//--- Erfolg
return(true);
}
|