problems with ea programming in mql4

 

Hi guys,

Before saying everything, please note that I'm not an experienced EA program designer however I've got some other experiences.

I've written an EA recently which does not seem to work properly. The problem is that it does not change variables however I wrote the change statements in the code. The other thing is that one variable works, the other not. For exmaple in OrderSend, "Lts" as lot size works but "TP" as takeprofit do not.

I wonder if you could have a look at the code and try to suggest some kind if solution. I do not want you to do the work for me but I'd like to know what was written incorrectly and how to correct it. Many thanks on this in advance!

Let's see the code below: (this is the only content of the mq4 file)


extern bool SignalMail = False;
extern bool EachTickMode = True;
extern double Lots = 0.01;
extern int Slippage = 3;
extern bool UseStopLoss = False;
extern int StopLoss = 200;
extern bool UseTakeProfit = True;
extern int TakeProfit = 10;
extern bool UseTrailingStop = False;
extern int TrailingStop = 200;


int start() {

double Lts = Lots;
int TP = TakeProfit;
int Total = 0;
int x=1;
double Mid = (Bid+Ask)/2;
double Dbarrier;
double Ubarrier;

//ha van már nyitott trade és az árfolyam elérte a szintünket, zárjuk az összes pozíciót

while (Total !=0) {

if (Bid >= Ubarrier+0.0010 || Ask <= Dbarrier-0.0010) {

int tot = OrdersTotal();

for(int k=tot-1;k>=0;k--) {

OrderSelect(k, SELECT_BY_POS);
int type = OrderType();

bool result = false;

switch(type) {


//Close opened long positions

case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5);
break;
Total = 0;

//Close opened short positions

case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5);
Total = 0;
}

if(result == false) {

Alert("Order ", OrderTicket(), " failed to close. Error:", GetLastError() );
Total = 1;
}

}



}


}

// ha nincs nyitott trade, akkor nyissunk egyet

while(Total==0) {

//ha x=1, akkor az előző trade long volt, a következőnek is longnak kell lennie

if (x==1) {

OrderSend(Symbol(), OP_BUY, Lts, Ask, 3, Bid-TP*100/10000, Ask+TP/10000);
Ubarrier = Ask;
Dbarrier = Ask-0.001;
x=2;
Total=1;

//ha x!=0, az előző trade sell volt, tehát a követkőznek is sellnek kell lennie
} else {

OrderSend(Symbol(), OP_SELL, Lts, Bid, 3, Ask+TP*100/10000, Bid-TP/10000);
Ubarrier = Bid+0.001;
Dbarrier = Bid;
x=1;
Total=1;
}

}

//ha már van nyitott trade, de ellenünk megy az árfolyam, további pozíciókat nyitunk

if (Total != 0) {

int i=1;

if(x==2 && Ask<=Dbarrier) {


OrderSend(Symbol(), OP_SELL, Lts+i*Lts, Bid, 3, Ask+TP*100/10000, Bid-TP/10000);
x=1;
i++;

}

if (x==1 && Bid>=Ubarrier) {

OrderSend(Symbol(), OP_BUY, Lts+i*Lts, Ask, 3, Bid-TP*100/10000, Ask+TP/10000);
x=2;
i++;

}

}

return(0);
} //program vége

 

TP*100/10000 TP/10000 .......

use double TP, TP * Point() to replease them !!!!!!

since maybe MT's integer give integer, double give double, so MT's 100/10000 =0, (Bid+Ask)/2 = integer........

 
DxdCn:

TP*100/10000 TP/10000 .......

use double TP, TP * Point() to replease them !!!!!!

since maybe MT's integer give integer, double give double, so MT's 100/10000 =0, (Bid+Ask)/2 = integer........


Thanks for sharing your thoughts!

But my main problem is that by starting this EA, the only thing that happens is that it starts buying by every single tick... Which is not the way it should work. It should only buy when variable Total==0. But at the end of the buying order I set Total to 1. So I guess, Total was not changed properly... Any thoughts on this? Highly appreciated!

 

Make total a STATIC or initialise it on the global scope. https://book.mql4.com/variables/types

As it stands, Total gets reset to zero every new tick

V

 
Viffer:

Make total a STATIC or initialise it on the global scope. https://book.mql4.com/variables/types

As it stands, Total gets reset to zero every new tick

V


Ohh man!... I haven't thought about it... But your absolutely right, so many thanks for your point!:)
Reason: