如何每5分钟执行一次功能?

时间:2016-11-29 04:24:46

标签: c# task scheduled-tasks dispatchertimer

我正在为我的交易建立一个EA(专家咨询),我想知道如果执行第一个订单,我怎么能设置该函数在5分钟后执行订单。

我当前代码中的问题是,一旦满足要求,它将继续下相同的订单,直到我用完钱,但我的想法是,一旦满足要求,它将下订单一次,并在5后分钟,它会再次检查条件是否仍符合要求,如果是,它会再次下订单,如果没有,它会继续检查每一分钟,直到满足要求为止。

简而言之,一旦执行第一个订单,它将不会在5分钟内下任何订单,并将在第5分钟后每分钟测试一次。

这是守则。

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);
}

3 个答案:

答案 0 :(得分:0)

你可以在任何时间间隔内调用该函数:

   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);      
    }
}

答案 1 :(得分:0)

你可以添加计时器,因为你的间隔为5分钟,意味着每隔5分钟就会触发一次,所以你可以把你的功能放在那个事件中

Timer objtimer;
static void Main()
{
     objtimer = new Timer();
     objtimer.Interval =50000;
     objtimer.Tick += objtimer_Tick;
}
 void objtimer_Tick(object sender, EventArgs e)
 {
      // your function
 }

答案 2 :(得分: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");
}
...