The function to get a list of all charts and the function to close a chart.
// List of all charts, including CHART_IS_OBJECT.int GetCharts( long &Charts[], long Chart = 0 )
{
if (Chart)
for (uint i = ObjectsTotal(Chart, -1, OBJ_CHART); (bool)i--;)
{
constlong NewChart = ObjectGetInteger(Chart, ObjectName(Chart, i, -1, OBJ_CHART), OBJPROP_CHART_ID);
if (NewChart)
{
Charts[ArrayResize(Charts, ArraySize(Charts) + 1) - 1] = NewChart;
GetCharts(Charts, NewChart);
}
}
else
{
ArrayFree(Charts);
for (long Chart = ChartFirst(); Chart != -1; Chart = ChartNext(Chart))
{
long Tmp;
if (!ChartGetInteger(Chart, CHART_IS_OBJECT, -1, Tmp))
ResetLastError();
elseif (!Tmp)
{
Charts[ArrayResize(Charts, ArraySize(Charts) + 1) - 1] = Chart;
GetCharts(Charts, Chart);
}
}
}
return(ArraySize(Charts));
}
// Closes and CHART_IS_OBJECT charts.bool ChartCloseFull( long chart_id = 0, long Chart = 0 )
{
bool Res = false;
long Tmp;
if (Chart)
for (uint i = ObjectsTotal(Chart, -1, OBJ_CHART); !Res && (bool)i--;)
{
conststring Name = ObjectName(Chart, i, -1, OBJ_CHART);
constlong NewChart = ObjectGetInteger(Chart, Name, OBJPROP_CHART_ID);
ResetLastError();
Res = ((NewChart == chart_id) && (ObjectDelete(Chart, Name) ||
(_LastError == ERR_OBJECT_ERROR) ||
(_LastError == ERR_OBJECT_NOT_FOUND))) ||
ChartCloseFull(chart_id, NewChart);
}
elseif (!ChartGetInteger(chart_id, CHART_IS_OBJECT, -1, Tmp) || Tmp)
{
ResetLastError();
if (!chart_id)
chart_id = ChartID();
for (long Chart = ChartFirst(); !Res && (Chart != -1); Chart = ChartNext(Chart))
if (!ChartGetInteger(Chart, CHART_IS_OBJECT, -1, Tmp))
ResetLastError();
elseif (!Tmp)
Res = ChartCloseFull(chart_id, Chart);
}
else
Res = ChartClose(chart_id);
return(Res);
}
Example.
long CreateChart( constlong Chart = 0 )
{
conststring Name = (string)MathRand();
return(ObjectCreate(Chart, Name, OBJ_CHART, 0, 0, 0) ? ObjectGetInteger(Chart, Name, OBJPROP_CHART_ID) : -1);
}
voidOnStart()
{
long NewCharts[];
constint Size = ArrayResize(NewCharts, 9);
for (int i = 0; i < Size; i++)
{
NewCharts[i] = CreateChart((bool)(i %3) ? NewCharts[i - 1] : 0);
Sleep(100);
}
Print("\nCreated charts:");
ArrayPrint(NewCharts);
long Charts[];
Print("\nList of all charts:");
GetCharts(Charts);
ArrayPrint(Charts);
for (int i = Size; (bool)i--;)
{
Print("\nDelete " + (string)NewCharts[i] + " - " + (string)ChartCloseFull(NewCharts[i]));
Sleep(100);
Print("List of all charts:");
GetCharts(Charts);
ArrayPrint(Charts);
}
}
Result.
Created charts:
[0] 132503570123939383132503570123939384132503570123939385132503570123939386132503570123939387
[5] 132503570123939388132503570123939389132503570123939390132503570123939391List of all charts:
[0] 132503570123939382132503570123939386132503570123939387132503570123939388132503570123939389
[5] 132503570123939390132503570123939391132503570123939383132503570123939384132503570123939385
Delete 132503570123939391 - true
List of all charts:
[0] 132503570123939382132503570123939386132503570123939387132503570123939388132503570123939389
[5] 132503570123939390132503570123939383132503570123939384132503570123939385
Delete 132503570123939390 - true
List of all charts:
[0] 132503570123939382132503570123939386132503570123939387132503570123939388
[4] 132503570123939389132503570123939383132503570123939384132503570123939385
Delete 132503570123939389 - true
List of all charts:
132503570123939382132503570123939386132503570123939387132503570123939388132503570123939383132503570123939384132503570123939385
Delete 132503570123939388 - true
List of all charts:
132503570123939382132503570123939386132503570123939387132503570123939383132503570123939384132503570123939385
Delete 132503570123939387 - true
List of all charts:
132503570123939382132503570123939386132503570123939383132503570123939384132503570123939385
Delete 132503570123939386 - true
List of all charts:
132503570123939382132503570123939383132503570123939384132503570123939385
Delete 132503570123939385 - true
List of all charts:
132503570123939382132503570123939383132503570123939384
Delete 132503570123939384 - true
List of all charts:
132503570123939382132503570123939383
Delete 132503570123939383 - true
List of all charts:
132503570123939382
Is there a modern language where you can't make a template Sort() ?
PS/ unlike multiple inheritance it can be done ugly through #define SORT(arr,func). But that's cheesy from the 80's. Why it's not in templates is a mystery to me
Template pointers to functions do not work in MQL5.
There is a nice solution to overcome that limitation ofMQL. The trick is to pass the templated CompareFunc to the generic sort function both as a function pointer + a type parameter T .
This trick helps the compiler to resolve the typename parameters..
GenericSortTemplated.mq5
// templated classtemplate <typename T>class Foo {
public:
Foo() { payload=(T)10*MathRand()/MathRand(); }
~Foo() {}
public:
T payload;
};
// templated compare function for different types of payloadstemplate <typename T>bool CompareByPayload(T one, T two)
{
return one.payload < two.payload;
}
//--------------------------------------------------------------------------------------------// TEMPLATE SORTING// function is templated for any type of array (e.g., int[], double[], pointers to objects[]).// The CompareFunc type is a pointer to comparison function that can accept different types.//--------------------------------------------------------------------------------------------template<typename T, typename CompareFunc>
void GenericSort(T &a[], CompareFunc compare)
{
for(int i=1; i < ArraySize(a); ++i)
for(int j=i; j>0 && compare( a[j], a[j-1] ); j--)
{
T tmp=a[j]; a[j]=a[j-1]; a[j-1]=tmp;
}
}
// custom sort function for different types of payloadstemplate<typename T >
void Sort(T& a[])
{
typedefbool (*CompareFunc)( T, T );
GenericSort< T,CompareFunc >( a, CompareByPayload< T >);
}
Foo<int> *arr[];
Foo<float> *flt[];
voidOnStart()
{
int n = 7;
ArrayResize(arr,n);
ArrayResize(flt,n);
for(int i=0;i<n;i++) {
arr[i]=new Foo<int>();
flt[i]=new Foo<float>();
}
Sort(arr);
Sort(flt);
for(int i=0;i<n;i++) {
printf("%d. payload=%s",i,(string)arr[i].payload);
delete arr[i];
}
for(int i=0;i<n;i++) {
printf("%d. payload=%s",i,(string)flt[i].payload);
delete flt[i];
}
}
The sort function is templated. The compare function is also templated. The highlight colors indicate how the typename parameters are specialized and resolved.
Functions are imported from compiled MQL5 modules (*.ex5 files) andfrom Windows dynamic library modules (*.dll files).
While I was conducting some tests I tried to build my application as .exe instead of .dll and imported it into my mql5 EA and saw that the compiler doesn't give me any errors and also I can use static functions inside the program.
Since I couldn't find anything about importing .exe programs in the documentation I was wondering if that implies something I am now aware of. Does it impact the lifecycle of the EA? Does the terminal manage .exe imports differently from .dlls in any way?
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Such a loop will loop through all charts, including even nested OBJ_CHART variants (for which OBJPROP_CHART_ID was called)
Example.
Result.
I use such a universal construction of setting on/off Common-folder via file name.
If the file name contains an asterisk, it will open in the Common folder, otherwise - as specified.
Who knows how to make this work ?
to get a Sort template ( array_pointers, compare_function )
Who knows how to make something like this work ?
to get a Sort template ( array_pointers, compare_function )
Template pointers to functions do not work in MQL5.
Template pointers to functions do not work in MQL5.
sadly... :-(
so until there is a pointer to a member-function (what a word that is), it is premature to talk about human OO in MQL.
It is premature to talk about human OO in MQL
Forum on trading, automated trading systems and testing trading strategies
Do I need multiple inheritance of interfaces in MQL?
Koldun Zloy, 2025.01.23 14:29
It is a feature that can easily be done without.
It will not untie anyone's hands.
It has nothing to do with development.
Is there a modern language where you can't make a template Sort() ?
PS/ unlike multiple inheritance it can be done ugly through #define SORT(arr,func). But that's cheesy from the 80's. Why it's not in templates is a mystery to meTemplate pointers to functions do not work in MQL5.
There is a nice solution to overcome that limitation of MQL. The trick is to pass the templated CompareFunc to the generic sort function both as a function pointer + a type parameter T .
This trick helps the compiler to resolve the typename parameters..
GenericSortTemplated.mq5
The sort function is templated. The compare function is also templated. The highlight colors indicate how the typename parameters are specialized and resolved.
output:
While I was conducting some tests I tried to build my application as .exe instead of .dll and imported it into my mql5 EA and saw that the compiler doesn't give me any errors and also I can use static functions inside the program.
Since I couldn't find anything about importing .exe programs in the documentation I was wondering if that implies something I am now aware of. Does it impact the lifecycle of the EA? Does the terminal manage .exe imports differently from .dlls in any way?
Thanks for your time.