I'm trying to code an EA (using some already made codes) which does the following:
I have the following issues:
- Opens two pending orders, a sell stop and a buy stop, at a specific time (in this case 23:00), both with an SL and a TP.
- Expires the pending order at a certain time (for example at 10:00).
- If two trades are opened and one reaches the SL, the other has its TP increased (I think an OrderModify will solve something like this).
So far I have this:
PHP Code
//+------------------------------------------------------------------+
//| TimeBasedEA.mq4 |
//| Copyright © 2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
//changed by: "[email protected]"
// Time frame: M5 and higher
extern int MagicNumber = 20080122;
extern double DistancefromAsk;
extern double DistancefromBid;
extern double TakeProfit = 34;
extern double StopLoss = 55;
extern double Lots = 0.1;
extern int StartHour = 2300; // Open Trade time
extern bool OpenBuy = true;
extern bool OpenSell = true;
extern int NumBuys = 1;
extern int NumSells = 1;
extern int Slippage = 2;
//+------------------------------------------------------------------+
//| S T A R T |
//+------------------------------------------------------------------+
int start()
{
int cnt, ticket, total;
int ct;
//-------------------------------------+
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
//-------------------------------------+
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
//-------------------------------------+
ct = Hour() * 100 + Minute();
total=OrdersTotal();
if(total<1)
{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if(ct == StartHour && Close[1]>Open[1] && OpenBuy)
//if(ct == StartHour && High[1]<Open[0] && OpenBuy)
{
for ( cnt = 0; cnt < NumBuys; cnt++)
{
ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+(DistancefromAsk*Point),Slippage,Bid-(StopLoss*Point),Ask+(TakeProfit*Point),"",MagicNumber,TimeCurrent()+39600,CLR_NONE);
ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-(DistancefromBid*Point),Slippage,Ask+(StopLoss*Point),Bid-(TakeProfit*Point),"",MagicNumber,TimeCurrent()+39600,CLR_NONE);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
}
return;
}
// check for short position (SELL) possibility
if(ct == StartHour && Close[1]<Open[1] && OpenSell)
//if(ct == StartHour && Low[1]>Open[0] && OpenSell)
{
for ( cnt = 0; cnt < NumSells; cnt++)
{
ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-(DistancefromAsk*Point),Slippage,Ask+(StopLoss*Point),Bid-(TakeProfit*Point),"",MagicNumber,TimeCurrent()+39600,CLR_NONE);
ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+(DistancefromBid*Point),Slippage,Bid-(StopLoss*Point),Ask+(TakeProfit*Point),"",MagicNumber,TimeCurrent()+39600,CLR_NONE);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
}
return;
}
}
return(0);
}
// the end.
- Only one pending order (either sell or buy) is opened at the time specified, while I need two orders opened. (Resolved thanks to ixbone).
- I'm a noob at coding MQL so I have no idea how to code the expiry (also resolved).
- All that's left is to sort out the modifying part.
If anyone can help me it would be greatly appreciated as I think this EA is nearing completion as it is so it might as well get done.
Thanks,
madmax3