Average order price and TP set problem

 

Hello

I am trying to build an EA which will add position if market goes against itself. TP is always 3 pips (30 pippettes) from average price. and new positions will execute if price goes 30 pips against last open order price. This will act in both buy and sell positions.

My EA opens positions as i expect but it fails to modify the set of buy (or sell) orders. Always sets more than 3 pips TP.

Can anyone help me out?


extern double    Pipstep=300.0;
extern double    takeprofit=30.0;
extern double    Slip=5;
extern int       buymagic=505050;
extern int       sellmagic=606060;
.
.
.
.
.
.
.
.
.
.
.
      //+------------------------------------+
      //| recalculate the average buy price  |
      //+------------------------------------+   
      cnt1=0; // new buy order is placed so release the number of buy orders to calculate again 
      for(i=0;i<OrdersTotal();i++)
         {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if((OrderType()==OP_BUY)&&(OrderMagicNumber()==buymagic))
            {
               BUY_L=OrderLots();
               BuyLotprice+=(BUY_L*OrderOpenPrice());
               buy_order_lots+=BUY_L;
               cnt1++;   //get the latest number of buy orders
            }
         
         }
         NewAverageBUYprice=BuyLotprice/buy_order_lots;
         tp=NormalizeDouble(NewAverageBUYprice+takeprofit*Point,Digits);
         //+------------------------------------+
         //| set new take profit levels         |
         //+------------------------------------+   
         for(i=0;i<OrdersTotal();i++)
            {
               OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
               if((OrderType()==OP_BUY)&&(OrderMagicNumber()==buymagic))
                  {
                     RefreshRates();
                     while(!IsTradeAllowed()) Sleep(100);
                     result=OrderModify(OrderTicket(),0,0,tp,0,Yellow);
                     if(result==false)Print("Buy Modify error is  ",GetLastError());      
                  }
            }  
 
if i declare BuyLotprice=0, and buy_order_lots=0 before the first for loop here, EA does not modify the orders at all.......... :(
 

The way you use OrderSelect is wrong. See OrderSelect example in the book here and here. The way you use OrderModify also wrong, You should not set other parameter to zero, if you set them to zero that mean you trying to modify them to zero as well. Set those parameter to value that OrderSelect found like what you did with OrderTicket().

Rewites the code and show again to us :)

This is my 300 comments

 

thanks onewithzachy............ i have tried to rewrite the code but problem still exists......... i appreciate your kind help

here is my partial Start function for buy orders. order opens nicely but the problem is to modify the open positions



extern double    Pipstep=300.0;
extern double    takeprofit=30.0;
extern double    Slip=5;
extern int       buymagic=505050;
extern int       sellmagic=606060;
double AverageBUYprice;
double AverageSELLprice;

.
.
.

int start()
  {
//----
   double buy_order_lots=0,sell_order_lots=0,BuyLotprice=0,SellLotprice=0,BUY_L=0,SELL_L=0,tp=0;
   double last_buy_price=0,last_sell_price=0;
   double NewAverageBUYprice, NewAverageSELLprice,price;
   int i,cnt1=0,cnt2=0,Ticket,buy,sell;
   int buytickets[],selltickets[],ticket;
   bool result;
   for(i=1;i<=OrdersTotal();i++)                                                        //+---------------FIND LAST ORDER PRICE-----+
   {                                                                                   //|
      if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES)==true){                                        //|-------------------------
      if((OrderType()==OP_BUY)&&(OrderMagicNumber()==buymagic))                          //|
         {                                                                             //|
            BUY_L=OrderLots();                                                             //|    
            buy_order_lots+=BUY_L;                                                             //|      
            BuyLotprice+=BUY_L*OrderOpenPrice();                                              //|          Last buy price
            AverageBUYprice=NormalizeDouble((BuyLotprice/buy_order_lots),Digits);
            last_buy_price=OrderOpenPrice();                                           //|
            cnt1++;                                                                    //|
         }                                                                             //|------------------------------
      if((OrderType()==OP_SELL)&&(OrderMagicNumber()==sellmagic))                    //|
         {                                                                             //|
            SELL_L=OrderLots();                                                             //|           Last sell price
            sell_order_lots+=SELL_L;                                                             //|
            SellLotprice+=SELL_L*OrderOpenPrice();                                              //|   
            AverageSELLprice=NormalizeDouble((SellLotprice/sell_order_lots),Digits);
            last_sell_price=OrderOpenPrice();                                          //|
            cnt2++;                                                                    //|
         }  }                                                                           //|
    }                                                                                  //+-------------------------
    if(cnt1<1){while(!IsTradeAllowed()) Sleep(100);Ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,Slip,0,Ask+takeprofit*Point,"BUY 0",buymagic,0,Green);}
    if(cnt2<1){while(!IsTradeAllowed()) Sleep(100);Ticket=OrderSend(Symbol(),OP_SELL,0.1,Bid,Slip,0,Bid-takeprofit*Point,"SELL 0",sellmagic,0,Red);}
