EventSetMillisecondTimer( 10 ); // 1st will read in 10 | if won't read try next EventSetMillisecondTimer( 250 ); // 2nd will read in 250 | if won't read try next EventSetMillisecondTimer( 1250 ); // 3rd will read in 1250 | if read then stop reading till next PERIOD_M5
datetime M1,M5,M15,M30,H1,H4,D1,W1,MN1; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ intOnInit() { //--- create timer EventSetTimer(60); //--- playsound PlaySound("alert2.wav"); //--- load open times M1=iTime(Symbol(),PERIOD_M1,0); M5=iTime(Symbol(),PERIOD_M5,0); M15=iTime(Symbol(),PERIOD_M15,0); M30=iTime(Symbol(),PERIOD_M30,0); H1=iTime(Symbol(),PERIOD_H1,0); H4=iTime(Symbol(),PERIOD_H4,0); D1=iTime(Symbol(),PERIOD_D1,0); W1=iTime(Symbol(),PERIOD_W1,0); MN1=iTime(Symbol(),PERIOD_MN1,0); //--- create moving timeline ObjectCreate(0,"Time",OBJ_VLINE,0,TimeCurrent(),0); //--- detect period switch(Period()) { casePERIOD_M1: MessageBox("Switched to 1 Minute Frame"); //Do Something... ObjectCreate(0,"1-Minute",OBJ_VLINE,0,Time[0],0); M1=Time[0];// store current time break; casePERIOD_M5: MessageBox("Switched to 5 Minutes Frame"); //Do Something... ObjectCreate(0,"5-Minutes",OBJ_VLINE,0,Time[0],0); M5=Time[0];// store current time break; casePERIOD_M15: MessageBox("Switched to 15 Minutes Frame"); //Do Something... ObjectCreate(0,"15-Minutes",OBJ_VLINE,0,Time[0],0); M15=Time[0];// store current time break; casePERIOD_M30: MessageBox("Switched to 30 Minutes Frame"); //Do Something... ObjectCreate(0,"30-Minutes",OBJ_VLINE,0,Time[0],0); M30=Time[0];// store current time break; casePERIOD_H1: MessageBox("Switched to 1 Hour Frame"); //Do Something... ObjectCreate(0,"1-Hour",OBJ_VLINE,0,Time[0],0); H1=Time[0];// store current time break; casePERIOD_H4: MessageBox("Switched to 4 Hour Frame"); //Do Something... ObjectCreate(0,"4-Hour",OBJ_VLINE,0,Time[0],0); H4=Time[0];// store current time break; casePERIOD_D1: MessageBox("Switched to Daily Frame"); //Do Something... ObjectCreate(0,"Daily",OBJ_VLINE,0,Time[0],0); D1=Time[0];// store current time break; casePERIOD_W1: MessageBox("Switched to Weekly Frame"); //Do Something... ObjectCreate(0,"Weekly",OBJ_VLINE,0,Time[0],0); W1=Time[0];// store current time break; casePERIOD_MN1: MessageBox("Switched to Monthly Frame"); //Do Something... ObjectCreate(0,"Monthly",OBJ_VLINE,0,Time[0],0); MN1=Time[0];// store current time break; } //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ voidOnDeinit(constint reason) { //--- destroy timer EventKillTimer(); //--- delete objects ObjectsDeleteAll(0,0,-1); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ voidOnTick() { //--- update timeline ObjectMove(0,"Time",0,TimeCurrent(),0); //--- check for new candles if(M1!=iTime(Symbol(),PERIOD_M1,0)) { Alert("New candle on M1"); ObjectMove(0,"1-Minute",0,iTime(Symbol(),PERIOD_M1,0),0); // Move line M1=iTime(Symbol(),PERIOD_M1,0);// Overwrite old with new value } if(M5!=iTime(Symbol(),PERIOD_M5,0)) { Alert("New candle on M5"); M1=iTime(Symbol(),PERIOD_M5,0);// Overwrite old with new value } if(M15!=iTime(Symbol(),PERIOD_M15,0)) { Alert("New candle on M15"); M15=iTime(Symbol(),PERIOD_M15,0);// Overwrite old with new value } if(M30!=iTime(Symbol(),PERIOD_M30,0)) { Alert("New candle on M30"); M30=iTime(Symbol(),PERIOD_M30,0);// Overwrite old with new value } // and so on to MN1... } //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ voidOnTimer() { //--- ObjectMove(0,"Time",0,TimeCurrent(),0);// Update timeline } //+------------------------------------------------------------------+
请描述你想做什么。
对不起,我弄糊涂 了。
我需要像下面那样设置3个'EventSetMillisecondTimer' (你说它不起作用...)。请问有其他的方法吗?
EventSetMillisecondTimer( 250 ); // 2nd will read in 250 | if won't read try next
EventSetMillisecondTimer( 1250 ); // 3rd will read in 1250 | if read then stop reading till next PERIOD_M5
最好。
计时器只在OnInit()函数中设置一次。
如果你想重新初始化,你必须先杀死旧的定时器,这通常发生在OnDeinit()函数上。
就像我说的,你也可以使用一个计数器。
如果你把代码放在例如一个10毫秒的定时器中,那么代码将每10毫秒执行一次。
这很可能会使你的终端冻结,因为它太快了。
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(1);// 1 second
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//--- increment counter
counter++;
//--- comment status on the chart
Comment(IntegerToString(counter));
//--- switch timer
switch(counter)
{
case 60:
Alert(" 1 Minute ");
// Do Something...
break;
case 300:
Alert(" 5 Minutes ");
// Do Something...
break;
case 900:
Alert(" 15 Minutes ");
// Do Something...
counter=0;// Reset counter on highest value.
break;
}
// end switch timer
if(counter>900)
{
counter=0;// Safety Mechanism
}
}
//+------------------------------------------------------------------+
请注意,1分钟只触发一次,所以不是每分钟。
5分钟也只在前5分钟后触发一次,所以不是在10分钟也不是在15分钟的时候。
如果你想每分钟触发一次,你必须为60秒、120秒、180秒等添加代码。
15分钟的标记将每15分钟触发一次,因为它把计数器设置为零。
你想使用什么机制,你想在定时的时间间隔内触发什么代码?
你想达到什么目的?void OnTimer()
{
static int counter=0;
static int next1m=60;
static int next5m=300;
static int next15m=900;
//--- increment counter
counter++;
//--- comment status on the chart
Comment(IntegerToString(counter));
//--- timer
if(counter==next1m)
{
Alert(" 1 Minute ");
// Do Something...
next1m+=60;
}
if(counter==next5m)
{
Alert(" 5 Minutes ");
// Do Something...
next5m+=300;
}
if(counter==next15m)
{
Alert(" 15 Minutes ");
// Do Something...
counter=0;// Reset counter on highest value.
next1m=60;
next5m=300;
}
}
好主意,马可。
你可以做这样的事情,在每个时间间隔触发
Marco vd Heijden:
你想用什么机制,你想用什么代码来触发定时的间隔?
你想达到什么目的?再次感谢你的评论,非常感谢。
//---
......我的指标是按照我的要求( /是) 工作的。
指标是这样工作的。
我需要用例子来说明 我的问题。
我打开欧元兑美元 的图表,并将我的指标附在图表上,工作得很完美,就像我想要的那样,移动/显示向前(甚至过去) 的VLINE,我可以改变TIMEFRAME PERIOD,并自动适应 该PERIODs(这是我想要的)。
我的指标问题在哪里? //也许这不是一个问题,但它需要多一点 "不相关 "的时间来更新。
如果我选择'EventSetMillisecondTimer( 10 );',然后我将TIMEFRAME PERIODs改为任何 TIMEFRAME PERIODs,几乎 我看不到(当它) 加载新的VLINE到图表上(我想要的)。
非常感谢你的评论 - Marco & whroeder1
(注意:我没有使用'开关运算符'--因为你的#18评论比一切都好,这是我升级了它,并且正在努力--它对我非常有用)
(英语不是我的母语)
Keith Watford:
好样的,马可。
你可以这样做,在每个时间间隔触发
也很好,基思。)
这对我帮助很大,我将在我的下一个指标中使用它。
祝您一切顺利。
我不知道其他人怎么想,但我发现你的字体、颜色、粗体和斜体等的组合让人心烦意乱,令人讨厌。事实上,我都懒得看这个帖子了。
再次感谢你的评论,非常感谢。
//---
......我的指标是按照我的要求( /是) 工作的。
指标是这样工作的。
我需要用例子来说明 我的问题。
我打开欧元兑美元 的图表,并将我的指标附在图表上,工作得很完美,就像我想要的那样,移动/显示向前(甚至过去) 的VLINE,我可以改变TIMEFRAME PERIOD,并自动适应 该PERIODs(这是我想要的)。
我的指标问题在哪里? //也许这不是一个问题,但它需要多一点 "不相关 "的时间来更新。
如果我选择'EventSetMillisecondTimer( 10 );',然后我将TIMEFRAME PERIODs改为任何 TIMEFRAME PERIODs,几乎 我看不到(当它) 加载新的VLINE到图表上(我想要的)。
非常感谢你的评论 - Marco & whroeder1
(注意:我没有使用'开关运算符'--因为你的#18评论比一切都好,这是我升级了它,并且正在努力--它对我非常有用)
(英语不是我的母语)
如果是关于切换时间段,那么计时器不是一个好的选择,因为计时器在切换框架时被破坏。
你可以在OnInit()函数 中创建你的线条,并在OnTick()或OnTimer()函数中更新它们。
这里有一个例子。
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(60);
//--- playsound
PlaySound("alert2.wav");
//--- create moving timeline
ObjectCreate(0,"Time",OBJ_VLINE,0,TimeCurrent(),0);
//--- detect period
switch(Period())
{
case PERIOD_M1:
MessageBox("Switched to 1 Minute Frame");
//Do Something...
ObjectCreate(0,"1-Minute",OBJ_VLINE,0,Time[0],0);
break;
case PERIOD_M5:
MessageBox("Switched to 5 Minutes Frame");
//Do Something...
ObjectCreate(0,"5-Minutes",OBJ_VLINE,0,Time[0],0);
break;
case PERIOD_M15:
MessageBox("Switched to 15 Minutes Frame");
//Do Something...
ObjectCreate(0,"15-Minutes",OBJ_VLINE,0,Time[0],0);
break;
case PERIOD_M30:
MessageBox("Switched to 30 Minutes Frame");
//Do Something...
ObjectCreate(0,"30-Minutes",OBJ_VLINE,0,Time[0],0);
break;
case PERIOD_H1:
MessageBox("Switched to 1 Hour Frame");
//Do Something...
ObjectCreate(0,"1-Hour",OBJ_VLINE,0,Time[0],0);
break;
case PERIOD_H4:
MessageBox("Switched to 4 Hour Frame");
//Do Something...
ObjectCreate(0,"4-Hour",OBJ_VLINE,0,Time[0],0);
break;
case PERIOD_D1:
MessageBox("Switched to Daily Frame");
//Do Something...
ObjectCreate(0,"Daily",OBJ_VLINE,0,Time[0],0);
break;
case PERIOD_W1:
MessageBox("Switched to Weekly Frame");
//Do Something...
ObjectCreate(0,"Weekly",OBJ_VLINE,0,Time[0],0);
break;
case PERIOD_MN1:
MessageBox("Switched to Monthly Frame");
//Do Something...
ObjectCreate(0,"Monthly",OBJ_VLINE,0,Time[0],0);
break;
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
//--- delete objects
ObjectsDeleteAll(0,0,-1);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
ObjectMove(0,"Time",0,TimeCurrent(),0);// Update timeline
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
ObjectMove(0,"Time",0,TimeCurrent(),0);// Update timeline
}
//+------------------------------------------------------------------+
另外,如果你想在出现新的蜡烛时更新这些线,你可以把前一页的例子和上一页的例子结合起来,这样就可以了。
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(60);
//--- playsound
PlaySound("alert2.wav");
//--- load open times
M1=iTime(Symbol(),PERIOD_M1,0);
M5=iTime(Symbol(),PERIOD_M5,0);
M15=iTime(Symbol(),PERIOD_M15,0);
M30=iTime(Symbol(),PERIOD_M30,0);
H1=iTime(Symbol(),PERIOD_H1,0);
H4=iTime(Symbol(),PERIOD_H4,0);
D1=iTime(Symbol(),PERIOD_D1,0);
W1=iTime(Symbol(),PERIOD_W1,0);
MN1=iTime(Symbol(),PERIOD_MN1,0);
//--- create moving timeline
ObjectCreate(0,"Time",OBJ_VLINE,0,TimeCurrent(),0);
//--- detect period
switch(Period())
{
case PERIOD_M1:
MessageBox("Switched to 1 Minute Frame");
//Do Something...
ObjectCreate(0,"1-Minute",OBJ_VLINE,0,Time[0],0);
M1=Time[0];// store current time
break;
case PERIOD_M5:
MessageBox("Switched to 5 Minutes Frame");
//Do Something...
ObjectCreate(0,"5-Minutes",OBJ_VLINE,0,Time[0],0);
M5=Time[0];// store current time
break;
case PERIOD_M15:
MessageBox("Switched to 15 Minutes Frame");
//Do Something...
ObjectCreate(0,"15-Minutes",OBJ_VLINE,0,Time[0],0);
M15=Time[0];// store current time
break;
case PERIOD_M30:
MessageBox("Switched to 30 Minutes Frame");
//Do Something...
ObjectCreate(0,"30-Minutes",OBJ_VLINE,0,Time[0],0);
M30=Time[0];// store current time
break;
case PERIOD_H1:
MessageBox("Switched to 1 Hour Frame");
//Do Something...
ObjectCreate(0,"1-Hour",OBJ_VLINE,0,Time[0],0);
H1=Time[0];// store current time
break;
case PERIOD_H4:
MessageBox("Switched to 4 Hour Frame");
//Do Something...
ObjectCreate(0,"4-Hour",OBJ_VLINE,0,Time[0],0);
H4=Time[0];// store current time
break;
case PERIOD_D1:
MessageBox("Switched to Daily Frame");
//Do Something...
ObjectCreate(0,"Daily",OBJ_VLINE,0,Time[0],0);
D1=Time[0];// store current time
break;
case PERIOD_W1:
MessageBox("Switched to Weekly Frame");
//Do Something...
ObjectCreate(0,"Weekly",OBJ_VLINE,0,Time[0],0);
W1=Time[0];// store current time
break;
case PERIOD_MN1:
MessageBox("Switched to Monthly Frame");
//Do Something...
ObjectCreate(0,"Monthly",OBJ_VLINE,0,Time[0],0);
MN1=Time[0];// store current time
break;
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
//--- delete objects
ObjectsDeleteAll(0,0,-1);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- update timeline
ObjectMove(0,"Time",0,TimeCurrent(),0);
//--- check for new candles
if(M1!=iTime(Symbol(),PERIOD_M1,0))
{
Alert("New candle on M1");
ObjectMove(0,"1-Minute",0,iTime(Symbol(),PERIOD_M1,0),0); // Move line
M1=iTime(Symbol(),PERIOD_M1,0);// Overwrite old with new value
}
if(M5!=iTime(Symbol(),PERIOD_M5,0))
{
Alert("New candle on M5");
M1=iTime(Symbol(),PERIOD_M5,0);// Overwrite old with new value
}
if(M15!=iTime(Symbol(),PERIOD_M15,0))
{
Alert("New candle on M15");
M15=iTime(Symbol(),PERIOD_M15,0);// Overwrite old with new value
}
if(M30!=iTime(Symbol(),PERIOD_M30,0))
{
Alert("New candle on M30");
M30=iTime(Symbol(),PERIOD_M30,0);// Overwrite old with new value
}
// and so on to MN1...
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
ObjectMove(0,"Time",0,TimeCurrent(),0);// Update timeline
}
//+------------------------------------------------------------------+
我不知道其他人怎么想,但我发现你的字体、颜色、粗体和斜体等的组合让人心烦意乱,令人讨厌。事实上,我都懒得看这个帖子 了。
Max Enrik:
我不知道其他人怎么想,但我发现你的字体、颜色、粗体和斜体等的组合让人心烦意乱,令人讨厌。事实上,我都懒得看这个帖子 了。