Download MetaTrader 5
To add comments, please log in or register
Let your idle PCs do something useful. Join MQL5 Cloud Network!
brettosm8
241
brettosm8 2014.01.29 12:35 

Hi there. Thanking you for assistance.

I'm trying to create an EA that works as follows. After I open a trade with 3 positions all with a common SL and entry price, with a TP1, TP2 and without a TP for the 3rd position, I want my EA to move to BE when the price reaches TP1. When price reaches TP2 I want to move the SL to the TP1 level. From that point I want to manually adjust the SL level for the 3rd position. I tried to make an EA that will look at every open order and create an index for the ticket numbers and for the take profit levels. After TP1 is reached the first orders is closed and so the new SL level for the 2nd and 3rd position applies. At TP2 the 2nd position is closed and so the new level for the 3rd position applies and no further adjustments are required. So without any further adieu here is my code but nothing appears to happen at all.


Again, thank you kindly.


int TicketArray[3];
double TakeProfitArray[3];

double TP1=0.00;
double TP2=0.00;
double BE=0.00;

bool MovedToBE;

int init()
   {
   // find open orders
   for(int i=0;i<=OrdersTotal()-1;i++)
     {
     // obtain the ticket numbers and TP levels of open orders
     if(OrderSelect(i,SELECT_BY_POS))TicketArray[i]=OrderTicket();
     if(OrderSelect(i,SELECT_BY_POS))TakeProfitArray[i]=OrderTakeProfit();
     }
     
     TP1=TakeProfitArray[0];
     TP2=TakeProfitArray[1];
     if(OrderSelect(0,SELECT_BY_POS))BE=OrderOpenPrice();
     
   Comment("TP1: "+TP1+
      "\nTP2: "+TP2+
      "\nBE: "+BE);
         
   return(0);
   }

int start()
  {    
   AdjustSL();

   return(0);
  }

//+------------------------------------------------------------------+

void AdjustSL()
   {
   // find if long or short
   if(OrderSelect(0,SELECT_BY_POS))
      {
      if(OrderType()==OP_BUY)
         {
         // if tp1 reached move to BE
         if(Ask>=TP1&&MovedToBE==false)
            {
               OrderModify(TicketArray[1],OrderOpenPrice(),BE,OrderTakeProfit(),OrderExpiration());
               OrderModify(TicketArray[2],OrderOpenPrice(),BE,OrderTakeProfit(),OrderExpiration());
               MovedToBE=true;
            }//End of Ask>=TakeProfitArray[0]&&MovedToBE==false
         // if tp2 reached move to tp1
         if(Ask>=TP2)
            {
               OrderModify(TicketArray[2],OrderOpenPrice(),TP2,OrderTakeProfit(),OrderExpiration());
            }//End of Ask>=TakeProfitArray[1]
         }//End of OrderType
      if(OrderType()==OP_SELL)
         {
          // if tp1 reached move to BE
         if(Bid<=TP1&&MovedToBE==false)
            {
               OrderModify(TicketArray[1],OrderOpenPrice(),BE,OrderTakeProfit(),OrderExpiration());
               OrderModify(TicketArray[2],OrderOpenPrice(),BE,OrderTakeProfit(),OrderExpiration());
               MovedToBE=true;
            }//End of Bid<=TakeProfitArray[0]&&MovedToBE==false
         // if tp1 reached move to tp1
         if(Bid<=TP2)
            {
               OrderModify(TicketArray[2],OrderOpenPrice(),TP2,OrderTakeProfit(),OrderExpiration());
            }//End of Bid<=TakeProfitArray[1]
         }//End OrderType
      }//End OrderSelect
   }//End MoveToBE
whroeder1
13607
whroeder1 2014.01.29 14:44  
brad: but nothing appears to happen at all.
int init()
   {
   // find open orders
   for(int i=0;i<=OrdersTotal()-1;i++)
Of course not. Init is called only once, when the EA is put on the chart. Put your code in start.
brettosm8
241
brettosm8 2014.02.06 23:25  

Once the EA is dropped on the chart it should determine the ticket numbers and take profit values once, not every time a new tick comes in, therefore isn't it in the right place?

Every time a tick comes in it compares price with values obtained when initialized.

Is there someone who can actually help with this??

whroeder1
13607
whroeder1 2014.02.07 00:59  
No it's not, because during init you don't yet have a chart or trade history downloaded. In addition, you don't update your variables when orders are opened or closed.
brettosm8
241
brettosm8 2014.02.07 12:46  

OK. Can you advise how to do so??

brettosm8
241
brettosm8 2014.02.09 23:45  
So, is there any one who can actually help?
Lam K
89
Lam K 2014.02.10 02:24  
int start()
  {    
// add here
init();
    AdjustSL();

   return(0);
  }
brettosm8
241
brettosm8 2014.02.12 12:56  

@WHRoeder: I understand your comment now about the chart or trade history not yet being downloaded. Since I only need the array's to be populated once I guess I will need some sort of switch so that it runs only once? Thanks.

@klam2404: Thanks also!

brettosm8
241
brettosm8 2014.02.13 06:43  

Today on the ausnzd d1 price dropped to 1.07489 (at time of writting). the screen shot below includes the low at the time though cant see cursor.


In the past when i've set tp levels the positions automatically close when price reaches the levels. My tp level was 1.07487 i.e. 2 pipettes below the shown low. As my code attempts to modify the SL level to BE when tp1 is reached and uses the bid for shorts the orders' SL were updated (which i found exciting!) but the broker didn't automatically activate my tp because it wasnt reached.. Should I use sth different than Bid so i'm using the same value as the broker??


 // if tp1 reached move to BE
         if(Bid<=TP1&&MovedToBE==false)

Much appreciated!!

Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2014.02.13 09:22  
brad:

Today on the ausnzd d1 price dropped to 1.07489 (at time of writting). the screen shot below includes the low at the time though cant see cursor.


In the past when i've set tp levels the positions automatically close when price reaches the levels. My tp level was 1.07487 i.e. 2 pipettes below the shown low. As my code attempts to modify the SL level to BE when tp1 is reached and uses the bid for shorts the orders' SL were updated (which i found exciting!) but the broker didn't automatically activate my tp because it wasnt reached.. Should I use sth different than Bid so i'm using the same value as the broker??


Much appreciated!!

A Sell is closed by a Buy, a Buy is executed at Ask
/
To add comments, please log in or register