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

 
Renat Akhtyamov:

The only thing to consider here is that after the loop block i==x

In this case Array[i] after for(...){} will return an array overrun error and that's it

By doing this you provide fertile ground for possible errors, for example, if further down the code you have a variable j, and you accidentally call it i. Or you may write [i] instead of array index [1] and so on.
I wrote here not about a particular code, but an example of what it may be. It may well happen that there will be no array boundaries and the error will be hard to catch, and you will spend many precious minutes (if not hours) to locate it.
 

(I need a hint, I'm starting to get laggy)

There is a construction like this:

int RangeBar = 10;
 HighRange = iHigh(symbol,0,iHighest(symbol,0,MODE_HIGH,RangeBar,1));
 LowRange = iLow(symbol,0,iLowest(symbol,0,MODE_LOW,RangeBar,1));

It findsHigh/Lowof the range in N candles.

Question: How to find the sameHigh/Low, but not by shadows, but by bodies?

 
Vitaly Muzichenko:

(I need a hint, I'm starting to get laggy)

There is a construction like this:

int RangeBar = 10;
 HighRange = iHigh(symbol,0,iHighest(symbol,0,MODE_HIGH,RangeBar,1));
 LowRange = iLow(symbol,0,iLowest(symbol,0,MODE_LOW,RangeBar,1));

It findsHigh/Lowof the range in N candles.

Question: How to find the sameHigh/Low, but not by shadows, but by bodies?

I need to find fmax(Open[i],Close[i]) for the top and fmin(Open[i],Close[i]) for the bottom of the candle
 
Artyom Trishkin:
I need to look for fmax(Open[i],Close[i]) for the top and fmin(Open[i],Close[i]) for the bottom

That is, do a loop loop instead of two lines looking for High/Low?

HighRange =iHigh(symbol,0,iHighest(symbol,0,MODE_HIGH,RangeBar,1));

LowRange =iLow(symbol,0,iLowest(symbol,0,MODE_LOW,RangeBar,1))

This is needed for the EA, not the indicator, it works correctly in the EA, but how to find the range by bodies - I can't think of one.

 
Vitaly Muzichenko:

That is, do a loop loop instead of two lines looking for High/Low?

HighRange =iHigh(symbol,0,iHighest(symbol,0,MODE_HIGH,RangeBar,1));

LowRange =iLow(symbol,0,iLowest(symbol,0,MODE_LOW,RangeBar,1));

I need it for Expert Advisor, not for indicator, it works correctly in Expert Advisor, but I can't figure out how to find the range by bodies.

I have thrown a test script. It may be inaccurate, I have not been working on it. I hope you will figure it out.

//+------------------------------------------------------------------+
//|                                      sFindRangeByCandlesBody.mq4 |
//|              Copyright 2016, Artem A. Trishkin, Skype artmedia70 |
//|                       https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Artem A. Trishkin, Skype artmedia70"
#property link      "https://login.mql5.com/ru/users/artmedia70"
#property version   "1.00"
#property strict
#property script_show_inputs
//--- input parameters
input int      Begin=1;       // Бар начала диапазона поиска
input int      RangeBars=20;  // Диапазон поиска
//---
int bars=Bars(Symbol(),PERIOD_CURRENT);
int begin=(Begin<0?0:Begin>bars-3?bars-3:Begin);
int rangeBars=(RangeBars<2?2:
               RangeBars>bars-begin?bars-begin:
               RangeBars); // Диапазон поиска
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   MqlRates array[];
   double   high=0, low=0;
   int      highest=-1, lowest=-1;
   if(CopyRates(Symbol(),PERIOD_CURRENT,begin,rangeBars,array)>0) {
      ArraySetAsSeries(array,true);
      high=GetHighestValue(begin,array,highest);
      low=GetLowestValue(begin,array,lowest);
      }
   Print("High=",DoubleToString(high,Digits()),", Highest=",highest,", Low=",DoubleToString(low,Digits()),", Lowest=",lowest);
  }
//+------------------------------------------------------------------+
double GetHighestValue(int bar_begin, MqlRates &array[], int &bar_highest){
   int      sz=ArraySize(array);
   if(sz==0) return(-1);
   double   high=DBL_MIN;
   bar_highest=-1;
   for(int i=0; i<sz; i++) {
      double value=fmax(array[i].open,array[i].close);
      if(value>high) {
         high=value;
         bar_highest=bar_begin+i;
         }
      }
   return(high);
}
//+------------------------------------------------------------------+
double GetLowestValue(int bar_begin, MqlRates &array[], int &bar_lowest){
   int      sz=ArraySize(array);
   if(sz==0) return(-1);
   double   low=DBL_MAX;
   bar_lowest=-1;
   for(int i=0; i<sz; i++) {
      double value=fmin(array[i].open,array[i].close);
      if(value<low) {
         low=value;
         bar_lowest=bar_begin+i;
         }
      }
   return(low);
}
//+------------------------------------------------------------------+
If you have any questions, just ask...
 
Artyom Trishkin:

I've put together a test script. There may be inaccuracies - I wrote it on the spot. I hope you get the hang of it.

//+------------------------------------------------------------------+
//|                                      sFindRangeByCandlesBody.mq4 |
//|              Copyright 2016, Artem A. Trishkin, Skype artmedia70 |
//|                       https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
...
//+------------------------------------------------------------------+
If anything, just ask...

Thank you, it works great!

PS. I understand that this thing will work in the fifth?

 
Vitaly Muzichenko:

Thank you, it works great!

PS. I take it that this thing will work in the fifth?

Yeah. Trying to do cross-platform code. Or with minimal modifications for fifth.
 
I have a very bad hand. please help me to add stop loss for sell (slevel).
int start()
{
//+--------------------------------------------------------------------+
//|   -= stop loss в без убыток =-                                      |
//+--------------------------------------------------------------------+
bool   result;
double stop;
int    cmd,error;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderProfit()>pOPCS)
{
cmd=OrderType();
double blevel=OrderStopLoss()<Bid-Point*TS;
double slevel=OrderStopLoss()>Ask+Point*TS;
//---
if(cmd==OP_BUY || cmd==OP_SELL)
{
while(true)
{
if(cmd==OP_BUY && blevel) stop=Bid-Point*TS;
else                      stop=Ask+Point*TS;
result=OrderModify(OrderTicket(),OrderOpenPrice(),stop,0,0,Orange);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
}
}
}
 
Artyom Trishkin:

I have attached a test script. It may be inaccurate, I have not been working on my hand. I hope you will understand it.

If you have any questions...

Sorry Artem, but I want to show the solution to this problem in a simpler way

  double openCandle[], closeCandle[];
  CopyOpen(_Symbol, PERIOD_CURRENT, 1, 15, openCandle);
  CopyClose(_Symbol, PERIOD_CURRENT, 1, 15, closeCandle);
  double maxCandle = fmax(openCandle[ArrayMaximum(openCandle)], closeCandle[ArrayMaximum(closeCandle)]);
  double minCandle = fmin(openCandle[ArrayMinimum(openCandle)], closeCandle[ArrayMinimum(closeCandle)]);

I hope that everyone will be able to write it in their code and add the required checks. The functionality is the same for both mql4 and mql5.

 
Alexey Viktorov:

Sorry, Artem, but I want to show a simpler solution to this problem

  double openCandle[], closeCandle[];
  CopyOpen(_Symbol, PERIOD_CURRENT, 1, 15, openCandle);
  CopyClose(_Symbol, PERIOD_CURRENT, 1, 15, closeCandle);
  double maxCandle = fmax(openCandle[ArrayMaximum(openCandle)], closeCandle[ArrayMaximum(closeCandle)]);
  double minCandle = fmin(openCandle[ArrayMinimum(openCandle)], closeCandle[ArrayMinimum(closeCandle)]);

I hope that everyone will be able to write it in their code and add the necessary checks. The functionality is the same for both mql4 and mql5.

О! Thank you. I haven't guessed it myself in the morning... It's true, the arrays should be checked for filling. I haven't seen it in quadruple, and in fives, the data are often not filled in the first time because of the lack of historical data.

ZS. You should sleep more - your thoughts will work in that direction.