【问题标题】:How to access the second if statement如何访问第二个 if 语句
【发布时间】:2015-12-31 09:20:08
【问题描述】:

第二个 if 语句无法执行,我完全筋疲力尽了。

我最初的想法是当波动率在 90 - 110 范围内时,程序将发送一个且只有一个订单。等到波动率达到111-150区间后,才会发出二单

如果我这里不使用bool函数,程序会在达到范围时发送countless命令。

有人可以帮帮我吗?

  if (  TodayMaxVolatilityPercentage >= 90.0
     && ( dayTrend  == 1 )
     && orderOpened == false
        )
  {
        Print( "Entered if clause" );
     // Print( "Today volatility percentage is: ", TodayMaxVolatilityPercentage + "%" );
     // ticket: Returns number of the ticket assigned to the order by the trade server or -1 if it fails.
        ticket = OrderSend( Symbol(),
                            OP_SELL,
                            0.3,
                            Bid,
                            3,
                             0 * MyPoint,
                            30 * MyPoint,
                            NULL,
                            MagicNumber,
                            0,
                            Blue
                            );
        Print( "Order is opened on", OrderOpenTime()+" at price: ", OrderOpenPrice() );
        Print( "trend number is ",dayTrend );

        if (  ticket > 0 )
        {
              if (  TakeProfit > 0 ) TheTakeProfit = Bid - TakeProfit * MyPoint;
              OrderSelect( ticket, SELECT_BY_TICKET ); // bool value
           /* OrderModify( OrderTicket(),
                           OrderOpenPrice(),
                           0,
                           NormalizeDouble( TheTakeProfit, Digits  ),
                           0,
                           Green
                           );
                         */
        }
        orderOpened = true;
        if (  TodayMaxVolatilityPercentage >= 110.0 ) orderOpened = false;              
  }
  if (  TodayMaxVolatilityPercentage >= 110.0
     && ( dayTrend  == 1 )
     && orderOpened == false
        )
  {
        Print( "Entered second if clause" );
     // ticket: Returns number of the ticket assigned to the order by the trade server or -1 if it fails.
        ticket = OrderSend(  Symbol(),
                             OP_SELL,
                             0.3,
                             Bid,
                             3,
                              0 * MyPoint,
                             30 * MyPoint,
                             NULL,
                             MagicNumber,
                             0,
                             Blue
                             );
        if (  ticket > 0 )
        {
              if (  TakeProfit > 0 ) TheTakeProfit = Bid - TakeProfit * MyPoint;
              OrderSelect( ticket, SELECT_BY_TICKET ); // bool value
           /* OrderModify( OrderTicket(),
                           OrderOpenPrice(),
                           0,
                           NormalizeDouble( TheTakeProfit, Digits ),
                           0,
                           Green
                           );
                       */
        }
        orderOpened = true;
     }

【问题讨论】:

  • 如果 TodayMaxVolatilityPercentage >= 110.0,那么可以肯定,TodayMaxVolatilityPercentage > 90.0。所以把第二个 if 子句放在前面,这样逻辑就会被执行。我想这会有所帮助。或者,您可以为 TodayMaxVolatilityPercentage 定义范围,因此您添加一个检查:if (TodayMaxVolatilityPercentage >=90 && TodayMaxVolatilityPercentage <110)
  • 在您的第一个 if(TodayMaxVolatilityPercentage>=90.0 正文中,您也有 if(TodayMaxVolatilityPercentage>=110.0),但没有任何内容写入 TodayMaxVolatilityPercentage(在您显示的代码中)。真的可以改变吗?

标签: trading algorithmic-trading mql4 metatrader4


【解决方案1】:

一个隐藏的表演者:

根据设计,第一个成功的 OrderSend() 会返回您分配给 ticket = OrderSend(...) 的值。

魔鬼藏在细节里。

[MQL4-Help] 说:

返回交易服务器分配给订单的票的编号,如果失败则返回-1

所以返回的第一个成功的OrderSend() 票证# 是0(在 StrategyTester 中,真正的 MT4/Server 会从它的票证的db.pool 中抽取大量数字)。

因此第二个 if ( ticket > 0 ) 永远不会让你进来。

通常宁愿使用 if ( ticket > EMPTY ) 以明确意图并使用符号而不是 -1 整数常量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 2015-12-24
    相关资源
    最近更新 更多