1

我已经尝试了很多东西,但无法让它与下面的代码一起使用。

我尝试了以下代码逻辑的变体,但失败了,不确定在哪里实现它:

if (  OrderSelect( OrdersHistoryTotal() - 1, SELECT_BY_POS, MODE_HISTORY ) )
{
      if (  OrderType() == OP_BUY )
      {
            if (  OrderClosePrice() >  OrderStopLoss() )      Print( "Hit TP" );
            else                                              Print( "Hit SL" );
      }
      else if (  OrderType() == OP_SELL )
           {
                 if (  OrderClosePrice() <  OrderStopLoss() ) Print( "Hit TP" );
                 else                                         Print( "Hit SL" );
           }
}  

或者

Orderselect...

if (  MathAbs( OrderClosePrice()
             - OrderTakeProfit()
               ) > MathAbs( OrderClosePrice()
                          - OrderStopLoss()
                            )
      ) Print( "StopLoss" );

if (  MathAbs( OrderClosePrice()
             - OrderTakeProfit()
               ) < MathAbs( OrderClosePrice()
                          - OrderStopLoss()
                            )
      ) Print( "TakeProfit" );

OrderSelect()对我来说一直是个问题,所以任何帮助将不胜感激。

下面是我试图将其添加到的 EA,但只要知道如何以及在何处放置它就会有所帮助。

extern int      MagicNumber     = 10001;
extern double   Lots            =     0.01;
extern double   StopLoss        =     1;
extern double   TakeProfit      =     1;
extern int      TrailingStop    =     0;
extern int      Slippage        =     3;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start() // New-MQL4.56789 #strict uses another constructor:         int OnTick(){...}
{
  double MyPoint = Point;
  if (  Digits == 3
     || Digits == 5
     )  MyPoint = Point*10;

  double TheStopLoss    = 0;
  double TheTakeProfit  = 0;

  if ( TotalOrdersCount() == 0 )
  {
     int result = 0;
     if (  ( iMA( NULL, 0, 30, 0, MODE_SMA, PRICE_CLOSE, 1 ) <  iMA( NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 1 ) )
        && ( iMA( NULL, 0, 30, 0, MODE_SMA, PRICE_CLOSE, 0 ) >  iMA( NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 0 ) )
        ) // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Here is your open BUY rule
     {
        result = OrderSend( Symbol(),
                            OP_BUY,
                            Lots,
                            Ask,
                            Slippage,
                            0,
                            0,
                            "Buy",
                            MagicNumber,
                            0,
                            Blue
                            );
        if (  result >  0 )
        {
              TheStopLoss   = 0;
              TheTakeProfit = 0;

              if (  TakeProfit >  0 ) TheTakeProfit = Ask + TakeProfit * MyPoint;
              if (  StopLoss   >  0 ) TheStopLoss   = Ask - StopLoss   * MyPoint;

              OrderSelect( result, SELECT_BY_TICKET );
              OrderModify( OrderTicket(),
                           OrderOpenPrice(),
                           NormalizeDouble( TheStopLoss,   Digits ),
                           NormalizeDouble( TheTakeProfit, Digits ),
                           0,
                           Green
                           );
        }
        return(0);
     }
     if (  ( iMA( NULL, 0, 30, 0, MODE_SMA, PRICE_CLOSE, 1 ) >  iMA( NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 1 ) )
        && ( iMA( NULL, 0, 30, 0, MODE_SMA, PRICE_CLOSE, 0 ) <  iMA( NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 0 ) )
        ) // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Here is your open SELL rule
     {
        result = OrderSend( Symbol(),
                            OP_SELL,
                            Lots,
                            Bid,
                            Slippage,
                            0,
                            0,
                            "Sell",
                            MagicNumber,
                            0,
                            Red
                            );
        if (  result >  0 )
        {     
              TheStopLoss   = 0;
              TheTakeProfit = 0;

              if (  TakeProfit >  0 ) TheTakeProfit = Bid - TakeProfit * MyPoint;
              if (  StopLoss   >  0 ) TheStopLoss   = Bid + StopLoss   * MyPoint;

              OrderSelect( result, SELECT_BY_TICKET );
              OrderModify( OrderTicket(),
                           OrderOpenPrice(),
                           NormalizeDouble( TheStopLoss,   Digits ),
                           NormalizeDouble( TheTakeProfit, Digits ),
                           0,
                           Green
                           );
        }
        return(0);
     }
  }

  for ( int cnt = 0; cnt <  OrdersTotal(); cnt++ )
  {     
        OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES );

        if (  OrderType()        <= OP_SELL
           && OrderSymbol()      == Symbol()
           && OrderMagicNumber() == MagicNumber
           )  
        {     
              if (  OrderType() == OP_BUY )
              {     
                    if (  TrailingStop >  0 )
                    {     
                          if (  Bid - OrderOpenPrice() >  MyPoint * TrailingStop )
                          {     
                                if (  OrderStopLoss()  <  Bid - MyPoint * TrailingStop )
                                {     
                                      OrderModify( OrderTicket(),
                                                   OrderOpenPrice(),
                                                   Bid - TrailingStop * MyPoint,
                                                   OrderTakeProfit(),
                                                   0,
                                                   Green
                                                   );
                                      return(0);
                                }
                          }
                    }
              }
              else 
              {     
                    if (  TrailingStop >  0 )
                    {     
                          if (  ( OrderOpenPrice() - Ask ) >  ( MyPoint * TrailingStop ) )
                          {     
                                if (  ( OrderStopLoss() >  ( Ask + MyPoint * TrailingStop ) )
                                   || ( OrderStopLoss() == 0 )
                                   )
                                {     
                                      OrderModify( OrderTicket(),
                                                   OrderOpenPrice(),
                                                   Ask + MyPoint * TrailingStop,
                                                   OrderTakeProfit(),
                                                   0,
                                                   Red
                                                   );
                                      return(0);
                                }
                          }
                    }
              }
        }
  }
  return(0);
}

int    TotalOrdersCount()
{      
       int result = 0;
       for (  int i = 0; i <  OrdersTotal(); i++ )
       {
           OrderSelect( i, SELECT_BY_POS, MODE_TRADES );

           if (  OrderMagicNumber() == MagicNumber ) result++;

       }
       return( result );
}
4

1 回答 1

0

... open WHEN ...是目标的关键部分

OrderSelect()对您解决“何时打开”难题没有多大帮助。它只是爬过localhost 内部 DBMS 负责处理的db.POOL记录MetaTrader Terminal 4,这是处理订单管理任务的缓慢且非常非常低效的方式,因此请尽量避免在生产级系统中对其进行任意扫描。在这里,您甚至不会从如此毁灭性db.POOL的扫描中获得任务所需的任何基本信息,因此让我们首先关注目标。


... WHAT ...目标背后的想法是什么?

EA是慢 200 / 快 30 简单移动平均线交叉的学术上微不足道的变化。

在那里,我想您想添加您的额外想法,即在基础 SMA 交叉发起的交易已{ SL | TP }分别达到其终止的情况下开设额外交易,对吧?

对于这种情况,经纪商MetaTrader Server 4通常允许您下一个所谓的挂单,指示经纪商维护您进入市场的意愿的记录,假设价格达到预定水平——(猜猜看,这些将是{ SL | TP }您在标题中要求的 - 并且此类记录正在等待(具有更多可配置选项),直到此类价格变动发生与否,在此期间此类头寸处于非活动状态等待,称为挂单.. .

对于条款和条件,请查看您的经纪人的合同和/或询问他们的客户服务代表。一些特殊的限制可能适用(更好地了解先验)。


... WHERE ... 把代码?

无需重新设计提供的代码(那里有一些小错误/效率低下),代码应放在此处:

    result = OrderSend( Symbol(),
                        OP_SELL,  // PRIMARY SELL <<<AT-MARKET>>>
                        Lots,
                        Bid,
                        Slippage,
                        0,
                        0,
                        "Sell",
                        MagicNumber,
                        0,
                        Red
                        );
    if (  result >  0 )
    {     
          TheStopLoss   = 0;
          TheTakeProfit = 0;

          if (  TakeProfit >  0 ) TheTakeProfit = Bid - TakeProfit * MyPoint;
          if (  StopLoss   >  0 ) TheStopLoss   = Bid + StopLoss   * MyPoint;

          OrderSelect( result, SELECT_BY_TICKET );

          OrderModify( OrderTicket(),
                       OrderOpenPrice(),
                       NormalizeDouble( TheStopLoss,   Digits ),
                       NormalizeDouble( TheTakeProfit, Digits ),
                       0,
                       Green
                       );
       // ---------------------------------------------------- HERE ADD
          OrderSend(   Symbol(),
                       OP_SELLSTOP,    // SELL-STOP PENDING ORDER ON SELL.TP
                       Lots,
                       NormalizeDouble( TheTakeProfit
                                      - MarketInfo( _Symbol, MODE_SPREAD ),
                                        Digits
                                        ),
                       Slippage,
                       0,              // SL: T.B.D.
                       0,              // TP: T.B.D.
                       "Sell on Sell.TP",
                       sTpMagicNumber, // avoid collisions with other logic
                       0,              // never expires
                       Red
                       );

          OrderSend(   Symbol(),
                       OP_BUYSTOP,    // BUY-STOP PENDING ORDER ON SELL.SL
                       Lots,
                       NormalizeDouble( TheStopLoss
                                      - MarketInfo( _Symbol, MODE_SPREAD ),
                                        Digits
                                        ),
                       Slippage,
                       0,              // SL: T.B.D.
                       0,              // TP: T.B.D.
                       "Buy on Sell.SL",
                       bSlMagicNumber, // avoid collisions with other logic
                       0,              // never expires
                       Red
                       );
    }

价差调整不是绝对的,因为在不稳定时期,市场会受到虚假价差波动的影响。同样,经纪人的条款和条件在这里适用,所以要小心。

于 2016-05-16T12:53:34.600 回答