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

 
Who knows - if you close part of an order, will its magician or comment disappear?
 
Renat Akhtyamov:
Who knows - if you close part of the order, its magik or comment will disappear?

The magik will remain, the order ticket will change and the comment will contain the parent order ticket from#xxxxxxxxxxx. The parent order will have a child order ticket: to#xxxxxxxxxxx

 
Artyom Trishkin:

The magik will remain, the order ticket will change and the comment will contain the parent order ticket from#xxxxxxxxxxx. The parent order will have a child order ticket: to#xxxxxxxxxxx

Ok. Thank you!
 
geratdc:


Yes, this K is also in externalvariables in top hat - maybe it's return value of some function...


Alexey, what can you say about my problem? I try to attach trailing stop to reverse orders. Have you encountered such a problem?

And in order to answer something on this problem, you have to understand the problem itself. And from this

geratdc:

I need help - I have 3 stop orders but example of trailing stop from video tutorials :) only for unlinked orders, in my case they are linked by algorithm, i.e. takeprofit is calculated by sum of three orders or one if I got into a trend. Well, I have thought of something but my Expert Advisor has not started to earn more - it has started to earn the same amount of orders but I get 2-3 times less trades compared to my previous EA without trailing. So my trailing is not trailing but some nonsense. How should I place it on linked orders? Can someone give me an idea? I'm afraid I can't think of anything else. Here is the result of my "trailing" - it is shown in blue:

The result for 1 test is attached. Is there a trailing stop there or not? I don't understand. Why was not the profit for the same period increased? Only the number of deals decreased by 2-3 times?


I cannot understand anything. The strangest thing is"I've invented something", but have you understood what you've invented? And this is"How do I make it on linked orders". But then there's"I'm afraid I can't think of anything else".

 
Alexey Viktorov:

And in order to answer anything on the problem, you have to understand the problem itself. And from this.

I can't understand anything. The weirdest thing is"I've got it figured out," but do you know what you've figured out? And then there's"How do I make it on the linked warrants?" But then there's"I'm afraid I can't think of anything else".


As I understand, trailing stops cannot be applied correctly to orders, so we should pay much attention to this function CalculateProfit, namely to variable "op" according to which orders are closed if the sum of profits for all 3 orders is equal to TakeProfit and try to trawl through this variable. I will try to think out something in this direction. I understand that it is difficult to deal with third-party abracadabra)))).
 
geratdc:

As I understand, trailing stops cannot be applied correctly to orders, that is why we should take a closer look at this function CalculateProfit, in particular, at variable "op" according to which orders are closed if the sum of profits for all 3 orders is equal to TakeProfit and try to trail through this variable. I will try to think out something in this direction. I understand that it's difficult to deal with third-party abracadabra))).
Why can't you apply it? It is necessary not to attach or invent something to other people's functions, and write your own so that they perform all that you want. What could be the problem? Whatever the number of orders, calculate the average price and trawl from it. When some condition is fulfilled, when going through all orders in the loop, rearrange their stops on a new level, which is calculated from the average price of all orders existing in one direction.
 
//+------------------------------------------------------------------+
//|                                     BLACKJACK&HOOKERS TrailX.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double  Lots           = 0.01;
extern double  TakeProfit     = 1;
extern int     Step           = 1;

extern double  TrailXStep     = 1;
extern double  TrailXStop     = 1;

extern int     MaPeriod       = 100;
extern int     MaShift        = 1;

extern int     Magic          = 123;
extern int     Slippage       = 5;

int timeprev=0;

extern double price,op,cn,tp;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(Digits == 3 || Digits == 5)
      //TakeProfit     *= 10;
      Step           *= 10;
      //TrailXStep     *= 10;
      //TrailXStop     *= 10;
      Slippage       *= 10;

      
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   if (timeprev == Time[0]) return;
   timeprev = Time[0];
   double maprice=iMA(Symbol(),0,MaPeriod,MaShift,MODE_SMA,PRICE_CLOSE,1); 
   op=CalculateProfit();
   cn=CountTrades();
   tp=TakeProfit;
   if (tp>TakeProfit)
   {
      TakeProfit+=tp;
   }
   
 

 
   if (cn==0 && CountBuy() + CountSell() == 0 && Ask > maprice)
   {
      if (OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "", Magic, 0, Blue)<0)
            Print("Не удалось открыть ордер на покупку");
            
   }
   
   if (cn==0 && CountBuy() + CountSell() == 0 && Bid < maprice)
   {
      if (OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "", Magic, 0, Red)<0)
            Print("Не удалось открыть ордер на продажу");
            
   }
   

   
   if(cn==1 && CountBuy()==1) 
   {
      price=FindLastBuyPrice();
      if((price-Ask)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"",Magic,0,Red)<1)
            Print("Не удалось открыть ордер на продажу");
      }
   }
   else if(cn==1 && CountSell()==1) 
   {
      price=FindLastSellPrice();
      if((Bid-price)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"",Magic,0,Blue)<1)
            Print("Не удалось открыть ордер на покупку");
      }
   }
   
   
   
   if(cn==2 && CountBuy()==1 && CountSell()==1) 
   {
      price=FindLastSellPrice();
      if((price-Bid)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"",Magic,0,Red)<1)
               Print("Не удалось открыть ордер на продажу");
               
      }
      else 
      price=FindLastSellPrice();
      if((Bid-price)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"",Magic,0,Blue)<1)
            Print("Не удалось открыть ордер на покупку");
           
      }
   }
   else if(cn==2 && CountSell()==1 && CountBuy()==1) 
   {
      price=FindLastBuyPrice();
      if((Ask-price)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"",Magic,0,Blue)<1)
               Print("Не удалось открыть ордер на продажу");
               
      }
      else 
      price=FindLastBuyPrice();
      if((price-Ask)>=Step*Point)
      {
         if(OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"",Magic,0,Red)<1)
            Print("Не удалось открыть ордер на покупку");
           
      }
   }
   
   Trailing();

   if (op>=tp)
   {
         CloseAll();    
   }


}    
//---------------------------------------------------------------------------------------
void Trailing()           
            {  

               if (op > (TakeProfit+TrailXStep))
               {
                  tp=(TakeProfit+TrailXStep);
               }
               if (op > (TakeProfit+TrailXStep-TrailXStop) && op < (TakeProfit+TrailXStep+TrailXStop))
               {
                  tp=(TakeProfit+TrailXStep-TrailXStop);
               }
               
            }
//---------------------------------------------------------------------------------------
double CalculateProfit()
  {
   double oprofit=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               oprofit+=OrderProfit();
              }
           }
        }
     }
   return(oprofit);
  }
//--------------------------------------------------------------------------------------
void CloseAll()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY)
              {
               if(!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage))
                  Print("Не удалось закрыть ордер на покупку");
              }
            if(OrderType()==OP_SELL)
              {
               if(!OrderClose(OrderTicket(),OrderLots(),Ask,Slippage))
                  Print("Не удалось закрыть ордер на продажу");
              }
           }
        }
     }
  }
//---------------------------------------------------------------------------------------------------
double FindLastBuyPrice()
  {
   int oldticket,ticket= 0;
   double oldopenprice = 0;
   for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_BUY)
           {
            oldticket=OrderTicket();
            if(oldticket>ticket)
              {
               ticket=oldticket;
               oldopenprice=OrderOpenPrice();
              }
           }
        }
     }
   return(oldopenprice);
  }
//---------------------------------------------------------------------------------------------------
double FindLastSellPrice()
  {
   int oldticket,ticket= 0;
   double oldopenprice = 0;
   for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL)
           {
            oldticket=OrderTicket();
            if(oldticket>ticket)
              {
               ticket=oldticket;
               oldopenprice=OrderOpenPrice();
              }
           }
        }
     }
   return(oldopenprice);
  }
//----------------------------------------------------------------------------------------------
int CountBuy()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_BUY)
            count++;
        }
     }
   return(count);
  }
//----------------------------------------------------------------------------------------------
int CountSell()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL)
            count++;
        }
     }
   return(count);
  }
//+---------------------------------------------------------------------------------+
int CountTrades()
{
    int count=0;
    for (int i=OrdersTotal()-1; i>=0; i--)
      {  
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                  count++; 
         }
      }
      return(count);
}
//----------------------------------------------------------------------------------+
int FindLastOrderType()
{
      for(int i = OrdersTotal()-1; i>=0; i--)
      {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
            return(OrderType());
         } 
      }
      return(-1);
}
//----------------------------------------------------------------------------------+

This is a variant of trailing by CalculateProfit() function, both trailing variants have the same indicators in trading - both in number of trades and in profits. Profits are not many - now I will add volatility and more accurate entry indicators. I think one and the same indicator can solve these problems, right? Say, which analyses the last 5 bars or something like that. What are such bar indicators called? And what is the best timeframe ? Share your thoughts please, how can I get two birds with one indicator ... I have two birds with one stone :)

How do you see the trailing bars on this EA?

 
Alexey Viktorov:
Why can't you apply it? You don't have to attach or invent something to someone else's function, but write your own so that it does what you want it to do. What could possibly be the problem? Whatever the number of orders, calculate the average price and trawl from it. When some condition is fulfilled, when going through all orders in the loop, rearrange their stops on a new level which is calculated from the average price of all orders of one direction.

Saved it, thank you. I don't think so yet. I should have a library in my head of codes and functions, while in the video tutorials we've gone through all the stuff I've implemented so far. Maybe I'll get better at it and put your advice into practice. What I have invented today, does it look like a trawl? Well, the Expert Advisor trawls according to the reports but I do not know which one is the correct one - the incorrect trawl will only increase the probability of loss. I would say it has a 0.9 probability to fail, roughly so)))) I would say it is very afraid of flat and the indicator is weak now - stupidly we join the trend and when we have 3 orders and the market suddenly changes direction and gives us a drawdown - we lose.
 
geratdc:

Saved it, thanks. I can't think like that yet. I must have a library in my head of codes and functions, and I have gone through the video tutorials, that's what I am using now. Maybe I'll get better at it and put your advice into practice. What I have invented today, does it look like a trawl? Well, the Expert Advisor trawls according to the reports but I do not know which one is the correct one - the incorrect trawl will only increase the probability of loss. I would say it has a 0.9 probability to fail, roughly so)))) I would say it is very afraid of flat and the indicator is weak now - we are just joining the trend and when we have 3 orders and the market suddenly changes direction and gives us a drawdown - we are out of the money.

Well, I'll try to do my bit more to educate you.

I'll address you as "you" when I send it. I have a snapshot of the pointer...

Please tell me, doesn't it seem wrong to call the same function 5 times on the same tick? But that's half the trouble. This function goes through all the orders. And all this 5 times in one tick... And I count 4 such functions. We can easily fit 3 more functions with the loop to search all the orders without racking our brains.

Here are two of them.

if (cn==0 && CountBuy() + CountSell() == 0 && Ask > maprice)
if (cn==0 && CountBuy() + CountSell() == 0 && Bid < maprice)
if(cn==1 && CountBuy()==1)


else if(cn==1 && CountSell()==1) 
if(cn==2 && CountBuy()==1 && CountSell()==1) 


else if(cn==2 && CountSell()==1 && CountBuy()==1)

These are .

Trailing();


}    
//---------------------------------------------------------------------------------------
void Trailing()           
            {  

               if (op > (TakeProfit+TrailXStep))
               {
                  tp=(TakeProfit+TrailXStep);
               }
               if (op > (TakeProfit+TrailXStep-TrailXStop) && op < (TakeProfit+TrailXStep+TrailXStop))
               {
                  tp=(TakeProfit+TrailXStep-TrailXStop);
               }
               
            }


It is not even close to trailing.

To understand this, we need to clearly understand the definition of trailing. I cannot remember verbatim, but it can be described in the following way: "Moving StopLoss level following the price in order to decrease possible losses or increase "guaranteed" profit.


And this

extern double price,op,cn,tp;

what does it look like when you run the Expert Advisor? Probably four lines... are they needed?


One last thing for today: You don't need to have a library in your head. It's enough to have documentation and know how to use it. I still can't remember all parameters of iMA(_Symbol, _Period, ,,,,,) and further I write only after looking at the documentation. And so almost all functions. Luckily they made tooltips not so long ago which don't help much. I'm not trying to memorize how to write these or those enums. So I have to look at the documentation every time.

 

Can you tell me which function checks the status of this checkbox?