2016-11-29 146 views
0

我正在为我的交易建立一个EA(专家咨询),我想知道如何在第一个订单执行后的5分钟后设置执行订单的功能。如何每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); 
} 
+0

标志初始化添加到真正的,下订单时将其设置为false。经过x次检查后,再次将其设置为true以在下一次迭代中下订单。 – Jules

+0

嗨,你能更详细地说明我该如何做到这一点?对不起,我真的是新的编码.. – DouglasQ

+0

你可以像下面的定时器使用System.Threading.Timer定时器tr =新定时器(val => { //代码运行每5秒 },0,0, 5000); –

回答

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

您可以添加计时器,在您的间隔时间为5个薄荷糖,意味着以后每五个mintues Tick事件将触发等等你可以把你的函数在这种情况下

Timer objtimer; 
static void Main() 
{ 
    objtimer = new Timer(); 
    objtimer.Interval =50000; 
    objtimer.Tick += objtimer_Tick; 
} 
void objtimer_Tick(object sender, EventArgs e) 
{ 
     // your function 
} 
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"); 
} 
... 
相关问题