//+----------- check threshold and place buy order and find new average buy 
    if(last_buy_price>(NormalizeDouble((Ask+Point*Pipstep),Digits)))
      {Print("total buy order is ",cnt1);
      switch(cnt1)
      {
      case 1: RefreshRates();while(!IsTradeAllowed()) Sleep(100);Ticket=OrderSend(Symbol(),OP_BUY,0.10,Ask,Slip,0,0,"BUY 1",buymagic,0,Green);
      if(Ticket<1)Print("Order send fail error #",GetLastError());break;
      case 2: RefreshRates();while(!IsTradeAllowed()) Sleep(100);Ticket=OrderSend(Symbol(),OP_BUY,0.20,Ask,Slip,0,0,"BUY 2",buymagic,0,Green);
      if(Ticket<1)Print("Order send fail error #",GetLastError());break;
      case 3: RefreshRates();while(!IsTradeAllowed()) Sleep(100);Ticket=OrderSend(Symbol(),OP_BUY,0.40,Ask,Slip,0,0,"BUY 3",buymagic,0,Green);
      if(Ticket<1)Print("Order send fail error #",GetLastError());break;
      case 4: RefreshRates();while(!IsTradeAllowed()) Sleep(100);Ticket=OrderSend(Symbol(),OP_BUY,1.00,Ask,Slip,0,0,"BUY 4",buymagic,0,Green);
      if(Ticket<1)Print("Order send fail error #",GetLastError());break;
      case 5: RefreshRates();while(!IsTradeAllowed()) Sleep(100);Ticket=OrderSend(Symbol(),OP_BUY,4.00,Ask,Slip,0,0,"BUY 5",buymagic,0,Green);
      if(Ticket<1)Print("Order send fail error #",GetLastError());break;
      case 6: RefreshRates();while(!IsTradeAllowed()) Sleep(100);Ticket=OrderSend(Symbol(),OP_BUY,10.00,Ask,Slip,0,0,"BUY 6",buymagic,0,Green);
      if(Ticket<1)Print("Order send fail error #",GetLastError());break;
      }
      //+------------------------------------+
      //| recalculate the average buy price  |
      //+------------------------------------+   
      cnt1=0; // new buy order is executed so release the number of buy orders to calculate again 
      for(i=1;i<=OrdersTotal();i++)
         {
         if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES)==true){
            if((OrderType()==OP_BUY)&&(OrderMagicNumber()==buymagic))
               {
               BUY_L=OrderLots();
               BuyLotprice+=(BUY_L*OrderOpenPrice());
               buy_order_lots+=BUY_L;
               cnt1++;   //get the latest number of buy orders
               }
            }
         }
         NewAverageBUYprice=BuyLotprice/buy_order_lots;
         tp=NormalizeDouble(NewAverageBUYprice+takeprofit*Point,Digits);
         //+------------------------------------+
         //| set new take profit levels         |
         //+------------------------------------+   
         for(i=1;i<=OrdersTotal();i++)
            {
               if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES)==true)
                 {
                  if((OrderType()==OP_BUY)&&(OrderMagicNumber()==buymagic))
                  {
                     RefreshRates();
                     while(!IsTradeAllowed()) Sleep(100);
                     result=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp,Slip);
                     if(result==false)Print("Buy Modify error is  ",GetLastError());      
                  }
                 }  
            }
      }






same logic for sell orders........
 

i make a mistake at OrderModify function, and rewrited again....... changed the Slip parameter to 0 ..... but again the problem exists


result=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp,Slip);




result=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp,0);
 
problem seems to be solved.......... i have used unnecessary expressions in the first for loop in the beginning of start function............
 for(i=1;i<=OrdersTotal();i++)                                                        //+---------------FIND LAST ORDER PRICE-----+
   {                                                                                   //|
      if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES)==true){                                        //|-------------------------
      if((OrderType()==OP_BUY)&&(OrderMagicNumber()==buymagic))                          //|
         {                                                                             //|
    //        BUY_L=OrderLots();                                                             //|    
    //        buy_order_lots+=BUY_L;                                                             //|      
    //        BuyLotprice+=BUY_L*OrderOpenPrice();                                              //|          Last buy price
    //        AverageBUYprice=NormalizeDouble((BuyLotprice/buy_order_lots),Digits);
            last_buy_price=OrderOpenPrice();                                           //|
            cnt1++;                                                                    //|
         }                                                                             //|------------------------------
      if((OrderType()==OP_SELL)&&(OrderMagicNumber()==sellmagic))                    //|
         {                                                                             //|
     //       SELL_L=OrderLots();                                                             //|           Last sell price
     //       sell_order_lots+=SELL_L;                                                             //|
     //       SellLotprice+=SELL_L*OrderOpenPrice();                                              //|   
      //      AverageSELLprice=NormalizeDouble((SellLotprice/sell_order_lots),Digits);
            last_sell_price=OrderOpenPrice();                                          //|
            cnt2++;                                                                    //|
         }  }                                                                           //|
    }        
 

Great, now it's correct and good looking :).

Uh ... well. What the error number and the value of tp ?. I think you should print the value of tp, so we can find out why ? :(

 

thanks onewithzachy......... for your kind help and inspirations....

right now i have spread 9.8 pips (or 98 pippettes) in my strategy tester as the market closed in super low liquidity......

EA works more or less well but got modify error 1

i am attaching the mq4 file. it had buy and sell order at the initialization (if no position found at the starting of the EA, it will open one long and one short position). I don't know whether placing order in the initialization function is right or wrong. plz give some advice.

it will be kind if you can test GBPUSD from April 18 to 20.

thanks

Files:
eaxmodifya2.mq4  12 kb
 
i am testing with Every tick model........... but spread shows always 9.8........ is there any way that i can test with the actual market condition, i mean with actual (variable) spread while the market was open?
 
MAK_9:
i am testing with Every tick model........... but spread shows always 9.8........ is there any way that i can test with the actual market condition, i mean with actual (variable) spread while the market was open?

Don't know the answer for that :(.

Run a test on EURUSD M1, most of the time, for buy order, tp was set below open price, that explain modify error. Strange thing is, some tp was set above open price and still got modify error. :(

Files:
 

i have used average price minimizing strategy, and used higher and higher lot size as the price goes against positions.

If there is more than 1 buy orders, some orders should be below TP and some should be above TP. and the set simultaneously give profit if TP is hit.

though the calculation is perfect, but i got error too. dont know why. :(