StopLoss moving where it shouldn't

 

Hello again.

I have recently had an issue resolved with the following function which was pretty silly now that I know what it is. I'm a little reluctant to post this issue just in case it is something as equally dumb.

But they do say, the only stupid question is the one unasked. Though I'm not too sure 'they' are I'm taking their advice and seeking help with the issue at hand.

The issue at hand: the following function sets stoploss, takeprofit and trailing stops for orders from two different arrays/strategies. Everything operates as it should when either strategy is allowed to trade without the other being active. When both strategies are activated, the trailingstops seems to move with the price in both directions and seem to update at every point change in price.

The previous issue, for those interested can be found here: https://forum.mql4.com/63917 and was an issue with OrderSelect (or lack thereof) and confusion over what functionality arrays possessed.

This issue has the same smell to it, though I can't put my finger on the exact problem.

The new stop loss is calculated and compared to both the old stoploss and the trailing step distance before the ordermodify command is sent.


            
Files:
 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Get your code to compile

    Your post
    Actual
       }}} //end of order select
       }  //end of counter holding orders
    return;                                  
    }  //end of function  
    
       }  // end of for                       
       }  // end of it                        
       }  // end of maSetSTOPS                
    ERROR   }  //end of counter holding orders
    ERROR return;                             
    ERROR }  //end of function                
    
       }  //end of counter holding orders
    return;                                  
    }  //end of function  
       }  //end of counter holding orders
    return;                                  
    }  //end of if CtCnt>=1
    MISSING }  //end of function ctSetSTOPS 
    

  3. Indent your code so you can follow it.
  4. Self document your unreadable code
    Using #defines
     Using structure
    ctSLTPticket=CtAr_New[y][1];
    ctSL=CtAr_New[y][7];
    ctTP=CtAr_New[y][8];
    ctOpen=CtAr_New[y][3];
    
    #define CT_XXX    0
    #define CT_TICKET 1
    :
    #define CT_SL     7
    #define CT_TP     8
      #define CT_SIZE   9
    double Ctr_New[][CT_SIZE]
    :
    ctSLTPticket=CtAr_New[y][CT_TICKET];
    ctSL        =CtAr_New[y][CT_SL];
    ctTP        =CtAr_New[y][CT_TP];
    ctOpen      =CtAr_New[y][CT_OOP];
    
    struct CT {
      int ticket;
      double OOP;
      double SL;
      double TP;
    }
    CT Ctr_New[];
    :
    ctSLTPticket=CtAr_New[y].ticket;
    ctSL        =CtAr_New[y].SL;
    ctTP        =CtAr_New[y].TP;
    ctOpen      =CtAr_New[y].OOP;
    why are you moving things out of the array? That just makes everything more confusing. Drop the ct variables and use the array directly.
 

Hi WHR,

Thank you gain for taking the time to respond to my problems. Every time I think I'm starting to get the hang of mql4 someone much more experienced, such as yourself, kindly reminds me how much more I have to learn.

I have attached the include file instead of posting the script.

I'm not sure where you're coming from with your second point. the code compiles fine. It is actually two separate functions within the same include file which may help out a little.

With the "define" example above, are you redefining the array or is this representative of an initial declaration. Whilst I've seen define used before I've never used it before in my own work. I guess if your redefining a versio of this would go in the current block, if you are declaring it (which I think you are) then it will have to go over to the fie where the arrays are declared and filled.

About all I understand about "struct" is that is was back added from mql5. I get it's for declaring functionally related variables but that's about it. In this particular example, are you declaring the 1d array then using it as a 'root' for each of the structure variable. As with the define example too, are you proposing this a redefinition of the array for these particular functions or are you suggesting them as a way of declaring the arrays initially.

PS I guess I was moving things out of the array because I didn't really know any better way to access the information contained within them :/