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

 
giannis1386:

I'm completely confused.

double blevel=OrderStopLoss()<Bid-Point*TS; works for me. SL goes after the price only in profit.

double slevel=OrderStopLoss()>Ask+Point*TS; I do not know how to add this one to else

It's not a bool.

Not a bool, but it's called implicit conversion

What are you doing anyway? Trawl?

 
Artyom Trishkin:

Not a bool, but it's called an implicit conversion

What are you doing anyway? Trawl?

Yes tried to do a trawl. the original script was just to close when the order reached profit
 
giannis1386:
Yes tried to make a trawl. the original script was just to close when the profit on the order was reached
Not a script, an EA. You are confusing the types of programs. The script is executed once after it is loaded on the chart and then deleted. If it is not looped. It is unlikely that someone will make a looped script to close positions.
 
giannis1386:
Yes, I tried to make a trawl. the original scrip was just to close it when it reached profit on an order

Logic:

If the profit of the current order selected in the loop is greater than the set value, if its stoploss is less (for Buy)/more (for Sell) than the calculated stoploss trawl level, then shift the stop to the new value.

This is in a nutshell.

 
Artyom Trishkin:
Isn't that what I suggested?

Yeah, well... I looked at your code so inattentively, it's better to say I didn't look at it at all. But that's half the trouble, then I've said such rubbish that I'm almost ashamed of. For some reason I suddenly had this crazy idea that ArrayMaximum() might be applied to an array of structures or even to a separate field of this array.

I repent, I do not advise anyone else to think so, it does not work...:)

 
Alexey Viktorov:

Yeah, well... I looked at your code so inattentively, it's better to say I didn't look at it at all. But that's half the trouble, then I've said such rubbish that I'm almost ashamed of. For some reason I suddenly had this crazy idea that ArrayMaximum() could be applied to an array of structures or even a separate field of this array.

I repent, I do not advise anyone else to think so, it does not work...:)

Well... And what about me? I looked through your code in the same way and thanked you - because it's really shorter...

In short - both worth a joke...

 
Artyom Trishkin:
Not a script, an Expert Advisor. You are confusing the types of programs. The script is executed once after it is loaded on the chart and then deleted. If it is not looped. Hardly anyone will make a looped script to close positions.

according to the author:

Here is a slightly modified standard MT4 close.mq4 script

Put the file in the MetaTrader\experts\Scripts folder,

 
Artyom Trishkin:

Well... And me? Just as I glanced at your code, I bowed in gratitude - it's really shorter...

In short - both worth a joke...

No, I don't agree. This code

  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)]);

tested and works. There are simple user-defined double arrays, and CopyRates() fills an array of structures and therefore you can't choose maximum and minimum by using functions of working with arrays.

 
giannis1386:

according to the author:

Here is a slightly modified standard MT4 close.mq4 script

Put the file in MetaTrader\experts\Scripts folder,

So his script works only once - I put it on the chart, the script worked(closed positions) and unloaded.

You don't need a script, you need an EA.

 
Alexey Viktorov:

Nope, I disagree. This code

  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)]);

is tested and works. Here are simple custom double arrays, and CopyRates() fills an array of structures and therefore you can't select max and min using array functions.

Well, I'm telling you - I was watching your code super attentively too. So - the joke's about me.

SZY... But my code returns four values: High/Low/Highest/Lowest, yours returns only High and Low, and my code checks success of data copying (though not all of them), yours doesn't...

That's how my code checks for complete copying of required data:

//+------------------------------------------------------------------+
//|                                      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)==rangeBars) {
      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);
}
//+------------------------------------------------------------------+
Reason: