mql5 - vertical line after a certain time since the last trade - page 2

 
Vladimir Karputov:

I would like to clarify the term "last trade".

Is it the last trade within the current day? Or for the last N-days?

The last deal within 24 hours - you don't need to go further back in history.

 
renatmt5:

the last transaction in 24 hours - no need to go through the story further.

Here's how it works:

The indicator uses the code to create, move and delete a vertical line fromOBJ_VLINE help. (only line creation works in the indicator so far). The line name is set in the input parameter"Vertical line name".

Last deal Move Vertical line

Thevertical line is created in OnOnit():

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   VLineCreate(0,InpVLineName);
//---
   return(INIT_SUCCEEDED);
  }


To minimize the load, we interrogate the history once per minute. For this purpose, let's declare the variableExtLastMove in the area of global variables of our program (in this case, in the indicator)

//---
datetime ExtLastMove=0;                   // "0" -> D'1970.01.01 00:00';

- this variable will store the time of the last request to the trading history.


Then in OnCalculate(), we subtract the stored timeExtLastMove from the current time (time of the last quote using anyTimeCurrent symbol) and if the difference is less than 60 seconds, we exit.

//---
   datetime time_current=TimeCurrent();
   if(time_current-ExtLastMove<60) // 60 seconds
      return(rates_total);
   ExtLastMove=time_current;

if the difference is larger, then we record a new time intoExtLastMove.


The trading history is accessed using LastProfitForPeriod().

Two dates are used here: from and to. The "To" date is set to the future (the current time plus one day), the"From" date is obtained by subtracting 24 hours from the current time

   datetime from_date   = time_current-60*60*24;   // time one day ago
   datetime to_date     = time_current+60*60*24;   // time from the future :)
   double   last_profit = LastProfitForPeriod(from_date,to_date);

   Comment("Last profit: ",DoubleToString(last_profit,2));


That's all for now, that's a lot of text...

 
Vladimir, thank you very much for your code and especially for comments to its fragments!
I don't understand only, why we need to_date equal to the future? May be up to the current moment? It seems that we can't determine the result of the deal in the future :) Or do we need this day in the future to draw the line in the future time?
Another question about the loop.
As far as I understand, we load an array with data on deals and loop through the array from zero deal to the last deal, comparing the time with the last written and overwrite the variable last_time, if the deal_time is later. Or maybe the reverse is true - the array can be searched from the end? Actually, the last element of the array will be the last closed transaction for which we need data?
Perhaps I make some logical errors - please don't judge too harshly :)
 
meant for(uint i=0;i<total;i++) change to for(uint i=total;i>=0;i--)
 
In general, you don't need a loop either - you just access the array element with the ordinal number i=total
 
I just don't understand why we need to_date equal to knock in the future?

We need it that way. To get guaranteed "from" and to the current time.

Or maybe it's possible to search the array from the end?

If the task is to find the last deal, then the array must go through and look for precisely the time - just in case something happens or the internal kernel logic is changed and deals will be reset randomly.

 
Vladimir Karputov:

This is the way to do it. To be guaranteed to get "from" and to the current time.

If the task is to find the last deal, then the Array must go through and look for the exact time - just in case something happens or internal kernel logic changes and deals will be discarded randomly.

Ok, got it. In principle, given the limitation of the history of 24 hours there will clearly be no significant load when going through the data :)

 
renatmt5:

OK, got it. In principle, given the 24 hour history limit, there's clearly not going to be any significant workload when going through the data :)

So, the final touch is to move the line depending on the profit made.

Do you remember what the moving rules are there?

 
I understand correctly that last_time and result are the variables we are working with. The result determines by how much time the line will be shifted (redrawn) into the future. Then the code block "Profit for the period" should be put before the block "Move the vertical line" to move the line taking into account the data of the last deal.
Is it correct or am I confusing something?
 

Here's an explanation of why the "before" parameter should be done in the future:

Forum on trading, automated trading systems and trading strategy testing

Errors, bugs, questions

Renat Fatkhullin, 2016.11.04 12:43

Never make TimeCurrent() as an end date, make TimeCurrent()+86400 to ensure all end trades are captured.

TimeCurrent is not the exact last time, but the "last known server time during updates", which is not 100% accurate. Especially at the start.


Reason: