Problem with total amount of open orders - page 4

 

Hi Everyone

 

Sorry, but I spoke too soon.  While adding RefreshRates() seem to make a difference, I am still having the same issue.  It sometimes open both pending orders, sometimes only one of the two, and sometimes neither.  I am still getting error 130 when it doesn't open an order, or both, but no errors when both actually get opened.  I also noticed that on those pairs where my inputs are below MODE_STOPLEVEL, it never opens an order and I always have error 130, even though the program adjusts my inputs as requested.  I am printing the values and they are adjusted as expected.  So I'm trying to figure out why my OrderSend doesn't realy work.

On a pair such as EURUSD where the stoplevel is 5, it usually send both orders, but not always.  However on a pair such as EURAUD where the stoplevel is 10, it never sends an order

extern int TrailingStart=20;
extern int TrailingStop=5;

extern int Hedge=10;
extern double Multiplier=3;

extern int StopLossOriginal=11;

extern int StopLossHedge=9;

extern double Percentage=1;
extern double Lotsize=0.01;
extern double MyMaxlots=30;

extern datetime StartTime1 = D'2016.03.25 16:50';

extern int Pipmove=5;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
//---
  {
   int TS=TrailingStart-TrailingStop;
   double stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10;
   if(StopLossOriginal<stoplevel || StopLossHedge<stoplevel || TS<stoplevel)
     {
      MessageBox("Please note: Your inputs for StopLossOriginal, StopLossHedge and/or"+
                 "\nTrailingStop are below the minimum levels required by your broker,"+
                 "\nand have been increased automatically to "+StringConcatenate(stoplevel)+" Pips");
     }
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int TS=TrailingStart-TrailingStop;
   Print("TS = ",TS);
   int sloss=StopLossOriginal-Pipmove;
   int stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10;
   Print("stoplevel = ",stoplevel);
     
   double Lots = NormalizeDouble(AccountEquity()*Percentage*Lotsize/100, 2),
          point=Point*10,
          Price=Pipmove*point,
          SL=(StopLossOriginal-Pipmove)*point,
          MinLots = MarketInfo(Symbol(),MODE_MINLOT),
          MaxLots = MarketInfo(Symbol(),MODE_MAXLOT),
          HedgeLots=NormalizeDouble(OrderLots()*Multiplier,2);
          
   if(sloss<=stoplevel) SL=stoplevel*point;
   Print("SL = ",SL);
   if(StopLossHedge<=stoplevel) StopLossHedge=stoplevel;
   Print("StopLossHedge = ",StopLossHedge);
   if(TS<=stoplevel) TrailingStart=(stoplevel+TrailingStop); 
   Print("TrailingStart = ",TrailingStart);     
          
   datetime time1=StartTime1-3300;      

   if(Lots>MaxLots) Lots=MaxLots;
   if(Lots<MinLots) Lots=MinLots;
   if(HedgeLots>MaxLots) HedgeLots=MaxLots;
   if(Lots>MaxLots || Lots<MinLots || HedgeLots>MaxLots)
     {
      MessageBox("Lotsize have been adjusted automatically");
     }

   int buy_ticket=0, sell_ticket=0, buystop_ticket=0, sellstop_ticket=0, total=0;
   for(int i= OrdersTotal()-1; i>= 0; i--)
      if(OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber()==magic && OrderSymbol()==Symbol())
        {
         total++;
         if(OrderType()==OP_BUYSTOP) buystop_ticket=OrderTicket();
         if(OrderType()==OP_SELLSTOP) sellstop_ticket=OrderTicket();
         if(OrderType()==OP_BUY) buy_ticket=OrderTicket();
         if(OrderType()==OP_SELL) sell_ticket=OrderTicket();
        }

   if(total==0 && Time[0]==time1)
     {
      buy_ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Price,30,0,0,"Pending",magic,0,Lime);
      OrderModify(OrderTicket(),OrderOpenPrice(),Ask-SL,OrderTakeProfit(),Yellow);
      Print("Buystop = ",GetLastError());
      Sleep(1000);
      RefreshRates();
      Sleep(1000);
      sell_ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-Price,30,0,0,"Pending",magic,0,Red);
      OrderModify(OrderTicket(),OrderOpenPrice(),Bid+SL,OrderTakeProfit(),Yellow);
      Print("Sellstop = ",GetLastError());
     }

 I have also tried it like this, but it makes no difference:

