Pls help... Order triggered order EA problem

[Deleted]  
hi, there. i am newbies in writing mql4 expert advisor..
recently i'm writing about grid concept EA, however face some order management source code problems:

1) EA supposes to open only 4 orders, HOWEVER, at each tick, the EA is opening many and many same 4 orders..
2) After an order is closed due to take profit point hit, EA will triggered to open a same new order, HOWEVER, no new order is opened after TP is hit..

Please do give your opinion how to resolve above 2 problems.. attached below is the FULL source code for this Order Triggered Order EA.
(If possible, please give a FULL WORKING source code?) :)

Thank you very much, sir.

==================================================================================================

//+------------------------------------------------------------------+
//| Order triggered order EA.mq4 |
//| leonytham@gmail.com |
//+------------------------------------------------------------------+
#property copyright "leonytham@gmail.com"
#property link "http://"

#property show_inputs
extern double Lot = 0.1;
extern int Take_Profit = 10;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//SECTION 1:
// (1) When EA activated, 4 orders will be opened ONCE only,
// that is 1 instant OP_BUY, 1 instant OP_SELL, 1 pending OP_BUYLIMIT and 1 pending OP_SELLLIMIT.
// (2) Problem at SECION 1: After EA activated, these 4 orders are opened MANY MANY times (everytime new tick, 4 new orders are opened AGAIN)
// (3) Your opinion : How to write the code in SECTION 1 so that once EA runs, it ONLY opens 4 orders?
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

OrderSend(Symbol(), OP_BUY, Lot, Ask, 3, 0, Bid+1*Take_Profit*Point, "Long at 1.3400", 1000, NULL, Lime);
OrderSend(Symbol(), OP_BUYLIMIT, Lot, Ask-1*Take_Profit*Point, 3, 0, Bid, "Long at 1.3390", 1001, NULL, CLR_NONE);

OrderSend(Symbol(), OP_SELL, Lot, Bid, 3, 0, Ask-1*Take_Profit*Point, "Short at 1.3400", 9000, NULL, Red);
OrderSend(Symbol(), OP_SELLLIMIT, Lot, Bid+1*Take_Profit*Point, 3, 0, Ask, "Short at 1.3410", 9001, NULL, CLR_NONE);





////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//SECTION 2:
// (1) At SECTION 2, when ANY ONE of above 4 orders is closed (due to take profit), EA will triggered to:
// (a) RE-OPEN SAME with previous order properties.
// (b) The process at (a) will repeat UNLIMITED times (Hit_TP_&_closed -> Reopen_same_order... Hit_TP_&_closed -> Reopen_same_order...)
// (Example: Say EURUSD market price is at 1.3400, then price move down to 1.3390 and hit the OP_BUYLIMIT.
// After a while, market price retraced to 1.3400, so take profit is achived, so earlier OP_BUYLIMIT is closed now.
// At this moment, EA is triggered to send a NEW same with previous order:
// OP_BUYLIMIT 0.1Lot at order price 1.3390, TP at 1.3400 and magic number 1001 =>same properties with earlier order.
// The EA will repeat the same, as long as TP is hit)
//
// (2) Problem at SECION 2: When one of the order TP is hit, there is NO new same properties order is Re-opened.
// (3) Your opinion : How to write the code in SECTION 2 so that the TP hit can trigger to send a new same properties order?
// That is when market price hit one TP, EA will be triggered to Re-open one new same properties order.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

for(int i=3;i>=0;i--) // Check the last 3 closed orders in history
{OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);

if(OrderMagicNumber()==1000) //previous OP_BUY order
OrderSend(Symbol(), OP_BUYLIMIT, Lot, Ask-Take_Profit*Point, 3, 0, Bid, "Repeating-Long at 1.3400", 1000, NULL, CLR_NONE);
if (OrderMagicNumber()== 1001) //previous OP_BUYLIMIT order
OrderSend(Symbol(), OP_BUYLIMIT, Lot, Ask-Take_Profit*Point, 3, 0, Bid, "Repeating-Long at 1.3390", 1001, NULL, CLR_NONE);

if(OrderMagicNumber()==9000) //previous OP_SELL order
OrderSend(Symbol(), OP_SELLLIMIT, Lot, Bid+Take_Profit*Point, 3, 0, Ask, "Repeating-Short at 1.3400", 9000, NULL, CLR_NONE);
if (OrderMagicNumber()== 9001) //previous OP_SELLLIMIT order
OrderSend(Symbol(), OP_SELLLIMIT, Lot, Bid+Take_Profit*Point, 3, 0, Ask, "Repeating-Short at 1.3410", 9001, NULL, CLR_NONE);


return(0);
}}
//+------------------------------------------------------------------+
[Deleted]  
if (OrdersTotal < 1){
	OrderSend(Symbol(), OP_BUY, Lot, Ask, 3, 0, Bid+1*Take_Profit*Point, "Long at 1.3400", 1000, NULL, Lime);
	OrderSend(Symbol(), OP_BUYLIMIT, Lot, Ask-1*Take_Profit*Point, 3, 0, Bid, "Long at 1.3390", 1001, NULL, CLR_NONE);
	
	OrderSend(Symbol(), OP_SELL, Lot, Bid, 3, 0, Ask-1*Take_Profit*Point, "Short at 1.3400", 9000, NULL, Red);
	OrderSend(Symbol(), OP_SELLLIMIT, Lot, Bid+1*Take_Profit*Point, 3, 0, Ask, "Short at 1.3410", 9001, NULL, CLR_NONE);
}
[Deleted]  
int    Prev_Orders = -1;                  //place this before the start section


//==================This goes in section 2========================================================
   if (Prev_Orders < 0)Prev_Orders = OrdersHistoryTotal();      //we have to start somewhere so set previous orders to history total for first tick
   if (OrdersHistoryTotal() > Prev_Orders)int MN = Get_Magic_Num());  //history orders has increased, so an order must have closed>>>>> lets get it's Magic Number & do something with it
   if (MN < 0)Alert("Error with getting last order's Magic Number!");//Get_Magic_Num routine has encountered an error
   Prev_Orders = OrdersHistoryTotal();                       //record the number of history orders for the next round
   switch(MN){
      case 1000 :
         OrderSend(Symbol(), OP_BUYLIMIT, Lot, Ask-Take_Profit*Point, 3, 0, Bid, "Repeating-Long at 1.3400", 1000, NULL, CLR_NONE);
          break;
      case 1001 :
         OrderSend(Symbol(), OP_BUYLIMIT, Lot, Ask-Take_Profit*Point, 3, 0, Bid, "Repeating-Long at 1.3390", 1001, NULL, CLR_NONE);
          break;
      case 9000 :
         OrderSend(Symbol(), OP_SELLLIMIT, Lot, Bid+Take_Profit*Point, 3, 0, Ask, "Repeating-Short at 1.3400", 9000, NULL, CLR_NONE);
          break;
      case 9001 :
          OrderSend(Symbol(), OP_SELLLIMIT, Lot, Bid+Take_Profit*Point, 3, 0, Ask, "Repeating-Short at 1.3410", 9001, NULL, CLR_NONE);
          break;
    }//end switch

//=========================end of start function here===============================






int Get_Magic_Num(){
   if (OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS,MODE_TRADES))return(OrderMagicNumber()) //select the last closed trade and return the magic number
   else {    
      Print( "OrderSelect(SELECT_BY_TICKET) - Error #",GetLastError() );//Oops, there is a problem give back a negative magic number to advise the user
      return(-1);
   }
}
[Deleted]  

I didn't debug the code so so just fix up any errors you encounter.

Also you should be normalising your doubles or you are headed from problems.

[Deleted]  
kennyhubbard:

I didn't debug the code so so just fix up any errors you encounter.

Also you should be normalising your doubles or you are headed from problems.


thank you, kenny. will try the given source code :)
[Deleted]  
RaptorUK:

kennyhubbard    hasn't posted here in almost 2 years,  why are you dredging up a thread that is almost 3 years old ?  It would be a good idea to delete this post and start your own thread, it's up to you though . . .  but . . .

 

Please edit your post . . . 

 

  

 



Agreed!