A newbie error with OrderModify

 

Hi Guys, i tried to correct the error, I did not succeed.

Iam wait for help...

CODE:

start error on the SyncProfit() 



//1 - OP_BUY
//2 - MY MAGIC
//3 - CURRECT SYMBOL
//4 - TARGET PROFIT
void SyncProfit(int ordertype,int magicnumber, string symbol, double lucro){  
int total = GetOrdersTotal(ordertype,magicnumber,symbol); //all order of type OP_BUY or OP_SELL
int index = 0;

        if(total<1){
                return(0);
        }else{   
                for (int i=0;i<OrdersTotal();i++) {
                        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
                                if (OrderSymbol() == symbol && OrderMagicNumber() == magicnumber && OrderType() == ordertype) {
                                
                                  RefreshRates();
                                        int    TK = OrderTicket();
                                        double TP = OrderTakeProfit();
                                        double SL = OrderStopLoss();
                                        double OP = OrderOpenPrice();
                                        double newtakeprofit = 0;
                                        double medianprice = GetMedianPrice(ordertype,magicnumber,symbol);//my median price
                                        color cor;
                                        bool modify = false; //flag to change tp

                                        switch(OrderType())     {
                                                case 0://buy
                                                        if(TP != GetTakeProfit(ordertype,medianprice, lucro)){ //bug
                                                                modify = true;    
                                                                newtakeprofit = GetTakeProfit(ordertype,medianprice, lucro);   //pass medianprice+profit (return this*Point)
                                                                cor = Blue;  
                                                        }   
                                                break;
                                                
                                                case 1://sell
                                                        if(TP != GetTakeProfit(ordertype,medianprice, lucro)){
                                                                modify = true;    
                                                                newtakeprofit = GetTakeProfit(ordertype,medianprice, lucro);    
                                                                cor = Red;    
                                                        }                                       
                                                break;
                                        }//exit switch
                              
                              if(modify && OrderTakeProfit() != newtakeprofit){ //bug
                                 OrderModify(TK,OP,SL,newtakeprofit,0,cor);
                                 Print("Sync(" + OrderTicket() + ") TP from:" + OrderTakeProfit() +  "    to:" + newtakeprofit); 
                                 break;  
                              }                                 
                                }
                        }
                }
        }       
}


double GetTakeProfit(int ordertype, double price, double stop){
double newtakeprofit = 0;

        if(stop == 0){
                return(0);
        }else{
                if(ordertype == OP_BUY){
                        newtakeprofit = price + stop * Point;
                }
                if(ordertype == OP_SELL){
                        newtakeprofit = price - stop * Point;
                }
        }
        return(newtakeprofit);
}


double GetMedianPrice(int ordertype, int magicnumber, string symbol){
   int total = GetOrdersTotal(ordertype,magicnumber,symbol);
   double medianprice = 0;
   double count = 0;
   int index = 0;
   
        if(total<1) {
                return (0);
        } else {
                for(index = total - 1; index >= 0 ; index--) {
                        if(OrderSelect(index,SELECT_BY_POS,MODE_TRADES)) {
                        if(OrderSymbol() == symbol && OrderMagicNumber() == magicnumber && OrderType() == ordertype) {
                                        medianprice += OrderOpenPrice() * OrderLots();
                                        count += OrderLots();
                                } else {
                                        continue;
                                }
                        }
                }
        }
        if(count>0){
                medianprice = NormalizeDouble(medianprice / count,Digits);
        }
        return (medianprice);
}


int GetOrdersTotal(int ordertype, int magicnumber, string symbol) {
        int count = 0;
        int index = 0;

        if(OrdersTotal()<1) {
                return (0);
        } else {
                for(index = OrdersTotal() - 1; index >= 0 ; index--) {
                        if(OrderSelect(index,SELECT_BY_POS,MODE_TRADES)) {
                        if(OrderSymbol() == symbol && OrderMagicNumber() == magicnumber && OrderType() == ordertype) {
                                        count++;
                                } else {
                                        continue;
                                }
                        }
                }
        }
        return (count);
}

 

 if(modify && OrderTakeProfit() != newtakeprofit){

   OrderModify(TK,OP,SL,newtakeprofit,0,cor);

   Print("Sync(" + OrderTicket() + ") TP from:" + OrderTakeProfit() +  "    to:" + newtakeprofit); 

   break;  

}

 

It is not logical to write the log.

The log:  

 

