Questions from Beginners MQL5 MT5 MetaTrader 5 - page 824

 
lil_lil:

Thanks, how do I write a loop to find the number of the bar crossing the two MAs when the first two points of the algorithm are met and there are values of the two MAs on the first bar?

/

.


Aha, you solved it not through arrays after all.

Note: The solution through arrays (copying of LAST THREE values from the indicators into arrays First[], Second[] and Third[]) isCrossing of two iMAs- OnTick():

//--- We look for crossing of two indicators
   double   First[];
   double   Second[];
   double   Third[];
   ArraySetAsSeries(First,true);    // index [0] - the most right bar on a charts
   ArraySetAsSeries(Second,true);   // index [0] - the most right bar on a charts
   ArraySetAsSeries(Third,true);    // index [0] - the most right bar on a charts
   int      buffer_num=0;           // indicator buffer number 
   int      start_pos=0;            // start position 
   int      count=3;                // amount to copy 
   if(!iMAGet(handle_iMA_First,buffer_num,start_pos,count,First))
      return;
   if(!iMAGet(handle_iMA_Second,buffer_num,start_pos,count,Second))
      return;
   if(InpFilterMA)
      if(!iMAGet(handle_iMA_Third,buffer_num,start_pos,count,Third))
         return;
//--- step 1: check in the arrays bars [0] and [1]
   if(First[0]>Second[0] && First[1]<Second[1]) // buy
     {
      if(InpFilterMA)
         if(Third[0]>=First[0])
            return;
      if(!RefreshRates())
        {
         PrevBars=iTime(1);
         return;
        }
      double sl=m_symbol.Bid()-InpStopLoss*m_adjusted_point;
      double tp=m_symbol.Ask()+InpTakeProfit*m_adjusted_point;
      OpenBuy(sl,tp);
      return;
     }
   else if(First[0]<Second[0] && First[1]>Second[1]) // sell
     {
      if(InpFilterMA)
         if(Third[0]<=First[0])
            return;
      if(!RefreshRates())
        {
         PrevBars=iTime(1);
         return;
        }
      double sl=m_symbol.Ask()+InpStopLoss*m_adjusted_point;
      double tp=m_symbol.Bid()-InpTakeProfit*m_adjusted_point;
      OpenSell(sl,tp);
      return;
     }
//--- step 2: on a step of 1 crossing haven't found. check in the arrays bars [0] and [2]
   if(First[0]>Second[0] && First[2]<Second[2]) // buy
     {
      //--- search in history
      if(SearchPositions(iTime(start_pos+3),iTime(start_pos)))
         return;
      if(!RefreshRates())
        {
         PrevBars=iTime(1);
         return;
        }
      double sl=m_symbol.Bid()-InpStopLoss*m_adjusted_point;
      double tp=m_symbol.Ask()+InpTakeProfit*m_adjusted_point;
      OpenBuy(sl,tp);
      return;
     }
   else if(First[0]<Second[2] && First[1]>Second[2]) // sell
     {
      //--- search in history
      if(SearchPositions(iTime(start_pos+3),iTime(start_pos)))
         return;
      if(!RefreshRates())
        {
         PrevBars=iTime(1);
         return;
        }
      double sl=m_symbol.Ask()+InpStopLoss*m_adjusted_point;
      double tp=m_symbol.Bid()-InpTakeProfit*m_adjusted_point;
      OpenSell(sl,tp);
      return;
     }


In your case (one value at a time), the loop will look like this:

   int      signal=0;                                 // "-1" -> Sell; "0" -> Not signal; "1" -> Buy
   double   MA1_curr=iMAGet(handle_iMA_1,0);          // the value of the 1st MA on i bar
   double   MA2_curr=iMAGet(handle_iMA_2,0);          // the value of the 1st MA on i bar
   for(int i=0;i<100;i++)
     {
      double   MA1_prev=iMAGet(handle_iMA_1,i+1);     // the value of the 1st MA on i+1 th bar
      double   MA2_prev=iMAGet(handle_iMA_2,i+1);     // the value of the 1st MA on i+1 th bar
      //---
      signal=0;                                       // "-1" -> Sell; "0" -> Not signal; "1" -> Buy
      if(MA1_prev<MA2_prev && MA1_curr>MA2_curr)
         signal=1;
      else if(MA1_prev>MA2_prev && MA1_curr<MA2_curr)
         signal=-1;
      //---
      MA1_curr=MA1_prev;
      MA2_curr=MA2_prev;
     }

MA1_curr - the value of the indicator MA1 on bar #i, MA1_prev - the value of the indicator MA1 on bar #i+1
MA2_curr - the value of the indicator MA2 on bar #i, MA2_prev - the value of the indicator MA2 on bar #i+1

When you get a crossing (the variable signal is either "-1" or "1") - the value of i will be the number of the bar where the crossing occurred.

This is the simplest algorithm for finding the crossover, but it is not the most accurate.

 
Vladimir Karputov:

Yeah, you decided not to use arrays after all...


Thank you, your otherMA Cross EA was used as an example

I want to enter it on the third or fifth or...n bars after crossing

Vladimir Karputov:

In your case (getting one value) the cycle will look like this

Why is there no exit from the loop when a crossover is found?

Can these conditions be put outside the loop?

 if(signal==-1)num_bar_b=i;
 if(signal==1)num_bar_s=i;

.

 

Good afternoon. Could you please tell me in which format to download historical data from Finam or MFD.ru server to import quotes history? I cannot do it at all. I can only save it from txt to csv and that's all.

 
Eol:

Good afternoon. Could you please tell me in which format to download historical data from Finam or MFD.ru server to import quotes history? I cannot do it at all. I can only save it from txt to csv and that's all.


You don't have to download anything in MetaTrader 5 :) - As soon as you connect to the trading server, the real tick history is available to you. Look towardsCopyTicks

 
Vladimir Karputov:

In MetaTrader 5 you don't need to download anything :) - As soon as you connect to the trade server, the real tick history is available to you. Look towardsCopyTicks


I just need 10 years worth of Russian Stocks. My broker doesn't give me that much and is unlikely to find one.

 
5.0 Build 1755

Problem description

Good evening, have a question on MQL5 if you can help.
I don't have any textbooks on 5 yet.
The question is.


#property indicator_level1 30 - I have defined the parameter. When I load the indicator on the chart. I get it.
I change the line by function ColorBuffer[], if the line descends below this parameter.

I have a problem here. If I open the indicator and change the parameter for another one, even after loading the indicator the level line is placed there, but the indicator calculates it using the parameter that was set before, i.e. #property indicator_level1 30

How should I address this level in order to be able to change it at runtime?

- Already tried to check the parameter. And re-calculate all the indicator chain.
But it still won't redraw it the way I want it to!

Help!
:)

Sequence of operations

RSI.mqh

#property indicator_level1 30

#property indicator_level2 70


In the Calculaite description code. At the end of the line where the line goes through For, I added the function to change colour of the line if it is below this level.
ExtColorsBuffer [i] =0;
if (ExtRSIBuffer [i]> indicator_level2& indicator_levelcolor!=NULL) {
ExtColorsBuffer [i] =1;
}
if (ExtRSIBuffer [i]< indicator_level1& indicator_levelcolor!=NULL) {
ExtColorsBuffer [i] =2;

Expected result


I see the same as on the picture but when I open settings and change level indicator_level1 / 2 in the process of using the indicator I get a redraw. The indicator is re-drawn so if the line crosses a level it changes its colour. But for me it is on the same level as before :)

More info

...

Files:
rrgpusgf5g.png  69 kb
 
Ivan Stepanenko:
5.0 Build 1755

Problem description

Good evening, have a question on MQL5 if you can help.
I don't have any textbooks on 5 yet.
The question is.


#property indicator_level1 30 - I have defined the parameter. When I load the indicator on the chart. I get it.
I change the line by function ColorBuffer[], if the line descends below this parameter.

I have a problem here. If I open the indicator and change the parameter for another one, even after loading the indicator the level line is placed there, but the indicator calculates it using the parameter that was set before, i.e. #property indicator_level1 30

How should I address this level in order to be able to change it at runtime?

- Already tried to check the parameter. And re-calculate all the indicator chain.
But it still won't redraw it the way I want it to!

Help!
:)

Sequence of operations

RSI.mqh

#property indicator_level1 30

#property indicator_level2 70


In the Calculaite description code. At the end of the line where the line goes through For, I added the function to change colour of the line if it is below this level.
ExtColorsBuffer [i] =0;
if (ExtRSIBuffer [i]> indicator_level2& indicator_levelcolor!=NULL) {
ExtColorsBuffer [i] =1;
}
if (ExtRSIBuffer [i]< indicator_level1& indicator_levelcolor!=NULL) {
ExtColorsBuffer [i] =2;

Expected result


I see the same as on the picture but when I open settings and change level indicator_level1 / 2 in the process of using the indicator I get a redraw. The indicator is re-drawn so if the line crosses a level it changes its colour. But for me it is on the same level as before :)

More info

...

IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrCrimson);      // Поменяет цвет самого первого по счёту уровня на цвет Crimson
IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrDeepSkyBlue);  // Поменяет цвет второго по счёту уровня на цвет DeepSkyBlue
 
Ivan Stepanenko:
5.0 Build 1755

Problem description

Good evening, have a question on MQL5 if you can help.
I don't have any textbooks on 5 yet.
The question is.

I would like to know why I don't like the answer in a separate topic.

This is the forum for trading, automated trading systems and strategy testing.

I need help in MQL5 #property indicator_level

Alexey Viktorov, 2018.01.31 09:06

Igor, we are both wrong. I didn't immediately understand the task...

Besides #property directive you can set the indicator level with the function

IndicatorSetDouble(INDICATOR_LEVELVALUE,0,20);

Accordingly, this level should be set in incoming indicator parameters. In this case, the level will be drawn by the specified value and it should be changed by this value.

Test case

#property indicator_separate_window
#property indicator_buffers     1
#property indicator_type1       DRAW_LINE
#property indicator_plots       1
#property indicator_color1      clrGreenYellow
#property indicator_level1      0
#property indicator_levelcolor  clrPurple

input double level = 0.6;
double buf[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, buf, INDICATOR_DATA);
   IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, level);
//---
   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 i = 0, limit = prev_calculated == 0 ? 0 : rates_total-1;
    for(i = limit; i < rates_total; i++)
     {
      buf[i] = i%2;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

I hope it is not difficult to change the colour according to the level defined in the input parameters.
 

Forum on trading, automated trading systems and trading strategy testing

Bugs, bugs, questions

Aleksey Rodionov, 2018.02.06 16:31

Here is an interesting one:

Print("Баланс на счёте = ",AccountInfoDouble(ACCOUNT_BALANCE));
printf("ACCOUNT_BALANCE =  %G",AccountInfoDouble(ACCOUNT_BALANCE));


If you remove %G in pintf then value will not be displayed, but in Print it is displayed without %G

Also in Print the balance value 10000.0 is shown, but in printf just the integer 10000

I am more interested in what is the difference between Print and Printf and most importantly, why it is not shown without %G. I did not understand anything in the reference book.


 

Hello colleagues, I have a question, I am trying to insert a widget of my signal in the signature on the forum "forex money", but they have disabled or blocked HTML, is there any way to convert it to a URL?

Here's the code <iframe frameborder="0" width="220" height="140" src="https://www.mql5.com/ru/signals/widget/signal/328l?t=green"></iframe>