How do I store the price?

 
MqlTick Ticks[];

void OnTick()
  {
   double Open1 = iOpen(Symbol(),PERIOD_CURRENT,1);
   double Close1 = iClose(Symbol(),PERIOD_CURRENT,1);
   double Open2 = iOpen(Symbol(),PERIOD_CURRENT,2);
   double Close2 = iClose(Symbol(),PERIOD_CURRENT,2);

   if(Open2 > Close2 && Close1 > Open1)
     {
      if(Close2 > Open1)
        {
         double Lowerlow = Close2
        }

      else
         if(Open1 > Close2)
           {
            double Lowerlow = Close2;
           }
     }
  }

Hi guys, may I ask after ea find Lowerlow, how to write to store the price?

Because it will always produce new ticks, which is also means that it will always form new Lowerlow,

What I want to do is store the price when it forms the first Lowerlow, But I've been thinking about it for a long time and can't figure out how to write it, I need some help.

 
ziyang2048:

Hi guys, may I ask after ea find Lowerlow, how to write to store the price?

Because it will always produce new ticks, which is also means that it will always form new Lowerlow,

What I want to do is store the price when it forms the first Lowerlow, But I've been thinking about it for a long time and can't figure out how to write it, I need some help.

Not sure if I understand fully, but I think you want the value of LowerLow to persist across ticks.

The way you have defined it, the scope is limited within the if and else/if sections. The simplest change you could make is to make it global, assuming that is what you want

MqlTick Ticks[];
double Lowerlow = 0; //make global

void OnTick()
  {
   double Open1 = iOpen(Symbol(),PERIOD_CURRENT,1);
   double Close1 = iClose(Symbol(),PERIOD_CURRENT,1);
   double Open2 = iOpen(Symbol(),PERIOD_CURRENT,2);
   double Close2 = iClose(Symbol(),PERIOD_CURRENT,2);

   if(Open2 > Close2 && Close1 > Open1)
     {
      if(Close2 > Open1)
        {
         Lowerlow = Close2; //use global
        }

      else
         if(Open1 > Close2)
           {
            Lowerlow = Close2; //use global
           }
     }
  }
 
R4tna C #:

Not sure if I understand fully, but I think you want the value of LowerLow to persist across ticks.

The way you have defined it, the scope is limited within the if and else/if sections. The simplest change you could make is to make it global, assuming that is what you want

hi R4tna, so first thing I wanna say sorry to my bad english,

I'm not a native speaker, I think u misunderstand what I mean, 

what I want is I want to store the first Lowerlow price, then stop storing it,

because if I let ea keep running, then the price and ticks will goes different,

so it might have a new Lowerlow, and the price will change to the newest Lowerlow price.


I've already try my best to explain, but not sure u understand or not.

 
ziyang2048 #:

hi R4tna, so first thing I wanna say sorry to my bad english,

I'm not a native speaker, I think u misunderstand what I mean, 

what I want is I want to store the first Lowerlow price, then stop storing it,

because if I let ea keep running, then the price and ticks will goes different,

so it might have a new Lowerlow, and the price will change to the newest Lowerlow price.


I've already try my best to explain, but not sure u understand or not.

Declare the low variable and assign a value to it. Then check if its value is equal to the assigned value, then store the low price in it.
 
Using a global is a good hint.

To make a variable store only lower lows, you need to initialize it with a higher value, like DBL_MAX.

Then use this to only update, when the new value is lower:

Lowerlow = (Lowerlow > Close2) ? Close2 : Lowerlow;
 
ziyang2048 #:

what I want is I want to store the first Lowerlow price, then stop storing it,

When you say "stop storing it" I guess you mean keep the first assigned value and never change it again?

 
_MAHA_ #
声明低变量并为其赋值。 然后检查它的值是否等于分配的值,然后将低价
Dominik Christian Egert #:
Using a global is a good hint.

To make a variable store only lower lows, you need to initialize it with a higher value, like DBL_MAX.

Then use this to only update, when the new value is lower:

I will try to do it, thanks.

 
R4tna C #:

When you say "stop storing it" I guess you mean keep the first assigned value and never change it again?

That's what I mean.

 
ziyang2048 #:

That's what I mean.

Ok you could try something like this - initialize with a negative value, and stop changing it once it is positive (assigned)

Just an example - adjust as needed

//+------------------------------------------------------------------+
//|                                            431185-StorePrice.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
MqlTick Ticks[];
double Lowerlow = -1; //make global

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   double Open1 = iOpen(Symbol(),PERIOD_CURRENT,1);
   double Close1 = iClose(Symbol(),PERIOD_CURRENT,1);
   double Open2 = iOpen(Symbol(),PERIOD_CURRENT,2);
   double Close2 = iClose(Symbol(),PERIOD_CURRENT,2);

   if((Open2 > Close2 && Close1 > Open1) && (Lowerlow < 0))
     {
      if(Close2 > Open1)
        {
         Lowerlow = Close2; //use global
        }

      else
         if(Open1 > Close2)
           {
            Lowerlow = Close2; //use global
           }
     }
  }
//+------------------------------------------------------------------+
 
R4tna C #:

Ok you could try something like this - initialize with a negative value, and stop changing it once it is positive (assigned)

Just an example - adjust as needed

ok I will try it, thanks.

 
ziyang2048: Hi guys, may I ask after ea find Lowerlow, how to write to store the price? Because it will always produce new ticks, which is also means that it will always form new Lowerlow, What I want to do is store the price when it forms the first Lowerlow, But I've been thinking about it for a long time and can't figure out how to write it, I need some help.

You are running that code on every tick — why? The prices quotes for previous bars will not change until the current bar is closed and a new on is formed. Checking them on every tick is redundant.

So, detect when a new bar is formed and update and check your values only then, and if needed, then store those values, either in globally scoped variables or in static variables.

Detecting the start of a new bar or candle
Detecting the start of a new bar or candle
  • www.mql5.com
Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.
Reason: