Need Help with Modified Trailing Stops...

 

Hey all -

I've been working on a new EA, but am having trouble with the method of trailing my stop. After a few hours of frustrating work, I still can't determine why the stops aren't being adjusted.

The basic principle is to buy once prices close above the 1sd bollinger band and trail the stop up a few pips below the 20sma (middle bollinger band)...and visa-versa with sell trades. Can anyone help me figure out why the stop adjustment isn't working?

Also, I used extreme prices as my TPs (10,000 for longs and 0 for shorts) because I only wanted to be taken out of trades at my trailing stop...is there a better way to do this?

Thanks!


(code attached)

Files:
xdfsdafasdf.mq4  11 kb
 
  1.          if (OrderSymbol()!=Symb)continue;                        // Another security
    
    is redundant
  2.       if(OrdersTotal()>0)
    
    use Total
  3.    if(Open_Buy==true)
    
    if True == true is redundant. if (Open_Buy)...
  4.       RefreshRates();                                             // Refresh Market Info
    
    Redundant unless you're using multiple server calls per tick.
  5. int ticket=OrderSend(Symb,OP_BUY,Lots(),Ask,2*points,SMA20-SL_distance*pips,10000," ",Magic);
    if (ticket<0) Alert("OrderSend failed: ", GetLastError());                                   
    
    Are you getting an error? On ECN brokers you must open and THEN set stops. If you do not want a take profit, use zero not 10000. You are adjusting for 4/5 digit brokers but I prefer my variable names to make it clear what the conversion units are. 2*points is misleading, you're converting 2 pips to points.
    //++++ 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
    

  6.    OrderSelect(ticket,SELECT_BY_TICKET); // Select order by ticket #                                 
       double Current_SL=OrderStopLoss();    // New variable Current_SL
    
    Always test return codes. If the OrderSelect fails, everything following is bogus. If the terminal is restarted (power failure, OS reboot, terminal closed by mistake) ticket will be unknown. EAs must be coded to recover. Put the trailing code at the top of start as part of the order count code.
  7.       Modify_error=OrderModify(ticket,0,SMA20+SL_distance*pips,0,0); // Modify SL to trail down SMA20
          if (Modify_error==false)
             {
             Alert("Order Modification failed, error " + GetLastError());
             }
          if (Modify_error==true)
             {
             Alert ("Order Modification Successful, new stop =" + OrderStopLoss());
    
    if (true=true) redundant, double test redundant
          if (!OrderModify(ticket,OrderOpenPrice(),SMA20+SL_distance*pips,0,0)){
             Alert("Order Modification failed, error " + GetLastError());           // Check for errors
             }
          else{
             Alert ("Order Modification Successful, new stop =" + OrderStopLoss()); // Alert that stop
    
    The message for new stop will probably be wrong since you do not reselect the order after the modification. double newSL=SMA20+SL...; orderMod(newSL); Alert(newSL) Also price=0 is bogus.
 
WHRoeder:
  1. is redundant
  2. use Total
  3. if True == true is redundant. if (Open_Buy)...
  4. Redundant unless you're using multiple server calls per tick.
  5. Are you getting an error? On ECN brokers you must open and THEN set stops. If you do not want a take profit, use zero not 10000. You are adjusting for 4/5 digit brokers but I prefer my variable names to make it clear what the conversion units are. 2*points is misleading, you're converting 2 pips to points.

  6. Always test return codes. If the OrderSelect fails, everything following is bogus. If the terminal is restarted (power failure, OS reboot, terminal closed by mistake) ticket will be unknown. EAs must be coded to recover. Put the trailing code at the top of start as part of the order count code.
  7. if (true=true) redundant, double test redundant The message for new stop will probably be wrong since you do not reselect the order after the modification. double newSL=SMA20+SL...; orderMod(newSL); Alert(newSL) Also price=0 is bogus.

Thanks for your feedback.


1. Right, fixed.

2. Fixed.

3. Fixed; didn't realize I could do that - thanks!

4. Don't know what that means, but I removed it.

5. Not on an ECN broker, fixed, ok.

6. & 7. This is where I get stuck. I keep getting error 1 with order modifications and the stop loss is not adjusted


Also, I removed the if(total>0) -> continue line, because I think it was causing the program to terminate prematurely (ie before stop loss modification on existing orders). I feel like I'm close but just can't quite figure out the logic with the stop adjustments...


Please help! (specifically with regard to the logic and why the stops still don't adjust) ...see attached

Files:
 

Error 1 is no change. You said they weren't being adjust, totally different.

You trailed the stop then you try to move the stop to the same place. Don't call modify if there is no change

double newSL = ...
if (newSL - OrderStopLoss() >= Point){ // Ok to modify buy SL
Reason: