When exporting trade results to a text file for further analysis, I want to export the EntryTime and ExitTime with the seconds timestamp. Because EntryTime and ExitTime don’t have such a feature, and I couldn’t find such a feature in the help manuals, I’m trying to find a workaround for this.
So far, with the storing of the bar number I’ve booked some progress, but for some reason or another the BarNumberOfEntry variable is not updated when a position is initiated.
This is my code:
Code: Select all
If currentbar = 1 then cleardebug;
// Indicator values
maSlow = Average(Close, 30);
maFast = Average(Close, 12);
// Enter long
if MarketPosition = 0 and maFast crosses over maSlow then begin
Buy("LE") 1 contract next bar at open;
BarNumberOfEntry = CurrentBar + 1; // +1 since the position is opened the next bar
// Exit Long
if MarketPosition = 1 and BarsSinceEntry(0) = 5 then begin
Sell("XL") next bar at open;
// Enter Short
if MarketPosition = 0 and maFast crosses under maSlow then begin
SellShort("SE") 1 contract next bar at open;
BarNumberOfEntry = CurrentBar + 1;
// Exit Short
if MarketPosition = -1 and BarsSinceEntry(0) = 5 then begin
BuyToCover("XS") next bar at open;
// Print data
if BarStatus = 2 and barssinceexit(1) = 0 and totaltrades > 0 then begin
Print("#", totaltrades:0:0, "_Open: ", time_s:6:0, " Close: ", time_s:6:0,
" barNumberOfEntry: ", BarNumberOfEntry, " currentBar - entry: ", (currentbar - BarNumberOfEntry));
Which produces the following error (the -1 values):
Code: Select all
#206_Open: 80224 Close: 81053 barNumberOfEntry: 6318.00 currentBar - entry: 6.00
#207_Open: 90145 Close: 91327 barNumberOfEntry: 6357.00 currentBar - entry: -1.00
#208_Open: 91408 Close: 93205 barNumberOfEntry: 6364.00 currentBar - entry: -1.00
#209_Open: 93601 Close: 94658 barNumberOfEntry: 6364.00 currentBar - entry: 6.00
The attached image displays these trades on a chart. As the image shows, these are 'normal' trades (i.e. no multiple entries per bar).
I cannot find the error in my code. Because the first 200 trades were calculated correctly, I guess there is some underlying error I made which I’m just overlooking at this moment. Perhaps my whole approach to this problem is wrong.
So my question is:
What am I doing wrong?
Or, more general:
How can the EntryTime and ExitTime be exported with seconds?
My “flow chart” (I saw other topics asking for this with coding problems) would look as the following:
- 1. Enter order,
2. Store bar number of that bar in a variable,
3. Exit order,
4. BarSinceExit(1) = 0 is True,
5. Use the stored bar number and the CurrentBar to calculate the number of bars of the trade,
6. Print Time_s and Time_s[CurrentBar – Stored bar number] to display the entry and exit time with seconds.
Thanks in advance for any thoughts/comments/suggestions,