MQL4:如果x大于y,则执行函数

时间:2016-08-22 21:38:16

标签: trading algorithmic-trading mql4 metatrader4 forex

使用MQL4,如果一个变量 BUY x 订单(如下所示) >的值为1,另一个变量 y 的值为3?

我需要它像这样工作:

变量x = 1

变量y = 3

因此,如果xMORE THAN y,请执行以下脚本:

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");

 }

如果xLESS THAN y,请执行此SELL脚本:

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");

}

我努力寻找建立变量的MQL4代码,然后可以将这些变量相互比较,例如x > y,反之亦然,所以任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

所以,关于变量:

MQL4曾经是一种编译的静态类型语言。

因此源代码包含所有先前的声明,以便允许编译器假设变量具有什么样的类型(以及内部表示)。

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";

最新MQL4语言重新设计为 - MQL4.56789带来了一些新类型(即struct - s)

基本数据类型为:

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

·逻辑(bool);

·文字(ushort);

·字符串(string); (但struct内部(!!),因此请在DLL API中小心)

·浮点数(double, float);

·color(color);

·日期和时间(datetime);

·枚举(enum)。

复杂的数据类型是:

·struct;

·class - 。ES

新语言也引入了类型转换,如下所示:

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

那么x > y

怎么样?

让我勾勒出一些SLOC:

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;
                }