0

I am totally drain out as the second if statement couldn't be executed.

My original idea was when volatility is in a range of 90 - 110, the program will send one and only one order. And it will wait and see till the volatility reaches in a range of 111 - 150, and then it will send the second order.

If I don't use a bool function here, the program will send countless order when the range is reached.

Could someone please help me?

  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;
     }
user3666197
  • 1
  • 6
  • 50
  • 92
  • If TodayMaxVolatilityPercentage >= 110.0, then it is for sure, TodayMaxVolatilityPercentage is > 90.0. So put the second if clause in the front, so that logic will be executed. I guess it helps. OR, you might define range for TodayMaxVolatilityPercentage, so you add a check: `if (TodayMaxVolatilityPercentage >=90 && TodayMaxVolatilityPercentage <110)` – a3.14_Infinity Oct 03 '15 at 06:05
  • Inside your first `if(TodayMaxVolatilityPercentage>=90.0` body, you also have `if(TodayMaxVolatilityPercentage>=110.0)`, but nothing writes to `TodayMaxVolatilityPercentage` (in the code you've shown) in-between. Can it really change? – T.J. Crowder Oct 03 '15 at 06:10

1 Answers1

0

A hidden show-stopper:

By design, the first successful OrderSend() returns a value you assign into an ticket = OrderSend(...).

The devil is hidden in detail.

[MQL4-Help] says:

Returns number of the ticket assigned to the order by the trade server or -1 if it fails.

So the first successful OrderSend() ticket# returned is 0 ( in StrategyTester, a real MT4/Server rather runs into high numbers drawn from it's db.pool of tickets ).

Thus the second if ( ticket > 0 ) will never let you in.

It is common to rather use if ( ticket > EMPTY ) so as to also clarify the intention and use symbolic instead of a -1 integer constant.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92