Time EA

 

Hello Friends !!!!!!!!!!!!!


Exists a time EA, with random entries ??


I want to say that if you open a trade, this trade will be closed after a period of time ??


Really thanks for your answers...

 

We developed an EA that opens either (depending on settings) a buy or a sell every one minute, and then closes them based on p/l setting. If you like I would provide this for testing provided that you share the results with the community.

 

Hello !!


Will be a pleasure.


Best regards.

 

Someone else wants to help me?


Bests regards.

 
CROM, please
//+------------------------------------------------------------------+
//| Function..: CloseAfter                                           |
//| Parameters: iPeriod - Period in minutes that an order may remain |
//|                       open.                                      |
//| Purpose...: Place an expiration time on a market order.          |
//| Returns...: true -  order has expired and closed, or the expiry  |
//|                     time for the order has not been reached,     |
//|             false - order could not be closed, the error number  |
//|                     can be obtained using GetLastError().        |
//| Notes.....: The order must have been selected using OrderSelect()|  
//| Sample....: void start() {                                       |
//|               for(int i=OrdersTotal()-1; i>=0; i--) {            |
//|                 if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {   |
//|                   if(CloseAfter(PERIOD_H4) == false) return;     |
//|                 }                                                |
//|                 else Print("OrderSelect error: ",GetLastError());|
//|               }                                                  |
//|             }                                                    |
//+------------------------------------------------------------------+
bool CloseAfter(int iPeriod) {
  if((OrderType() == OP_BUY) && (TimeCurrent() > OrderOpenTime()+iPeriod*60)) {
    return(OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5));
  }
  else if((OrderType() == OP_SELL) && (TimeCurrent() > OrderOpenTime()+iPeriod*60)) {
    return(OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5));
  }
  return(true); 
}
try this
 
sxTed:
CROM, please try this


Really thanks sxted.


I want to do some tests, I think that there are some intraday hourly patterns.


Best regards.

 

Hello,

I have this code, but I try to close the trade after a period of time ( for example 3 hours ) but I can´t.

If someone else wants to help me....


I´m looking for intraday hourly patterns. ( 15:00 - 18:00 / for eurusd) ;)


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

//| twd.mq4 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Zaharov Sergey."

extern double lot = 0.1;
extern double trailingStop = 0; // trail stop in points
extern double takeProfit = 20; // recomended no more than 20
extern double stopLoss = 50; // do not use s/l
extern double slippage = 3;
extern int hour=15;

extern double proc=0.1;
extern double maxLoss=1000;
// EA identifier. Allows for several co-existing EA with different values
extern string nameEA = "twd";
extern int startTS=10;
//extern bool perevorot=false;

int ticket;
int profit;
double realTP, realSL, lots;
bool isBuying=false, isSelling=false, firstPos=false;
bool isLastOrderSL;

bool CloseAfter(int iPeriod) {
if((OrderType() == OP_BUY) && (TimeCurrent() > OrderOpenTime()+iPeriod*60)) {
return(OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5));
}
else if((OrderType() == OP_SELL) && (TimeCurrent() > OrderOpenTime()+iPeriod*60)) {
return(OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5));
}
return(true);
}


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

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

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int total;

// Check for invalid bars and takeprofit
if(Bars < 200)
{
Print("Not enough bars for this strategy - ", nameEA);
return(-1);
}
// Calculate indicators' value

total=OrdersTotal();

int cnt;
for(cnt = 0; cnt < total; cnt++)
{

// the next line will check for ONLY market trades, not entry orders
OrderSelect(cnt, SELECT_BY_POS);
// only look for this symbol, and only orders from this EA
if(OrderSymbol() == Symbol() && OrderType() <= OP_SELL )
{
//numPos++;
// Check for close signal for bought trade
if(OrderType() == OP_BUY){
if(Time[0]==CurTime()&& OrderProfit()>0 && takeProfit==0)
{
// Close bought trade
OrderClose(OrderTicket(),OrderLots(),Bid, slippage,Blue);
prtAlert("Day Trading: Closing BUY order");
}
if(trailingStop > 0)
{
if(Bid-(OrderOpenPrice()+startTS*Point) > trailingStop*Point)
{
if(OrderStopLoss() < (Bid - trailingStop*Point))
OrderModify(OrderTicket(), OrderOpenPrice(),
Bid-trailingStop*Point,OrderTakeProfit(),0,CLR_NONE);
}
}
// Check trailing stop

}
else
// Check sold trade for close signal
{
if(Time[0]==CurTime()&& OrderProfit()>0 && takeProfit==0)
{
OrderClose(OrderTicket(), OrderLots(), Ask, slippage, Green);
prtAlert("Day Trading: Closing SELL order");
}
if(trailingStop > 0)
// Control trailing stop
{
if((OrderOpenPrice()-startTS*Point) - Ask > trailingStop*Point)
{
if(OrderStopLoss() == 0 || OrderStopLoss() > Ask + trailingStop*Point)
OrderModify(OrderTicket(), OrderOpenPrice(),
Ask + trailingStop*Point, OrderTakeProfit(),
0, CLR_NONE);
}
}

}
}


}

total=OrdersTotal();
calculatePosition();

// Check for BUY entry signal
//if(total<1){
if(AccountFreeMargin() < 1000*lots)
{
Print("Not enough money to trade ", lots, " lots. Strategy:", nameEA);
return(0);
}
if(lot==0){
lots=MathRound(AccountBalance()*proc/maxLoss*10)/10;
if(lots>1000){
lots=1000;
}
if(lots<0.1){
lots=0.1;
}
}
else{
lots=lot;
}
//Print("Ïåðâûé "+(isBuying)+ " "+(isSelling));
if(isBuying && !isSelling)
{
if(stopLoss > 0)
realSL = Ask - stopLoss * Point;
if(takeProfit > 0)
realTP = Ask + takeProfit * Point;
else
realTP=0;
// Buy
ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, slippage, realSL, realTP,
"Ïîêóïêà ", 16384,0,Red);
if(ticket < 0)
Print("OrderSend (",nameEA,") failed with error #", GetLastError());
prtAlert("Day Trading: Buying");
}
// Check for SELL entry signal
if(isSelling && !isBuying)
{
if(stopLoss > 0)
realSL = Bid + stopLoss * Point;
if(takeProfit > 0)
realTP = Bid - takeProfit * Point;
else
realTP=0;

// Sell
ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, slippage, realSL, realTP,
nameEA, 16384, 0, Red);
if(ticket < 0)
Print("OrderSend (",nameEA,") failed with error #", GetLastError());
prtAlert("Day Trading: Selling");
}
//}

//----
return(0);
}
//+------------------------------------------------------------------+
void prtAlert(string str = "")
{
Print(str);
Alert(str);
// SpeechText(str,SPEECH_ENGLISH);
// SendMail("Subject EA",str);
}
void calculatePosition(){
double prevDay;
if(Time[0]==CurTime() && TimeHour(CurTime())==hour){
prevDay=iOpen(NULL,PERIOD_D1,1)-iClose(NULL, PERIOD_D1, 1);
if(prevDay<0){
isBuying=true;
isSelling=false;
}
else{
isBuying=false;
isSelling=true;
}
}
else{
isBuying=false;
isSelling=false;
}


}
Reason: