Download MetaTrader 5
To add comments, please log in or register
Deposit to your account via Visa QIWI Wallet
DeanDeV
330
DeanDeV 2015.06.23 16:43 

Hey guys,

I need some help please. I send a buy stop, when price hits the buy stop it opens a buy order. When this happens I would like my EA to send a corresponding sell stop when price enters the buy stop. Now; this for loop works most of the time, however, there are a few occasions it does not. Any reason why it would not sometimes?

for(int u=OrdersTotal()-1; u>=0; u--) 
    {
     if(!OrderSelect(u,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()==Symbol())
       if(OrderMagicNumber()==MagicNumber1) 
        {
         if(TimeCurrent()-OrderOpenTime()==0)     //When price enters buy stop
          if(OrderType()==OP_BUY)
           {
            if(Bars!=ThisBarTrade)
             {
              ThisBarTrade=Bars;
            
              int SellStopOrder=OrderSend(Symbol(),OP_SELLSTOP,LotSize,Price,3,StopLoss,TakeProfit,"sell stop order",MagicNumber2,0,clrRed);
               if(SellStopOrder<0) Print("Failed to place sell stop order, error#",GetLastError());
             }
           } 
        }

 Also note that it does not return any error values so I don't think it even makes it to the OrderSend().

whroeder1
13641
whroeder1 2015.06.23 19:49  
  1.         if(TimeCurrent()-OrderOpenTime()==0)     //When price enters buy stop
    No need for this test. OrderType==OP_BUY tells you it has open. The test fails because the order opens, time passes, next tick occurs, you get called - so TimeCurrent() can be different than OrderOpenTime(). They will be equal only if you receive multiple ticks per second (broker and network dependent.)

  2. if(Bars!=ThisBarTrade)
    Bars is unreliable (max bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
DeanDeV
330
DeanDeV 2015.06.24 00:43  
WHRoeder:
  1. No need for this test. OrderType==OP_BUY tells you it has open. The test fails because the order opens, time passes, next tick occurs, you get called - so TimeCurrent() can be different than OrderOpenTime(). They will be equal only if you receive multiple ticks per second (broker and network dependent.)

Hey WHRoeder,

Always appreciate your responses. I used this: 

if(TimeCurrent()-OrderOpenTime()==0) 

 Such that it only trades once the stop order opens, if I remove it, it trades every new candle. Is there any other filter I can use...? 

Could I not use something like this:

if(TimeCurrent()-OrderOpenTime()<=60) 
whroeder1
13641
whroeder1 2015.06.24 00:57  
DeanDeV: I used this: Such that it only trades once the stop order opens, if I remove it, it trades every new candle.

That's not what it does. It tests if the order was opened the same second: TimeCurrent() == OrderOpenTime()

The

if(Bars!=ThisBarTrade){
   ThisBarTrade=Bars;
should prevent a second open.
DeanDeV
330
DeanDeV 2015.06.24 01:17  
WHRoeder:

That's not what it does. It tests if the order was opened the same second: TimeCurrent() == OrderOpenTime()

The

should prevent a second open.

Isn't the ThisBarTrade equivalent to isNewBar as a filter?

It is still sending every candle after entering..

for(int u=OrdersTotal()-1; u>=0; u--) 
    {
     if(!OrderSelect(u,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()==Symbol())
       if(OrderMagicNumber()==MagicNumber1) 
        {
         if(OrderType()==OP_BUY)
           {
            if(isNewBar)
             {
              if(Bars!=ThisBarTrade)
               {
                ThisBarTrade=Bars;
            
              int SellStopOrder=OrderSend(Symbol(),OP_SELLSTOP,LotSize,Price,3,StopLoss,TakeProfit,"sell stop order",MagicNumber2,0,clrRed);
               if(SellStopOrder<0) Print("Failed to place sell stop order, error#",GetLastError());
               }
             }
           } 
        }

 

 I also need it to send the sell stop on the same candle the buy stop gets hit so isNewBar does not help? 

/
To add comments, please log in or register