How can I extract variable value outside if statement

 

Hi all,
I am a newbie in coding and might be asking a very silly question. I want to store Open price of the first order in a variable. there might be multiple orders running simultaneously. I need to store this data so that I can set stoploss/Take profits for upcoming pending orders from this price.

I am trying something like this. But out side the if statement it doesn't seem to work.

void OnStart()
  
//---
  {
  
 double firstopenprice=0;
    
  if(OrderSelect(0, SELECT_BY_POS,MODE_TRADES)==true)
    
      {
      double firstopenprice= OrderOpenPrice();
      
      Alert ("This is first one ",firstopenprice);
      }
  else {Alert("There is still no first open trade");}
  
  
   if(OrderSelect(1, SELECT_BY_POS,MODE_TRADES)==true)
    
      {Alert ("This is second one ",OrderOpenPrice());
      
      }
  else {Alert("There is still no first open trade");}
  
  
  Alert ("Now we are checking ", firstopenprice);
  
 
  }

Thanks so much in advance!

 
MD Hafijul: I want to store Open price of the first order in a variable.
      double firstopenprice= OrderOpenPrice();

Don't declare it inside the if.

 
William Roeder:

Don't declare it inside the if.

MD Hafijul:

Hi all,
I am a newbie in coding and might be asking a very silly question. I want to store Open price of the first order in a variable. there might be multiple orders running simultaneously. I need to store this data so that I can set stoploss/Take profits for upcoming pending orders from this price.

I am trying something like this. But out side the if statement it doesn't seem to work.

William Roeder:

Don't declare it inside the if.


Hey, thanks for your reply. 
It works till I have only one open order. if I have another order open and for some reason I select that order then the value changes to the second open price's value.