Coding troubles...

 

Hello folks!

I've written a pretty complicated EA which makes a large use of arrays and I have some troubles to get things right.

It seems like the program skips execution of some lines or mess up operations... yep, kind of strange and that's why I'm writing here.


I've been programing forever in C, basic, pascal, VBA, Delphi.... and I've never seen something like this.

Here one example:


for(i = 0; i<cnt; i++)
{
// Bid-OpenPrice
offset=(Bid-data[i][3])/Point;
if (offset<data[i][38])
{
// Order in loss - save maxloss
data[i][38]=offset;
data[i][39]=TimeCurrent();
}
if (offset>data[i][40])
{
// Order in profit - save maxprofit
data[i][40]=offset;
data[i][41]=TimeCurrent();
}
}



...when I print the array in a CSV file data[i][38] and data[i][40] are always 0... like this part of the code is never executed!

cnt is obviously >0 when the code is executed.


Do U have any idea??? ...maybe type mismatching?

Are there any known limitations/bugs to the MQL4???


I'm totally pissed off cause the EA was coming out so good and this malfunction is draining all the time I have :(

Any comment is welcome!!!



Cheers,

Zyp

 

How about data[i][39] and [41] ?

"like this part of the code is never executed"

Well, is it?

 
phy:

How about data[i][39] and [41] ?

"like this part of the code is never executed"

Well, is it?

of course not :)

...I just wrote 38 and 40 cause the info I need the most is there.


I'd really need to have a hint on how to fix this cause I'm going crazy :S

 

if (offset<data[i][38])
Is that true?

Offset is less than 0?

 
indeed if U r in loss.
 
phy:

if (offset<data[i][38])
Is that true?

Offset is less than 0?

Ok, here I attach the complete code of this exercise.

The program works in H1 and is ment to place orders every hour and keep track of profit and loss in the time to come. When you stop the program all the orders (up to 10000) are written to a CSV file together with time, maxprofit and maxloss info.

For each order the Open and Close values of the last 16 bars are also written to the CSV file.

All this is to get a nice and sound base of data to pass to my neural network looking for patterns in the market... but if I dont get the data out I'm not going very far with the rest of the work :(


What is wrong in the code is that bars after nr 8 are always reported as 0 and maxloss/maxprofit seem not to be ever written to the array... like the for cycle is never accessed.

I've been trying to find the bug but now I'm stuck and I need help :(

...I'm sure it's some stupid bulshit that I am overlooking :)


Here is the code...


***************************************************************


