Help, about basic variable question.

 

Hi, I have a basic question.

   if(a==b)
     {
      my_price == ask;
     }

// ask variable was defined earlier and it gives instant ask value of parity.

   if(c==d)
     {
      trade.Buy(......,my_price......);
     }


// I want to use my_price variable in this if fucntion. But of course I cant use it because my_price variable is not global variable. My question is how can I make my_price variable global in first if function. Is there any way to do that ?

 

You need to declare the variable at the global program level (in the "header" of the advisor)

//+------------------------------------------------------------------+
//|                                                     Expert 1.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//--- input parameters
input int      Input1=9;
//---
double   my_price;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
 
 if(a==b)
     {
      my_price == ask;
     }

I think that you mean

 if(a==b)
     {
      my_price = ask;
     }
if(c==d)
     {
      trade.Buy(......,my_price......);
     }

Why do you need the variable my_price? Why not just use ask?

if(c==d)
     {
      trade.Buy(......,ask......);
     }
 
Keith Watford:

I think that you mean

Why do you need the variable my_price? Why not just use ask?

Because ask value is always changing. I want trade with my_price variable which is the value of when necessery conditions are met.
 
ack234:
Because ask value is always changing. I want trade with my_price variable which is the value of when necessery conditions are met.

Yes, ask is always changing, but if you are placing a buy trade, the entry price has to be the current ask or you will get invalid price errors.

Reason: