BTC USD 63,489.3 Gold USD 4,475.50
Time now: Jun 1, 12:00 AM

Minta tolong... Membina Order Triggered Order EA

leonytham

Freshie
Messages
31
Joined
Oct 17, 2009
Messages
31
Reaction score
4
Points
11
hi, saya pelajar baru membuat expert advisor sendiri, tetapi hadapi masalah masa nak bina expert advisor berkaitan grid.

Sepatutnya:
1) EA ini membuka 4 posisi SAHAJA, TETAPI, setiap tick, EA membuka posisi tak berhenti-henti.
2) Apabila TP terkena (take profit), EA akan membuka posisi baru (dengan nilai sama dengan posisi sebelumnya), TETAPI, EA ini TIDAK membuka apa-apa posisi apabila TP terkena.


Harap kawan boleh memberi bantuan menyelesaikan masalah yang dihadapi di atas.. (kalau boleh, bagi juga full source code yang berjaya).

Terima kasih banyak-banyak.

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

//+------------------------------------------------------------------+
//| 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);
}}
//+------------------------------------------------------------------+
 
Last edited:
hi, saya pelajar baru membuat expert advisor sendiri, tetapi hadapi masalah masa nak bina expert advisor berkaitan grid.

Sepatutnya:
1) EA ini membuka 4 posisi SAHAJA, TETAPI, setiap tick, EA membuka posisi tak berhenti-henti.
2) Apabila TP terkena (take profit), EA akan membuka posisi baru (dengan nilai sama dengan posisi sebelumnya), TETAPI, EA ini TIDAK membuka apa-apa posisi apabila TP terkena.


Harap kawan boleh memberi bantuan menyelesaikan masalah yang dihadapi di atas.. (kalau boleh, bagi juga full source code yang berjaya).

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


maksud lain ke atas tu bro??
 
maksud lain ke atas tu bro??

:) sori, saya tersalah tulis (yang betul seperti berikut):

Sepatutnya:
1) EA ini membuka 4 posisi SAHAJA, TETAPI, setiap tick, EA membuka posisi tak berhenti-henti.
2) Apabila TP terkena (take profit), EA akan membuka posisi baru (dengan nilai sama dengan posisi sebelumnya), TETAPI, EA ini TIDAK membuka apa-apa posisi apabila TP MASIH BELUM terkena.
 
:) sori, saya tersalah tulis (yang betul seperti berikut):

Sepatutnya:
1) EA ini membuka 4 posisi SAHAJA, TETAPI, setiap tick, EA membuka posisi tak berhenti-henti.
2) Apabila TP terkena (take profit), EA akan membuka posisi baru (dengan nilai sama dengan posisi sebelumnya), TETAPI, EA ini TIDAK membuka apa-apa posisi apabila TP MASIH BELUM terkena.

noted bro,,harap ade yg bole menolong tt ni
 
Code:
//+------------------------------------------------------------------+
//| Order triggered order EA.mq4 |
//| [email protected] |
//+------------------------------------------------------------------+
#property copyright "[email protected]"
#property link "http://"

extern double Lot = 0.1;
extern int Take_Profit = 10;
//----
int start()
{
   if (Take_Profit<1) return (-1);
   
   double buyLimitPrice, sellLimitPrice;
   
   buyLimitPrice=Ask-Take_Profit*Point;
   sellLimitPrice=Bid+Take_Profit*Point;

//----   
   if (totalOrder(9000)+totalOrder(9001)+totalOrder(1000)+totalOrder(1001) == 0)
   {      
      OrderSend(Symbol(), OP_BUY, Lot, Ask, 3, 0, Bid+Take_Profit*Point, "Long at 1.3400", 1000, NULL, Lime);
      OrderSend(Symbol(), OP_BUYLIMIT, Lot, buyLimitPrice, 3, 0, Bid, "Long at 1.3390", 1001, NULL, CLR_NONE);

      OrderSend(Symbol(), OP_SELL, Lot, Bid, 3, 0, Ask-Take_Profit*Point, "Short at 1.3400", 9000, NULL, Red);
      OrderSend(Symbol(), OP_SELLLIMIT, Lot, sellLimitPrice, 3, 0, Ask, "Short at 1.3410", 9001, NULL, CLR_NONE);
   }
   else
   {         
      if (totalOrder(1000) == 0) 
         OrderSend(Symbol(), OP_BUYLIMIT, Lot, buyLimitPrice, 3, 0, Bid, "Repeating-Long at 1.3400", 1000, NULL, CLR_NONE);
      if (totalOrder(1001) == 0)
         OrderSend(Symbol(), OP_BUYLIMIT, Lot, buyLimitPrice, 3, 0, Bid, "Repeating-Long at 1.3390", 1001, NULL, CLR_NONE);
      if (totalOrder(9000) == 0)
         OrderSend(Symbol(), OP_SELLLIMIT, Lot, sellLimitPrice, 3, 0, Ask, "Repeating-Short at 1.3400", 9000, NULL, CLR_NONE);
      if (totalOrder(9001) == 0)  
         OrderSend(Symbol(), OP_SELLLIMIT, Lot, sellLimitPrice, 3, 0, Ask, "Repeating-Short at 1.3410", 9001, NULL, CLR_NONE);
   }
//----
   return(0);
}

int totalOrder (int magic)
{
   int i, count;
   for (i=0; i<OrdersTotal(); i++)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber()!=magic || OrderSymbol()!=Symbol()) continue;
      count++;
   }
   return (count);
}
 
Last edited:

Latest Posts

Live Forex Chart

Currency
Rates
EUR / USD
1.16104
USD / JPY
160.024
GBP / USD
1.34238
USD / CHF
0.78952
USD / CAD
1.39094
EUR / JPY
185.795
AUD / USD
0.71336
Back
Top
Log in Register