Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1590

 

Good evening and good mood everyone!

I am trying to understand one question. Let's say there are two grids of orders set in both directions from the current price. When the current price moves upwards, all pending orders can be activated (this is an ideal variant). Or they may not activate, but it is not so important for now. If all pending orders are activated upwards, then how to determine the prices of the first and last positions of the upper grid (marked with red arrows in the picture). This is the most important point in my question!!! When the price moves downwards, it is the same.

I prepared a small script to start at least from something. Yes, and one more important point - the number of open positions up and down can change in the course of trading. What are your thoughts on this issue?

Regards, Vladimir.

//+------------------------------------------------------------------+
//|Test.mq5 |
//+------------------------------------------------------------------+
#property script_show_inputs
input int Quantity = 3; // Number of last orders
void OnStart()
  {
   if(HistorySelect(0, TimeCurrent()))
     {
      Print("Story Selection = ", HistorySelect(0, TimeCurrent()));
      int total_deals = HistoryDealsTotal(); // get the number of deals in the list
      Print("Number of transactions on the current account = ", total_deals); // and output the value to the log
      int total_orders = HistoryOrdersTotal(); // get the number of orders in the list
      Print("Number of historical orders = ", total_orders); // and output the value to the log
      for(int i = total_orders - Quantity; i < total_orders; i++) // loop through the required number of orders
        {
         ulong ticket; // declare a variable for the ticket
         if((ticket = HistoryOrderGetTicket(i)) > 0) // if the order ticket is received by its number in the list
           {
            //--- then we get the required order properties
            double open_price     = HistoryOrderGetDouble(ticket, ORDER_PRICE_OPEN);
            datetime time_setup   = (datetime)HistoryOrderGetInteger(ticket, ORDER_TIME_SETUP);
            datetime time_done    = (datetime)HistoryOrderGetInteger(ticket, ORDER_TIME_DONE);
            string symbol         = HistoryOrderGetString(ticket, ORDER_SYMBOL);
            long order_magic      = HistoryOrderGetInteger(ticket, ORDER_MAGIC);
            long positionID       = HistoryOrderGetInteger(ticket, ORDER_POSITION_ID);
            double initial_volume = HistoryOrderGetDouble(ticket, ORDER_VOLUME_INITIAL);
            string type           = GetOrderType((int)HistoryOrderGetInteger(ticket, ORDER_TYPE));
            //--- and display information about the order properties
            printf("#ticket %d %s %G %s at %G was set up at %s => done at %s, pos ID=%d",
                   ticket,                   // order ticket
                   type,                     // type
                   initial_volume,           // exposed volume
                   symbol,                   // the character that was set
                   open_price,               // specified opening price
                   TimeToString(time_setup), // order setting time
                   TimeToString(time_done),  // time of execution or deletion
                   positionID                // ID of the position in which the order trade was entered
                  );
           }
        }
     }
  }
//+------------------------------------------------------------------+
//|Function returns the string name of the order type |
//+------------------------------------------------------------------+
string GetOrderType(int type)
  {
   string str_type = "unknown operation";
   switch(type)
     {
      case(ORDER_TYPE_BUY): return("buy");
      case(ORDER_TYPE_SELL): return("sell");
      case(ORDER_TYPE_BUY_LIMIT): return("buy limit");
      case(ORDER_TYPE_SELL_LIMIT): return("sell limit");
      case(ORDER_TYPE_BUY_STOP): return("buy stop");
      case(ORDER_TYPE_SELL_STOP): return("sell stop");
      case(ORDER_TYPE_BUY_STOP_LIMIT): return("buy stop limit");
      case(ORDER_TYPE_SELL_STOP_LIMIT): return("sell stop limit");
     }
   return(str_type);
  }  
//+------------------------------------------------------------------+
 
Good evening. Strategy tester MT5. Optimisation mode. Parameters tab. How can I make it so that when I tick a parameter line that should be optimised, the font of this line switches from normal font to bold font?
[Deleted]  
_amba #:
Good evening. Strategy tester MT5. Optimisation mode. Parameters tab. How can I make it so that when I tick a parameter line that should be optimised, the font of this line switches from normal font to bold font?

It already does that! At least it does on my terminal (build 5260, on Windows 10).


 
MrBrooklin #:

Good evening and good cheer everyone!

Regards, Vladimir.

You forgot to write that the account is netting.

You have the time ofposition opening . Request the history of trades from the beginning of position opening. Choose the first and the last trade.

 
MrBrooklin pending orders can be activated (this is an ideal variant). Or they may not activate, but it is not so important for now. If all pending orders are activated upwards, then how to determine the prices of the first and last positions of the upper grid (marked with red arrows in the picture). This is the most important point in my question!!! If the price moves downwards, it is the same.

I prepared a small script to start at least from something. Yes, and one more important point - the number of open positions up and down can change in the course of trading. What are your thoughts on this issue?

Regards, Vladimir.

If the account is hedge, you will have open positions for each trade.

If netting, then you have one position that has an opening time. Request the history of trades from the time of opening the position. Find the first and last trade.

If the orders are limit orders, you can check the orders, but if stop orders, it is better to check the price of trades, because a trade on a stop order can open with slippage.

I haven't checked it, but that's how it looks like.

//+------------------------------------------------------------------+
void OnStart(void)// finds the first and the last deal in the netting position
  {
   double Price1 = 0, Price2 = 0;
   if(PositionSelect(_Symbol))
     {
      DealsPrice(PositionGetInteger(POSITION_TIME), TimeCurrent(), PositionGetInteger(POSITION_TYPE), Price1, Price2);
      Print("Price1 ", Price1, "; Price2 ", Price1);
     }
  }
//+------------------------------------------------------------------+
void DealsPrice(datetime from_date, datetime to_date, long type, double & Price1, double & Price2)
  {
   if(HistorySelect(from_date, to_date))
      for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
        {
         ulong ticket = HistoryDealGetTicket(i);
         if(ticket > 0)
            if(HistoryDealGetString(ticket, DEAL_SYMBOL) == _Symbol)
               if(HistoryDealGetInteger(ticket, DEAL_ENTRY) == DEAL_ENTRY_IN)
                  if(HistoryDealGetInteger(ticket, DEAL_TYPE) == type)
                    {
                     Price2 = HistoryDealGetDouble(ticket, DEAL_PRICE);
                     if(Price1 == 0)
                        Price1 = Price2;
                    }
        }
  }
//+------------------------------------------------------------------+
 
Aleksandr Slavskii #:

If the account is hedge, you will have open positions for each trade.

If netting, you have one position with an open time. Request the history of trades from the time the position was opened. Find the first and last trade.

If the orders are limit orders, you can check the orders, but if stop orders, it is better to check the price of trades, because a trade on a stop order can open with slippage.

I haven't checked it, but that's how it looks like.

Thank you, Alexander, for your response and the code! The account is a hedge. The difficulty for me is that out of a bunch of open positions (let's say five up and five down from the current price) I will need to find two extreme positions, both on the grid up from the current price and on the grid down.

Thanks again for the code!

Regards, Vladimir.

 
MrBrooklin #:

Thank you, Alexander, for the response and the code! The account is a hedge. The difficulty for me is that out of a bunch of open positions (let's say five up and five down from the current price) I will need to find two extreme positions, both on the grid up from the current price and on the grid down.

Thanks again for the code!

Regards, Vladimir.

Maybe like this.

//+------------------------------------------------------------------+
void OnStart(void)
  {
   double Up = -DBL_MAX, Dn = DBL_MAX;

   PositionUpDn(0, Up, Dn);
   Print("Type Buy; Up ", Up);
   Print("Type Buy; Dn ", Dn);

   Up = -DBL_MAX; Dn = DBL_MAX;
   PositionUpDn(1, Up, Dn);
   Print("Type Sell; Up ", Up);
   Print("Type Sell; Dn ", Dn);
  }
//+------------------------------------------------------------------+
void PositionUpDn(long type, double& Up, double& Dn)
  {
   for(int i = PositionsTotal() - 1; i >= 0; i--)
      if(PositionGetTicket(i))
         if(PositionGetString(POSITION_SYMBOL) == _Symbol)
            if(PositionGetInteger(POSITION_TYPE) == type)
              {
               double priceOpen = PositionGetDouble(POSITION_PRICE_OPEN);
               if(Up < priceOpen)
                  Up = priceOpen;
               if(Dn > priceOpen)
                  Dn = priceOpen;
              }
  }
//+------------------------------------------------------------------+
 
MrBrooklin pending orders can be activated (this is an ideal variant). Or they may not activate, but it is not so important for now. If all pending orders are activated upwards, then how to determine the prices of the first and last positions of the upper grid (marked with red arrows in the picture). This is the most important point in my question!!! If the price moves downwards, it is the same.

I prepared a small script to start at least from something. Yes, and one more important point - the number of open positions up and down can change in the course of trading. What are your thoughts on this issue?

Regards, Vladimir.

Vladimir, in case of restarting the Expert Advisor, we list all open positions in OnInit()...

And then the OnTradeTransaction function works in which the price is compared and if it is the next position, the variable takes this value.

You can insert the symbol and magik checks yourself.

double priceMax=0.0,
       priceMin=DBL_MAX;
/*******************Expert initialization function*******************/
int OnInit()
 {
  int posTotal = PositionsTotal();
  for(int i=posTotal; i-- > 0;)
   {
    ulong posTicket = PositionGetTicket(i);
    if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
      priceMax=fmax(priceMax,PositionGetDouble(POSITION_PRICE_OPEN));
    if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
      priceMin=fmin(priceMin,PositionGetDouble(POSITION_PRICE_OPEN));
   }
  return(INIT_SUCCEEDED);
 }/******************************************************************/

/************************Expert tick function************************/
void OnTick()
 {
 }/******************************************************************/

/*********************TradeTransaction function**********************/
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
 {
  if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
   {
    HistoryDealSelect(trans.deal);
    if(HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_IN)
     {
      if(trans.deal_type == DEAL_TYPE_BUY)
        priceMax = fmax(priceMax, trans.price);
      if(trans.deal_type == DEAL_TYPE_SELL)
        priceMin = fmax(priceMin, trans.price);
     }
   }
 }/******************************************************************/
 
Fernando Carreiro #:

This is already happening! At least on my terminal (build 5260, on Windows 10).


That's the thing, I wrote a simple Expert Advisor, and the font under the tick does not become bold. But there is someone else's Expert Advisor (I downloaded the demo version) and there just I saw how the font becomes bold. I also have build 5260, on Windows 10.
 
Aleksandr Slavskii #:

Maybe so.

I am at work today and can't get to the code yet. Thank you, Alexander. I will try it tomorrow.

Regards, Vladimir.