Here is my code. The EA can open positions properly, but cannot close them. The main problem seems to lie in "
if(CurTime()-OrderOpenTime()==24*60*60)". If I deleted this line, EA can close positions 4 hours after their opening.
Any help is greatly appreciated.
int start()
{
// static variable to track bar of last entry
static datetime tLastEntry = 0;
// if there wasn't an entry this bar, perform entry check
if(tLastEntry < Time[0])
if(doOrderEntry())
tLastEntry = Time[0];
if(CurTime()-OrderOpenTime()==24*60*60)
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
}
// place your logic checks for order entry here
// if order is placed, return true
// if order isn't placed, return false
bool doOrderEntry()
{
int cnt, ticket;
double dDiffp, dDiffp2;
dDiffp = iClose(NULL,PERIOD_H4,1) - iOpen(NULL,PERIOD_H4,1);
if(dDiffp > 1.5*Point && TimeHour(CurTime())==8)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoploss*Point,Ask+TakeProfit*Point,
"My EA",12345,0);
return(true);
}
else
return(false);
}
if(CurTime()-OrderOpenTime()==24*60*60)". If I deleted this line, EA can close positions 4 hours after their opening.
Any help is greatly appreciated.
int start()
{
// static variable to track bar of last entry
static datetime tLastEntry = 0;
// if there wasn't an entry this bar, perform entry check
if(tLastEntry < Time[0])
if(doOrderEntry())
tLastEntry = Time[0];
if(CurTime()-OrderOpenTime()==24*60*60)
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
}
// place your logic checks for order entry here
// if order is placed, return true
// if order isn't placed, return false
bool doOrderEntry()
{
int cnt, ticket;
double dDiffp, dDiffp2;
dDiffp = iClose(NULL,PERIOD_H4,1) - iOpen(NULL,PERIOD_H4,1);
if(dDiffp > 1.5*Point && TimeHour(CurTime())==8)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoploss*Point,Ask+TakeProfit*Point,
"My EA",12345,0);
return(true);
}
else
return(false);
}