Download MetaTrader 5
To add comments, please log in or register
Did you like the article? Share it with your friends!
pluck6
28
pluck6 2016.08.23 07:33 

Using MQL4, how can I execute a BUY order (seen below) if one variable x has a value of 1 and another variable y has a value of 3? I need it to work like this:

Variable x = 1

Variable y = 3

So if x is MORE THAN y, execute this script:

extern int TakeProfit = 10;
extern int StopLoss = 10;

void OnStart()
  {
   double TakeProfitLevel;
   double StopLossLevel; 

   TakeProfitLevel = Bid + TakeProfit*Point;
   StopLossLevel = Bid - StopLoss*Point;

   Alert("TakeProfitLevel = ", TakeProfitLevel);
   Alert("StopLossLevel = ", StopLossLevel);

   OrderSend("USDCAD", OP_BUY, 1.0, Ask, 10, StopLossLevel, TakeProfitLevel, "first order");
 

 }

And if x is LESS THAN y, execute this SELL script:

extern int TakeProfit = 10;
extern int StopLoss = 10;

void OnStart()
{
double TakeProfitLevel;
double StopLossLevel; 

TakeProfitLevel = Bid + TakeProfit*Point;
StopLossLevel = Bid - StopLoss*Point;

Alert("TakeProfitLevel = ", TakeProfitLevel);
Alert("StopLossLevel = ", StopLossLevel);

OrderSend("USDCAD", OP_SELL, 1.0, Ask, 10, StopLossLevel, TakeProfitLevel, "first order");
 

}

I'm struggling to find the MQL4 code which establishes variables which can then be compared against each other e.g. x > y and vice versa, so any help would be greatly appreciated. 
Keith Watford
Moderator
7990
Keith Watford 2016.08.23 08:15  
if(x>y)
   {
    //Execute Buy order
   }
else 
if(x<y)
   {
    //Execute Sell order
   }

.

Keith Watford
Moderator
7990
Keith Watford 2016.08.23 10:36  
pluck6:

 but how do I establish the variables? Would it be something like this:

var x = 1;

var y = 3;


That wouldn't make sense. There would have to be some conditions that would assign a value to the variables.

Finally, should I wrap this whole code within void OnStart() {} ?

Either that or in a function.
Fernando Carreiro
1528
Fernando Carreiro 2016.08.23 11:08  
pluck6: I'm struggling to find the MQL4 code which establishes variables which can then be compared against each other e.g. x > y and vice versa, so any help would be greatly appreciated. 
  • Before programing in MQL4, I suggest you first familiarise yourself with code in general such as in a language called "C".
  • After that, read through the "MQL4 Book/Tutorial" section on this site, which will introduce you to all the basic things you first need to learn, such as how to declare variables, etc.
  • When in doubt, and before posting here, always consult the Documentation, whether that be the Online Version on via the Help (F1) in the Editor/Compiler.
whroeder1
13644
whroeder1 2016.08.23 17:02  
learn to code it, or pay someone. We're not going to code it FOR you.
We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
/
To add comments, please log in or register