Download MetaTrader 5
To add comments, please log in or register
Deposit to your account via WebMoney
jtubbs13791
71
jtubbs13791 2014.11.24 16:52 

I am having a issue with some coding for an EA. I am wanting it to Not open another trade at the same positions. So if I have 5 open positions. The EA is not opening a trade at the same position as the previous trades, but the EA is opening trades at the same position as the others. I need a way to code the EA Not to open trades at the other open positions. This is how far I have gotten. Thanks for any help in advance


bool OpenOrderRangeBuyHigh ()
{
for(int b= OrdersTotal()-1; b >= 0; b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
     if(OrderMagicNumber() == MagicNumber)
        if(OrderSymbol() == Symbol())
          if(OrderType()==OP_BUY)
            if(OrderOpenPrice()< Ask-OpenOrderPipDiff*pips)
                 return(true);
            else return(false);
 
   }
  return(true);
 
}
bool OpenOrderRangeBuyLow ()
{
for(int b= OrdersTotal()-1; b >= 0; b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
     if(OrderMagicNumber() == MagicNumber)
        if(OrderSymbol() == Symbol())
          if(OrderType()==OP_BUY)
            if(OrderOpenPrice()> Ask+OpenOrderPipDiff*pips)
                 return(true);
            else return(false);
 
   }
  return(true);
 
}

Agent86
915
Agent86 2014.11.24 20:00  
Seems like the  last return(true); for each function is not needed you already have a return for these functions

Or maybe this:

bool OpenOrderRangeBuyHigh ()
{
for(int b= OrdersTotal()-1; b >= 0; b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
     if(OrderMagicNumber() == MagicNumber)
        if(OrderSymbol() == Symbol())
          if(OrderType()==OP_BUY)
            if(OrderOpenPrice()< Ask-OpenOrderPipDiff*pips)
               {  
               return(true);
               }
 
   }
  return(false);
}
 
Keith Watford
Moderator
7862
Keith Watford 2014.11.24 20:07  
Agent86:
Seems like the  last return(true); for each function is not needed you already have a return for these functions


Not so.

If there are no open orders or no orders that meet the criteria, the returns in the loop will not be executed 

Keith Watford
Moderator
7862
Keith Watford 2014.11.24 20:26  
jtubbs13791:

 So if I have 5 open positions. The EA is not opening a trade at the same position as the previous trades

Your loops return true or false based on the first order checked

I don't know for sure what you are doing with the result, but maybe use a continue instead of return?

bool OpenOrderRangeBuyHigh ()
{
for(int b= OrdersTotal()-1; b >= 0; b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
     if(OrderMagicNumber() == MagicNumber)
        if(OrderSymbol() == Symbol())
          if(OrderType()==OP_BUY)
            if(OrderOpenPrice()< Ask-OpenOrderPipDiff*pips)
                 continue;
            else return(false);
  
   }
  return(true);
Agent86
915
Agent86 2014.11.24 20:55  
GumRai:

Not so.

If there are no open orders or no orders that meet the criteria, the returns in the loop will not be executed 

Are you sure ?
My last return(false) is outside of the for loop, so if the for loop simply exits with no value then it gives control to the next statement in the functions which is return(false) then exits the function execution with it's bool value

Do I have this wrong ?
Keith Watford
Moderator
7862
Keith Watford 2014.11.24 21:18  
Agent86:
Are you sure ?
My last return(false) is outside of the for loop, so if the for loop simply exits with no value then it gives control to the next statement in the functions which is returns(false)

Do I have this wrong ?


You were referring to Jtubbs code,so it was that code that I was referring to.

I was replying to your unedited post that only said

Agent86:
Seems like the  last return(true); for each function is not needed you already have a return for these functions

 

 You had not added the code at that time

Agent86
915
Agent86 2014.11.24 22:37  
GumRai:

You were referring to Jtubbs code,so it was that code that I was referring to.

I was replying to your unedited post that only said

 You had not added the code at that time

 

AHHH got it thanks, scared me for a min. there thought I was way off for min. there



jtubbs13791
71
jtubbs13791 2014.11.24 23:44  
ok. I'll try both and let you know how it goes.
jtubbs13791
71
jtubbs13791 2014.11.24 23:57  

so using the "continue;" work really good. is there a way to combine the to 2 functions into one. Now that is working, only one works at a time. This is how I have it coded.


if(OpenOrderRangeBuyHigh())
               if(OpenOrderRangeBuyLow())OrderEntryBuy();

whroeder1
13590
whroeder1 2014.11.25 01:19  
            if(OrderOpenPrice()< Ask-OpenOrderPipDiff*pips)
                 return(true);
            else return(false);
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. If you find any orders that opened below your pip difference, you return true, i.e. open a new one. What you asked for is open only if you don't find any. Your test is reversed. Either GumRai version or this equivalent:
    bool OpenOrderRangeBuyHigh (){
       for(int iPos= OrdersTotal()-1; iPos >= 0; --iPos) if(
          OrderSelect(iPos, SELECT_BY_POS)
       && OrderMagicNumber() == MagicNumber
       && OrderSymbol()      == Symbol()
       && OrderType()        == OP_BUY
       ){
          if(OrderOpenPrice() > Ask-OpenOrderPipDiff*pips) return false;
       } // for
       return true;
    }

jtubbs13791
71
jtubbs13791 2014.11.30 22:46  
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. If you find any orders that opened below your pip difference, you return true, i.e. open a new one. What you asked for is open only if you don't find any. Your test is reversed. EitherGumRai version or this equivalent:
thanks. it works. I really appreciate it
/
To add comments, please log in or register