why is metatrader program iterating the start function for every second?

 

Why does MetaTrader repeat the start function for every second? It does that to my code:


//+------------------------------------------------------------------+
//| alanFirst.mq4 |
//| Copyright © 2010, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
int iterateinit = 0;

int ordcountday[1000];

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Print("Called the init function. iterate number : " + iterateinit);
iterateinit++;
//ArrayInitialize(ordcountday, 0);

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

int res;
int i2, i1, i,j,LoopCount;
int closeb = 0;
int openb = 0;
int timesize = ArraySize(Time);
Print("---------------------------------------starting AlanFirst program. ------------------------------ ");
WriteAlsLog("starting AlanFirst program. ");
Print("# of Bars : " + iBars("EURUSD", PERIOD_M30));
double CDclose = 0.0;
double CDO = 0.0;
int KPeriod;
string opentimes = "2010." + TimeMonth(TimeCurrent()) + "." + TimeDay(TimeCurrent()) + " 17:30:00";
string closetimes = "2010." + TimeMonth(TimeCurrent()) + "." + TimeDay(TimeCurrent()) + " 23:30:00";
datetime open_time = StrToTime(opentimes);
datetime close_time = StrToTime(closetimes);
//Print("Day: " + TimeDay(TimeCurrent()) + " hour: " + TimeHour(TimeCurrent()) + " minute : " + TimeMinute(TimeCurrent()));
prtcurrenttime();
//openb = openBarInt();
openb = iBarShift("EURUSD", PERIOD_M30, open_time);
CDO = iOpen("EURUSD", PERIOD_M30, openb);
//closeb = closeBarInt();
closeb = iBarShift("EURUSD", PERIOD_M30, close_time);
CDclose = iClose("EURUSD", PERIOD_M30, closeb);


if (TimeDay(Time[openb]) == TimeDay(Time[closeb])) {


//if (ordcountday[TimeDay(Time[openb])] != 1 ) {
if (OrdersTotal <= Period) {


if (CDO > CDclose) {
Print(" -------------------------------------- ");
Print("Buy Market current day close at value : " + CDclose + " compared to CDO : " + CDO + " for day " + TimeDay(Time[closeb]));
Print("bar closeb = " + closeb + " Month : " + TimeMonth(Time[closeb]) + " Day : " + TimeDay(Time[closeb]) + ", Hour : " + TimeHour(Time[closeb]) + ", Minute : " + TimeMinute(Time[closeb])
+ " close of day : " + CDclose);
Print("Open time : Day : " + TimeDay(Time[openb]) + " hour :" + TimeHour(Time[openb]) + " minutes : " + TimeMinute(Time[openb]) + " open value :" + iOpen("EURUSD", PERIOD_M30, openb));
Print(" -------------------------------------- ");
WriteAlsLog("Buy Market current day close at value : " + CDclose + " compared to CDO : " + CDO);
res=OrderSend("EURUSD", OP_BUY, 1.0, Ask, 3, 0, 0, "", 1, 0, Blue);
}
if (CDO < CDclose) {
Print(" -------------------------------------- ");
Print("Sell Market current day close at value : " + CDclose + " compared to CDO : " + CDO + " for day " + TimeDay(Time[openb]));
Print("bar openb = " + openb + " Month : " + TimeMonth(Time[openb]) + " Day : " + TimeDay(Time[openb]) + ", Hour : " + TimeHour(Time[openb]) + ", Minute : " + TimeMinute(Time[openb])
+ " Open of day : " + iOpen("EURUSD", PERIOD_M30, openb));
Print("Close time : Day : " + TimeDay(Time[closeb]) + " hour :" + TimeHour(Time[closeb]) + " minutes : " + TimeMinute(Time[closeb]) + " close value : " + iClose("EURUSD", PERIOD_M30, closeb));
Print(" -------------------------------------- ");
WriteAlsLog("Sell Market current day close at value : " + CDclose + " compared to CDO : " + CDO);
res=OrderSend("EURUSD", OP_SELL, 1.0, Bid, 3, 0, 0, "", 1,0,Red);
}
if (CDO == CDclose) {
Print("Market values are equal. Neither buy nor sell." + " for day " + TimeDay(Time[openb]));
}

ordcountday[TimeDay(Time[openb])] = 1;
} else {
Print("more than one trade for day : " + TimeDay(Time[openb]));

}

}//endif-days-equal



//----
return(0);
}


void isopeneqclose(int openb, int closeb) {
if (TimeDay(Time[openb]) == TimeDay(Time[closeb])) {
Print("Open day does not match Close Day.");
}
}

void prtcurrenttime() {
Print("Month : " + TimeMonth(TimeCurrent()) + " Day: " + TimeDay(TimeCurrent()) + " hour: " + TimeHour(TimeCurrent()) + " minute : " + TimeMinute(TimeCurrent()));
}


int openBarInt() {
int i2 = 0;
int openb2 = 0;
for (i2 = Bars - 1; i2 > Bars - 672; i2--) {
if ((TimeMinute(Time[i2]) == 30) && (TimeHour(Time[i2]) == 17)) {
//keep i2
openb2 = i2;
break;
}
}
//CDO = iOpen("EURUSD", PERIOD_M30, i2);
return (openb2);
}

int closeBarInt() {
int i1 = 0, closeb1 = 0;

int openb2 = openBarInt();

for (i1 = Bars - 1; i1 > Bars - 672; i1--) {
if ((TimeMinute(Time[i1]) == 30) && (TimeHour(Time[i1]) == 23)) {
//keep i1
if (TimeDay(Time[i1]) == TimeDay(Time[openb2])) {
closeb1 = i1;
break;
}
}
}
return (closeb1);
}




//extern int writelog = 0;
void WriteAlsLog(string content) {
//if (writelog==1) {
int handle = FileOpen("alanFirst.log", FILE_CSV|FILE_WRITE, ";");
FileSeek(handle,0,SEEK_END);
FileWrite(handle,content);
FileFlush(handle);
FileClose(handle);
//}
}

//+------------------------------------------------------------------+

 

> Why does MetaTrader repeat the start function for every second?

Well it doesnt really, but may seem to...

It actually runs the start function every time a new tick is received

A tick in MT is not an element of time but 'any change in any of the MarketInfo values'
These can occur many times a second or you may wait a minute or more for one to come along at quiet times

FWIW

-BB-

 
BarrowBoy wrote >>

> Why does MetaTrader repeat the start function for every second?

Well it doesnt really, but may seem to...

It actually runs the start function every time a new tick is received

A tick in MT is not an element of time but 'any change in any of the MarketInfo values'
These can occur many times a second or you may wait a minute or more for one to come along at quiet times

FWIW

-BB-



Is there a way I could Order a Buy or Sell just once a day? I was thinking of including if(OrdersTotal() < (TimeDayOfYear(Time[openb]) - TimeDayOfYear(Time[i])) ) { execute Buy or Sell order }
 
alanc:
Is there a way I could Order a Buy or Sell just once a day?

Definitely, check out all the Time based functions https://book.mql4.com/functions/datetime

-BB-