recording a "moment in time"

 

Hello forum,

I have a feeling this may be quite basic but am not sure how to proceed, my goal is to code the following simple logic: 

"if some [condition 1] is met, then [create a record], ONCE, and keep it for reference till [condition 2]".

For example, I want to create a record of "bid" level at the moment when, say, MAIN STOCHASTIC line in Bar 0 goes above 80 for the first time. I want this "bid" record to be preserved unchanged, for other uses, untill another set of conditions is met (say, until Stochastic oscilator falls below 50 in Bar 0) when the  "bid" record will either be deleted or replaced with a new [record]...

I think this will work with global variables (these are stored in permanent memory if I recall correctly?) - but if I set a global variable under Start function when [conditions] are met, then:

1) How do I prevent the record being updated with each tick, instead of ONCE when conditions first met?

2) How do I avoid deleting it accidentally, say upon restaring the terminal, updating the EA, etc?

 

Many thanks

Dan 

 

Is pretty easy whether it's MetaTrader Global Variable (https://docs.mql4.com/globals) or globally declared variable,

double My_Value = 0.0; //--- This is globally declared variable.

//--- condition 1
if (My_Value == 0.0) My_Value = Bid;

//--- condition 2
if (My_Value != 0.0) My_Value = 0.0;

Go figure :D

 

[scratching head]

yep, it is easy when you know it.. : D .. I now realize there's actually a distinction between the two types of "global" variables.. I will try it out once market opens on Monday.

Just to clarify.. so this should be, on global scope of the EA:


double My_Value = 0.0; 

if { Condition 1
      {if (My_Value == 0.0) My_Value = Bid;}  // --> unless condition 2 below happens, this will freeze the value unchanged for a few weeks, until automatically deleted, correct?
   }
if  { Condition 2
      {if (My_Value != 0.0) My_Value = 0.0;}  // --> does this mean "set My_Value to zero" unless already zeroed? right?
   }
if  { Condition 3
      {if (My_Value = 0.0) My_Value = Bid;}  // --> this will record a new value, right, until otherwise changed or deleted,etc.?
   }



int start () { }
int other_functions () {} etc..

 
Dannoo007:
1) How do I prevent the record being updated with each tick, instead of ONCE when conditions first met?
2) How do I avoid deleting it accidentally, say upon restaring the terminal, updating the EA, etc?
  1. bool isRecordValid = false;
    void Analyze(int iBar){
       static bool isCondition1; bool wasCondition1 = isCondition1; isCondition1 = ..;
       static bool isCondition2; bool wasCondition2 = isCondition2; isCondition2 = ..;
       if (isCondition1 && !wasCondition1){ CreateRecord(); isRecordValid=true; }
       if (isCondition2 && !wasCondition2)  isRecordValid = false; 

  2. Either you use permanent storage (write a disk file on each update, read in on init) or recreate all conditions on startup.
    int nAnalyzed;
    int init(){ nAnalyzed = 0; ..}
    int start(){
       for(iBar = Bars - 1 - nAnalyzed; iBar >=0; iBar--) Analyze(iBar);
       nAnalyzed = Bars - 1; // ReAnalyze bar zero next tick.
       :

 
Dannoo007:

[scratching head]

yep, it is easy when you know it.. : D .. I now realize there's actually a distinction between the two types of "global" variables.. I will try it out once market opens on Monday.

Just to clarify.. so this should be, on global scope of the EA:

Make sure that My_Value variable is declared globally - outside start(), init(), deinit(), funtions()

If you want to keep the value for at least 4 weeks,  let's use MT Global Variable as well (https://docs.mql4.com/globals).

My_Value = GlobalVariableGet ("De_Value");  //--- this will retrieve the value from MT GV called "De_Value"

GlobalVariableSet ("De_Value", My_Value);   //--- this will set new value to MT GV called "De_Value"

// --> unless condition 2 below happens, this will freeze the value unchanged for a few weeks, until automatically deleted, correct? ==>> correct  

if ( Condition 1 ) 
    {
    if (My_Value == 0.0) 
       {
       My_Value = Bid;                              
       GlobalVariableSet ("De_Value", My_Value); //--- immediately set new value to MT GV, just in case MT suddenly stop running, and it will keep for weeks like you said
       }
    } 

 // --> does this mean "set My_Value to zero" unless already zeroed? right? ==>> right or set new value

//--- set new value to zero
if  ( Condition 2 )
   {
   if (My_Value != 0.0) 
      {
      My_Value = 0.0;
      GlobalVariableSet ("De_Value", My_Value);
      }
   }

//--- or set to a new one - if you like
if  ( Condition 2 )
   {
   if (My_Value != 0.0) 
      {
      My_Value = Ask - Point;
      GlobalVariableSet ("De_Value", My_Value);
      }
   }

 // --> this will record a new value, right, until otherwise changed or deleted,etc.? ==>> right 

if ( Condition 3 ) 
    {
    if (My_Value == 0.0) 
       {
       My_Value = Bid;
       GlobalVariableSet ("De_Value", My_Value);

       }
    } 
 
You can NOT use GlobalVariableSet for persistent storage. If the OS reboots, power fail, etc, the updated global variables will not be written to disk. ONLY an orderly shutdown of the terminal wil.
Reason: