Organising the order cycle - page 8

 
Alexey Viktorov:

Did I make it sound that complicated? The DC earns money on spreads and commissions, the programmer earns money writing code.

One of the traders says that we should save on spreads and commissions (not pay as much as possible).

The other trader says that it is not fair to write the code for the money (if possible, do not pay).

They both take care of their own wallet. They don't seem to differ from each other...

Only you, as a programmer, support the first and argue with the second proving him wrong.

I understand the trader who wants to find a cheaper programmer. I also understand the trader who is looking for better trading conditions. What is the problem? What is wrong with wanting to save money?

But here it is a different story. To think through your own algorithm to make it work more effectively with orders, and thereby increase the profit of the system - means to be a good developer. In my opinion, one can neglect several pips only at the stage of rough testing of an idea.

 
Andrey Khatimlianskii:

I understand a trader who wants to find a cheaper programmer. I also understand the trader who is looking for better trading conditions. What is the problem? What is wrong with wanting to save money?

But here it is a different story. To think through your own algorithm to make it work more effectively with orders, and thereby increase the profit of the system - means to be a good developer. I think we can neglect several pips only at the stage of rough testing of an idea.

Fine. And somehow I understand your reluctance to write for free more. No problem either...

In general, I'm not against trying to save money, but not to the point of madness. You can't put the savings on the spread at the head of trading...

 
Alexey Viktorov:

I don't mind trying to save money in general, but not to the point of being insane. You can't put the savings on the spread at the centre of trading...

If you do the math, it's not that crazy. But I'm not trying to convince.

 
Andrey Khatimlianskii:

If you do the maths, it's not that daft. But I'm not trying to convince.

You have to count the profits, not the costs.

If you save costs, it is very easy to make losses. If the TS is designed to bear expenses, but it gives a profit, then this is a good TS, because in a different scenario, it may make a loss.

In fact, there is no judging of winners!

 

Can't page 7 to the current page be moved to a separate topic (you could even link to it like "Further discussion here") and continue what it was created for in this thread?

 
Vitaly Muzichenko:

You have to count the profits, not the costs.

When you save costs, it is very easy to make losses. If the TS is designed to incur expenses, but at the same time it gives income, then it is a good TS, because in another scenario, it may make a loss.

And in general, winners are not judged!

I do not understand what this comment is about.

Is it about the unimportance of commission size for super profitable strategies?

 
Let us run the following Expert Advisor on the MT4 EURUSD
// В случае изменения количества ордеров по основному символу, сигнализирует об их количестве

// #include <MT4Orders.mqh>

const bool Init =  EventSetMillisecondTimer(1);

int AmountOrders( const string &Symb )
{
  int Res = 0;
  
  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS) && (OrderSymbol() == Symb))
      Res++;
      
  return(Res);
}

void OnTimer()
{
  static int PrevAmount = 0;
  
  const int Amount = AmountOrders(_Symbol);
  
  if (Amount != PrevAmount)
  {
    PrevAmount = Amount;
    
    Alert(Amount);
  }    
}


Let us open 5 EURUSD and 5 USDJPY orders at random. Now let's take a look at the AmountOrders innocuous code and imagine the following scenario

  1. We are in AmountOrders at runtime where i == 3.
  2. We close a position on USDJPY. Orders are then shaken out in the table of current orders.
  3. When i == 2, we may encounter the same order as in point 1 through OrderSelect. Because of this, AmountOrders may return one more value than it actually was.


Here is a confirmation of what we have said

#property strict

void OnStart()
{
  for (int i = 0; i < 5; i++)
    OrderSend(_Symbol, OP_BUY, 1, Ask, 100, 0, 0);
    
  int PrevTicket = 0;
  
  for (int i = OrdersTotal() - 1; i > 0; i--)
    if (OrderSelect(i, SELECT_BY_POS))
    {
      const int Ticket = OrderTicket();
      
      if (Ticket == PrevTicket)
        Alert("Hello World!");
        
      PrevTicket = Ticket;
      
      if (OrderSelect(i - 1, SELECT_BY_POS))      
        OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 100);
    }
}


Result

2017.10.06 01:28:05.885 TestTets EURUSD,M1: close #240725107  buy 1.00 EURUSD at 1.17121 at price 1.17099
2017.10.06 01:28:05.775 TestTets EURUSD,M1: Alert: Hello World!
2017.10.06 01:28:05.775 TestTets EURUSD,M1: close #240725108  buy 1.00 EURUSD at 1.17121 at price 1.17099
2017.10.06 01:28:05.673 TestTets EURUSD,M1: Alert: Hello World!
2017.10.06 01:28:05.673 TestTets EURUSD,M1: close #240725110  buy 1.00 EURUSD at 1.17121 at price 1.17099
2017.10.06 01:28:05.578 TestTets EURUSD,M1: Alert: Hello World!
2017.10.06 01:28:05.578 TestTets EURUSD,M1: close #240725111  buy 1.00 EURUSD at 1.17121 at price 1.17099
2017.10.06 01:28:05.480 TestTets EURUSD,M1: open #240725112  buy 1.00 EURUSD at 1.17121 ok
2017.10.06 01:28:05.343 TestTets EURUSD,M1: open #240725111  buy 1.00 EURUSD at 1.17121 ok
2017.10.06 01:28:05.253 TestTets EURUSD,M1: open #240725110  buy 1.00 EURUSD at 1.17121 ok
2017.10.06 01:28:05.138 TestTets EURUSD,M1: open #240725108  buy 1.00 EURUSD at 1.17121 ok
2017.10.06 01:28:05.035 TestTets EURUSD,M1: open #240725107  buy 1.00 EURUSD at 1.17121 ok


