Download MetaTrader 5

Help with code

To add comments, please log in or register
Deposit to your account using bank cards via Gate2Shop
bm0899 2016.07.18 04:18 
Please look at code below, I have a time based stop loss function below but it is not working properly as It only looping on on the first time only (TimeBasedSLperiodLoss1), and does not go to TimeBasedSLperiodLoss2. Any help would be highly appreciated.


extern string  Percent_Close="% of position to close at each time interval of a losing trade, <=100%";
extern double  PercentClose1       = 25;//position percent to close
extern double  PercentClose2       = 50;//position percent to close
extern double  PercentClose3       = 75;//position percent to close
extern double  PercentClose4       = 100;//position percent to close

extern string  Loss_Time="Seconds: For each interval how many seconds to wait before losing trade is closed"; 
extern double  TimeBasedSLperiodLoss1   = 300; // in seconds
extern double  TimeBasedSLperiodLoss2   = 600;
extern double  TimeBasedSLperiodLoss3   = 900;
extern double  TimeBasedSLperiodLoss4   = 1200;

void TimeBasedSL(string symbol,double TimeBasedSLperiodLoss1,double TimeBasedSLperiodLoss2,double TimeBasedSLperiodLoss3,double TimeBasedSLperiodLoss4) {
 bool result;
 double TimeBasedSL=0.0;

 string sTimeBasedSLperiod;
 int totalopenorders=0;
 for (int cnt=OrdersTotal()-1; cnt>=0; cnt--) {
  if (!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) continue;
  if (OrderSymbol()!=symbol) continue;
    double profit = (OrderProfit()+OrderCommission()+OrderSwap());
    //Order is in loss
     TimeBasedSL = TimeBasedSLperiodLoss1;//seconds
        double  minLot  = MarketInfo(Symbol(), MODE_MINLOT),
                 lotStep = MarketInfo(Symbol(), MODE_LOTSTEP),
                 Lots    = OrderLots();
    bool alreadyExecuted1 = false;
    bool alreadyExecuted2 = false;
    bool alreadyExecuted3 = false;
    bool alreadyExecuted4 = false;

// I DO NOT KNOW HOW TO CODE THE BELOW. PLEASE HELP! It keeps looping on TimeBasedSLperiodLoss1 (300 seconds) and does not go to TimeBasedSLperiodLoss2 (600 seconds).  
// How do I make sure for each symbol's trade  TimeBasedSLperiodLoss1 is only executed once.

    if (profit<=0.0) {

    if(!alreadyExecuted1) {
    if ((TimeCurrent() - OrderOpenTime()) >= TimeBasedSLperiodLoss1) {

     while(IsTradeContextBusy()) Sleep(100);
     if (OrderType()==OP_BUY) {//buy
      if (result) Print("TimeBasedSL(): **** BUY order ticket "+OrderTicket()+" (opentime: "+TimeToStr(OrderOpenTime())+") "+sTimeBasedSLperiod+" of "+DoubleToStr(profit,2)+" "+AccountCurrency());
     if (OrderType()==OP_SELL) {//sell
      if (result) Print("TimeBasedSL(): **** SELL order ticket "+OrderTicket()+" (opentime: "+TimeToStr(OrderOpenTime())+") "+sTimeBasedSLperiod+" of "+DoubleToStr(profit,2)+" "+AccountCurrency());
     alreadyExecuted1 = true;

Vasyl Nosal
Vasyl Nosal 2016.07.18 10:28  

and does not go to TimeBasedSLperiodLoss2

When it must go tp period2? 

bm0899 2016.07.18 15:11  
Can you please fix the above bug?
Ernst Van Der Merwe
Ernst Van Der Merwe 2016.07.18 17:33  

Make these variables static or declare globally.

    static bool alreadyExecuted1 = false;
    static bool alreadyExecuted2 = false;
    static bool alreadyExecuted3 = false;
    static bool alreadyExecuted4 = false;
Also, use integers when working with time.
extern int  TimeBasedSLperiodLoss1   = 300; // in seconds
extern int  TimeBasedSLperiodLoss2   = 600;
extern int  TimeBasedSLperiodLoss3   = 900;
extern int  TimeBasedSLperiodLoss4   = 1200;

int TimeBasedSL=0;
bm0899 2016.07.20 01:29  
Thanks, but the above did not fix the bug. :(
To add comments, please log in or register