1

I'm building a EA (Expert Advisory) for my trading, i wonder how can i set the function to execute the order 5 minutes later if the 1st order is executed.

The problem in my current code is, once it meet the requirement, it will keep placing the same order until i ran out money, but my idea is once it meet the requirement, it will place the order once, and after 5 minutes, it will check again if the condition is still meet the requirement, if yes, it will place the order again, if not it will keep checking for every minutes then, until the requirement is met.

in short, once the 1st order is executed, it wont place any order within 5 minutes, and will test the condition every minutes after the 5th minutes.

Here is the Code.

extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=0;
extern double TakeProfit=0;
extern int TrailingStop=0;
extern int Slippage=3;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start() 
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;

  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()>=0 ) 
  {
     int result=0;
     if((iRSI(NULL,PERIOD_M1,5,PRICE_CLOSE,1)<21)) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     if((iRSI(NULL,PERIOD_M1,5,PRICE_CLOSE,1)>79)) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
  }

  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
              if((iRSI(NULL,PERIOD_M1,5,PRICE_CLOSE,1)>70)) //here is your close buy rule
              {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
              }
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
                if((iRSI(NULL,PERIOD_M1,5,PRICE_CLOSE,1)<30)) // here is your close sell rule
                {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                }
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}
Prajwal
  • 3,930
  • 5
  • 24
  • 50
DouglasQ
  • 33
  • 1
  • 9
  • Add a flag init to true, when order is placed set it to false. After x times checking, set it to true again to place order in the next iteration. – Jules Nov 29 '16 at 04:28
  • Hi, can you be more specified on how can i do this? sorry that i'm really new on coding.. – DouglasQ Nov 29 '16 at 04:41
  • You can use System.Threading.Timer like the following Timer tr = new Timer( val => { // code to run every 5 seconds },0,0,5000); – Samvel Petrosov Nov 29 '16 at 04:43
  • 1
    Looking at this hurts my eyes with the mixed up non-standard Naming Conventions, eg the 2 methods `start` and `TotalOrdersCount` and the member variables are upper camel case. Ouch. As @Jules said use bool's and a `Thread.Sleep(5 * 60 * 1000)` until you check again. – Jeremy Thompson Nov 29 '16 at 04:44

3 Answers3

1

you can add timer, in that your interval is 5 mints, mean after every five mintues tick event will fire so you can put your function in that event

Timer objtimer;
static void Main()
{
     objtimer = new Timer();
     objtimer.Interval =50000;
     objtimer.Tick += objtimer_Tick;
}
 void objtimer_Tick(object sender, EventArgs e)
 {
      // your function
 }
farrukh aziz
  • 162
  • 1
  • 2
  • 9
0

You can do call of the function in any interval like this:

   static void Main(string[] args)
{
    int num = 0; 
    TimerCallback tm = new TimerCallback(Count);
    Timer timer = new Timer(tm, num, 0, 2000);//where the 4-th number is interval in milliseconds

    Console.ReadLine();
}
public static void Count(object obj)
{
    int x = (int)obj;
    for (int i = 1; i < 9; i++, x++)
    {
        Console.WriteLine("{0}", x*i);      
    }
}
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
0
...
System.Windows.Forms.Timer t=new System.Windows.Forms.Timer();
t.Interval=300000;
t.Enabled=true;
t.Tick+= new EventHandler(t_Tick);
...
void t_Tick(object sender, EventArgs e)
{
Console.WriteLine("5 MIN");
}
...
huse.ckr
  • 530
  • 12
  • 39