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

 
DanilaMactep:

I HAVE CALCULATED A PRICE FOR SETTING A BREAKAWAY LIKE THIS (MAKAR, YOU ARE AT LEAST NOT MY TYPE TO BE TOUCHED;-))

HOW DO YOU CHECK IF THE OBTAINED VALUE FALLS WITHIN ALLOWED RANGES? THAT'S WHAT I DON'T UNDERSTAND, AND HOW DO YOU MAKE SURE THAT IF THE CALCULATED VALUE IS LESS THAN ALLOWED, THE ORDER WOULD BE OPENED AT MINIMUM POSSIBLE DISTANCE ANYWAY?

Try this,

I think you'll figure it out.

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 02.07.2013                                                     |
//|  Описание : Выполняет корректирование ценовых уровней под STOPLEVEL.       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование торгового инструмента                                 |
//|    op - торговая операция                                                  |
//|    pp - цена открытия/установки                                            |
//|    sl - ценовой уровень StopLoss                                           |
//|    tp - ценовой уровень TakeProfit                                         |
//+----------------------------------------------------------------------------+
void CorrectingPrice(string sy, int op, double& pp, double& sl, double& tp) {
  if (sy=="" || sy=="0") sy=Symbol();
  RefreshRates();
  int    di=MarketInfo(sy, MODE_DIGITS);
  int   msl=MarketInfo(sy, MODE_STOPLEVEL);
  int    sp=MarketInfo(sy, MODE_SPREAD);
  double mp=MarketInfo(sy, MODE_POINT);
  double pa=MarketInfo(sy, MODE_ASK);
  double pb=MarketInfo(sy, MODE_BID);
  double ds=NormalizeDouble(pp-sl, di);
  double dp=NormalizeDouble(pp-tp, di);

  if (msl==0) msl=2*sp;
  switch (op) {
    case OP_BUY:
      pp=pa;
      sl=pp-ds;
      tp=NormalizeDouble(pp-dp, di);
      if (sl>pp-msl*mp) sl=pp-msl*mp;
      if (tp>0 && tp<pp+msl*mp) tp=pp+msl*mp;
      break;
    case OP_SELL:
      pp=pb;
      sl=NormalizeDouble(pp-ds, di);
      tp=pp-dp;
      if (sl>0 && sl<pp+msl*mp) sl=pp+msl*mp;
      if (tp>pp-msl*mp) tp=pp-msl*mp;
      break;
    case OP_BUYLIMIT:
      if (pp>pa-msl*mp) {
        pp=pa-msl*mp;
        sl=pp-ds;
        tp=NormalizeDouble(pp-dp, di);
      }
      if (sl>pp-msl*mp) sl=pp-msl*mp;
      if (tp>0 && tp<pp+msl*mp) tp=pp+msl*mp;
      break;
    case OP_BUYSTOP:
      if (pp<pa+msl*mp) {
        pp=pa+msl*mp;
        if (sl>0) sl=pp-ds;
        if (tp>0) tp=NormalizeDouble(pp-dp, di);
      }
      if (sl>pp-msl*mp) sl=pp-msl*mp;
      if (tp>0 && tp<pp+msl*mp) tp=pp+msl*mp;
      break;
    case OP_SELLLIMIT:
      if (pp<pb+msl*mp) {
        pp=pb+msl*mp;
        sl=NormalizeDouble(pp-ds, di);
        tp=pp-dp;
      }
      if (sl>0 && sl<pp+msl*mp) sl=pp+msl*mp;
      if (tp>pp-msl*mp) tp=pp-msl*mp;
      break;
    case OP_SELLSTOP:
      if (pp>pb-msl*mp) {
        pp=pb-msl*mp;
        sl=NormalizeDouble(pp-ds, di);
        tp=pp-dp;
      }
      if (sl>0 && sl<pp+msl*mp) sl=pp+msl*mp;
      if (tp>pp-msl*mp) tp=pp-msl*mp;
      break;
    default:
      Message("CorrectingPrice(): Неизвестная торговая операция!");
      break;
  }
}
 
Alekseu Fedotov:

Try this,

I think you'll get the hang of it.

Alexey, thank you very much for this piece of code. But unfortunately I can't read other people's code due to the consequences of a stroke. May I ask you for an instruction on how to use this code and a recommendation on what to attach it to and how to do it?

Here is what has come to my mind. I was looking for a minimum price for x-bars like this

  int min_bars= iLowest(NULL, // ТЕКУЩИЙ СИМВОЛ
          double min_Price=iHigh(Symbol(),// СИМВОЛ ТЕКУЩЕГО ИНСТРУМЕНТА(НА КОТОРОМ РАБОТАЕТ ГРААЛЬ;-)
          PeriodForWork_pending_orders, // ТФ НА КОТОРОМ АСЧИТЫВАЕМ ЦЕНУ МИНИМУМА
          min_bars//  НА КАКОМ БАРЕ БЫЛ МАКС ТОТ И ПОДСТАВИМ;)
        

but this is not correct, because I assign min price aihai, not low. corrected for low and the error is less, but still occur(((

So if it is not difficult, send instructions on how to use a piece of discounted code...

 

Hi all againJ tried to apply the advice you wrote to my last post, but it didn't help. I'm still getting error 130L, but it's probably not the stop loss itself, but the open price. The open price for a buy stop and a sell stop is obtained by determining the maximum/minimum price for an x number of candles, in this way.

          int max_bars= iHighest(NULL, // ТЕКУЩИЙ СИМВОЛ
          PeriodForWork_pending_orders,// ТФ ДЛЯ ПОИСКА МАКСИМУМА  УСТАНОВКИ ОТЛОЖЕННОГО ОРДЕРА
          MODE_HIGH,// ИЩЕМ МАКСИМАЛЬНОЕ ЗНАЧЕНИЕ ХАЙ
          range_max // ДИАПОЗОН ПОИСКА ИЗ ЭН СВЕЧЕЙ
          ); // ПОЛУЧАЕМ НОМЕР СВЕЧИ НА КОТОРОЙ ИМЕЕТСЯ МАКСИМАЛЬНОЕ ЗНАЧЕНИЕ
          double max_Price=iHigh(Symbol(),// СИМВОЛ ТЕКУЩЕГО ИНСТРУМЕНТА(НА КОТОРОМ РАБОТАЕТ ГРААЛЬ;-)
          PeriodForWork_pending_orders, // ТФ НА КОТОРОМ АСЧИТЫВАЕМ ЦЕНУ МАКСИМУМА
          max_bars//  НА КАКОМ БАРЕ БЫЛ МАКС ТОТ И ПОДСТАВИМ;)
        
          
          );  //КОНЕЦ ПОЛУЧЕНИЯ ДАННЫХ ДЛЯ ПОКУПАК*********** 
          
          //----ПОЛУЧЕНИЕ ДАННЫХ ДЛЯ ОТЛОЖЕННЫХ ПРОДАЖ
          
          
                    int min_bars= iLowest(NULL, // ТЕКУЩИЙ СИМВОЛ
          PeriodForWork_pending_orders,// ТФ ДЛЯ ПОИСКА МАКСИМУМА  УСТАНОВКИ ОТЛОЖЕННОГО ОРДЕРА
          MODE_HIGH,// ИЩЕМ МАКСИМАЛЬНОЕ ЗНАЧЕНИЕ ХАЙ
          range_max // ДИАПОЗОН ПОИСКА ИЗ ЭН СВЕЧЕЙ
          ); // ПОЛУЧАЕМ НОМЕР СВЕЧИ НА КОТОРОЙ ИМЕЕТСЯ МАКСИМАЛЬНОЕ ЗНАЧЕНИЕ
          double min_Price=iLow(Symbol(),// СИМВОЛ ТЕКУЩЕГО ИНСТРУМЕНТА(НА КОТОРОМ РАБОТАЕТ ГРААЛЬ;-)
          PeriodForWork_pending_orders, // ТФ НА КОТОРОМ АСЧИТЫВАЕМ ЦЕНУ МАКСИМУМА
          min_bars//  НА КАКОМ БАРЕ БЫЛ МАКС ТОТ И ПОДСТАВИМ;)
        
          
          ); // КОНЕЦ АЙЛОУ

The problem is not with the stop because I use the same exact stop and atp and fixed, market orders open without errors. It means the problem is in the calculation of price for setting a pending order. I open orders themselves like this

OrderSend(Symbol(),OP_SELLSTOP,lot,min_Price,slippages,min_Price+sl , min_Price-tp ,comment_ao_otloj_sell); // ОТКРКЫВВАЕМ ОТЛОЖЕННЫЙ СЕЛ ОРДЕР

OrderSend(Symbol(),OP_BUYSTOP,lot,max_Price,slippages,max_Price-sl,max_Price+tp ,comment_ao_otloj_buy,magik_number,TimeCurrent()+order_life_time); // ОТКРЫВАЕМ ОТЛОЖЕННЫЙ БАЙ ОРДЕР


P/S and I forgot to mention that I want to use pending orders to simulate some kind of price filter for opening a position, i.e. if the price goes in the right direction, a pending order will trigger, but if not, it will be cancelled and I am out of the market. Please, what am I doing wrong in determining the opening price? And maybe there is an easier solution on price filter for deal?

p/s very kindly, if you write a code that can be applied, write a detailed instruction for it as I'm having serious trouble reading the code. A detailed instruction I mean, what this code is for and how it can be applied (to attach it to a variable and do something with it or something else, and please write your comments to the code in a more understandable way). Thanks in advance ;-) P / S who has knowledge and has experience do not go by, help a brotherly - do not screw around ;-) have to finish the Grail OP ;-).

 
Colleagues!

Who worked with objects. Please send me an example of a piece of code, where a manually drawn triangle, ellipse or square (I think it has a name on the chart) would perform some action - for example, an alert (in case of price hitting the object). I am begging you very much. I do not know much about it and I cannot understand the theory in the reference book, but I suspect that the principle is elementary.
 
DanilaMactep:

Hi all againJ tried to apply the advice you wrote to my last post, but it didn't help. I'm still getting error 130L, but it's probably not the stop loss itself, but the open price. The open price for a buy stop and a sell stop is obtained by determining the maximum/minimum price for an x number of candles, in this way.

The problem is not with the stop because I use the same exact stop and atp and fixed, market orders open without errors. It means the problem is in the calculation of price for setting a pending order. I open orders themselves like this


P/S and I forgot to tell you that I want to use pending orders to simulate some kind of price filter for opening a position, i.e. if the price goes in the right direction, a pending order will trigger, but if it doesn't, it will cancel and I am out of market. What am I doing wrong in determining the opening price? And maybe there is an easier solution on price filter for deal?

p/s very kindly, if you write a code that can be applied, write a detailed instruction for it as I'm having serious trouble reading the code. A detailed instruction I mean, what this code is for and how it can be applied (to attach it to a variable and do something with it or something else, and please write your comments to the code in a more understandable way). Thanks in advance ;-) P / S who has knowledge and has experience do not go by, help a brotherly - do not screw around ;-) I have to finish the Grail O_o ;-)

In the iLowest function, instead of MODE_HIGH, insert MODE_LOW.

 

Can you give me a hint? I have drawn a line segment on a graph. I need to know the start and end prices of the segment. Can it be done?

Files:
EURUSDM1.png  22 kb
 
Forallf:

Can you give me a hint? I have drawn a line segment on a graph. I need to know the start and end prices of the segment. Can it be done?

You can do it at
 
Forallf:

Can you give me a hint? I have drawn a line segment on a graph. I need to know the start and end prices of the segment. Can it be done?

It is desirable that this segment has an original name

 
Alekseu Fedotov:

In the iLowest function, instead of MODE_HIGH, insert MODE_LOW.

Thank you very much for noticing and prompting - I corrected this case and during the 2-year test there were 130 26 errors. At the same time 115 trades opened normally
 
Порт-моне тв:
Colleagues!

Who worked with objects. Please send me an example of a piece of code, where a manually drawn triangle, ellipse or square (I think it has a name on the chart) would perform some action - for example, an alert (in case of price hitting the object). I am begging you very much. I do not know much about this subject and do not understand the theory in the reference book, but I suspect that the principle is elementary.

Someone help me?