【问题标题】:MQL4: Execute function if x is more than yMQL4:如果 x 大于 y,则执行函数
【发布时间】:2016-12-29 13:42:25
【问题描述】:

使用MQL4,如果一个变量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 反之亦然,因此我们将不胜感激。

【问题讨论】:

    标签: trading algorithmic-trading mql4 metatrader4 forex


    【解决方案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);

    ·日期和时间 (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;
                    }
    

    【讨论】:

    • 感谢您的回答,这正是我想要做的。您认为可以设置变量 y 来阅读经济新闻公告吗?我知道这完全是另一个话题,但这可能吗?我想手动输入变量 x 作为经济公告预测,然后将其与获取实际数字的变量 y 进行比较。我知道延迟等会引起问题,但是仍然可以实现吗?
    • 不客气,幸运儿。 随时问更多,MQL4 相关与否,问题,这就是 StackOverflow 的习惯。
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 2021-06-19
    • 1970-01-01
    • 2012-12-23
    • 2022-01-13
    相关资源
    最近更新 更多