Download MetaTrader 5

EA Compilation Problem

To add comments, please log in or register
xanderhinds
108
xanderhinds  

Hi, any help will be dearly appreciated.

 

So I had tested a few strategies out which ran on a single Symbol, and analysed data only from a single Period.

It was coded so that positions can only be open when there is no open positions of that type already open.


while (OMNS5 < T)
{

 when a position was successfully initiated OMNS5 would increase and no longer be less than T which is always '1'

        int Order_Magic1 = 5;
        if (OrderSend( Symbol(), OP_SELL, LotSize, OpenPriceS, 13,(Bid+(500*Point())),(Bid-(9999*Point())), "XSFEA",
        Order_Magic1, 0, Red) < 0)
        
              {
               _GetLastError = GetLastError();
               Alert( "Error OrderSend S1# ", _GetLastError );
               return(-1);
              }

        else
        {
        OMNS5++;
        break;
        }

 Then, on the other end I reduced OMNS5 whenever a position was closed 

 

  
                if ( !OrderClose( OrderTicket(), OrderLots(), Ask, 5, Red ) )
                 {
                    _GetLastError = GetLastError();
                    Alert( "Error OrderClose № ", _GetLastError );
                    return(-1);
                 }
                else
                {
                  OMNS5--;
                  continue;
                    
                }
            }

 but not when I brought the stop loss up to a few pips above (or in this case below) breakeven.

           else if (((OrderOpenPrice() - 250*Point()) > Ask ))
              
                 {
                   if (OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()-5*Point()),OrderTakeProfit(),0,Blue)== true)
                    {
                  
                     continue;
                    
                    }
                   else
                    {
                     Alert ("Problem whilst moving SL to BE");
                     return(0);
                    }
                 }  

So to combat the problem of OMNS5 not decreasing in value when the Stop loss was moved to break even, and as one position was the maximum ever open at any time I could get around this problem through resetting OMNS5 to 0 whenever no positions were open like this;

if (OrdersTotal() == 0)
  {
     OMNB5 = 0;
     OMNS5 = 0;
  }
  

 Now I've reached a stage where I wish to compile together strategies unique to each timeframe into a single EA. The problem I am facing is that OMNS5 is no longer able to be reset to 0 as effectively because there is often another position open that was initiated by another timeframe strategy.

Whilst a position is open the reset function is ineffective at returning the OMNS5 to 0.

Now, to solve this solution I thought I could give OMNS5 a unique magicnumber.. but how then do i search through open orders to check none with this unique MN exists, and in that case then return the correlative OMNS5 to 0?

Equally, if I wanted to trade multiple strategies on the same Period, would it still be able to identify that no ticket number that correlates to a particular strategy is open (whilst others may be) and return a value of 0 its OMNS/B so that it would look for the conditions to open a position?

Anyway i try is just an intellectual abomination.. 

All help will be appreciated! 

xanderhinds
108
xanderhinds  

This is the abomination.. I just can't see how to condition it to realise the absence of a MagicNumber..

for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--)
    {
      if(!OrderSelect(iPos, SELECT_BY_POS))continue;
       if(OrderSymbol()!=Symbol())continue;
        if(OrderMagicNumber()!= 511)
         if(OrderMagicNumber()!= 512)
          if(OrderMagicNumber()!= 521)
           if(OrderMagicNumber()!= 522)
         return(OMNS511 = 0);
     }

 The problem remains that OMNS511 will only be reset when no other position is open..

Keith Watford
Moderator
10533
Keith Watford  

Don't hard code the magic number, have it as an input and input a different number for each EA instance that is running.

Check if there is an order with the magic number not for orders without.

Use descriptive variable names, especially when you want others to comment on your code.

xanderhinds
108
xanderhinds  
Keith Watford:

Don't hard code the magic number, have it as an input and input a different number for each EA instance that is running.

Check if there is an order with the magic number not for orders without.

Use descriptive variable names, especially when you want others to comment on your code.

Ok, I won't hard code the final magic numbers, please excuse the non-descriptive variable names. 

I'll try to check for the magic number, as I understand the folly in trying to identify an absent number.. 

Yet because other positions will be open I'm struggling to write the code to look for the magic number and achieve the desired outcome..

The problem is that I can't check if there is an order with the magic number and then reset if not.. I can't imagine how to put that down in code effectively.

Is there a way?

The best I come up with is something like this;

for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--)
    {
     if (!OrderSelect(iPos, SELECT_BY_POS))continue;
      if(OrderSymbol()!=Symbol())continue;
        if(OrderMagicNumber()!= 511||512||521||522)
          {
          OMN511 = 0;
          OMN512 = 0;
          OMN521 = 0;
          OMN522 = 0;          
          }
          

but that's almost as ineffective as the previous if(OrdersTotal == 0) function..

honest_knave
Moderator
2342
honest_knave  

This probably isn't doing what you think it is:

if(OrderMagicNumber()!= 511||512||521||522)

You have written the same as:

if(OrderMagicNumber() != 1)
xanderhinds
108
xanderhinds  
honest_knave:

This probably isn't doing what you think it is:

if(OrderMagicNumber()!= 511||512||521||522)

You have written the same as:

if(OrderMagicNumber() != 1)
Yeh I'm really struggling.. 
xanderhinds
108
xanderhinds  

I'm beginning to think it's not possible as every time it grabs a number with magic number 1 it then resets 2,3 and 4..


for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--)
    {
     if (!OrderSelect(iPos, SELECT_BY_POS))continue;
      if(OrderSymbol()!=Symbol())continue;
        if(OrderMagicNumber()== 511||512||521||522)
         {
         if((OrderMagicNumber()!= 511) && (OrderMagicNumber()== 512||521||522))
          {
          OMN511 = 0;        
          }
         if((OrderMagicNumber()!= 512) && (OrderMagicNumber()== 511||521||522))
          {
          OMN512 = 0;        
          }
         if((OrderMagicNumber()!= 521) && (OrderMagicNumber()== 511||512||522))
          {
          OMN521 = 0;        
          }
         if((OrderMagicNumber()!= 522) && (OrderMagicNumber()== 512||521||511))
          {
          OMN522 = 0;        
          }
         }
        else
            {
             OMN511 = 0;
             OMN512 = 0;
             OMN521 = 0;
             OMN522 = 0;
            }
     }
    

 Every piece I write is ineffective.. just a rehash of the same nonsense. 

honest_knave
Moderator
2342
honest_knave  
   int magic1=511,
       magic2=512,
       magic3=521,
       magic4=522;
   bool found_magic1=false,
        found_magic2=false,
        found_magic3=false,
        found_magic4=false;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;
      int magic_number = OrderMagicNumber();
      if(magic_number == magic1)      found_magic1 = true;
      else if(magic_number == magic2) found_magic2 = true;
      else if(magic_number == magic3) found_magic3 = true;
      else if(magic_number == magic4) found_magic4 = true;
     }
   if(!found_magic1) OMN511 = 0; // if no orders with magic1 found, set OMN511 to 0
   if(!found_magic2) OMN512 = 0;
   if(!found_magic3) OMN521 = 0;
   if(!found_magic4) OMN522 = 0;
xanderhinds
108
xanderhinds  
honest_knave:
   int magic1=511,
       magic2=512,
       magic3=521,
       magic4=522;
   bool found_magic1=false,
        found_magic2=false,
        found_magic3=false,
        found_magic4=false;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;
      int magic_number = OrderMagicNumber();
      if(magic_number == magic1)      found_magic1 = true;
      else if(magic_number == magic2) found_magic2 = true;
      else if(magic_number == magic3) found_magic3 = true;
      else if(magic_number == magic4) found_magic4 = true;
     }
   if(!found_magic1) OMN511 = 0; // if no orders with magic1 found, set OMN511 to 0
   if(!found_magic2) OMN512 = 0;
   if(!found_magic3) OMN521 = 0;
   if(!found_magic4) OMN522 = 0;
Of course! Honest Knave thank you!!!
honest_knave
Moderator
2342
honest_knave  
xanderhinds:
Of course! Honest Knave thank you!!!

Glad you got it sorted.

Just to reiterate my early post, don't do this:

OrderMagicNumber()== 512||521||522

It is not the same as:

OrderMagicNumber()== 512 || OrderMagicNumber()==521 || OrderMagicNumber()==522
Marco vd Heijden
Moderator
6147
Marco vd Heijden  

Can also use a switch

switch (OrderMagicNumber())
{
  case 512:

  break;

  case 521:

  break;

  case 522:

  break;

  //etc.
}
To add comments, please log in or register