Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 168

 
Virtuon:

Please tell me what I'm doing wrong. I am trying to make a simple indicator:

It works in the chart window, but when I try to test it in the Expert Advisor, it either draws whatever it is or "no steps".

      if (MathMod(OptClose,step1)==0)
         Buffer0[i] = Close[i];
      else
         Buffer0[i] = Buffer0[i+1];
 
PapaYozh:

Tried it. Even just in the window doesn't work this way. Apparently there's some kind of trick here. It works this way now, but is terribly slow:
 for(i=WindowBarsPerChart(); i>=0; i--)
   {
      double OptClose = Close[i]*10000;
      if (MathMod(i+step1,step1)==0)
      {
      Buffer0[i] = Close[i];
      last = Close[i];
      }
      else
      Buffer0[i] = last;
      Comment(Buffer0[WindowBarsPerChart()]," ",Buffer0[0]);
   }
This is with the last bar recalculation check disabled.
 

Hello, MQL4 programming gurus! Please advise how this is possible:

A piece of program code:

   LogAdd("Начинаю вычисления переменных для текущего тика ... ");  
   for (int i = OrdersTotal()-1; i >= 0; i--) {
      OrderSelect(i, SELECT_BY_POS);           
      LogAdd("Выбираю ордер с тикетом: " + i); 
      if ((OrderOpenTime() > opnTime) && (OrderType() == OP_BUY) || (OrderType() == OP_SELL)) {    
         opnTime = OrderOpenTime();
         currentTicket = OrderTicket(); 
         currentStopLoss = OrderStopLoss();          
         LogAdd("Ордер открыт позже предыдущего, не является отложенным. Записываю переменные: opnTime - " + opnTime + ", currentTicket - " + currentTicket + ", currentStopLoss - " + currentStopLoss + ".");
      }
      if (OrderType() == OP_BUY) {         
         buyCntr ++;   
         buyOpnPrice = OrderOpenPrice();
         lotsSumBuy += OrderLots();
         TrailingByShadows(OrderTicket(), Period(), 11, 0); 
         LogAdd("Ордер не является отложенным, тип ордера - на покупку, обновляю переменные, пытаюсь трейлить: buyCntr - " + buyCntr + ", buyOpnPrice - " + buyOpnPrice + ", lotsSumBuy - " + lotsSumBuy + ".");  
      }  
      if (OrderType() == OP_SELL) {
         sellCntr ++;
         sellOpnPrice = OrderOpenPrice();
         lotsSumSell += OrderLots();
         TrailingByShadows(OrderTicket(), Period(), 11, 0);
         LogAdd("Ордер не является отложенным, тип ордера - на продажу, обновляю переменные, пытаюсь трейлить: sellCntr - " + sellCntr + ", sellOpnPrice - " + sellOpnPrice + ", lotsSumSell - " + lotsSumSell + ".");
      }  
      if (OrderType() == OP_SELLSTOP) {
         sellOpnPrice = OrderOpenPrice();         
         LogAdd("Ордер является отложенным, тип ордера - на продажу, фиксирую цену открытия: sellOpnPrice - " + sellOpnPrice + ".");
      }  
      if (OrderType() == OP_BUYSTOP) {
         buyOpnPrice = OrderOpenPrice();
         LogAdd("Ордер является отложенным, тип ордера - на покупку, фиксирую цену открытия: buyOpnPrice - " + buyOpnPrice + ".");
      }  
   }
   LogAdd("Вычисления переменных для текущего тика завершены."); 

A piece of log for this section:

2013.10.2 15:59:0 - Начинаю вычисления переменных для текущего тика ... 
2013.10.2 15:59:0 - Выбираю ордер с тикетом: 5
2013.10.2 15:59:0 - Ордер является отложенным, тип ордера - на продажу, фиксирую цену открытия: sellOpnPrice - 1.35250000.
2013.10.2 15:59:0 - Выбираю ордер с тикетом: 4
2013.10.2 15:59:0 - Ордер открыт позже предыдущего, не является отложенным. Записываю переменные: opnTime - 1380728738, currentTicket - 93537240, currentStopLoss - 0.00000000.
2013.10.2 15:59:0 - Ордер не является отложенным, тип ордера - на покупку, обновляю переменные, пытаюсь трейлить: buyCntr - 1, buyOpnPrice - 1.35620000, lotsSumBuy - 0.06000000.
2013.10.2 15:59:0 - Ордер не является отложенным, тип ордера - на продажу, обновляю переменные, пытаюсь трейлить: sellCntr - 1, sellOpnPrice - 1.35250000, lotsSumSell - 0.01000000.
2013.10.2 15:59:0 - Выбираю ордер с тикетом: 3
2013.10.2 15:59:0 - Ордер не является отложенным, тип ордера - на продажу, обновляю переменные, пытаюсь трейлить: sellCntr - 2, sellOpnPrice - 1.35250000, lotsSumSell - 0.04000000.
2013.10.2 15:59:0 - Выбираю ордер с тикетом: 2
2013.10.2 15:59:0 - Ордер не является отложенным, тип ордера - на покупку, обновляю переменные, пытаюсь трейлить: buyCntr - 2, buyOpnPrice - 1.35620000, lotsSumBuy - 0.07000000.
2013.10.2 15:59:0 - Ордер не является отложенным, тип ордера - на продажу, обновляю переменные, пытаюсь трейлить: sellCntr - 3, sellOpnPrice - 1.35250000, lotsSumSell - 0.05000000.
2013.10.2 15:59:0 - Выбираю ордер с тикетом: 1
2013.10.2 15:59:0 - Ордер не является отложенным, тип ордера - на покупку, обновляю переменные, пытаюсь трейлить: buyCntr - 3, buyOpnPrice - 1.35620000, lotsSumBuy - 0.08000000.
2013.10.2 15:59:0 - Ордер не является отложенным, тип ордера - на продажу, обновляю переменные, пытаюсь трейлить: sellCntr - 4, sellOpnPrice - 1.35250000, lotsSumSell - 0.06000000.
2013.10.2 15:59:0 - Выбираю ордер с тикетом: 0
2013.10.2 15:59:0 - Ордер не является отложенным, тип ордера - на продажу, обновляю переменные, пытаюсь трейлить: sellCntr - 5, sellOpnPrice - 1.35250000, lotsSumSell - 0.07000000.
2013.10.2 15:59:0 - Вычисления переменных для текущего тика завершены.
The question is how for the same order the conditions could have worked:
      if (OrderType() == OP_BUY) {         
          ...
      }  
      if (OrderType() == OP_SELL) {
          ...
      } 

