Retard the alert several seconds - page 2

 
OnTimer()
 
Thanks eevviill, but my skills are too complicated. I'm starting out with programming, they are self-taught and it's hard for me to understand some things.
Thanks for everything, Massimo.
 
omissamf:
Thanks eevviill, but my skills are too complicated. I'm starting out with programming, they are self-taught and it's hard for me to understand some things.
Thanks for everything, Massimo.
int seconds=3;

int OnInit()
  {  
   EventSetTimer(seconds);
 
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }
//_____________________________________________
//_____________________________________________
int start()
{
return(0);
}

void OnTimer(){
     
      //Indicator Buffer 1
      if(iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, 0) < 30
      
      )
        {
         Buffer1[0] = Low[0] - iATR(NULL, PERIOD_CURRENT, 14, 0); //Set indicator value at Candlestick Low - Average True Range
         if(0 == 0 && Time[0] != time_alert) { myAlert("indicator", "Buy"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer1[0] = 0;
        }
      //Indicator Buffer 2
      if(iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, 0) > 70
     
      )
        {
         Buffer2[0] = High[0] + iATR(NULL, PERIOD_CURRENT, 14, 0); //Set indicator value at Candlestick High + Average True Range
         if(0 == 0 && Time[0] != time_alert) { myAlert("indicator", "Sell"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer2[0] = 0;
        }
     }
  
  }
 
Thanks eevviill, your code works, but I'm not from the signal after three seconds that opened the candle, but if, for example, RSI is above level 70 after 30 seconds that the candle is open, the code lets 3/2 yet and then brings up the arrow.
For now the code that comes closest is the one who suggested I GumRai, but gives me the signal and sets the opening of the candle and not after 3 seconds.

Thanks for everything, Massimo.

int start(){
if (BarStart !=Time[0]) 
{
  BarStart = Time[0]; 
//This is the code suggested by GumRai. This works well, but the arrow appears and fixed the opening of the candle, instead of 3 seconds after its opening.
 

but I'm not from the signal after three seconds that opened the candle

Better to write it first and not "I need every 3 seconds alert." 

 
extern int seconds=3;
int time_dif;
bool current_candle_alert_been;
int prev_bars;

int OnInit()
  { 
prev_bars=Bars;
 
   EventSetTimer(1);
   time_dif=int(TimeLocal()-TimeCurrent());
   ...


void OnTimer()
{
if(Bars!=prev_bars) current_candle_alert_been=false;
prev_bars=Bars;

if(current_candle_alert_been) return;
if(TimeLocal()-time_dif<Time[0]+seconds) return;
current_candle_alert_been=true;



...
 
   static datetime BarStart=0;
   static bool check=false;
   if(BarStart!=Time[0])
     {
      BarStart=Time[0];
      check=true;
     }
   if(check && TimeCurrent()>=Time[0]+3)
     {
      check=false;
      //Check Condition
     }

This should check at the first received tick that is at least 3 seconds after the bar open time

Note that if the bar has been open n seconds when the indicator is initialized and n is >3, it will be checked at that time. Also, obviously, it will not work with historical bars.

 
GumRai:

This should check at the first received tick that is at least 3 seconds after the bar open time

Note that if the bar has been open n seconds when the indicator is initialized and n is >3, it will be checked at that time. Also, obviously, it will not work with historical bars.

:))))

1)I would like to modify this code so that the alert, instead of appearing at the opening of the candle, detects the conditions after a few seconds. 

 2)but I'm not from the signal after three seconds that opened the candle

 

 

P.S. Do not use Time[0], use Bars 

 
eevviill:

:))))

1)I would like to modify this code so that the alert, instead of appearing at the opening of the candle, detects the conditions after a few seconds. 

 2)but I'm not from the signal after three seconds that opened the candle


What is your point?

eevviill:

:))))


P.S. Do not use Time[0], use Bars 

Why not? There is nothing wrong with using Time[0] to detect a new bar.

 
Hi guys, you are great !!!
I do not know how to thank you for your cooperation.
The code posted eevviill works very well, that's what I meant.
The GumRai code instead makes repaint and give the signal whenever the RSI crosses the 30/70 levels.
I insert the working code according to eevviill suggestions, hoping that it will serve some more.

Thanks for everything, Massimo.


//+------------------------------------------------------------------+
//|                                                   Test Delay.mq4 |
//|                                                          Massimo |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Massimo"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property description ""

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"


extern int seconds =3;
int time_dif;
bool current_candle_alert_been;
int prev_bars;



datetime time_alert; //used when sending alert
//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern int Period1 = 2;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | Test @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   prev_bars=Bars;
    EventSetTimer(1);
   time_dif=int(TimeLocal()-TimeCurrent());
   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }
//________________________________
 
int start()
{
return(0);
}

//_________________________________

void OnTimer(){
{
if(Bars!=prev_bars) current_candle_alert_been=false;
prev_bars=Bars;

if(current_candle_alert_been) return;
if(TimeLocal()-time_dif<Time[0]+seconds) return;
current_candle_alert_been=true;
     
      //Indicator Buffer 1
      if(iRSI(NULL, PERIOD_CURRENT, Period1, PRICE_CLOSE, 0) < 30
      
      )
        {
         Buffer1[0] = Low[0] - iATR(NULL, PERIOD_CURRENT, 14, 0); //Set indicator value at Candlestick Low - Average True Range
         if(0 == 0 && Time[0] != time_alert) { myAlert("indicator", "Buy"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer1[0] = 0;
        }
      //Indicator Buffer 2
      if(iRSI(NULL, PERIOD_CURRENT, Period1, PRICE_CLOSE, 0) > 70
     
      )
        {
         Buffer2[0] = High[0] + iATR(NULL, PERIOD_CURRENT, 14, 0); //Set indicator value at Candlestick High + Average True Range
         if(0 == 0 && Time[0] != time_alert) { myAlert("indicator", "Sell"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer2[0] = 0;
        }
     }
     }
 //-----------------------------------------------------------------------------------------------------------------------------