EA has problem, the function work in stragety tester but not in demo and live

 

Dear all, 


This function in my EA work in tester but not in demo and live, please help.

It return the deal of 2 hours before divided by minimun lots allowed.

e.g.   2 hours before the deal in was  0.05 lot, then it should return 5.

 

Use the CODE button (Alt-S) when inserting code.

A moderator corrected the formatting this time. Please format code properly in future; posts with improperly formatted code may be removed.

int LastLotCnt()

{

   long l_timecurrent = TimeCurrent();  

         

// history from "10 days before to current time

   if(HistorySelect(l_timecurrent - 864000 ,l_timecurrent))  

   {

   

      int total = HistoryDealsTotal();

      

      for (int j = total - 1 ; j >= 0 ; j--) 

      {

         ulong ticket =  HistoryDealGetTicket(j);

         

         if (ticket <= 0) 

         {   Print("Failed to get ticket"); 

             return 0; 

         }

         

         if (!HistoryDealSelect(ticket))

         {   Print("Failed to select by ticket"); 

             return 0; 

         }

           

         long magicno;

         if (!HistoryDealGetInteger(ticket,DEAL_MAGIC,magicno)) 

         {   Print("Failed to get magic number"); 

             return 0; 

         }

         



         if (magicno == InpMagicNumber)

         {   

         

               long l_dealentry;

               if (!HistoryDealGetInteger(ticket,DEAL_ENTRY,l_dealentry)) 

               {   Print("Failed to get deal entry type"); 

                   return 0; 

               }

         

               if (l_dealentry == DEAL_ENTRY_IN)

               {

                     long l_dealtime ;

                     if (!HistoryDealGetInteger(ticket,DEAL_TIME,l_dealtime)) 

                     {   Print("Failed to get time"); 

                         return 0; 

                     }

                     

                     // 2 hours before

                     if ( (l_timecurrent - l_dealtime) > 7200 )  

                     {

                         string l_curr;                

                         if(!HistoryDealGetString(ticket,DEAL_SYMBOL,l_curr))

                         {

                           Print("Failed to get deal symbol"); 

                           return 0; 

                         }     

      

                         double vol;

                         if (!HistoryDealGetDouble(ticket,DEAL_VOLUME,vol))

                         {

                             Print("Failed to get volumn");

                             return 0; 

                         }

                        

                         double LotsMin = SymbolInfoDouble(l_curr, SYMBOL_VOLUME_MIN);

                         

                         return  (int)(vol/LotsMin);

      

                     }

               }

               }

      }

   }



  return 0; 

}


Peter

 
Kam Yuk Wong:

Dear all, 


This function in my EA work in tester but not in demo and live, please help.

It return the deal of 2 hours before divided by minimun lots allowed.

e.g.   2 hours before the deal in was  0.05 lot, then it should return 5.

 

Use the CODE button (Alt-S) when inserting code.

A moderator corrected the formatting this time. Please format code properly in future; posts with improperly formatted code may be removed.


Peter


   More information, in demo, I used it for multiple symbols , in stragety tester only one symbol.

 
Hi, the problem with the strategy tester is the ticks, the historical data is only good for about the last two months, while the older tick data has a different sequence.

For example, this is the sequence in the real market:
10001010100110010100111010000111101010..... and so on

But the older records look like this:
10101010101010111111111111111111111111111111111111111111111110

The second reason is data holes in some parts of the charts for historical data, there are only candles, but other data is missing and this appears as if the strategy has blown up. Verify only on short segments and not older periods than the last one to two months.


Milan

 
Milan Hnila #:
Hi, the problem with the strategy tester is the ticks, the historical data is only good for about the last two months, while the older tick data has a different sequence.

For example, this is the sequence in the real market:
10001010100110010100111010000111101010..... and so on

But the older records look like this:
10101010101010111111111111111111111111111111111111111111111110

The second reason is data holes in some parts of the charts for historical data, there are only candles, but other data is missing and this appears as if the strategy has blown up. Verify only on short segments and not older periods than the last one to two months.


Milan

  Can you suggest how to fix the bug ? It work in tester but not in demo and live.
 
That's the point, it's not a bug, you can try deleting the historical data and trying to download it again, but it probably won't help. If you only test on the last few months, it's fine. Or have the binary sequence of ticks written to the overlay, so you can check if the ticks are real or synthetic at that point. I haven't been able to solve this problem otherwise, but I haven't tried it much.

In short, for some reason the older data is synthetically rearranged, but I haven't found out what the reason is yet.
 
Ah, sorry, you're asking about something different than what I'm talking about. 😀

HistorySelect returns empty history
Because TimeCurrent() on a live account ≠ deal time.

Backtest:

TimeCurrent = candle time → deal-time is always in range.

Live/Demo:

TimeCurrent = server time → deal-time can be out of range → nothing is selected.

Use TimeTradeServer()

long now = TimeTradeServer();

HistorySelect(now - 864000, now);

Don't use DEAL_ENTRY_IN, but filter by positions

ulong pos_ticket = PositionGetTicket(0);

HistorySelectByPosition(pos_ticket);
 
Milan Hnila #:
Ah, sorry, you're asking about something different than what I'm talking about. 😀

HistorySelect returns empty history
Because TimeCurrent() on a live account ≠ deal time.

Backtest:

TimeCurrent = candle time → deal-time is always in range.

Live/Demo:

TimeCurrent = server time → deal-time can be out of range → nothing is selected.

Use TimeTradeServer()

long now = TimeTradeServer();

HistorySelect(now - 864000, now);

Don't use DEAL_ENTRY_IN, but filter by positions

ulong pos_ticket = PositionGetTicket(0);

HistorySelectByPosition(pos_ticket);
Thanks, I will try.
 

Peter, one more thing to check besides the time functions. TimeCurrent() is the server time of the last received quote, so on demo/live the HistorySelect() window itself is normally fine — it only lags on a symbol that has not ticked for a while. That alone usually does not explain tester-vs-live differences.

The more likely culprit, given your note in #1 that demo runs multiple symbols: the loop has no symbol filter. It returns on the first DEAL_ENTRY_IN older than 2 hours whose magic matches — and if the same magic number is shared across several charts, that deal can belong to a different symbol. The tester runs one symbol, so the problem never shows there. Move the DEAL_SYMBOL read up and add a check like if(l_curr != _Symbol) continue; before the time test, or give each chart instance its own magic.

Also, inside the loop you return 0 whenever a single property read fails, which aborts the whole scan — on live accounts the history also contains balance deals (the deposit) with no symbol, so one odd entry can end the search early. Using continue for those cases instead of return makes the function much more robust. Finally, note the condition (now - dealtime) > 7200 selects the newest deal older than 2 hours, not the deal placed exactly 2 hours ago — worth confirming that is really the intent.