IF conditions using

 

so i have created an expert that is drawing 2 horizontal lines in the prev weeks close Highest and Lowest value.

I want to check if the price has gone above the prev weeks high and if so to check if the daily rsi 2 days ago is < yesterday's rsi value and if yesterday's rsi value is > than current todays rsi value.

if this condition is true i want to check the 4h timeframe rsi for the same patern and wait for the current 4h candle to close to open a trade. Below is my code that i think its not that good.

Any help or ideas would be appreciated.

(Note the code is not completed)

int OnInit()
{

   Init_myLine(); 
   
   return(INIT_SUCCEEDED);
}


void OnTick()
{
   // We work only at the birth of a new bar
   datetime time_0 = iTime(_Symbol,PERIOD_H4,0);
   datetime current_time=TimeCurrent();

   datetime next_candle_open = time_0 + (4 * 3600);

   // Get the highest and lowest prices of the last closed week
   double lastWeekHigh = iHigh(_Symbol, PERIOD_W1, 1);
   double lastWeekLow = iLow(_Symbol, PERIOD_W1, 1);
   
   ObjectMove(_Symbol, "PrevWeekHIGH",0,0,lastWeekHigh); // draw line HIGH
   ObjectMove(_Symbol, "PrevWeekLOW",0,0,lastWeekLow); // draw line LOW
   
   // Get the current price
   double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   
   //DAILY RSI
   double myRsiArray_Daily[];
   
   int myRsiDefinition_Daily = iRSI(_Symbol,PERIOD_D1,14,PRICE_CLOSE);
   
   ArraySetAsSeries(myRsiArray_Daily,true);
   CopyBuffer(myRsiDefinition_Daily,0,0,3,myRsiArray_Daily);
   
   double myRSIValue_Daily = NormalizeDouble(myRsiArray_Daily[0],2);
   double yestRSIValue_Daily = NormalizeDouble(myRsiArray_Daily[1],2);
   double D2_before_RSI_Daily = NormalizeDouble(myRsiArray_Daily[2],2);
   
   //4H RSI
   double myRsiArray_4H[];
   
   int myRsiDefinition_4H = iRSI(_Symbol,PERIOD_H4,14,PRICE_CLOSE);
   
   ArraySetAsSeries(myRsiArray_4H,true);
   CopyBuffer(myRsiDefinition_4H,0,0,3,myRsiArray_4H);
   
   double myRSIValue_4H = NormalizeDouble(myRsiArray_4H[0],2);
   double prev_candle_RSIValue_4H = NormalizeDouble(myRsiArray_4H[1],2);
   double prev_2_cancle_RSI_4H = NormalizeDouble(myRsiArray_4H[2],2);
   Comment("Current PRICE: ", currentPrice," Last Week High: ",lastWeekHigh," Last Week Low: ",lastWeekLow,
           "\nDAILY RSI: Today RSI: ",myRSIValue_Daily," Yesterday's RSI: ",yestRSIValue_Daily," 2D_Before RSI: ", D2_before_RSI_Daily , 
           "\n4H RSI: Current_4H RSI: ",myRSIValue_4H," Prev_4H RSI: ",prev_candle_RSIValue_4H," Prev_8H RSI: ", prev_2_cancle_RSI_4H,
           "\nPrev4H candle close: ", time_0,"\nNext 4H candle_open: ", next_candle_open,"\nCurrent Time: ",current_time);
           
            
  // if conditions to check if the current price has gone above or belong the previous weeks High and Low.        
   if (currentPrice > lastWeekHigh){
      Print("currentPrice > lastWeekHigh TRUE...currentPrice > lastWeekHigh TRUE...currentPrice > lastWeekHigh TRUE...");
      if (D2_before_RSI_Daily < yestRSIValue_Daily && yestRSIValue_Daily > myRSIValue_Daily){
         Print("D2_before_RSI_Daily < yestRSIValue_Daily && yestRSIValue_Daily > myRSIValue_Daily TRUE.......D2_before_RSI_Daily < yestRSIValue_Daily && yestRSIValue_Daily > myRSIValue_Daily TRUE.......D2_before_RSI_Daily < yestRSIValue_Daily && yestRSIValue_Daily > myRSIValue_Daily TRUE.......");
         if (prev_2_cancle_RSI_4H < prev_candle_RSIValue_4H && prev_candle_RSIValue_4H > myRSIValue_4H){
            Print("prev_2_cancle_RSI_4H < prev_candle_RSIValue_4H && prev_candle_RSIValue_4H > myRSIValue_4H TRUE.......prev_2_cancle_RSI_4H < prev_candle_RSIValue_4H && prev_candle_RSIValue_4H > myRSIValue_4H TRUE.......prev_2_cancle_RSI_4H < prev_candle_RSIValue_4H && prev_candle_RSIValue_4H > myRSIValue_4H TRUE.......");
            if (current_time == time_0 + (4*3600)){
               Print("INSIDE ALL 4 IFS.....................INSIDE ALL 4 IFS.....................INSIDE ALL 4 IFS.....................INSIDE ALL 4 IFS.....................");
            }//4th if        
         }//3rd if
      }//2nd if
   }//1st if

 
  }//onTick



