1

我是 -code 世界的新手MQL4

我来自一个C++背景,我正在努力学习MQL4语言和惯例。

我正在编写一个简单的智能交易系统(我的第一个)。
它可以编译,但是当我尝试测试它时,它没有结果。我附上代码以更好地说明我正在尝试做的事情:

//+------------------------------------------------------------------+
//|                                                MyFirstExpert.mq4 |
//|                                                  Leonardo        |
//|                            http://investinmarkets.altervista.org |
//+------------------------------------------------------------------+
#property copyright "Leonardo "
#property link      "http://investinmarkets.altervista.org"
#property version   "1.00"
#property strict
input int BarCount = 3;
      int Ticket   = 0;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() { 
     int    BarCountTemp = BarCount + 1;
     double bars[];
     ArrayResize( bars, BarCountTemp );
     for ( int i = 0; i < BarCountTemp; i++ ) {
           bars[i] = Close[i + 1];
     }
     int   i = 0;
     bool  is_p;
     do
     {
        if ( bars[i] > bars[i+1] && i < BarCountTemp ) is_p = true;
        else is_p = false;
        i++;
     }
     while ( is_p );

     if ( is_p == true && Ticket == 0 ) {
          Ticket = OrderSend(_Symbol,OP_SELL,0.1,Bid,0,0,0,"Sell Order Custom",110);
          Alert("Sell order opened to match found.");
          Comment("Sell order opened #"+Ticket+".");
     }
     if ( Ticket != 0 ) {
          bool select = OrderSelect(Ticket,SELECT_BY_TICKET);
          if ( Close[1] > Close[2] ) {
               bool close = OrderClose(Ticket,OrderLots(),Ask,0,clrGreen);
               Alert("Sell order closed.");
               Comment("Sell order closed #"+Ticket+".");
               Ticket = 0;
          }
     }
  }
//+------------------------------------------------------------------+

我想简单地计算柱数(由用户输入)然后执行检查:如果例如 3 个柱都是正数,则打开一个卖单(目前只是这种情况)。如果打开,则检查下一个柱是否仍然为正,如果不是则关闭交易。

我总是得到空白的结果。

先感谢您!

4

1 回答 1

1

欢迎来到MQL4世界,莱昂纳多

让我们回顾一下语法:

for ( int i = 0; i < BarCountTemp; i++ ) {
      bars[i] = Close[i + 1];
     }
     int   i = 0;
     bool  is_p;
     do
     {
        if ( bars[i] > bars[i+1] && i < BarCountTemp ) is_p = true;
        else is_p = false;
        i++;
     }
     while ( is_p );

可以合并/简化为单个循环/中断结构:

bool is_p = True;                          // FYI: FALSE if not initialised
                                           // WARNING: "New"-MQL4 has changed variable visibility-scope to be limited just to the innermost syntax-construct and variables easily "cease" exist outside that syntax-construct boundary ... for(){bool is_p ...visible...} ...invisible...
for ( int  i = 0;                          // .SET
           i < BarCountTemp;               // .TEST: [**]
           i++ ) {                         // .INC
           if (  Close[i+1] > Close[i+2]   // avoid TimeSeries' replica(s)
           // && i          < BarCountTemp // ALWAYS TRUE [^**]
              ) continue; // ---------------------------- LOOP-^
           else {
                is_p = False;
                break;    // ---------------------------- EXIT-v
}

下一个:在图表窗口顶部得到至少一个 Comment() 注释?

int   Ticket =  EMPTY;                        // Rather initialise as = EMPTY;

if (  is_p   == True
   && Ticket == EMPTY                         // un-ambiguous meaning
   ) {
      Ticket =  OrderSend( _Symbol,           // .SYM
                           OP_SELL,           // .OP
                           0.1,               // .LOTs check sizing, MarketInfo()
                           Bid,               // .PRICE
                           0,                 // .SLIPPAGE
                           0,                 // .SL
                           0,                 // .TP
                          "Sell Order Custom",// .COMMENT
                           110                // .MAGNUM
                           );
      if (  Ticket == EMPTY ){                // EXC. HANDLER
                  ...
      }
      else {
            Alert(   "Sell order opened to match found." );     // .NOP if isTesting()
            Comment( "Sell order opened #" + Ticket + "." );    // .GUI is visible????
      }
}

最后:包括可能出现错误的情况的异常处理程序

 if (  Ticket   != EMPTY               // TEST 1st,
    && Close[1] >  Close[2]            // TEST 2nd, prevent dbPool-ops, if not True
    ) {
             bool select = OrderSelect( Ticket, SELECT_BY_TICKET );
             if (!select ){           // EXC. HANDLER
                  ...
             }
             bool close  = OrderClose(  Ticket,
                                        OrderLots(),
                                        Ask,
                                        0,
                                        clrGreen
                                        );
             if (!close ){           // EXC. HANDLER
                  ...
             }
             Alert(   "Sell order closed." );
             Comment( "Sell order closed #" + Ticket + "." );
             Ticket = EMPTY;                            // .SET EMPTY
       }
 }
于 2015-08-25T09:37:11.347 回答