Help on my first EA ?

 

Hi, I'm new to programming MQL4, or even programming at all; I've been working on the trailing stop part of the EA. I use a regular very basic SMA cross to generate the BUY and SELL signals and it is irrelevant at this time.

The issue is : I'm satisfied with the trailing of BUY trades, but somehow unsuccessful for the SELL trades; and it looks pretty much the same to me.

The idea here is:

Buy trades: -price goes up and is equal the initial stop loss, then the stop loss is moved to break even.

-price keeps going up (in my EA: 30 pips) then stop moves +30 pips.

Sell trades: -price goes down and is equal the initial stop loss, then stop loss is moved to break even.

-price keeps going down (30 pips) then stop should move -30 pips. But somehow it doesn't ??


I would really appreciate any help; beside if anyone would like to make any suggestion, please don't hesitate and keep in mind this is new to me. Thanks. Snoopy


//----TRAILING STOPS
for(cnt = 0; cnt < total; cnt++) //trailing stops settings
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
stopini=OrderStopLoss();
price=OrderOpenPrice();

if(OrderType()==OP_BUY) //long positions
{
if (OrderOpenPrice()!=OrderStopLoss())
{if(Bid-OrderOpenPrice()==price-stopini) //profit=stoploss
{
OrderModify (OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);//move stop BE
Print("Stop moved to break even");
OldBid=Bid;
}
}
else if(Bid>=OldBid+30*Point) //price moved +30pips
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopini+30*Point,OrderTakeProfit(),0,CLR_NONE);
Print("Move stop +30");
}
}

if(OrderType()==OP_SELL) //short positions
{
if(OrderOpenPrice()!=OrderStopLoss())
{if(OrderOpenPrice()-Ask==stopini-price) //profit=stoploss
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);//move stop BE
Print("Stop moved to break even");
OldAsk=Ask;
}
}
else if(Ask<=OldAsk-30*Point) //price moved -30pips
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopini-30*Point,OrderTakeProfit(),0,CLR_NONE);
Print("Move stop -30");
}
}
}

Reason: