mql4tradingalgorithmic-tradingforexmt4

Why does this string function not update? MQL4


I'm super confused right now, been at this for some time. I'm trying to figure out why this string function won't update the string when I believe the if statement to be true.

void OnTick()
{
     string aboveprofit = "";

     for ( int b = OrdersTotal() - 1; b >= 0; b-- )
     {
          if ( OrderSelect( b, SELECT_BY_POS, MODE_TRADES ) )
          if ( OrderSymbol() == Symbol() )
          {
               if ( OrderSymbol() == OP_SELL )
               {
                    OrderModify( OrderTicket(),
                                 OrderOpenPrice(),
                                 Bid + ( 150 * _Point ),
                                 OrderTakeProfit(),
                                 0,
                                 CLR_NONE
                                 );
                    if ( Ask < OrderOpenPrice() )
                    {    
                         abovepoint = "321";
                    }
               }
               if ( OrderSymbol() == OP_BUY )
               {
                    OrderModify( OrderTicket(),
                                 OrderOpenPrice(),
                                 Ask - ( 150 * _Point ),
                                 OrderTakeProfit(),
                                 0,
                                 CLR_NONE
                                 );
                    if ( Bid > OrderOpenPrice() )
                    {
                         abovepoint = "123";
                    }
               }
     }
 }

Solution

  • Q : "why this string function won't update the string when I believe the if statement to be true?"

    Well, how do you know it did not?

    Add a Print( Volume[0], abovepoint ); command right onto the first line right upon the entry to the void OnTick(){...}-code-block, so as to check the validity of your above posted assumption in the Client Terminal log.

    The code fragment above is missing an actual abovepoint declaration, so it is impossible to add anything more than this.


    ERRATA : OrderSymbol() will most probably never match { OP_BUY | OP_SELL }-constants, it ought be OrderType(), ought be not?

    The OrderModify()-commands are almost sure to generate server-side rejections of any XTO-instruction, that will violate either a Broker-side defined FREEZE_LEVEL and/or minimum permitted distances of modified { TP | SL }-targets from the actual { Ask | Bid }-price-levels. Kindly review your Broker's T&C and best adapt your code to prevent falling into those conditions colliding instructions.