The same ticket may get out twice in a harmless loop of order counting!

Совершение сделок - Торговые операции - Справка по MetaTrader 5
Совершение сделок - Торговые операции - Справка по MetaTrader 5
  • www.metatrader5.com
Торговая деятельность в платформе связана с формированием и отсылкой рыночных и отложенных ордеров для исполнения брокером, а также с управлением текущими позициями путем их модификации или закрытия. Платформа позволяет удобно просматривать торговую историю на счете, настраивать оповещения о событиях на рынке и многое другое. Открытие позиций...
 
fxsaber:

The same ticket can get out twice in an innocuous order counting cycle!

To put all doubts to rest, do the following.

Install the Expert Advisor

// Советник будет алертовать, если OrderSelect соседних индексов выберет один и тот же ордер
const bool Init =  EventSetMillisecondTimer(1);

void OnTimer()
{
  int PrevTicket = 0;
  
  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS))
    {
      const int Ticket = OrderTicket();
      
      if (Ticket == PrevTicket)
        Alert("Hello World!");
        
      PrevTicket = Ticket;
      
      Sleep(1); // 1 миллисекунда - это много или мало?
    }
}


Launch the script

#property strict

void OnStart()
{
  // Открыли позиции
  for (int i = 0; i < 25; i++)
    OrderSend(_Symbol, OP_BUY, 1, SymbolInfoDouble(_Symbol, SYMBOL_ASK), 100, 0, 0);

  int Tickets[];
  const int Total = OrdersTotal();
  
  ArrayResize(Tickets, Total);
  
  for (int i = 0; i < Total; i++)
    if (OrderSelect(i, SELECT_BY_POS))
      Tickets[i] = OrderTicket();
    
  // Закрыли позиции
  for (int i = 0; i < Total; i++)
    OrderClose(Tickets[i], 1, SymbolInfoDouble(_Symbol, SYMBOL_BID), 100);
}


And we observe that the EA selects the same orders under different indices. And this can lead to complete failure of the trading logic.

 
fxsaber:

the EA selects the same orders under different indices. And this can lead to a complete breakdown of the trading logic.

So, this is the main issue in its entirety! How to arrange the loop of orders? For example, how to correctly write such a function?

// Возращает количество ордеров данного символа
int AmountOrders( const string &Symb )
{
  int Res = 0;
  
  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS) && (OrderSymbol() == Symb))
      Res++;
      
  return(Res);
}


This thread started with specifying the necessity of some repeated actions when there is a pause in the search cycle. A pause of about a millisecond is enough here to see the effect.

So far such a crutch.

bool IsChange( const bool InitFlag = false )
{
  static int PrevTotal = 0;
  static int PrevHistoryTotal = 0;
  
  const int Total = OrdersTotal();
  const int HistoryTotal = OrdersHistoryTotal();    
  
  if (InitFlag)
  {
    PrevTotal = Total;
    PrevHistoryTotal = HistoryTotal;    
  }
  
  return(!InitFlag && ((Total != PrevTotal) || (HistoryTotal != PrevHistoryTotal)));
}

// Возращает количество ордеров данного символа
int AmountOrders( const string &Symb )
{
  int Res = 0;
  
  IsChange(true);
  
  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (IsChange())
    {
      i = OrdersTotal();
      
      Res = 0;
    }
    else if (OrderSelect(i, SELECT_BY_POS) && (OrderSymbol() == Symb))
      Res++;
      
  return(Res);
}

But there is no certainty that it will always work correctly.

 
fxsaber:

To put all doubts to rest, do the following.

Install the Expert Advisor

Run the script

And we see that the EA selects the same orders under different indices. And this can lead to complete failure of the trading logic.

I have changed

// Советник будет алертовать, если OrderSelect соседних индексов выберет один и тот же ордер
const bool Init =  EventSetMillisecondTimer(1);

bool IsChange( const bool InitFlag = false )
{
  static int PrevTotal = 0;
  static int PrevHistoryTotal = 0;
  
  const int Total = OrdersTotal();
  const int HistoryTotal = OrdersHistoryTotal();    
  
  if (InitFlag)
  {
    PrevTotal = Total;
    PrevHistoryTotal = HistoryTotal;    
  }
  
  return(!InitFlag && ((Total != PrevTotal) || (HistoryTotal != PrevHistoryTotal)));
}

void OnTimer()
{
  int PrevTicket = 0;
  
  IsChange(true);
  
  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (IsChange())
    {
      i = OrdersTotal();
      
      PrevTicket = 0;
    }
    
    else if (OrderSelect(i, SELECT_BY_POS))
    {
      const int Ticket = OrderTicket();
      
      if (Ticket == PrevTicket)
        Alert("Hello World!");
        
      PrevTicket = Ticket;
      
      Sleep(1); // 1 миллисекунда - это много или мало?
    }
}

We have changed the script without alerts.