Wie vermutet, sind die Abfragen von diversen Zuständen des Systems (zB AccountInfoDouble(), PositionGetDouble()) relativ langsam.
Möchte ich also das Maximum an Performance erzielen und verwende ich zB PositionGetDouble(POSITION_SL) oder ähnliches mehrmals in OnTick(), speichere ich diese Werte temporär in Variablen.
Am Beispiel AccountInfoDouble(ACCOUNT_BALANCE) sieht man den Unterschied:
Bei 1e7 Durchläufen beträgt die Differenz etwa 2 Sekunden (2094 zu 47 msec) mit meinem i7 mit 4GHz.
Code:
void OnStart()
{
uint n=1e7;
uint start,time;
double v,w;
start=GetTickCount();
for(int i=0; i<n; i++)
v=AccountInfoDouble(ACCOUNT_BALANCE);
time=GetTickCount()-start;
Print("Elapsed time for ",n," calculations 1: ",time," msec");
w=AccountInfoDouble(ACCOUNT_BALANCE);
start=GetTickCount();
for(int i=0; i<n; i++)
v=w;
time=GetTickCount()-start;
Print("Elapsed time for ",n," calculations 2: ",time," msec");
}
Des weiteren programmiert es sich einfach übersichtlicher auf diese Art.
Code:
void OnTick()
{
PosSL=PositionGetDouble(POSITION_SL);
PosTP=PositionGetDouble(POSITION_TP);
.
.
.
}
Noch übersichtlicher wird der Programmablauf durch Auslagern in eine mqh-Datei, doch dazu mehr in einem nächsten Beitrag.