Jump to content

Recommended Posts

Posted

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 |

//| [email protected] |

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

#property copyright "[email protected]"

#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);

}}

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...