I don't understand something, but then logically it turns out that for this order:

OP_BUY == OP_SELL
Basically I don't care why this happens, what matters is that sellCntr++ and buyCntr++ are executed unambiguously for a certain type of open order, help me fix it?
 
Mepkypuu:

Hello, MQL4 programming gurus! Please advise how this is possible:

A piece of program code:

A piece of log for this section:

The question is how for the same order the conditions could have worked:

I don't understand something, but then logically it turns out that for this order:

Basically I don't care why this happens, what matters is that sellCntr++ and buyCntr++ are executed unambiguously for a certain type of open order, help me fix?
How do you know it is the same order? You don't print the ticket ;).... I'm sure: when trawling, there is an overflow of orders ;) - The induced error is from there.
TrailingByShadows(OrderTicket(), Period(), 11, 0);  ?????????????????????????????? Вы при трале разве ордера не перебираете ?????????? 
 
VladislavVG:
How can you be sure that it is the same order? You don't print the ticket ;).... I'm sure: when you trawl, the order is oversampled ;) - The induced error is from there.

Logically, why would I go through the orders in the trawl if I already know which specific order I need to trawl? There is no brute force, here is the code:

void TrailingByShadows(int ticket,int tmfrm,int bars_n, int indent) {  
   int i;
   double new_extremum;

   if ((bars_n<1) || (indent<0) || (ticket==0) || ((tmfrm!=1) && (tmfrm!=5) && (tmfrm!=15) && (tmfrm!=30) && (tmfrm!=60) && (tmfrm!=240) && (tmfrm!=1440) && (tmfrm!=10080) && (tmfrm!=43200)) || (!OrderSelect(ticket,SELECT_BY_TICKET))) {
      Print("Трейлинг функцией TrailingByShadows() невозможен из-за некорректности значений переданных ей аргументов.");
      return(0);
   } 
   if (OrderType()==OP_BUY) {
      for(i = 1; i <= bars_n; i++) {
         if (i == 1) new_extremum = iLow(Symbol(), tmfrm, i); 
         else if (new_extremum > iLow(Symbol(), tmfrm, i)) new_extremum = iLow(Symbol(), tmfrm, i);
      }
      if(((new_extremum - indent * Point) > OrderStopLoss() + 1.0 * Point) || (OrderStopLoss() == 0))
      if((new_extremum - indent * Point) > OrderOpenPrice())
      if(new_extremum - indent * Point < Bid - MarketInfo(Symbol(), MODE_STOPLEVEL) * Point)
      if(AcountProfitEx(new_extremum) > ProfitSize)
      OrderModify(ticket, OrderOpenPrice(), new_extremum - indent * Point, OrderTakeProfit(), OrderExpiration());
   }
   
   if (OrderType() == OP_SELL) {
      for(i = 1; i <= bars_n; i++) {
         if (i == 1) new_extremum = iHigh(Symbol(), tmfrm, i); 
         else if (new_extremum < iHigh(Symbol(), tmfrm, i)) new_extremum = iHigh(Symbol(), tmfrm, i);
      }
      if (((new_extremum + (indent + MarketInfo(Symbol(),MODE_SPREAD)) * Point) < OrderStopLoss() - 1.0 * Point) || (OrderStopLoss() == 0)) 
      if ((new_extremum + (indent + MarketInfo(Symbol(),MODE_SPREAD)) * Point) < OrderOpenPrice())                          
      if ((new_extremum + (indent + MarketInfo(Symbol(),MODE_SPREAD)) * Point > Ask + MarketInfo(Symbol(),MODE_STOPLEVEL) * Point)) 
      if (AcountProfitEx(new_extremum) > ProfitSize)
      OrderModify(ticket, OrderOpenPrice(), new_extremum + (indent + MarketInfo(Symbol(), MODE_SPREAD)) * Point, OrderTakeProfit(), OrderExpiration());
   }      
}
PS: By the way, this problem occurs only in real conditions, i.e. it is not reproduced in the tester.
 
Dear programmers, could you please tell me why when I rebooted my computer again, when I opened the terminal instead of a normal chart I saw a grey window, and the open positions, advisor, indicators disappeared, but when I opened a new chart, the positions appeared, but there were no advisor and indicators, I had to install them again. What has this got to do with it? I have had to install them again. Thank you.
 

Such a problem has arisen. The strategy tester only works once. More precisely, the visualisation test is triggered once, and the next times you press the "start" button, the "visualisation" checkbox is reset and nothing happens (sort of doesn't happen). How can this be defeated?

 
Mepkypuu:

Logically, why would I go through the orders in the trawl if I already know which specific order I need to trawl? There's no brute force, here's the code:

PS: By the way, this problem occurs only in real conditions, i.e. it is not reproduced in the tester.


Try to run it in this variant:

   LogAdd("Начинаю вычисления переменных для текущего тика ... ");  
   for (int i = OrdersTotal()-1; i >= 0; i--) 
   {
      if(!OrderSelect(i, SELECT_BY_POS)) continue;           
      int OrdType=OrderType() ;
      int OrdTckt = OrderTicket(); 
      LogAdd("Выбираю ордер ( "+i+" ) с тикетом: " + OrdTckt); 
      if ((OrderOpenTime() > opnTime) && (OrdType == OP_BUY) || (OrdType == OP_SELL)) {    
         opnTime = OrderOpenTime();
         currentTicket = OrderTicket(); 
         currentStopLoss = OrderStopLoss();          
         LogAdd("Ордер открыт позже предыдущего, не является отложенным. Записываю переменные: opnTime - " + opnTime + ", currentTicket - " + currentTicket + ", currentStopLoss - " + currentStopLoss + ".");
      }
      if (OrdType == OP_BUY) 
      {         
         buyCntr ++;   
         buyOpnPrice = OrderOpenPrice();
         lotsSumBuy += OrderLots();
         TrailingByShadows(OrderTicket(), Period(), 11, 0); 
         LogAdd("Ордер "+OrdTckt  <-> OrderTicket()+" не является отложенным, тип ордера - на покупку, обновляю переменные, пытаюсь трейлить: buyCntr - " + buyCntr + ", buyOpnPrice - " + buyOpnPrice + ", lotsSumBuy - " + lotsSumBuy + ".");  
      }  
      if (OrdType== OP_SELL) 
      {
         sellCntr ++;
         sellOpnPrice = OrderOpenPrice();
         lotsSumSell += OrderLots();
         TrailingByShadows(OrderTicket(), Period(), 11, 0);
         LogAdd("Ордер "+OrdTckt  <-> OrderTicket()+" не является отложенным, тип ордера - на продажу, обновляю переменные, пытаюсь трейлить: sellCntr - " + sellCntr + ", sellOpnPrice - " + sellOpnPrice + ", lotsSumSell - " + lotsSumSell + ".");
      }  
      if (OrderType() == OP_SELLSTOP) 
      {
         sellOpnPrice = OrderOpenPrice();         
         LogAdd("Ордер "+OrdTckt  <-> OrderTicket()+" является отложенным, тип ордера - на продажу, фиксирую цену открытия: sellOpnPrice - " + sellOpnPrice + ".");
      }  
      if (OrderType() == OP_BUYSTOP) 
      {
         buyOpnPrice = OrderOpenPrice();
         LogAdd("Ордер "+OrdTckt  <-> OrderTicket()+" является отложенным, тип ордера - на покупку, фиксирую цену открытия: buyOpnPrice - " + buyOpnPrice + ".");
      }  
   }
   LogAdd("Вычисления переменных для текущего тика завершены."); 

See what the log will say.

 
Is it possible to set the indentation programmatically, not through the template? I can get the indentation size through
WindowBarsPerChart() - WindowFirstVisibleBar() - 2;

you can, but how to expose it.

Added

It seems that you can do it programmatically through templates. Create a template with an indicator, with _lread kernel32.dll read shift_size value, with _lwrite write there a number from 10 to 50, like such ranges can accept this parameter, then through InternalMsg load a template. But we don't know if it is 33511 or 35511. In WinUser32.mqh, it is the first option, while in the forum it is the second. The question must be addressed to Junko. It is much easier in MT5. CHART_SHIFT_SIZE is available there through ChartSetString() and ChartGetString() from any point in the code... Maybe there are other options in MT4? Silly question, though...

 
KottCoos:

Such a problem has arisen. The strategy tester only works once. More precisely, the visualisation test is triggered once, and the next times you press the "start" button, the "visualisation" checkbox is reset and nothing happens (sort of doesn't happen). How can this be defeated?


Don't you have the OPTIMISATION box checked along with the VISUALisation?
Reason: