Search code examples
tradingalgorithmic-tradingmql4metatrader4forex

MQL4: Execute function if x is more than y


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.


Solution

  • So, on the variables:

    MQL4 used to be a compiled, static typed language.

    So the source-code contains all prior declarations, so as to allow the compiler to assume, what the heck the variable has as it's type ( and internal representation ).

    int             ii = 0;
    double       coeff = 1.23456789;
    color  anMQL_color = 0x224466;             // could be stated as {int|hex|literals||colornames}
    datetime  aTimeNOW = D'2016.08.23';
    string    aLastMSG = "[ALARM] This TracePoint shall never be executed";
    

    Recent MQL4 language redesigns into a New-MQL4.56789 have brought both a few new types ( i.e. struct-s )

    The basic data types are:

    ·integers (char, short, int, long, uchar, ushort, uint, ulong);

    ·logical (bool);

    ·literals (ushort);

    ·strings (string); ( but are struct internally (!!), so be carefull in DLL API )

    ·floating-point numbers (double, float);

    ·color (color);

    ·date and time (datetime);

    ·enumerations (enum).

    Complex data types are:

    ·struct;

    ·class-es.

    New language has also introduced a type casting, like this one:

    int aFactoredNUMBER = EMPTY;                             // declaration + initial value assignment
        aFactoredNUMBER = (int) ( coeff * 3.1412592653598 ); // operation with a resulting value type casting into (int)
    

    So how about the x > y ?

    let me sketch a few SLOCs:

    double x = 1,
           y = 3;
    ...
    ..
    .
    
    if (  x > y ) { ... ;
                    OrderSend( , OP_BUY,  ... );
                    return;
                    }
    
    if (  x < y ) { ... ;                            // THIS
                    OrderSend( , OP_SELL, ... );     // COULD BE A CALL TO FUN( { OP_BUY | OP_SELL } )
                    return;
                    }