2013.06.05 21:30:05 2010.01.05 18:00  Buy 1.1.5 EURUSD,H1: Sync(5) TP from:1.45687000    to:1.45687000

2013.06.05 21:30:05 2010.01.05 18:00  Buy 1.1.5 EURUSD,H1: OrderModify error 1

2013.06.05 21:30:05 2010.01.05 17:00  Buy 1.1.5 EURUSD,H1: Sync(5) TP from:1.45687000    to:1.45687000

2013.06.05 21:30:05 2010.01.05 17:00  Buy 1.1.5 EURUSD,H1: OrderModify error 1

2013.06.05 21:30:05 2010.01.05 16:00  Buy 1.1.5 EURUSD,H1: Sync(5) TP from:1.45687000    to:1.45687000

2013.06.05 21:30:05 2010.01.05 16:00  Buy 1.1.5 EURUSD,H1: OrderModify error 1

2013.06.05 21:30:05 2010.01.05 15:00  Buy 1.1.5 EURUSD,H1: Sync(5) TP from:1.45764000    to:1.45687000

2013.06.05 21:30:05 2010.01.05 15:00  Buy 1.1.5 EURUSD,H1: modify #5 buy 0.10 EURUSD at 1.44337 sl: 0.00000 tp: 1.45687 ok

2013.06.05 21:30:05 2010.01.05 15:00  Buy 1.1.5 EURUSD,H1: open #7 buy 0.12 EURUSD at 1.44052 ok

2013.06.05 21:30:05 2010.01.05 14:00  Buy 1.1.5 EURUSD,H1: Sync(5) TP from:1.45764000    to:1.45764000

2013.06.05 21:30:05 2010.01.05 14:00  Buy 1.1.5 EURUSD,H1: OrderModify error 1

2013.06.05 21:30:05 2010.01.05 13:00  Buy 1.1.5 EURUSD,H1: Sync(5) TP from:1.45764000    to:1.45764000

2013.06.05 21:30:05 2010.01.05 13:00  Buy 1.1.5 EURUSD,H1: OrderModify error 1

2013.06.05 21:30:05 2010.01.05 12:00  Buy 1.1.5 EURUSD,H1: Sync(5) TP from:1.45764000    to:1.45764000

2013.06.05 21:30:05 2010.01.05 12:00  Buy 1.1.5 EURUSD,H1: OrderModify error 1

2013.06.05 21:30:05 2010.01.05 11:00  Buy 1.1.5 EURUSD,H1: Sync(5) TP from:1.45837000    to:1.45764000

2013.06.05 21:30:05 2010.01.05 11:00  Buy 1.1.5 EURUSD,H1: modify #5 buy 0.10 EURUSD at 1.44337 sl: 0.00000 tp: 1.45764 ok

Files:
bug.mq4  4 kb
 
  1. for (int i=0;i<OrdersTotal();i++) {
       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
          if (OrderSymbol() == symbol && OrderMagicNumber() == magicnumber && OrderType() == ordertype) {
             RefreshRates();
             int    TK = OrderTicket();
             double TP = OrderTakeProfit();
             double SL = OrderStopLoss();
             double OP = OrderOpenPrice();
             double newtakeprofit = 0;
             double medianprice = GetMedianPrice(ordertype,magicnumber,symbol);//my median price
             color cor;
             bool modify = false; //flag to change tp
    
             switch(OrderType()) 
    You are inside a orderSelect loop. But then GetMedianPrice does a orderSelect loop (two including GetOrdersTotal,) so switch( orderType() ) no longer refers to the same order as TK
  2.  if(modify && OrderTakeProfit() != newtakeprofit){ //bug
        OrderModify(TK,OP,SL,newtakeprofit,0,cor);
    OrderTakeProfit can refer to any order here, definitely not TK

  3. double GetMedianPrice(int ordertype, int magicnumber, string symbol){
       int total = GetOrdersTotal(ordertype,magicnumber,symbol);
       double medianprice = 0;
       double count = 0;
       int index = 0;
       
            if(total<1) {
                    return (0);
            } else {
                    for(index = total - 1; index >= 0 ; index--) {
                            if(OrderSelect(index,SELECT_BY_POS,MODE_TRADES)) {
    GetOrdersTotal returns the total count of orders matching the type, mn, symbol. Then you go through just that count, not all orders, so the result could be zero (the matching order(s) was beyond total.)
  4. medianprice += OrderOpenPrice() * OrderLots();
    count += OrderLots();
    You're not computing the median price, your computing a lots weighted average price.