why doesnt this simple loop work?

To add comments, please log in or register
marth tanaka
207
marth tanaka  
 for( int n=0; n<total; n++ ){
      if( !OrderSelect(n,SELECT_BY_POS,MODE_TRADES) )
      if(OrderMagicNumber()!=333 && number_of_buys == 7) OrderSend(Symbol(),OP_SELL,50,Bid,3,0,0,"hedged",333,0,clrRed);
      if(OrderMagicNumber()!=334 && number_of_sells == 7) OrderSend(Symbol(),OP_BUY,50,Ask,3,0,0,"hedged",334,0,clrGreen);
      
     }

What should be happening is that the loop scans for trades by magic number and if there are no current trades with the magic number 333 and there are 7 buys, then it places one 50 lot sell trade. The vice versa is true for sell.


However what does happen is that it places infinite 50 lot trades until it crashes. It should only place a 50 lot trade if the conditions are true.


Any idea guys?

Keith Watford
Moderator
16064
Keith Watford  

Your loop doesn't check that there are no current trades with the magic number, it checks if the selected order does not have the magic number.

Where are the values for number_of_buys and  number_of_sells calculated?

lippmaje
393
lippmaje  
marth tanaka:

...
Any idea guys?

My personal impression is that this loop is - most of the time it is called - about to head into something very, very evil...
marth tanaka
207
marth tanaka  
Keith Watford:

Your loop doesn't check that there are no current trades with the magic number, it checks if the selected order does not have the magic number.

Where are the values for number_of_buys and  number_of_sells calculated?

Ohhhhhhhhh I see. So my loop only selects ONE order and sees if it has no magic number = to the one I want and thats why it places so many trades.

Well the fix then would be to make it go through all open orders, make sure THEY dont have the magic number 333 for example and then place a trade. How would I do this? I dont know how to write a loop that would make sure there isnt a magic number in all the open trades before performing an action.

int _get_number_of_trades(int ttype){ //function that returns number of currently opene trades on current pair
   int tCount = 0;
   
   for(int cnt=0;cnt<OrdersTotal();cnt++){
      bool result = OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
      
      if(OrderSymbol() == Symbol() && OrderType() == ttype) tCount++;
   }
   
   return(tCount);
}  

   int number_of_buys = _get_number_of_trades(OP_BUY);
   int number_of_sells = _get_number_of_trades(OP_SELL);


Keith Watford
Moderator
16064
Keith Watford  
      
      if(OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES))
        if(OrderMagicNumber == MagicNumber && OrderSymbol() == Symbol() && OrderType() == ttype)
           tCount++;
marth tanaka
207
marth tanaka  
Keith Watford:
This is what I have now (Still doesnt work maybe you can see why)


int _get_number_of_trades(int ttype){ //function that returns number of currently opene trades on current pair
   int tCount = 0;
   
   for(int cnt=0;cnt<OrdersTotal();cnt++){
      bool result = OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
      
      if(OrderSymbol() == Symbol() && OrderType() == ttype && OrderMagicNumber()!=333||334) tCount++;
   }
   
   return(tCount);
}  

int _get_number_of_trades_hedge_buy(int ttype){ //function that returns number of currently opene trades on current pair
   int tCount = 0;
   
   for(int cnt=0;cnt<OrdersTotal();cnt++){
      bool result = OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
      
      if(OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES))
        if(OrderMagicNumber() == 333 && OrderSymbol() == Symbol() && OrderType() == ttype)
           tCount++;
   }
   
   return(tCount);
}

int _get_number_of_trades_hedge_sell(int ttype){ //function that returns number of currently opene trades on current pair
   int tCount = 0;
   
   for(int cnt=0;cnt<OrdersTotal();cnt++){
      bool result = OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
      
      if(OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES))
        if(OrderMagicNumber() == 334 && OrderSymbol() == Symbol() && OrderType() == ttype)
           tCount++;
   }
   
   return(tCount);
}  
void hedger()
{
   int buy_trades_number = _get_number_of_trades(OP_BUY);
   int sell_trades_number = _get_number_of_trades(OP_SELL);
   int numberofhedgedbuys = _get_number_of_trades_hedge_buy(OP_BUY);
   int numberofhedgedsells = _get_number_of_trades_hedge_sell(OP_SELL);

            if (buy_trades_number == 7 && numberofhedgedbuys == 0)
               {OrderSend(Symbol(),OP_SELL,50,Bid,3,0,0,"hedged",333,0,clrRed);}
            if (sell_trades_number == 7 && numberofhedgedsells == 0)
               {OrderSend(Symbol(),OP_BUY,50,Ask,3,0,0,"hedged",334,0,clrGreen);}

     
}
hedger() runs in my onTick() function. Essentially it checks if there are 7 current buys or sells in and if there are 7 buys in, for example, it should place a 50 lot sell trade. Vice versa.

However it doesnt. It still places multiple and I cant see where Im going wrong.
Keith Watford
Moderator
16064
Keith Watford  
if(OrderSymbol() == Symbol() && OrderType() == ttype && OrderMagicNumber()!=333||334) tCount++;

Why are you using !=  ??

if(OrderSymbol() == Symbol() && OrderType() == ttype && (OrderMagicNumber()==333 || OrderMagicNumber()==334)) tCount++;

Is that what you intended?

marth tanaka
207
marth tanaka  
Keith Watford:

Why are you using !=  ??

Is that what you intended?

I am using != in the first function to make sure I dont pick up any trades from prexising hedges before I count all my "normal non 50 lot buy trades".


Basically, if I have 7 other trades (not the 50 lot ones), I want to place a 50 lot trade in the other direction
Keith Watford
Moderator
16064
Keith Watford  

Then you probably intended

if(OrderSymbol() == Symbol() && OrderType() == ttype && OrderMagicNumber()!=333 && OrderMagicNumber()!=334) tCount++;
To add comments, please log in or register