void Init_myLine()
{

   ObjectCreate(
            _Symbol, 
            "PrevWeekHIGH",  
            OBJ_HLINE,  
            0, //int sub_window
            0, //datetime time1 
            0 // y value on chart
   ); 
   
   ObjectSetInteger(
            0, // long chart_id
            "PrevWeekHIGH",
            OBJPROP_COLOR, //int properti_modifier
            clrDarkTurquoise       // long value
   );
   
   ObjectSetInteger(0,"PrevWeekHIGH",OBJPROP_WIDTH, 1); // line width. 
   
   
   ObjectCreate(
            _Symbol, 
            "PrevWeekLOW",  
            OBJ_HLINE,  
            0, //int sub_window
            0, //datetime time1 
            0 // y value on chart
   ); 
   
   ObjectSetInteger(
            0, // long chart_id
            "PrevWeekLOW",
            OBJPROP_COLOR, //int properti_modifier
            clrDarkViolet       // long value
   );
   
   ObjectSetInteger(0,"PrevWeekLOW",OBJPROP_WIDTH, 1); // line width. 
}
 

If you are interested to learn to code:

Quickstart for newbies: https://www.mql5.com/en/articles/496
and: https://www.mql5.com/en/articles/100
cookbook: https://www.mql5.com/en/search#!keyword=cookbook
Bear in mind there's virtually nothing that hasn't already been programmed for MT4/MT5 and is ready for you - so searching gives better results than AI or ChatGPT!

And her you can find what to do if the programs compiles without an error but does not do what it should:

Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272

Tips from a professional programmer (Part  I): Code storing, debugging and compiling. Working with projects and logs
https://www.mql5.com/en/articles/9266
Tips from a professional programmer (Part II): Storing and exchanging parameters between an Expert Advisor, scripts and external programs
https://www.mql5.com/en/articles/9327
Tips from a professional programmer (Part III): Logging. Connecting to the Seq log collection and analysis system
https://www.mql5.com/en/articles/10475

Quick Start: Short Guide for Beginners
Quick Start: Short Guide for Beginners
  • www.mql5.com
Hello dear reader! In this article, I will try to explain and show you how you can easily and quickly get the hang of the principles of creating Expert Advisors, working with indicators, etc. It is beginner-oriented and will not feature any difficult or abstruse examples.
 
Do your EA need the lines on the chart? Would it be possible to calculate the prices of the two lines and check the ticks againts these variable values - it a lot faster and easier.
 

Please don't post randomly in any section. Your question is not related to the section you posted. I have moved your topic to the correct section — Expert Advisors and Automated Trading

 
Carl Schreiber #:
Do your EA need the lines on the chart? Would it be possible to calculate the prices of the two lines and check the ticks againts these variable values - it a lot faster and easier.

the 2 lines need to be there for visualize the prev week High and low. all i need to do is when the price going higher than prev week high start searching for daily rsi conditions (2 days before RSI < yesterday RSI && today RSI < yesterday RSI. But i dont want to checking all the time the current price. If it passes once above the line start checking for the daily rsi conditions. And once these conditions come true start checking for 2 prev 4h_candle rsi (iRSI[2] < 1 prev 4h_candle rsi (iRSI[1] && current 4h_candle rsi (iRSI[0]).

I also tried to make it throw this code changes but it didnt work.

// Get prev 1 minute candle close price
   double last_low_1m = iLow(_Symbol,PERIOD_M1,1);
   double last_high_1m = iHigh(_Symbol, PERIOD_M1,1);
// if conditions to check if the current price has gone above or belong the previous weeks High and Low.
   if(last_high_1m > lastWeekHigh || last_low_1m > lastWeekHigh)
     {
      
      bool high_trigger = true;
      while(high_trigger == true)
        {
         );
         Print("currentPrice > lastWeekHigh TRUE...currentPrice > lastWeekHigh TRUE...currentPrice > lastWeekHigh TRUE...");
         if(D2_before_RSI_Daily < yestRSIValue_Daily && yestRSIValue_Daily > myRSIValue_Daily)
           {
           
            bool daily_rsi_trigger = true;
            while(daily_rsi_trigger==true)
              {
               
               Print("D2_before_RSI_Daily < yestRSIValue_Daily && yestRSIValue_Daily > myRSIValue_Daily TRUE.......D2_before_RSI_Daily < yestRSIValue_Daily && yestRSIValue_Daily > myRSIValue_Daily TRUE.......D2_before_RSI_Daily < yestRSIValue_Daily && yestRSIValue_Daily > myRSIValue_Daily TRUE.......");
               if(prev_2_cancle_RSI_4H < prev_candle_RSIValue_4H && prev_candle_RSIValue_4H > myRSIValue_4H)
                 {
                 
                  bool h4_rsi_trigger = true;
                  while(h4_rsi_trigger == true)
                    {    
                    
                     Print("prev_2_cancle_RSI_4H < prev_candle_RSIValue_4H && prev_candle_RSIValue_4H > myRSIValue_4H TRUE.......prev_2_cancle_RSI_4H < prev_candle_RSIValue_4H && prev_candle_RSIValue_4H > myRSIValue_4H TRUE.......prev_2_cancle_RSI_4H < prev_candle_RSIValue_4H && prev_candle_RSIValue_4H > myRSIValue_4H TRUE.......");
                     if(iTime(_Symbol, PERIOD_H4, 0) == TimeCurrent())
                       {
                       
                        Print("A LONG TRADE...A LONG TRADE...A LONG TRADE...A LONG TRADE...A LONG TRADE...");
                        high_trigger = false;
                        daily_rsi_trigger = false;
                        h4_rsi_trigger = false;


                       }//4th if HIGH
                    }// while 3rd
                 }//3rd if HIGH
              }// while 2nd
           }//2nd if HIGH
        }// while 1st

     }//1st if HIGH
 
  1. dio1zervas #: I also tried to make it throw this code changes but it didnt work.

    “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

  2. Drop your loops. If the condition isn't true, nothing is changing. Return and wait for a new tick and reevaluate.

  3. dio1zervas: Any help or ideas would be appreciated.
       int myRsiDefinition_4H = iRSI(_Symbol,PERIOD_H4,14,PRICE_CLOSE);
       
       ArraySetAsSeries(myRsiArray_4H,true);
       CopyBuffer(myRsiDefinition_4H,0,0,3,myRsiArray_4H);

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

Reason: