Ritardare l'allarme di alcuni secondi - pagina 2

 
OnTimer()
 
Grazie eevviill, ma le mie competenze sono troppo complicate. Sto iniziando con la programmazione, sono autodidatta ed è difficile per me capire alcune cose.
Grazie di tutto, Massimo.
 
omissamf:
Grazie eevviill, ma le mie competenze sono troppo complicate. Sto iniziando con la programmazione, sono autodidatta ed è difficile per me capire alcune cose.
Grazie di tutto, 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;
        }
     }
  
  }
 
Grazie eevviill, il tuo codice funziona, ma non mi da il segnale dopo tre secondi che ha aperto la candela, ma se, per esempio, RSI è sopra il livello 70 dopo 30 secondi che la candela è aperta, il codice lascia ancora 3/2 e poi fa salire la freccia.
Per ora il codice che si avvicina di più è quello che mi ha suggerito GumRai, ma mi dà il segnale e imposta l' apertura della candela e non dopo 3 secondi
.

Grazie di tutto, 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.
 

ma nonsono dalsegnale dopotre secondiche ha apertola candela

Meglio scriverlo prima e non "ho bisogno di un allarme ogni 3 secondi".

 
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
     }

Questo dovrebbe controllare al primo tick ricevuto che è almeno 3 secondi dopo il tempo di apertura della barra

Si noti che se la barra è stata aperta n secondi quando l'indicatore è inizializzato e n è >3, sarà controllato in quel momento. Inoltre, ovviamente, non funzionerà con le barre storiche.

 
GumRai:

Questo dovrebbe controllare al primo tick ricevuto che è almeno 3 secondi dopo il tempo di apertura della barra

Notate che se la barra è stata aperta n secondi quando l'indicatore è inizializzato e n è >3, sarà controllata in quel momento. Inoltre, ovviamente, non funzionerà con le barre storiche.

:))))

1)Vorrei modificare questo codice in modo che l'allarme, invece di apparire all'apertura della candela, rilevi le condizioni dopo alcuni secondi.

2)ma nonsono dalsegnale dopotre secondiche ha aperto lacandela

P.S. Non usare Time[0], usa Bars

 
eevviill:

:))))

1)Vorrei modificare questo codice in modo che l'allarme, invece di apparire all'apertura della candela, rilevi le condizioni dopo alcuni secondi.

2)ma nonsono dalsegnale dopotre secondiche ha aperto lacandela


Qual è il tuo punto?

eevviill:

:))))


P.S. Non usare Time[0], usa Bars

Perché no? Non c'è niente di sbagliato nell'usare Time[0] per rilevare una nuova barra.

 
Ciao ragazzi, siete grandi !!!
Non so come ringraziarvi per la vostra collaborazione.
Il codice postato eevviill funziona molto bene, è quello che intendevo.
Il codice GumRai invece fa repaint e il segnale ogni volta che l' RSI attraversa i livelli 30/70.
Inserisco il codice funzionante secondo i suggerimenti di eevviill, sperando che serva ancora a qualcosa.

Grazie di tutto, 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;
        }
     }
     }
 //-----------------------------------------------------------------------------------------------------------------------------