Coding Glitch: Input Needed!

 

I'm tearing my hair out!:)

I have just programmed a risk management EA but I have one problem which I just can't figure; the picture shows my problem. I have a trailing stop of 7 pips but for some reason, sometimes it doesn't work. As you can see in the picture, I enter a long position which immediately goes against me but for some reason the trailing stop doesn't kick in. I have gone through the code top to bottom and can't figure out what is going wrong. If anybody has an idea of what I might be missing please drop me a line. Attached the EA as well.

P.S. Opening price is 1.2270 and price drops to 1.2246, much more than 7 pips. Go figure!

.

Cheers...

 
Moniesjon:

I'm tearing my hair out!:)

P.S. Opening price is 1.2270 and price drops to 1.2246, much more than 7 pips. Go figure!

There is no need to tear your hair out, and neither is there any need to figure anything out.

Just keep adding more Print() statements until you have the exact reason why it's not doing what you want.

 
Moniesjon:

I'm tearing my hair out!:)

I have just programmed a risk management EA but I have one problem which I just can't figure; the picture shows my problem. I have a trailing stop of 7 pips but for some reason, sometimes it doesn't work. As you can see in the picture, I enter a long position which immediately goes against me but for some reason the trailing stop doesn't kick in. I have gone through the code top to bottom and can't figure out what is going wrong. If anybody has an idea of what I might be missing please drop me a line. Attached the EA as well.

P.S. Opening price is 1.2270 and price drops to 1.2246, much more than 7 pips. Go figure!

.

Cheers...


if its going against you the TS can not work because you look for Bid-OrderOpenPrice()>MyPoint*TS_MinProfit

So, Bid must be more big then OrderOpenPrice, and in this case is not -->> because price is going against you

 
  1. ticket=OrderSend(Symbol(),OP_BUY,Lotz,Ask,Slippage,0,0,TicketComment,MagicNumber,0,Green);
    if(ticket>0)OrderModify(ticket, OrderOpenPrice(), aStopLoss, aTakeProfit, 0, CLR_NONE); 
    
    OrderOpenPrice() is meaningless here since you haven't selected the order first. Always test return codes.

  2. int total = OrdersTotal();
    for(int cnt=0;cnt<total;cnt++)
    {
        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
    
        if(OrderType()<=OP_SELL &&
             OrderSymbol()==Symbol() &&
             OrderMagicNumber()==MagicNumber)
        {
           if(OrderType()==OP_BUY)   // buy position is opened   
    
    Get in the habit of always counting down. You MUST count down when closing/deleting in the presence of multiple orders (multiple charts.) Always test return codes
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol() ){              // and my pair.
        if(OrderType()==OP_BUY)   // buy position is opened   
    

  3. OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(MyPoint*TrailingStop),OrderTakeProfit(),0,Green);
    
    Always test return codes
    if (!OrderModify(...))
       Alert("OrderModify(",OrderTicket(),") Failed: ",GetLastError())

  4. EA's should adjust TP, SL, AND SLIPPAGE
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

Reason: