How do I save a specfic number in specfic time in mql5?

 

Hi, I have some experience in pinescript and other languages but new to mql5. I want to save a number in US session open. But I have some problems.

double test = 0;
if(time.hour == usOpenHour && time.min == usOpenMin){
         test = 1;
}
Comment("\n session max : " + test);

The problem is at US 9:30 am, test becomes 1, but after 9:30 am, test becomes 0 again. I want test to be 1 after 9:30.

I can save the number in pinescript by test := test[1].

How do I save the number in mql5?

Thanks in advanced

Improperly formatted code edited by moderator. Please use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
openformrm:

I can save the number in pinescript by test := test[1].

How do I save the number in mql5?

In your posted MQL5 code, you're merely declaring a double type variable.

What you described in Pinescript is an array in MQL5:  MQL5 Programming Basics: Arrays - MQL5 Articles

By the way, it would probably be more sensible to use an int type array for your purposes.

MQL5 Programming Basics: Arrays
MQL5 Programming Basics: Arrays
  • www.mql5.com
Arrays are an integral part of almost any programming language along with variables and functions. The article should be of interest primarily to novice MQL5 programmers, while experienced programmers will have a good opportunity to summarize and systematize their knowledge.
 
openformrm:

Hi, I have some experience in pinescript and other languages but new to mql5. I want to save a number in US session open. But I have some problems.

The problem is at US 9:30 am, test becomes 1, but after 9:30 am, test becomes 0 again. I want test to be 1 after 9:30.

I can save the number in pinescript by test := test[1].

How do I save the number in mql5?

Thanks in advanced

Improperly formatted code edited by moderator. Please use the CODE button (Alt-S) when inserting code.

Hi

Try this : (place in indicators , i assume you want a value carried for "checks" until it is time to update it again)-the normal flow part is empty , you gotta add some stuff there too when a new bar forms

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot value
#property indicator_label1  "value"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
input int ServerTimezone_HourOfInterest=12;//hour of interest(server timezone)
input int ServerTimezone_MinuteOfInterest=00;//minute of interest(server timezone)
//--- indicator buffers
double         valueBuffer[];//we store the value here whenever we encounter the time you mentioned
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,valueBuffer,INDICATOR_DATA);//this "tethers" the array to data when more data comes it autoscales BUT you have to calculate the new value
   //we don't draw when the value of the above array is zero
     PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
  //reset call
    if(prev_calculated==0){//if the indicator "controller" tells you theres new data but not just a new bar
      ArrayFill(valueBuffer,0,ArraySize(valueBuffer),0.0);//dump all values held
    //loop from the past till the future , only in fully formed bars
      for(int i=0;i<rates_total-1;i++){//last accessed is rates_total-2
         //propagate the previous value , carry
           if(i>0){valueBuffer[i]=valueBuffer[i-1];}
         //the time of this bar is held in time[i]
           int thisHour=0,thisMinute=0,prevHour=0,prevMinute=0;
           if(getTimeHourAndMinutes(time[i],thisHour,thisMinute)){
             //if smack dab on the time of interest
               if(thisHour==ServerTimezone_HourOfInterest&&thisMinute==ServerTimezone_MinuteOfInterest){
                 //get the value - the high for this test
                   valueBuffer[i]=high[i];
                 }
               else{
                 //get the previous bar time
                   if(i>0&&getTimeHourAndMinutes(time[i-1],prevHour,prevMinute)){
                   int thisMinutes=thisHour*60+thisMinute;
                   int prevMinutes=prevHour*60+prevMinute;
                   int ourMinutes=ServerTimezone_HourOfInterest*60+ServerTimezone_MinuteOfInterest;
                   if(prevMinutes<ourMinutes&&thisMinutes>ourMinutes){
                     valueBuffer[i]=high[i];
                     }
                   }
                 }
             }
         }
    }else{
    //normal flow
    }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

bool getTimeHourAndMinutes(datetime time,int &hour,int &minutes){
MqlDateTime mqt;
if(TimeToStruct(time,mqt)){
  hour=mqt.hour;
  minutes=mqt.min;
  return(true);
  }
return(false);
}
 
Thanks