buy_ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Price,30,Ask-SL,0,"Pending",magic,0,Lime);
      RefreshRates();
sell_ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-Price,30,Bid+SL,0,"Pending",magic,0,Red);

 And even if I do this it makes no difference:

if(total==0) // && (Time[0]==time1)
 

Thanks for all the help Everyone.  I finally got it to work.  The only way I could get it to work consistently was by changing it to this:

 if(total==0)
     {
      buystop_ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Price,30,Ask-SL,0,"Pending",magic,0,Lime);
      Print("buystop = ",GetLastError());
      RefreshRates();
     }
   if(total==1)
     {
      sellstop_ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-Price,30,Bid+SL,0,"Pending",magic,0,Red);
      Print("sellstop = ",GetLastError());
     }

I also figured out that the Pipmove level before the pending order gets activated must also be higher than stoplevel.  So everything seems to work now..  Thanks

 
Trader3000:

Thanks for all the help Everyone.  I finally got it to work.  The only way I could get it to work consistently was by changing it to this:

I also figured out that the Pipmove level before the pending order gets activated must also be higher than stoplevel.  So everything seems to work now..  Thanks

Nope this doesn't work either because it will now continue to open a sell_stop as long as there is one trade open
 
The simple answer is not to try to open pending orders so close to the current price. 5 Points is usually half a pip
 
GumRai:
The simple answer is not to try to open pending orders so close to the current price. 5 Points is usually half a pip

Thank you for the reply.  My calculation is actually in pips, so the pending orders are at least 50 points (5 pips away from the current price), however, it does seem to work if I move it at least 1 pip further away from the stoplevel, which is 50 points on the EURUSD.  It apears as if it now opens both trades except for the first one after I drag it onto the chart.  But I'm okay with this for now.  My code now looks like this:

extern int TrailingStart=20;
extern int TrailingStop=5;
extern int Hedge=10;
extern double Multiplier=3;
extern int StopLossOriginal=11;
extern int StopLossHedge=9;
extern double Percentage=1;
extern double Lotsize=0.01;
extern double MyMaxlots=30;
extern datetime StartTime1 = D'2016.03.25 14:50';
extern int Pipmove=5;
int i,TS=TrailingStart-TrailingStop,stoplevel=(MarketInfo(Symbol(),MODE_STOPLEVEL))/10; //stoplevel has been converted from points to pips (/10)

int start()
  {
   double Lots = NormalizeDouble(AccountEquity()*Percentage*Lotsize/100, 2),
          point=Point*10,
          Price=Pipmove*point,
          SL=StopLossOriginal*point,
          MinLots = MarketInfo(Symbol(),MODE_MINLOT),
          MaxLots = MarketInfo(Symbol(),MODE_MAXLOT),
          HedgeLots=NormalizeDouble(OrderLots()*Multiplier,2);

   if(StopLossHedge<stoplevel) StopLossHedge=stoplevel;
   if(TS<stoplevel) TrailingStart=(stoplevel+TrailingStop);
   if(Pipmove<stoplevel+1) Pipmove=stoplevel+1;
   if(StopLossOriginal<=StopLossHedge) StopLossOriginal=StopLossHedge+1;

   datetime time1=StartTime1-300;

   int buy_ticket=0,sell_ticket=0,buystop_ticket=0,sellstop_ticket=0,total=0;
   for(i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber()==magic && OrderSymbol()==Symbol())
        {
         total++;
         if(OrderType()==OP_BUYSTOP) buystop_ticket=OrderTicket();
         if(OrderType()==OP_SELLSTOP) sellstop_ticket=OrderTicket();
         if(OrderType()==OP_BUY) buy_ticket=OrderTicket();
         if(OrderType()==OP_SELL) sell_ticket=OrderTicket();
        }

   if(total<1 && Time[0]==time1)
     {
      buystop_ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Price,30,Ask-SL,0,"Pending",magic,0,Lime);
      RefreshRates();
      sellstop_ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-Price,30,Bid+SL,0,"Pending",magic,0,Red);
     }

I ran into a separate but similar problem.  Once one of the pending orders get triggered, one of two things could happen.  It either triggers the TrailingStop at which point the other pending order gets deleted.  or if that trade goes against me it should open a hedge in the opposite direction.  Depending on how I write the code, it will either open more than one hedge or no hedge at all.  I've tried everething including the following two:

if(OrderSelect(buy_ticket,SELECT_BY_TICKET) && OrderType()==OP_BUY)
     {
      if(Bid-OrderOpenPrice()>TrailingStart*point)
        {
         if(OrderStopLoss()<Bid-TrailingStop*point)
           {
            if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*point,OrderTakeProfit(),Blue))
              {
               if(OrderSelect(sellstop_ticket,SELECT_BY_TICKET) && OrderType()==OP_SELLSTOP)
                 {
                  if(OrderDelete(sellstop_ticket,Orange))
                     return(0);
                 }
              }
           }
        }
      if(OrderOpenPrice()>Bid+Hedge*point && buy_ticket==1 && sell_ticket<=1)
            {
              sell_ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLossHedge*point,0,"Hedge",magic,0,Red);
            }  
        }
//Same for Sell

  Or:


//Same up to here:
else if(total<=2 && OrderOpenPrice()>Bid+Hedge*point)
        {
         sell_ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLossHedge*point,0,"Hedge",magic,0,Red);
        }

 Should I use a separate for loop for this?  Thank you

 

Hi Everyone

 

It's been weeks of trying and I still have not made any progress.  Everything works now, except that under certain conditions the EA opens more than one hedge trade on the original trade.  The SL on the original trade is 11 pips and the SL on the Hedge trade is 9 pips.  Sometimes the hedge trade get stopped out at 9 pips while the original trade is still open.  It will then open a second hedge trade and even a 3rd and 4th while the original trade is still open.  I simply want to limit the amount of hedge trades to one and if it gets stopped out, just wait and see what happens with the original trade.

This is the type of results I get:

 576 2015.01.15 11:39 buy stop 29 0.48 1.16786 1.16616 0.00000 0.00 4834.24

577 2015.01.15 11:39 sell stop 30 0.48 1.16642 1.16812 0.00000 0.00 4834.24

578 2015.01.15 11:39 sell         30 0.48 1.16642 1.16812 0.00000 0.00 4834.24

579 2015.01.15 11:39 delete 29 0.48 1.16786 1.16616 0.00000 0.00 4834.24

580 2015.01.15 11:42 buy         31 1.44 1.16743 1.16653 0.00000 0.00 4834.24

581 2015.01.15 11:42 s/l         31 1.44 1.16653 1.16653 0.00000 -129.60 4704.64

582 2015.01.15 11:44 buy         32 1.44 1.16742 1.16652 0.00000 0.00 4704.64

583 2015.01.15 11:44 s/l         30 0.48 1.16812 1.16812 0.00000 -81.60 4623.04

584 2015.01.15 11:48 modify 32 1.44 1.16742 1.16893 0.00000 0.00 4623.04

 

The buystop and sellstop orders (29 and 30) are opened as they should.  The price then drops and the sell order (30) gets filled while the buystop (29) gets deleted.  The price then goes up again and the hedge(martingale) order (31) gets triggered (3*lotsize).  The price then drops again and the hedge (31) gets stopped out, but because 30 is still open it triggers another hedge (32), etc.  How can I prevent order 32 from getting triggered?  Thank you


 

Hi Everyone.  Its been more than a month now that I've tried to resolve this and I'm beginning to think that it is programatically impossible to code.  So if someone will please confirm this, I can put it to rest and move on.  Is it not possible to set a deep level for the number of hedge(martingale) orders as explained in the post above?  Thank you.

The best I have so far is:

int start()
{
   int buy_ticket=0;
   int sell_ticket=0;
   int total=0;
   for(int i= OrdersTotal()-1; i>= 0; i--)
      if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==Symbol())
     {
         total++;
         if(OrderType()==OP_BUY) buy_ticket=OrderTicket();
         if(OrderType()==OP_SELL) sell_ticket=OrderTicket();
        }
   /*if(total==1 && OrderSelect(buy_ticket,SELECT_BY_TICKET) && OrderType()==OP_BUY) <------- this blocked out code is irrelevant, but I want to put it here for completeness
     {
      if(Bid-OrderOpenPrice()>TrailingStart*point)
        {
         if(OrderStopLoss()<Bid-TrailingStop*point)
           {
            if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop *point,OrderTakeProfit(),Blue))
               return(0);
           }
        }*/
      else if(OrderOpenPrice()>Bid+Hedge*point)
        {
         sell_ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLossHedge*point,0,"Hedge",magic,0,Blue);
         return(0);
        }
     }
  }
 

There are different ways that this can be achieved.

When the hedge is opened, create a Global Variable of the client terminal.

Give it a name that includes the ticket number of the main trade.

Give it a value that acts as a flag that a hedge trade has been opened for that ticket number, or a hedge count if necessary.

Check the GV before opening a hedge.


The hedge and main trade have different lot sizes.

Before opening a hedge, check open orders and history to see if an opposite order has been opened with the relevant lot size with an OrderOpenTime() later than than the main trade's open  time.

 
GumRai:

There are different ways that this can be achieved.

When the hedge is opened, create a Global Variable of the client terminal.

Give it a name that includes the ticket number of the main trade.

Give it a value that acts as a flag that a hedge trade has been opened for that ticket number, or a hedge count if necessary.

Check the GV before opening a hedge.


The hedge and main trade have different lot sizes.

Before opening a hedge, check open orders and history to see if an opposite order has been opened with the relevant lot size with an OrderOpenTime() later than than the main trade's open  time.

Thank you very much I will look into these options and let you know.
 

So I tried to to achieve this through a Global Variable, but since adding this code, it is not opening a hedge trade at all.  I think that the issue is that the EA is doing a GlobalVariableCheck, but since none has been created yet, it won't continue.  It does, however, select and Print the correct ticket no.  Perhaps I'm doing it wrong.  Here is the relevant code:

//+------------------------------------------------------------------+
//|  Hedge                                                           |
//+------------------------------------------------------------------+
void Hedgetrade(){
int buy_hedge=0,sell_hedge=0,total=0;
double Pip=Point*10,HedgeLots=NormalizeDouble(OrderLots()*Multiplier,2),SLHedge=StopLossHedge*Pip;
   for(int i=OrdersTotal()-1; i>=0; i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&& OrderSymbol()==Symbol()){
      total++;
      int ticket=OrderTicket();
      Print("ticket = ",ticket);
     }          
         if(OrderType()==OP_BUY){
            if(buy_hedge==0 && sell_hedge==0 && OrderOpenPrice()>Bid+Hedge*Pip)
               GlobalVariableCheck(ticket);
               sell_hedge=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+SLHedge,0,"Hedge",0,0,Blue);
                  GlobalVariableSet(ticket,1);
                 }
         if(OrderType()==OP_SELL){
            if(sell_hedge==0 && buy_hedge==0 && OrderOpenPrice()<Ask-Hedge*Pip)
               GlobalVariableCheck(ticket);
               buy_hedge=OrderSend(Symbol(),OP_BUY,HedgeLots,Ask,3,Ask-SLHedge,0,"Hedge",0,0,Red);
                  GlobalVariableSet(ticket,1);
              }
            }
          }
Reason: