MQL5Book/Scripts/p6/SymbolRemoveUnused.mq5

59 lines
2.3 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:09:41 +02:00
//+------------------------------------------------------------------+
//| SymbolRemoveUnused.mq5 |
//| Copyright 2022, MetaQuotes Ltd. |
//| https://www.mql5.com |
//| Remove unused symbols from the Market Watch. |
//+------------------------------------------------------------------+
#include "..\..\Include\MqlError.mqh"
#define PUSH(A,V) (A[ArrayResize(A, ArraySize(A) + 1, ArraySize(A) * 2) - 1] = V)
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// request user confirmation
if(IDOK == MessageBox("This script will remove all unused symbols"
" from the Market Watch. Proceed?", "Please, confirm", MB_OKCANCEL))
{
const int n = SymbolsTotal(true);
ResetLastError();
string removed[];
// loop through the symbols backwards
for(int i = n - 1; i >= 0; --i)
{
const string s = SymbolName(i, true);
if(SymbolSelect(s, false))
{
// collect removed symbols
PUSH(removed, s);
}
else
{
// show error description otherwise
PrintFormat("Can't remove '%s': %s (%d)", s, E2S(_LastError), _LastError);
}
}
const int r = ArraySize(removed);
PrintFormat("%d out of %d symbols removed", r, n);
if(r > 0)
{
ArrayPrint(removed);
// if some symbols have been removed, we have an option to restore them
// (at this moment Market Watch is already prunned)
if(IDOK == MessageBox("Do you want to restore removed symbols"
" in the Market Watch?", "Please, confirm", MB_OKCANCEL))
{
int restored = 0;
for(int i = r - 1; i >= 0; --i)
{
restored += SymbolSelect(removed[i], true);
}
PrintFormat("%d symbols restored", restored);
}
}
}
}
//+------------------------------------------------------------------+