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

 
Tanita Gajduchok:
Hi. How do I make a leaderboard here?

top ?

 
Greetings!

How to write a function like this:

The sum of the rising candlestick volume should be 2 times the sum of the falling volume, e.g. for the last 10 bars.

Like the one on the screenshot.


Files:
IMG_9206.PNG  87 kb
 

Good afternoon, all. I am trying to attach a lot size calculation to the Grail machine, so that the lot is set as a percentage of the deposit in case of loss. In other words, if a stop loss triggers, the specified percentage of the deposit is lost, or if the deposit is small for this percentage, then the lot is set to the minimum possible for the broker... I found a script on some website that does such things and transferred the script code to myself, but the lot is not considered correctly... I did so. In the input variables I declared a variable responsible for the maximum risk.

extern int MaxRisk=1;// МАКСИМАЛЬНЫЙ % УБЫТКА ПРИ СТОП ЛОССЕ?

Then I declare variables in the on tick. A variable that stores the amount of free funds in the account. A variable of a point value of a symbol. A variable of a broker's minimum lot. A variable that stores the value of maximum lot at the broker. And the variable which stores the lot size step.

  double Free =AccountFreeMargin();// ПОЛУЧАЕМ СВОБОДНЫЕ СРЕДССТВА ДЛЯ РАСЧЁТА ОБЪЁМА ЛОТА
  double LotVal =MarketInfo(Symbol(),MODE_TICKVALUE); //СТОИМОСТЬ 1 ПУНКТА
  double Min_Lot =MarketInfo(Symbol(),MODE_MINLOT);   // МИНИМАЛЬНЫЙ ЛОТ РАЗРЕШЁННЫЙ БРОКЕРОМ
double Max_Lot =MarketInfo(Symbol(),MODE_MAXLOT);     // МАКСИМАЛЬНЫЙ ЛОТ РАЗРЕШЁННЫЙ БРОКЕРОМ
double Step =MarketInfo(Symbol(),MODE_LOTSTEP);    // ШАГ ИЗМЕНЕНИЯ РАЗМЕРА ЛОТА

And then the lot volume with a given risk at a certain stop loss is calculated. Stop Loss is calculated by atp or fixed in pips - this calculation works correctly because if I put a fixed lot then all is well open and works. The formula for calculating the lot volume is as follows.

double lot =MathFloor((Free*MaxRisk/100)/(sl*LotVal)/Step)*Step; //СТРАШНАЯ ФОРМУЛА РАСЧЁТА ОБЪЁМА ЛОТА, КОТОРУЮ Я СОВСЕМ НЕ ПОНИМАЮ((((
  if(lot<Min_Lot) lot=Min_Lot; //ЕСЛИ ЛОТ ПОЛУЧИЛСЯ МЕНЬШЕ ЧЕМ МИНИМАЛЬНЫЙ ЛОТ У БРОКЕРА ТО ЛОТ ПРИСВАЕМАЕМ МИНИМАЛЬНЫЙ ЛОТ У БРОКЕРА
if(lot>Max_Lot) lot=Max_Lot;  //ЕСЛИ ЛОТ ПОЛУЧИЛСЯ БОЛЬШЕ ЧЕМ МАКСИМАЛЬНЫЙ ЛОТ У БРОКЕРА ТО ОЛТ ПРИСВАЕВАЕМ МАКС ЛОТ У БРОКЕРА

 


After all of the calculations through the print print lot value to view it.

Print("ЛОТ ДО НОРМАЛИЗАЦИИ= "+lot);
 lot=NormalizeDouble(lot,Digits());
Print("ЛОТ ПОСЛЕ НОРМАЛИЗАЦИИ= "+lot);

What is displayed in the logbook what is logged for lot and stop - the stop values are correct but the lot is not((((

Документация по MQL5: Константы, перечисления и структуры / Состояние окружения / Информация о счете
Документация по MQL5: Константы, перечисления и структуры / Состояние окружения / Информация о счете
  • www.mql5.com
Информация о счете - Состояние окружения - Константы, перечисления и структуры - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
DanilaMactep:

Good afternoon, all. I am trying to attach a lot size calculation to the Grail machine, so that the lot is set as a percentage of the deposit in case of loss. In other words, if a stop loss triggers, the specified percentage of the deposit is lost, or if the deposit is small for this percentage, then the lot is set to the minimum possible for the broker... I found a script on some website that does such things and transferred the script code to myself, but the lot is not considered correctly... I did so. In the input variables I declared a variable responsible for the maximum risk.

Then I declare variables in the on tick. A variable that stores the amount of free funds in the account. A variable of a point value of a symbol. A variable of a broker's minimum lot. A variable that stores the value of maximum lot at the broker. And the variable which stores the lot size step.

And then the lot volume with a given risk at a certain stop loss is calculated. Stop Loss is calculated by atp or fixed in pips - this calculation works correctly because if I put a fixed lot then all is well open and works. The formula for calculating the lot volume is as follows.


After all these calculations I print the lot value to check it.

What is printed in the logbook can be seen at ***

At first glance, the function seems to be OK. The only thing you should put in the formula is not the order stoploss price but the distance from the order opening to the stop in points.

Then we need to normalize the lot to accuracy, not _Digits but to Step - (incremental step of lot size). Print should be outputted through DoubleToString() with the same accuracy, then you will see what you want to see.

 
DanilaMactep:

Good afternoon everyone. I'm trying to get a lot size calculation into the Grail machine,

I've done this

Order_Lots = NormalizeDouble((AccountBalance()/100*Percen)/(MarketInfo(Symbol(),MODE_TICKVALUE)*Stop_Point)-0.005,2);
 
Alexey Viktorov:

At first glance the function seems fine. The only thing we should add to the formula is the distance in points from the order opening to the stop, rather than the stoploss price of the order.

Furthermore: we should normalize the lot to accuracy, not to _Digits but to Step - (incremental step of lot size) and you should output it to Print using DoubleToString() with the same accuracy.

My mathematics is not very good - how to calculate distance from order opening to stop and replace sl with this one?

DoubleToString("ЛОТ ПОСЛЕ НОРМАЛИЗАЦИИ= "+lot);// ВЫВОД ЗНАЧЕНИЯ ЛОТА
Anormalised the lot value like this
lot=NormalizeDouble(lot,Step);// НОРМАЛИЗАЦИЯ ЗНАЧЕНИЯ ЛОТА 

So, it remains to be seen how to calculate the distance from the open position to the stop in the code.

 
DanilaMactep:

So it remains to be seen how to calculate the distance from the open to the stop in the code?

if(buy)
Stop_Point= (Pric_UP-Loss_UP)/Point+Spread; 
else
Stop_Point= (Loss_DN-Pric_DN)/Point+Spread; 
 
MakarFX:

Thank you very much for the piece of code, but the question now is what type to declare the variables in this piece of code and what values to assign to them? I'm not a wizard, I'm just learning.

 
DanilaMactep:

Thank you very much for the piece of code, but the question now is what type to declare the variables in this piece of code and what values to assign to them? I'm not a magician, I'm just learning

Pric_UP

open price to buy

Loss_UP

buy stop loss price

   Spread = StrToInteger(DoubleToString(MarketInfo(Symbol(),MODE_SPREAD),0));
spread
 
Порт-моне тв:
Greetings!

How to write a function like this:

The sum of the rising candlestick volume should be 2 times the sum of the falling volume, e.g. for the last 10 bars.

Like the one on the screenshot.


Can someone help me?

Reason: