Open opposite order - page 2

 

You can't do this to adjust Slippage for 4/5 digit Brokers,  it just makes no sense.

Slippage MUST be an int,  so a whole number,  for a 4 digit Broker slippage is in Pips,  so Slippage = 3 means 3 Pips,  for a 5 Digit Broker slippage is in points,  so Slippage = 3 means 3 points or 0.3 Pips.  So to compensate, for a 5 digit Broker the slippage figure must be multiplied by ten giving 30 . . .  30 points = 3 Pips.

 

So this code is wrong . . .

int init()
   {
   if (Digits ==3 || Digits ==5) pt = 10 * Point;
   else pt = Point;
   Slippage = Slippage * pt;    
   }
 

Try this modified version of your OpenOppositeOrder()  function . . .

void OpenOppositeOrder() 
   {
   int Op;  
 
   for(int Counter = OrdersTotal()-1; Counter >= 0; Counter--)
      {
      if(OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES))  // only continue if the OrderSelect() worked
         {
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
            {
            Op = OrderType();

            if(Op == OP_BUY && Bid < OrderOpenPrice())
               {      
               Ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, Slippage, 0, 0, "Sell Order", MagicNumber, 0, Red);
               if(Ticket > 0) 
                  {
                  Print("Opposite Sell order placed # ", Ticket);
                  AddLimitsSell();
                  }
               else
                  {
                  Print("Order Send failed, error # ", GetLastError() );
                  }
               }
               
            if(Op == OP_SELL && Ask > OrderOpenPrice())
               {      
               Ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, 0, 0, "Buy Order", MagicNumber, 0, Green);
               if(Ticket > 0)
                  {
                  Print("Opposite Buy order placed # ", Ticket);
                  AddLimitsBuy();
                  }
               else
                  {  
                  Print("Order Send failed, error # ", GetLastError() );
                  }   
               }
            }
         }
      }
   }
 
RaptorUK:

Try this modified version of your OpenOppositeOrder()  function . . .

 

 


Hi RaptorUk,

 Before all thank you for help provided so far. Now The EA starts to work. I've done others modifications on how to close all if an order have profit and that is ok but as you already saw and predict for this strategy work is necessary that the order that reaches profit have the lot size increased to compensate for former orders. with that in mind I've introduced code to do that.  So I'm dealing with a confusion here. I need to confirm if the last order had a profit or not and i want to use OrderProfit() in a way to adjust the next order lot size but, don't I need to close the former order to get that information ?

Here is the code to open an opposite order that's  working fine(thanks to you) where I introduce the extra code to find if the former order that is open have a profit. Am I in the right way to get that ?

Thank you

Luis 

 void OpenOppositeOrder() 
 {
// LastOrder Loss Compensation 
   int Orders;
   Orders= OrdersTotal();
   if (Orders>0)
   {  
    for(i=0;i<Orders;i++)
   {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    if(OrderSymbol()==Symbol()&& OrderMagicNumber()== MagicNumber)
   {
    lastprofit = OrderProfit();
    lastlot = OrderLots();
   }
  }
 }
  mlots=0;
  if (lastprofit<0)mlots=NormalizeDouble(lastlot*Multiplier,Digits);
  else mlots=LotSize;
 
//+------------------------------------------------------------------+ 
//Open Opposite Order 
   int Op;  
 
   for(int Counter = OrdersTotal()-1; Counter >= 0; Counter--)
      {
      if(OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES))  
         {
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
            {
            Op = OrderType();

            if(Op == OP_BUY && Bid < OrderOpenPrice())
               {  
               if(Martingale)ilots=mlots;else ilots=LotSize; 
               Ticket = OrderSend(Symbol(), OP_SELL, ilots, Bid, UseSlippage, 0, 0, "Sell Order", MagicNumber, 0, Red);
               if(Ticket > 0) 
                  {
                  Print("Opposite Sell order placed # ", Ticket);
                  AddLimitsSell();
                  }
               else
                  {
                  Print("Order Send failed, error # ", GetLastError() );
                  }
               }
               
            if(Op == OP_SELL && Ask > OrderOpenPrice())
               { 
               if(Martingale)ilots=mlots;else ilots=LotSize;
               Ticket = OrderSend(Symbol(), OP_BUY, ilots, Ask, UseSlippage, 0, 0, "Buy Order", MagicNumber, 0, Green);
               if(Ticket > 0)
                  {
                  Print("Opposite Buy order placed # ", Ticket);
                  AddLimitsBuy();
                  }
               else
                  {  
                  Print("Order Send failed, error # ", GetLastError() );
                  }   
               }
            }
         }
      }
   }
 
luisneves:


Hi RaptorUk,

 Before all thank you for help provided so far. Now The EA starts to work. I've done others modifications on how to close all if an order have profit and that is ok but as you already saw and predict for this strategy work is necessary that the order that reaches profit have the lot size increased to compensate for former orders. with that in mind I've introduced code to do that.  So I'm dealing with a confusion here. I need to confirm if the last order had a profit or not and i want to use OrderProfit() in a way to adjust the next order lot size but, don't I need to close the former order to get that information ?

Here is the code to open an opposite order that's  working fine(thanks to you) where I introduce the extra code to find if the former order that is open have a profit. Am I in the right way to get that 

I suspect that this is going to cause you problems . . .

mlots = NormalizeDouble(lastlot * Multiplier, Digits);

 for example,  if the result was 1.12316734 lots your NormalizeDouble() on a 5 digit par will change it to 1.12316  and this will not comply with the Lot Step requirement . . . .  you need to use some mathematics to make your modified lot size a multiple of Lot Step  MarketInfo(Symbol(), MODE_LOTSTEP)

 
RaptorUK:

I suspect that this is going to cause you problems . . .

 for example,  if the result was 1.12316734 lots your NormalizeDouble() on a 5 digit par will change it to 1.12316  and this will not comply with the Lot Step requirement . . . .  you need to use some mathematics to make your modified lot size a multiple of Lot Step  MarketInfo(Symbol(), MODE_LOTSTEP)

 


Hi RaptorUK,

Concerning your advise to look for maintain my lot size a multiple of Lot Step I'm looking to document myself about the subject.

now I've another issue (more one...)

To open the orders above and under the current price I want to use a line where once the Ea is start to running I compare the price with the the line that is still.

 A first problem come; how can I compare the line with a difference in the price ?

Here is the code that I have so far. Here how can I give the name to compare because if I put out iHigh in after priceline it just doesn't work.

 Thank you in advance fo any clues in these issues.

Luis 

priceline=iHigh(Symbol(),0,0);
 ObjectCreate("highline",OBJ_HLINE,0,0,Bid); 
 ObjectSet("highline",OBJPROP_COLOR,LimeGreen);
 ObjectSet("highline",OBJPROP_WIDTH,1);
 WindowRedraw();

 

 This just seems to not work at all (...)

if(Bid > priceline+Distance*pt)                      
                {       
// Open buy order

                            while(IsTradeContextBusy()) Sleep(10);
                                 RefreshRates();        
                       
             Ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,0,0,"Buy Order",MagicNumber,0,Green);
             if(Ticket>0)AddLimitsBuy();
            }

 

 I've put here the object delete, because I need that the line be deleted with the close of all the orders. am I correct ?

