mql4algorithmic-tradingmetatrader4forexmt4

Can I make a while-loop that uses RSI value as the condition?


When I try to run this while-loop in MQL4, it can not seem to update the rsi from within the loop. It always returns the same value of the rsi and thus stays in an eternal loop. I have tried it on an EA as well as a script.

I have also tried making rsi a globally defined variable too but still did not work.

void OnTick()
{

     double rsi = iRSI(NULL,0,14,PRICE_CLOSE,0);        // defining rsi
                 
     while( rsi < 50 )
     {
            double rsi = iRSI(NULL,0,14,PRICE_CLOSE,0); // update rsi 
            Sleep(10000);                               // slow down loop 
            Alert("this is from inside while loop rsi is "+rsi); 
     }
        
     Alert("While loop exited, condition met");         // now code goes
     Alert("opening an order now " );                   //     to place an order
}

Solution

  • Can I make a while-loop that uses RSI value as the condition?

    Oh sure you can.

    Let's move out the colliding elements.

    Given the code as-is, there are several brutal misconceptions to repair:

    Solution

    MQL4 is a responsive-ecosystem, where OnTick() is automatically called whenever a new QUOTE-message arrives from FX-Market, so design your algorithms so as these never block.

    For your learning-directed inspiration, you may try your next few steps using this template :

    string MASK  = "[%s]_INF:: "                           // DEF. & INIT...
                 + "This is a QUOTE# %5d "
                 + "Having arrived at [%s]_FX-MarketTIME\n"
                 + "|Ask %16.8f\n"
                 + "|Bid %16.8f\n"
                 + "|Vol %16d\n"
                 + "|RSI %16.8f";
    double rsi   = EMPTY;                                  // DEF. & INIT...
    int   nTicks = 0;                                      // DEF. & INIT...
    
    void OnDeinit( const int aDeinitReason )
    {    EventKillTimer();
    }
    
    int  OnInit()
    {    EventSetTimer( 1 );
         Comment( "------------------------------------------------------\n ",
                        "WAITING for a first QUOTE-arrival from FX-Market\n",
                  "------------------------------------------------------"
                   );
         return( 0 );
    }
    
    void OnTick()
    {     nTicks++;                                        // update COUNTER
         rsi = iRSI( NULL, 0, 14, PRICE_CLOSE, 0 );        // update rsi
         Comment( SetupComment() );                        // update GUI
    
      // -------------------------------- FIRST 50 FX-Market Ticks delivered
         if ( nTicks < 50 ) return;
    
      // -------------------------------- AFTER 50 FX-Market Ticks delivered
      // --------------------------------       BE WARNED, THIS IS AN AWFUL ANTI-PATTERN
         while( True )
         {
                rsi = iRSI( NULL, 0, 14, PRICE_CLOSE, 0 ); // update rsi
                Comment( SetupComment() );                 // update GUI
                Sleep( 10000 );
         }
      // --------------------------------       NEVER GETS HERE
         Alert( "?" );
    }
    
    string SetupComment()
    {      return( StringFormat( MASK,
                                 TimeToStr( TimeLocal(),
                                            TIME_DATE|TIME_MINUTES|TIME_SECONDS
                                            ),
                                 nTicks,
                                 TimeToStr( TimeCurrent(),
                                            TIME_DATE|TIME_MINUTES|TIME_SECONDS
                                            ),
                                 NormalizeDouble( Ask, 8 ),
                                 NormalizeDouble( Bid, 8 ),
                                 Volume[0],
                                 NormalizeDouble( rsi, 8 )
                                 )
                   );
    }
    
    void OnTimer()
    {    Comment( ChartGetString( 0, CHART_COMMENT ),
                  "\n",
                  TimeToStr( TimeLocal(),
                             TIME_DATE|TIME_MINUTES|TIME_SECONDS
                             )
                  );
         Print(   ChartGetString( 0, CHART_COMMENT ) );
    }