Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 579

 
PolarSeaman:
Still haven't figured out how to count the seconds until the bar closes on the current period. I need your help.

I gave you the function.

 
Artyom Trishkin:

I gave you the function.

Yes, but the code above, in the comment, counts down smoothly every second and the function is jerky. On M1, out of 60 bars, 3 or 4 times there is no alert.

#property strict
#define  MILLISEC_TIMER_INTERVAL         500 
//--- input parameters
input int s_clo=2;
input int Period_=13,
Shift_=0;
input     ENUM_MA_METHOD Method_MA_=MODE_SMA;
input ENUM_APPLIED_PRICE Apply_to_=PRICE_CLOSE;
double ma_fast;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int OnInit()
{

   if(!EventSetMillisecondTimer(MILLISEC_TIMER_INTERVAL))
   {
      Print("Не могу запустить таймер");
      return INIT_FAILED;
   }
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTimer()
  {
   RefreshRates();
//---
ma_fast=0;
   if(SecondsToCandleClose(Symbol(),0)>=s_clo)return;
   
   { ma_fast=ma(Period_,Shift_,Method_MA_,Apply_to_,0); Alert("ma_fast",ma_fast,"время откр. бара ",Time[0]);}
   
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
long SecondsToCandleClose(const string symbol_name,const ENUM_TIMEFRAMES timeframe)
  {
   datetime array[];
   return(CopyTime(symbol_name,timeframe,0,1,array)==1 ? PeriodSeconds(timeframe)+array[0]-TimeCurrent() : 0);
  }

//+------------------------------------------------------------------+
double ma(int period,int ma_shift,ENUM_MA_METHOD ma_method,ENUM_APPLIED_PRICE ap_price,int shift)
  {
   return(ND(iMA(NULL,(int)0,period,ma_shift,ma_method,ap_price,shift)));
  }
  //
  double ND(double A)
  {
   return(NormalizeDouble(A,Digits));
  }
  //
 
PolarSeaman:

Yes, but the above code, in the comment, counts down smoothly every second, while the function is jerky. On M1, out of 60 bars 3 or 4 times there is no alert.

That's because the function uses TimeCurrent() - arrival time of the last quote. You need to replace this time with a local TimeLocal() with a calculated offset (you have already been told about it).

 
Artyom Trishkin:

You need to replace this time with local TimeLocal() with calculated offset (you were already told about this)

I don't know how to do it correctly, so I found a code that counts down time to close H1 without ticks, and I'm trying to use it, I replacedTimeCurrent() in your function, but it doesn't want to show me seconds to close.

#property strict
#property indicator_chart_window
//--- input parameters
#define  MILLISEC_TIMER_INTERVAL         500 
int            timeOffset;
datetime       ServerLocalOffset;
datetime       prevTime,myTime,localtime;
bool           newBar = false;
datetime sec;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   EventSetMillisecondTimer(MILLISEC_TIMER_INTERVAL);
   
  datetime srvtime,tmpOffset;
   RefreshRates();
   srvtime = TimeCurrent();
   // Modified
   localtime = TimeLocal()+TimeGMTOffset();
   if(TimeHour(srvtime)>TimeHour(localtime)){
      // Server Time is still ahead of us
      int newOffset = TimeHour(srvtime)-TimeHour(localtime);
      ServerLocalOffset = (newOffset*60*60);
   }else if(TimeHour(srvtime)<TimeHour(localtime)){
      // Server Time is Behind us
      int newOffset = TimeHour(localtime)-TimeHour(srvtime);
      ServerLocalOffset = (newOffset*60*60);
   }else{
      // No modification required
      ServerLocalOffset = srvtime;
   }
   localtime = TimeLocal()-ServerLocalOffset;
   
   tmpOffset = TimeSeconds(srvtime) - TimeSeconds(localtime);
   if(tmpOffset < 30 && tmpOffset >= 0){
      timeOffset = TimeSeconds(srvtime) - TimeSeconds(localtime);
   }
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
  {
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   datetime localtime;
   localtime = TimeLocal()+(TimeGMTOffset()+(60*60));
 sec=Time[0]+Period()*60-localtime-timeOffset;//
 
 if(SecondsToCandleClose(Symbol(),0)<=2){Alert("время откр. бара ",Time[0]);}
      Comment(" Time 1: ",TimeToStr(sec,TIME_SECONDS )," Time 2: ",TimeToStr(SecondsToCandleClose(Symbol(),0),TIME_SECONDS ));
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
long SecondsToCandleClose(const string symbol_name,const ENUM_TIMEFRAMES timeframe)
  {
   datetime array[];
   return(CopyTime(symbol_name,timeframe,0,1,array)==1 ? PeriodSeconds(timeframe)+array[0]-sec : 0);
  }
////
 
Please advise how to implement the idea in the code:
If warrants were open and now there are none = Alert.

Something like this probably...
double x=0;
If(OrdersTotal >0) {x=1;}
If (OrdersTotal <x) {Alert ;}
x=0;

 
Tigerfreerun:
Please tell me how to implement the idea in the code:
If warrants have opened and now there are no warrants = Alert.

Something in this style probably...
double x=0;
If (OrdersTotal >0) {x=1;}
If (OrdersTotal <x) {Alert ;}
x=0;

If(OrdersTotal ==0) {Alert ;}
 
Alekseu Fedotov:
If (OrdersTotal ==0) {Alert ;}
Then the signal is cyclic. And even if no orders were opened. The idea is that 1) Orders are open 2) now there are no orders 3)1 Alert
 

Guys!

There are a lot of objects on the graph.

But when you access it.

Comment(ObjectsTotal());

It says there's only three.

Why isn't it counting arrows?


 
Tigerfreerun:
Please tell me how to implement the idea in the code:
If warrants were open and now there are none = Alert.

Something like this probably...
double x=0;
If (OrdersTotal >0) {x=1;}
If (OrdersTotal <x) {Alert ;}
x=0;

Do it like this. The code is almost correct. Only one word is missing there:

static double x=0;
If (OrdersTotal >0) {x=1;} 
If (OrdersTotal <x) {Алерт ; x= 0;} 
 
Vladimir Tkach:

Guys!

There are a lot of objects on the graph.

But when you access it.

It says there's only three.

Why isn't it counting arrows?


Maybe because they are Wingdings font symbols