extern int mtbo=3600;
//----
double Lots=0.01;
int OpenOrders=0, cnt=0, i;
int ticket;
int slippage=0;
int LastOrderTime=0;
int LastTicket=0;
int p0=8;
int p1=7;
int p2=6;
int p3=5;
int p4=4;
int p5=3;
int p6=2;
int p7=1;
double pattern=0;
double sl=0, tp=0;
double LastPrice=0;
double LastLots=0;
double data[10000][20];
int offset, handle;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
ArrayInitialize(data,0);
handle=FileOpen("feedfile.txt", FILE_CSV|FILE_WRITE, ';');
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
for(i = 0; i < cnt; i++)
{
FileWrite(handle,i);
FileWrite(handle,TimeToStr(data[i][1],TIME_DATE|TIME_SECONDS),DoubleToStr(data[i][2],4),DoubleToStr(data[i][3],4),
DoubleToStr(data[i][4],4),DoubleToStr(data[i][5],4),DoubleToStr(data[i][6],4),DoubleToStr(data[i][7],4),
DoubleToStr(data[i][8],4),DoubleToStr(data[i][9],4),DoubleToStr(data[i][10],4),DoubleToStr(data[i][11],4),
DoubleToStr(data[i][12],4),DoubleToStr(data[i][13],4),DoubleToStr(data[i][14],4),DoubleToStr(data[i][15],4),
DoubleToStr(data[i][16],4),DoubleToStr(data[i][17],4),DoubleToStr(data[i][18],4),DoubleToStr(data[i][19],4),
DoubleToStr(data[i][20],4),DoubleToStr(data[i][21],4),DoubleToStr(data[i][22],4),DoubleToStr(data[i][23],4),
DoubleToStr(data[i][24],4),DoubleToStr(data[i][25],4),DoubleToStr(data[i][26],4),DoubleToStr(data[i][27],4),
DoubleToStr(data[i][28],4),DoubleToStr(data[i][29],4),DoubleToStr(data[i][30],4),DoubleToStr(data[i][31],4),
DoubleToStr(data[i][32],4),DoubleToStr(data[i][33],4),DoubleToStr(data[i][34],4),DoubleToStr(data[i][35],4),
DoubleToStr(data[i][36],4),DoubleToStr(data[i][37],4),DoubleToStr(data[i][38],4),DoubleToStr(data[i][39],4),
DoubleToStr(data[i][40],4),DoubleToStr(data[i][41],4),DoubleToStr(data[i][0],4));
}
FileClose(handle);
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{

if (TimeCurrent()-LastOrderTime>mtbo)
{
RefreshRates();
pattern =
((Open[0]-Ask)/Point*p0+(Open[1]-Close[1])/Point*p1+(Open[2]-Close[2])/Point*p2+(Open[3]-Close[3])/Point*p3+
(Open[4]-Close[4])/Point*p4+(Open[5]-Close[5])/Point*p5+(Open[6]-Close[6])/Point*p6+(Open[7]-Close[7])/Point*p7);

ticket=OrderSend(Symbol(), OP_BUY, Lots, Ask, slippage, sl, tp, NULL, 0, 0, Blue);
LastOrderTime = TimeCurrent();

data[cnt][0]=pattern;
data[cnt][1]=LastOrderTime;
data[cnt][2]=ticket;
data[cnt][3]=Ask;
data[cnt][4]=Open[0];
data[cnt][5]=Close[1];
data[cnt][6]=Open[1];
data[cnt][7]=Close[2];
data[cnt][8]=Open[2];
data[cnt][9]=Close[3];
data[cnt][10]=Open[3];
data[cnt][11]=Close[4];
data[cnt][12]=Open[4];
data[cnt][13]=Close[5];
data[cnt][14]=Open[5];
data[cnt][15]=Close[6];
data[cnt][16]=Open[6];
data[cnt][17]=Close[7];
data[cnt][18]=Open[7];
data[cnt][19]=Close[8];
data[cnt][20]=Open[8];
data[cnt][21]=Close[9];
data[cnt][22]=Open[9];
data[cnt][23]=Close[10];
data[cnt][24]=Open[10];
data[cnt][25]=Close[11];
data[cnt][26]=Open[11];
data[cnt][27]=Close[12];
data[cnt][28]=Open[12];
data[cnt][29]=Close[13];
data[cnt][30]=Open[13];
data[cnt][31]=Close[14];
data[cnt][32]=Open[14];
data[cnt][33]=Close[15];
data[cnt][34]=Open[15];
data[cnt][35]=Close[16];
data[cnt][36]=Open[16];
data[cnt][37]=Close[17];
data[cnt][38]=0; //maxloss
data[cnt][39]=0;
data[cnt][40]=0; //maxprofit
data[cnt][41]=0;

cnt++;
}


RefreshRates();
for(i=0; i<cnt; i++)
{
// Bid-OpenPrice
offset=(Bid-data[i][3])/Point;
if (offset<data[i][38])
{
// Order in loss - save maxloss
data[i][38]=offset;
data[i][39]=TimeCurrent();
}
if (offset>data[i][40])
{
// Order in profit - save maxprofit
data[i][40]=offset;
data[i][41]=TimeCurrent();
}
}


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

 

Why this?

double data[10000][20];

Reason: