ea programming issues

 

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

 

I have not looked at the rest of your EA, but the integer calculations in SL and TP cannot work. A small integer divided by a larger one yields ZZEERROO, resulting in invalid stops. Make the 10000 in both places into doubles by appending .0, which forces floating point calculation, and it should work.

Note! You may also run into trouble specifying SL and TP as anything but zero in the OrderSend() call. I've had to change EAs, that worked fine a while back, to set SL and TP using OrderModify().

Reason: