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

 

Why can't the EA change the pending order price (sell stop) to a more profitable one?


Here is the function that defines the need to move the open price of a pending order

if(orders_total > 0 && position_total == 0)
     {
      OrderGetTicket(0)
      if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_STOP)
        {
         if(OrderGetDouble(ORDER_PRICE_OPEN) - SymbolInfoDouble(_Symbol,SYMBOL_ASK) > ((double)(dist+1)) * _Point) Mode_Order("M",SymbolInfoDouble(_Symbol,SYMBOL_ASK) + ((double)dist * _Point));
        }
      else if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_STOP)
        {
         if(SymbolInfoDouble(_Symbol,SYMBOL_BID) - OrderGetDouble(ORDER_PRICE_OPEN) > ((double)(dist+1)) * _Point) Mode_Order("M",SymbolInfoDouble(_Symbol,SYMBOL_BID) - ((double)dist * _Point));
        };
     };


... and this is the function which already moves the open price of the pending order

void Mode_Order(string act, double price)
  {
   
   MqlTradeRequest request; ZeroMemory(request);
   MqlTradeResult  result;  ZeroMemory(result);
   
   request.symbol = _Symbol;
   
   if(act == "S")
     {
      request.action   = TRADE_ACTION_SLTP;
      request.position = PositionGetTicket(0);
      request.sl       = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY ? PositionGetDouble(POSITION_PRICE_OPEN) - ((double)stop * _Point) : PositionGetDouble(POSITION_PRICE_OPEN) + ((double)stop * _Point));
      request.tp       = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY ? PositionGetDouble(POSITION_PRICE_OPEN) + ((double)profit * _Point) : PositionGetDouble(POSITION_PRICE_OPEN) - ((double)profit * _Point));
     }
   else
     {
      request.action   = TRADE_ACTION_MODIFY;
      request.position = OrderGetTicket(0);
      request.price    = NormalizeDouble(price,_Digits);
     };
   
   if(!OrderSend(request,result)) Print("Не удалось изменить параметры ордера. Код ошибки "+(string)result.retcode);
   
  }
 
Alexandr Sokolov:

Why can't the EA change the pending order price (sell stop) to a more profitable one?


Here is the function that defines the need to move the open price of a pending order


... And this is the function which already moves the open price of the pending order

Is this order the only one? Does the SLTP modification work? If not, the take and stop should be normalized

dist1 is equal to what? Here the order should be set instead of the position

request.position = OrderGetTicket(0);
 
Aleksey Mavrin:

Is the order the only one? Does SLTP modification work? If not, you should normalise the take and stop

dist1 is equal to what? We should set the order instead of the position

Thank you very much! I hadn't noticed that, it seems to be a trifle - but it doesn't work without it


... The order is the only one, everything works, the only problem was moving the pending order

 
Alexandr Sokolov:

Thank you very much! I hadn't noticed that, seemingly a small thing - but it doesn't work without it


... The order is the only one, everything works, the only problem was moving the pending order

1. Please 2. Hmm, I'll normalise it anyway, against the sin. Your price is obtained from the integer multiplied by a normalized double, in the general case apparently also a normalized double is obtained, but I read somewhere that not always, a couple of times a year it may get

something like 1.13250000000000000000001

and then trouble, at the right time, when the most powerful trend doesn't work, and the mortgage again, hello)))

I could be wrong, but I myself reassure and normalise always.

 
Aleksey Mavrin:

1. Please 2. Hmmm, I'm normalising anyway for sin's sake. You get the price from a whole multiplied by a normalised double, in the general case apparently you get a normalised double too, but I read somewhere that not always, a couple of times a year you might get

something like 1.13250000000000000000001

and then trouble, at the right time, when the most powerful trend doesn't work, and the mortgage again, hello)))

I could be wrong, but I myself reassure and normalise always.

Mathematical operations on two normalised numbers give non-normalised results.

In order to get a normalized result of mathematical operations with real numbers, you must return the normalized value of these mathematical operations.

Norm(1.11111) + Norm(2.22222) = NoNorm(res)

Norm(1.11111 + 2.22222) = Norm(res)

 

@ Artyom Trishkin

I have attached the code with this theme.

I found its code in the code base. There was a problem when changing the code.

The input part has the following entries:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
input eTradeType                                    Inp_element_12357534       = TRADETYPE_BUY;                        // First Trade Type
input double                                        Inp_element_3632865        = 0.01;                                 // Start Lot
input double                                        Inp_element_12844407       = 1.0;                                  // Lot Multiplier
input #ifdef __MQL4__ int #else long #endif          Inp_element_2542795        = 400;                                  // Grid Step, points
input #ifdef __MQL4__ int #else long #endif          Inp_element_3135636        = 100;                                  // Target, points
input #ifdef __MQL4__ int #else long #endif          Inp_element_3634918        = 7373;                                 // Magic
sinput string                                       Inp_element_3640359        = "Grid EA";                            // Comment
//+------------------------------------------------------------------+

I want this parameter to change while the program is running: input #ifdef __MQL4__ INT #else long #endif Inp_element_2542795 = 400 // Step grid, points.

Please let me know what I can do to change this parameter each time I run it.

Files:
Manage.mq5  859 kb
 
jaffer wilson :

@ Artyom Trishkin

I have attached the code with this theme.

I found its code in the code base. There was a problem when changing the code.

The input part has the following entries:

I want this parameter to change while the program is running: input #ifdef __MQL4__ INT #else long #endif Inp_element_2542795 = 400 // Step grid, points.

Please let me know what I can do to change this parameter each time I run it.

Honestly, the question is not clear.

Do you want to change the input parameter Inp_element_2542795 every time you start the adviser?

Well, change it in the advisor settings when it starts. Or, if the adviser is already running, and you want to change this parameter, then press F7 - the advisor settings window will appear, and change this parameter in it.

 
Artyom Trishkin:

Honestly, the question is not clear.

Do you want to change the input parameter Inp_element_2542795 every time you start the adviser?

Well, change it in the advisor settings when it starts. Or, if the adviser is already running, and you want to change this parameter, then press F7 - the advisor settings window will appear, and change this parameter in it.

Let me clear. I guess the problem is translation. Actually, it is input value. and I am not getting where this value is getting used for the calculation. I do not want that every time 400 points should be used. I want to keep changing the values. I want to change the values to suitable calculation. Can you help me where I can make changes so that the value become dynamic during the program is running?

 
jaffer wilson :

Let me clear it up. I think the problem is with the translation. It is actually an input value. and I don't understand where this value is used to calculate. I don't want 400 points to be used every time. I want to keep changing the values. I want to change the values for a suitable calculation. Can you help me where I can make changes so that the value becomes dynamic while the program is running?

Create a new global variable. Set the value of the input parameterInp_element_2542795 to it:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
cRunner *Runner;
long g_element_2542795=0;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit(void)
  {
//===============
   g_element_2542795=Inp_element_2542795;
//===============
/* DEBUG MACROS' START */ TRACEERRORS_START
//===============
 
//===============
   Runner=new cRunner;
//===============
 

and replace the entire entry of the string Inp_element_2542795 with this new variable g_element_2542795 using Ctrl + H

 

Hello!

I'm just learning MQL5, so I'm asking for help.

There is a class -https://www.mql5.com/ru/code/18721(Vlad Karputova). Thanks to Vladimir.

It calculates profit (in points, in currency of deposit, swap and commission). But it has no filter for Magik.

I have added filter by Magik (I am not sure if it is correct, so I don't want to post it with errors).

Got the result.

Search deals with POSITION_IDENTIFIER 53821539
DEAL_ENTRY_IN, DEAL_TYPE_SELL, price 1.11164, Deal_Symbol EURUSD, Deal_Magic  222, volume 0.03, commission -0.05, swap 0.00, profit 0.00
DEAL_ENTRY_OUT, DEAL_TYPE_BUY, price 1.11170, Deal_Symbol EURUSD, Deal_Magic  0, volume 0.03, commission -0.05, swap 0.00, profit -0.18
Profit in points -6.0, commission -0.10, swap 0.00, profit -0.18

Search deals with POSITION_IDENTIFIER 53821617
DEAL_ENTRY_IN, DEAL_TYPE_SELL, price 1.11159, Deal_Symbol EURUSD, Deal_Magic  222, volume 0.03, commission -0.05, swap 0.00, profit 0.00
DEAL_ENTRY_OUT, DEAL_TYPE_BUY, price 1.11172, Deal_Symbol EURUSD, Deal_Magic  0, volume 0.03, commission -0.05, swap 0.00, profit -0.39
Profit in points -13.0, commission -0.10, swap 0.00, profit -0.39

Search deals with POSITION_IDENTIFIER 53821750
DEAL_ENTRY_IN, DEAL_TYPE_SELL, price 1.11163, Deal_Symbol EURUSD, Deal_Magic  222, volume 0.03, commission -0.05, swap 0.00, profit 0.00
DEAL_ENTRY_OUT, DEAL_TYPE_BUY, price 1.11169, Deal_Symbol EURUSD, Deal_Magic  0, volume 0.03, commission -0.05, swap 0.00, profit -0.18
Profit in points -6.0, commission -0.10, swap 0.00, profit -0.18

Search deals with POSITION_IDENTIFIER 53847372
DEAL_ENTRY_IN, DEAL_TYPE_SELL, price 1.10971, Deal_Symbol EURUSD, Deal_Magic  222, volume 0.03, commission -0.05, swap 0.00, profit 0.00
DEAL_ENTRY_OUT, DEAL_TYPE_BUY, price 1.11002, Deal_Symbol EURUSD, Deal_Magic  0, volume 0.03, commission -0.05, swap 0.00, profit -0.93
Profit in points -31.0, commission -0.10, swap 0.00, profit -0.93

Search deals with POSITION_IDENTIFIER 53848455
DEAL_ENTRY_IN, DEAL_TYPE_SELL, price 1.10997, Deal_Symbol EURUSD, Deal_Magic  222, volume 0.03, commission -0.05, swap 0.00, profit 0.00
DEAL_ENTRY_OUT, DEAL_TYPE_BUY, price 1.10741, Deal_Symbol EURUSD, Deal_Magic  0, volume 0.03, commission -0.05, swap 0.00, profit 7.68
Profit in points 256.0, commission -0.10, swap 0.00, profit 7.68

Search deals with POSITION_IDENTIFIER 53856130
DEAL_ENTRY_IN, DEAL_TYPE_SELL, price 1.10735, Deal_Symbol EURUSD, Deal_Magic  222, volume 0.03, commission -0.05, swap 0.00, profit 0.00
DEAL_ENTRY_OUT, DEAL_TYPE_BUY, price 1.10769, Deal_Symbol EURUSD, Deal_Magic  0, volume 0.03, commission -0.05, swap 0.00, profit -1.02
Profit in points -34.0, commission -0.10, swap 0.00, profit -1.02
All_Profit in points 166.0, All_commission -0.60, All_swap 0.00, All_profit 4.98
All_Profit in points 0.0, All_commission 0.00, All_swap 0.00, All_profit 0.00

Result for original class and class with filter by Magik coincide. But, there is one point here.

1.Why OUT trades have zero magic.

2.If I have incorrectly corrected the class (adding a magik) - please make the required change to the original class and post it.

(And it would be good to pass a symbol to the class as a parameter).

Thanks for the help.

HistoryPositionInfo version 2
HistoryPositionInfo version 2
  • www.mql5.com
Новое в версии 2: функция GetProfitInPoints осталась, но рекомендуется теперь использовать новую функцию GetProfit: Функция GetProfit возвращает для восстановленной позиции, кроме прибыли в пунктах: комиссию, своп и прибыль в деньгах. Класс CHistoryPositionInfo предназначен для получения прибыли позиции в пунктах, комиссии, свопа и прибыли в...
Reason: