Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1521

 
MakarFX:

You're comparing the wrong rsi instead of "per".

So that's it?

 int Signal(int i)
{
  // Снимем показания индикатора
   double Current   = iRSI(NULL, 0, per, PRICE_CLOSE, i);
   double Previous    = iRSI(NULL, 0, per, PRICE_CLOSE, i+1);
   
    double SCurrent   = iRSI(NULL, 0, per, PRICE_CLOSE, i);
     // Для предыдущего бара
   double SPrevious    = iRSI(NULL, 0, per, PRICE_CLOSE, i+1);
      // А теперь проверим всловия для наличия сигналов
   
   // есть сигнал на покупку?
   if(Current <= niz && Close[1]>Open[1])
   
   return(BUY);
   
   if(Current >= verx && Close[1]<Open[1])
    return(SELL);
   
   // Сигнала нет   
   return(-1);
 
jarikn:

So that's it?

Yes, but you can't just rewrite one indicator into another.

Build the logic of the indicator and then write it.


In this case, not all the logic is clear or there is extra data that is not used.
 
MakarFX:

Yes, but you can't just rewrite one indicator into another.

Build the logic of the indicator and then write it.


In this case, not all the logic is clear or there is extra data that is not being used.

see on the screenshot what I have got.

my idea is this.

if rsi is above the 70 level and the candle closes below the open - there should be a signal for sell

I have an idea. If rsi is below 30 and the candle closes above the open - a buy signal.


don't judge, i'm just trying to learn a bit))))

Now tell me what i did wrong.

Files:
 
jarikn:

Look at the screenshot of what I've got.

My idea is this.

If rsi is above 70 and the candle closes below the open - there should be a signal to sell.

If rsi is below 30 and the candle closes above the open - a buy signal.


don't judge, i'm just trying to learn a bit))))

The only thing I'm trying to learn is this.

1) use the button to paste screenshots Image

2) according to your condition - it is correct.

//+------------------------------------------------------------------+
//|                                                   jarikn_RSI.mq4 |
//|                                           Copyright 2020, DrMak. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, DrMak."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Покупаем
#property indicator_label1  "Продаём"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Продаём
#property indicator_label2  "Покупаем"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int   RSI_Period  =14;  //Период рассчета RSI
input int   AlertLevUp  =70;  //Уровень для покупок 
input int   AlertLevDn  =30;  //Уровень для продаж
//--- indicator buffers
double      Buy[];            // Буфер для покупок
double      Sell[];           // Буфер для продаж
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Buy);
   SetIndexBuffer(1,Sell);  
   // Устанавливаем нулевые значения для индикатора, при которых не будет сигнальных стрелок
   SetIndexEmptyValue (0, 0);
   SetIndexEmptyValue (1, 0);
   //Определяем стиль отображения индикаторных линий - стрелка
   SetIndexStyle (0, DRAW_ARROW);
   SetIndexStyle (1, DRAW_ARROW); 
   // Установим значки "стрелки" для буферов
   SetIndexArrow(0, 234);  //Стрелка "вниз" для продаж
   SetIndexArrow(1, 233);  //Стрелка "вверх" для покупок
   //Устанавливаем текст описания стрелок индикатора для отображения информации в всплывающей подсказке.
   SetIndexLabel(0, "Продаём");
   SetIndexLabel(1, "Покупаем");
   //Определяем разрядность значений индикаторных линий - приравниваем разрядности фин. инструмента
   IndicatorDigits (Digits);
   //Строка с кратким названием индикатора выводится в сплывающей подсказке при наведении указателя мыши на стрелку
   IndicatorShortName ("Мой первый индикатор");
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int limit=rates_total-prev_calculated-1;
   if(limit<1) return(0);
   for(int i=limit;i>=0;i--)
     {
      // Снимем показания индикатора
      double RSI = iRSI(NULL,0,RSI_Period,PRICE_CLOSE,i);
      if(RSI>AlertLevUp&&open[i]>close[i])
         Buy[i]=high[i];
      if(RSI<AlertLevDn&&open[i]<close[i])
         Sell[i]=low[i];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
MakarFX:

1) use the button to insert screenshots

2) according to your condition - this is correct.

Thanks. I'll look into it.

 
MakarFX:

1) use the button to insert screenshots

2) according to your condition - right this way

how to write the condition * if rsi crosses from bottom to top AlertLevDn then buy? is it possible or do I need an additional slider?

{
//---
   int limit=rates_total-prev_calculated-1;
   if(limit<1) return(0);
   for(int i=limit;i>=0;i--)
     {
      // Снимем показания индикатора
      double RSI = iRSI(NULL,0,RSI_Period,PRICE_CLOSE,i);
      if(RSI>AlertLevUp)
         Buy[i]=high[i];
      if(RSI<AlertLevDn)
         Sell[i]=low[i];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
jarikn:

how do you write this condition * if rsi crosses AlertLevDn from bottom to top then buy? is it possible or do you need an additional slider?

Your question gives you the answer...

If rsi cross es from bottom to top - it means that the current value is higher and the previous one is lower.

current value

double RSI = iRSI(NULL,0,RSI_Period,PRICE_CLOSE,i);

previous value should be done

double RSI2 = iRSI(NULL,0,RSI_Period,PRICE_CLOSE,i+1);

after that compare

if RSI2 is less than AlertLevDn and RSI is greater than AlertLevDn

you have a buy)

 
MakarFX:

Your question gives you the answer...

If the rsi crosses from bottom to top, it means the current value is higher and the previous value is lower.

current value

the previous value must be done

after that compare

if RSI2 is less than AlertLevDn and RSI is greater than AlertLevDn

You have a buy)

Got it. Thanks)

 
jarikn:

Got it. Thank you.)

correct

int limit=rates_total-prev_calculated-2;
 

Greetings, could you give me a hint? MT5.

I need to get the values of Bollinger Bands.

I write

#property strict

#include <Indicators\Trend.mqh>
CiBands       m_bands;

input int Magic = 778871;

int OnInit(){//////////////***OnInit()****///*************OnInit()*******/////////***/////////////////////////OnInit()


Print(m_bands.Create(Symbol(), PERIOD_CURRENT, 20, 0, 2, PRICE_CLOSE));
Print((string)m_bands.MaPeriod());
Print((string)m_bands.Base(Bars(NULL, PERIOD_CURRENT)));

return(INIT_SUCCEEDED);
} 

Outputs something similar to double_max.

At the same time it gets period, offset, and everything else correct.