void CloseAll()
      {
         int i,Op,Error;
         int Total=OrdersTotal();
     
         if(Total>0)
         {
         for(int pos=OrdersTotal()-1;pos>=0;pos--)
         if(OrderSelect(pos,SELECT_BY_POS)
         && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
         {
         Op=OrderType();
         if(Op==OP_BUY)
         {
         OrderClose(OrderTicket(),OrderLots(),Bid, UseSlippage, Yellow);
         }
         if(Op==OP_SELL)
         {
         OrderClose(OrderTicket(),OrderLots(),Ask, UseSlippage, Yellow);
         }
         else
         {
         ObjectDelete("highline");
         }
        }
       }
      }
 
luisneves:

 

This just seems to not work at all (...)

 

 I've put here the object delete, because I need that the line be deleted with the close of all the orders. am I correct ?

OK,  I think I understand what you want to do, you want to check where Bid is compared to your line,  to do this you need to get the position of the line . . .  use ObjectGet()  with the object name and object property index  OBJPROP_TIME1  this will return the price that the line is placed at.

Your ObjectDelete() call looks OK to me.  I particularly dislike the use of   ObjectsDeleteAll()  I think it is lazy and dangerous . . .  or at the very least inconvenient for others.

 
RaptorUK:

OK,  I think I understand what you want to do, you want to check where Bid is compared to your line,  to do this you need to get the position of the line . . .  use ObjectGet()  with the object name and object property index  OBJPROP_TIME1  this will return the price that the line is placed at.

Your ObjectDelete() call looks OK to me.

 

 


Hi RaptorUK,

I'm trying to close all orders if one close in profit, but when that happens a pending order is still open nevertheless the instruction to close is included in the CloseAll() function. Any clues about this issue ?

Thank you in advance for any help you provide

Luis 

int GoToClose()
 {
  int pos;  
 {
  for(int Profit=OrdersTotal()-1;pos>=0;pos--)
  if(OrderSelect(Profit,SELECT_BY_POS)
  && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol()
  && OrderProfit()+OrderSwap()+OrderCommission()>0||OrdersTotal()> MaxOrders)CloseAll();  
 }
}
//+---------------------------------------------------------------------------+  
      void CloseAll()
      {
         int Op,Error;
                    
         for(int pos=OrdersTotal()-1;pos>=0;pos--)
         if(OrderSelect(pos,SELECT_BY_POS)
         && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
         {
         Op=OrderType();
         if(Op==OP_BUY)
         {
         RefreshRates();
         OrderClose(OrderTicket(),OrderLots(),Bid, UseSlippage, Yellow);
         }
         if(Op==OP_SELL)
         {
         RefreshRates();
         OrderClose(OrderTicket(),OrderLots(),Ask, UseSlippage, Yellow);
         }
         if(Op==OP_SELLSTOP||OP_BUYSTOP||OP_SELLLIMIT||OP_BUYLIMIT)
         {
         RefreshRates();
         OrderDelete(OrderTicket());
         }
         else Print("Error opening BUY Stop Order : ",GetLastError());         
         }
        }             
 
luisneves:


Hi RaptorUK,

I'm trying to close all orders if one close in profit, but when that happens a pending order is still open nevertheless the instruction to close is included in the CloseAll() function. Any clues about this issue ?

You need to understand how expressions work . . .  you can't do this . . .

if(Op == OP_SELLSTOP || OP_BUYSTOP || OP_SELLLIMIT || OP_BUYLIMIT)

 it's a similar situation to this one,  but not exactly the same:  https://www.mql5.com/en/forum/141790

 
What is the result of   . . .

WhatValue = (5  || 4  ||  3  ||  2 );

 ||  is a Bolean operator so only works on bools . . .  


this results in a bool resultant value . . .

( Op == OP_SELLSTOP )

 

 . . . if you wan to use a bitwise operator on your values then you can use this . . . .

int WhatValue = (5  | 4  |  3  |  2 );

  . . .  but it isn't what you are trying to do.

 
RaptorUK:

You need to understand how expressions work . . .  you can't do this . . .

 it's a similar situation to this one,  but not exactly the same:  https://www.mql5.com/en/forum/141790

 
What is the result of   . . .

 ||  is a Bolean operator so only works on bools . . .  


this results in a bool resultant value . . .

 

 . . . if you wan to use a bitwise operator on your values then you can use this . . . .

  . . .  but it isn't what you are trying to do.

 


Hi RaptorUK,

 

 I see, but now with that in mind  I've modify the code and the pending order still there...

 

void CloseAll()
      {
         int Op,Error;
                    
         for(int pos=OrdersTotal()-1;pos>=0;pos--)
         if(OrderSelect(pos,SELECT_BY_POS)
         && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
         {
         Op=OrderType();
         if(Op==OP_BUY)
         {
         RefreshRates();
         OrderClose(OrderTicket(),OrderLots(),Bid, UseSlippage, Yellow);
         }
         if(Op==OP_SELL)
         {
         RefreshRates();
         OrderClose(OrderTicket(),OrderLots(),Ask, UseSlippage, Yellow);
         }
         if(Op==OP_SELLSTOP)
         {
         RefreshRates();
         OrderDelete(OrderTicket());
         }
         if(Op==OP_BUYSTOP)
         {
         RefreshRates();
         OrderDelete(OrderTicket());
         }
         else Print("Error opening BUY Stop Order : ",GetLastError());         
         }
        }        
 
luisneves:


Hi RaptorUK,

 I see, but now with that in mind  I've modify the code and the pending order still there...

It can be if it has a different Magic Number than  MagicNumber  or it is for a symbol other than the symbol that the EA is running on or if it is a OP_SELLLIMIT or OP_BUYLIMIT . . .   also, you need to check the return values from your OrderDelete() and OrderClose()  calls . . .

Why do you need to call RefreshRates()  ?  I don't mean get rid of them,  but can you explain why you are calling RefreshRates() where you are ?


What are Function return values ? How do I use them ? 

Reason: