How to save variable outside of if statement

 

I do not know how to save a variable outside of my if statement in my code:
the variables I wish to save are   

double buyzone;

 double buysl;
 double buytp;
double sellzone;
double  sellsl;

double  selltp;

Code is:

void OnTick()
  {
//---
// Ask & Bid
double Ask= NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK) , _Digits);
double Bid= NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID) , _Digits);


// Candlesticks
 
   double buyzone;
 double buysl;
 double buytp;
double sellzone;
double  sellsl;
double  selltp;

// Price Array
MqlRates PriceInfo[];
ArraySetAsSeries(PriceInfo,true);
int PriceData=CopyRates(_Symbol,_Period,0,7,PriceInfo);


//type 2
if(Ask==PriceInfo[1].high){

  buyzone= PriceInfo[1].high;
buysl= PriceInfo[3].low;
 buytp= Ask+PriceInfo[1].high-PriceInfo[3].low;
//--------------


if(Bid==PriceInfo[1].low){

 sellzone= PriceInfo[1].low;
  sellsl= PriceInfo[3].high;
 selltp= Bid-(PriceInfo[3].high-PriceInfo[1].low);

//--------------


if(PriceInfo[1].close>buyzone && PriceInfo[1].open < buyzone){
if(PriceInfo[0].open==Ask)
trade.Buy(0.01,NULL,Ask,buysl,buytp,NULL);
}
if(PriceInfo[1].close<sellzone && PriceInfo[1].open > sellzone){
if(PriceInfo[0].open==Bid)
trade.Sell(0.01,NULL,Bid,sellsl,selltp,NULL);
}


}
 
micobez:

I do not know how to save a variable outside of my if statement in my code:
the variables I wish to save are   

double buyzone;

 double buysl;
 double buytp;
double sellzone;
double  sellsl;

double  selltp;

Code is:

You are declaring them before any other code, so the values are saved and available outside of the if's until OnTick() returns.

If you want to access the values in subsequent ticks declare them as static variables.

 
if(Ask==PriceInfo[1].high){
Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum
 
Keith Watford:

You are declaring them before any other code, so the values are saved and available outside of the if's until OnTick() returns.

If you want to access the values in subsequent ticks declare them as static variables.

the code doesnt work if I do static double buyzone

I dont know what I am meant to do 

 
micobez:

the code doesnt work if I do static double buyzone

I dont know what I am meant to do 

First, do not equate doubles. 

Second, don't die with a single code. Just explain to us what you are trying to do, then maybe we can advice on the best approach to use.

 
Nelson Wanyama:

First, do not equate doubles. 

Second, don't die with a single code. Just explain to us what you are trying to do, then maybe we can advice on the best approach to use.

I am trying to save the low value of a candle and when another candle crosses over it completely it will initiate a trade. Something like


candle 1 is doji 

next candle is doji therefore save the lowest of candle 2

 
micobez: I am trying to save the low value of a candle and when another candle crosses over it completely it will initiate a trade. Something like

candle 1 is doji next candle is doji therefore save the lowest of candle 2

  1. Unless you can define your condition concretely, it can't be coded. E.g. define Doji
    double body = MathAbs(C-O), range = H-L;
    bool doji = body < 10/100.*range;
  2. What does a Doji have to do with your "trying to save … when …?"

  3. Where do you define "crosses over it completely?" Where do you save the low? Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21
 
William Roeder:
  1. Unless you can define your condition concretely, it can't be coded. E.g. define Doji
  2. What does a Doji have to do with your "trying to save … when …?"

  3. Where do you define "crosses over it completely?" Where do you save the low? Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

I am simply trying to save the low value of a candle outside an if statement

When I type static int saveval=PriceInfo[1].low I get an error

 
micobez:

I am simply trying to save the low value of a candle outside an if statement

When I type static int saveval=PriceInfo[1].low I get an error

 static int saveval=PriceInfo[1].low

You must initialize a static variable with a constant.

Also PriceInfo sounds like it would probably be a double, so why are you trying to store it in an integer variable.

 
You initialize a static variable only with a constant. You assign to it whenever you wish.
 
micobez:

I am simply trying to save the low value of a candle outside an if statement

When I type static int saveval=PriceInfo[1].low I get an error

My suggestion would be to do the following:

//at the top of your program, where you define global variables
double saveval = -1.0;

//in the OnItick() function:

void OnTick()
{
        //collect the data you need.
        //then assign value to saveval, but only if it is a valid value:
        if(PriceInfo[1].low > 0) saveval = PriceInfo[1].low;

        //by the time you want to use saveval:
        if (saveval > 0)
        {
        //insert here the code which uses saveval
        }
}

I assume that the value you will store in saveval will always be a positive value. I initialize saveval with a negative value so that I can check on the first run that it has received a valid value from PriceInfo[1].low This may help to avoid your program using saveval with an invalid value. For example if an error occurred and no prices have been received.