Need help for my first code - wrong arrow placements

 

Hello,

Im 3 month old programming student, i need help to debug this code. The indicator is supposed to place arrows on buy and sell signals but it just scatters arrows all over the chart on candles that dont meet the signal conditions and i have failed to understand what is wrong here.

The buy and sell conditions are +ve RSI movement on M15, buy when RSI on M1 <35, and -ve RSI movement on 1M5, sell when RSI on M1>65.

//------------------------------------------------------------------+
//|                          TRIAL- ARROWS ON BUY-SELL SIGNALS
//+------------------------------------------------------------------+

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 White
#property indicator_color2 Red


//---- input parameters
    
input int    Buy_Level = 35; // Buy Level
input int    Sell_Level= 65;   // Sell Level 
input int    RSIPeriod_M1= 7;   // RSI Period




//---- buffers

double ExtMapBuffer1[];
double ExtMapBuffer2[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,234);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,233);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
   
   int i;
   double buf0,buf1,buf2;

//----
   
   for (i=Bars-50;i>=0;i--)
      {
         buf0 = iRSI(NULL,PERIOD_M15,14,0,i)  ;
         buf1 = iRSI(NULL,PERIOD_M15,14,0,i+1) ;
         buf2 = iRSI(NULL,PERIOD_M1,RSIPeriod_M1,0,i);   
 //--       
        if ((buf0 > buf1) && (buf2 < Buy_Level))
            
            ExtMapBuffer1[i]=Low[i]-5*Point; 
            
        if ((buf0 < buf1) && (buf2 > Sell_Level))
               
            ExtMapBuffer2[i]=High[i]+5*Point;
      
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+
Files:
Trial1.mq4  5 kb
 
vluyima:

Im 3 month old programming student, i need help to debug this code. The indicator is supposed to place arrows on buy and sell signals but it just scatters arrows all over the chart on candles that dont meet the signal conditions and i have failed to understand what is wrong here.

The buy and sell conditions are +ve RSI movement on M15, buy when RSI on M1 <35, and -ve RSI movement on 1M5, sell when RSI on M1>65.

The same 'i' cannot be used to access data from different timeframes. If your chart runs on M1, your 'i' and 'Bars' are for M1, and you must make use of iBarShift() to get the bar index for other timeframes, like this:

         int i4M15 = iBarShift(Symbol(),Period(),Time[i]);
         buf0 = iRSI(NULL,PERIOD_M15,14,0,i4M15)  ;
         buf1 = iRSI(NULL,PERIOD_M15,14,0,i4M15+1) ;
 
  1. Why did you post your MT4 question in the Root / MT5 Indicators section  instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2.          buf0 = iRSI(NULL,PERIOD_M15,14,0,i)  ;
             buf1 = iRSI(NULL,PERIOD_M15,14,0,i+1) ;
             buf2 = iRSI(NULL,PERIOD_M1,RSIPeriod_M1,0,i);
    
    Like Seng said don't mix apples and oranges.

  3. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing candle values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

  4.    for (i=Bars-50;i>=0;i--)
    
    Why are recomputing all bars every tick? See How to do your lookbacks correctly.

  5. Don't use NULL.
    • You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not.
    • Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    • Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    • MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].

 
Seng Joo Thio:

The same 'i' cannot be used to access data from different timeframes. If your chart runs on M1, your 'i' and 'Bars' are for M1, and you must make use of iBarShift() to get the bar index for other timeframes, like this:

Hairs were jumping off my head but Oh Man, you just saved me from a nightmare, im adding you to my list of saviours. 

Arrows are plotting well now but another problem though, the arrows are plotting on each candle as long as the condition still holds (see image). Is there a way i can make the arrows appear only on the first candle when condition is true and ignore the rest until the condition reverses.?..

Files:
 
William Roeder:
  1. Why did you post your MT4 question in the Root / MT5 Indicators section  instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Like Seng said don't mix apples and oranges.

  3. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing candle values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

  4. Why are recomputing all bars every tick? See How to do your lookbacks correctly.

  5. Don't use NULL.
    • You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not.
    • Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    • Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    • MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].

William, apologies for posting in the wrong place, had not entirely read the rules. I appreciate your feedback, very good and informative. Didn't know about all that but im catching up bit by bit.

Thank you.. I highly appreciate.

 
vluyima:

Arrows are plotting well now but another problem though, the arrows are plotting on each candle as long as the condition still holds (see image). Is there a way i can make the arrows appear only on the first candle when condition is true and ignore the rest until the condition reverses.?..

You can probably try adding this declaration first:

   int i;
   double buf0,buf1,buf2;
   static int lastBuySignal=-1, lastSellSignal=-1;

Then within each of your 'if' statement, add this:

        if ((buf0 > buf1) && (buf2 < Buy_Level))
        {
          if (lastBuySignal==-1 || lastBuySignal>i+1)
            ExtMapBuffer1[i]=Low[i]-5*Point; 
          lastBuySignal = i;
        }

Edit: But for the above to work, you'll have to heed @William Roeder's advice to fix your "for (i=Bars-50; i>=0; i--)" first... 

 
Seng Joo Thio:

You can probably try adding this declaration first:

Then within each of your 'if' statement, add this:

Edit: But for the above to work, you'll have to heed @William Roeder's advice to fix your "for (i=Bars-50; i>=0; i--)" first... 

Thanks again, it works well.  I highly appreciate you Seng.