Questions from Beginners MQL5 MT5 MetaTrader 5 - page 935

 

This glitch seems to occur if there are other orders in the market but of a different size.

Suppose we have 4 sells of 1 lot and 4 buys of 1 lot. And 1 buy 0.5 lots.

How to avoid this? I think this function needs a loop.

 
EgorKim:

This glitch seems to occur if there are other orders in the market but of a different size.

Suppose we have 4 sells of 1 lot and 4 buys of 1 lot. And 1 buy 0.5 lots.

How to avoid this? Perhaps, there should be a loop in this function.

I don't know where the "drubashka" is. It is his specialty to work through the standard library. I don't know what it's picking.

      if(m_position.SelectByIndex(i))

And it would be too lazy to sort it out.

I would have approached the solution a little differently. I would create two arrays with Buy and Sell position tickets separately and close the positions in the second loop in pairs.

Or we may create one two-dimensional array. But one has to be very attentive in distributing the indexes. So that there are no gaps...

 
Alexey Viktorov:

Somewhere the drummer has disappeared. It's his thing to work through the standard library. I don't know what he's choosing.

Yes, and I'm too lazy to work it out.

I would have approached the solution a little differently. I would create two arrays with Buy and Sell position tickets separately and close them in the second loop in pairs.

Or we can use one two-dimensional array. But I have to be very attentive in distributing the indexes. So that there are no gaps...

I've thought about it myself. Can you share the code with two arrays? I cannot do it myself.

As a temporary solution, I created a limited loop. I do not know if this code is correct or not, but it may be something. True, it closes strangely with splitting lots.

Apparently even the volumes of the counter should be compared to avoid splitting. I give up.

void CloseBy()
  {
   int s=10;
   do // цикл
     {
      s--;
      ulong ticket_buy=ULONG_MAX;
      ulong ticket_sell=ULONG_MAX;
      for(int i=0;i<PositionsTotal();i++) // ATTENTION! Here, specially began a detour with "0"
         if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY && ticket_buy==ULONG_MAX)
               ticket_buy=m_position.Ticket();

            if(m_position.PositionType()==POSITION_TYPE_SELL && ticket_sell==ULONG_MAX)
               ticket_sell=m_position.Ticket();
           }
      if(ticket_buy!=ULONG_MAX && ticket_sell!=ULONG_MAX)
         m_trade.PositionCloseBy(ticket_buy,ticket_sell);
     }
   while(s>1);// цикл
   return;
  }
 
EgorKim:

I myself have thought about it. Can you share the code with two arrays?

No, for several reasons.

1. I don't use counter closure myself.

2. There is no ready-made code, and there is no desire to write it specially.

3. Yesterday I had such an idea. Yesterday I had such an idea. Today I have another one, and tomorrow, God forbid, I will have another one. Consequently I only write what is in my mind at the moment.

And the main reason - I think that help and do someone else's work, it's two big differences.

I can only help. But I can't help you, I almost don't use standard library and to understand your code, I need to understand SB.

 

Hi All

Can you give me an example of an indicator or function that allows you to create an indicator that will only show the result on the chart in a certain range or area on the chart!

For example from 1:00 to 2:35 terminal time, And this choice should be made with the mouse directly on the chart.


I specifically need to count the number of bar or sell candles in the selected range.

 
Milhail Novgorodcev:

Hi All

Can you give me an example of an indicator or function that allows you to create an indicator that will only show the result on the chart in a certain range or area on the chart!

For example from 1:00 to 2:35 terminal time, And this choice should be made with the mouse directly on the chart.


I specifically need to count the number of Buy or Sell candlesticks in the selected range.

The first thing that came to mind:

  • a script, two input parameters - time from- and time to-
  • a rectangle is drawn manually and the script should be launched with the rectangle name in the input parameters
  • Expert Advisor or indicator which traces creation of a rectangle with a given name ...

 
Milhail Novgorodcev:

Hi All

Can you give me an example of an indicator or function that allows you to create an indicator that will only show the result on the chart in a certain range or area on the chart!

For example from 1:00 to 2:35 terminal time, And this choice should be made with the mouse directly on the chart.


I specifically need to count the number of Buy or Sell candlesticks in the selected range.

What is the problem?

In OnChartEvent () to two variables, in turn, the time of the mouse click in two points is assigned (not the current time, but the time of the bar, on which you have clicked) and, provided both variables are not zero, this range is calculated. After the output of these variables, they are reset to zero and wait for the next mouse click.

The only problem I see is if the first click is made by mistake, how to undo it. But, I think it is possible to come up with something carefully after reading the documentation. For example, pressing a key or scrolling the mouse wheel...

 

Please help!

Tired of fixing the script to take screenshots on history. The script is supposed to scroll to 8am each day and take a screenshot.

The script is not working as it should.

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   ChartSetInteger(0,CHART_SHIFT,false);
   ChartSetInteger(0,CHART_AUTOSCROLL,false);
   int _bars=Bars(Symbol(),0);
 
   for(int i=0; i<_bars; i++)
     {
      MqlDateTime time_now;
      //datetime candle_time=iTime(NULL,_period,0);
      TimeToStruct(iTime(NULL,_Period,i),time_now);  // change time to struct

      if(time_now.hour==8 && time_now.min==0)
        {
         //--- прокрутим на 10 баров вправо от начала истории 
         ChartNavigate(0,CHART_BEGIN,i);
         Sleep(5000);
         //--- получим номер самого первого видимого на графике бара (нумерация как в таймсерии) 
         long first_bar=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);
         Print(first_bar);
         string name=_Sumbol_N(string(Symbol()))+"_"+Symbol()+"_"+TFMigrate(Period())+" "+string(time_now.year)+"."+string(time_now.mon)+"."+string(time_now.day)+" "+string(time_now.hour)+"."+string(time_now.min);

         ChartScreenShot(0,name+" 2560_1600.PNG",2560,1600,ALIGN_LEFT);
         //i=5;
        };
     };

//Print(iTime(NULL,_Period,0));

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string _Sumbol_N(string ft)
  {
   if(ft ==  "EURUSD" ) return("01");
   if(ft ==  "GBPUSD" ) return("02");

   return "";
  }
//+------------------------------------------------------------------+
string TFMigrate(int tf)
  {
   switch(tf)
     {
      case 0: return("CURRENT");
      case 1: return("M1");
      case 5: return("M5");
      case 15: return("M15");
      case 30: return("M30");
      case 60: return("H1");
      case 240: return("H4");
      case 1440: return("D1");
      case 10080: return("W1");
      case 43200: return("MN1");

     }
  }
//+------------------------------------------------------------------+
 
oleg360:

Please help!

Tired of fixing the script to take screenshots on history. The script is supposed to scroll to 8am each day and take a screenshot.

The script is not working as it should.

So far, the scrolling function is working erratically (or rather working unpredictably). The administrator is sorting it out.

 
Vladimir Karputov:

So far, the scrolling function is malfunctioning (or rather working unpredictably). The administrator is sorting it out.

Thank you!