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
vars:
maSlow(0),
maFast(0),
BarNumberOfEntry(0);
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
end;
// Exit Long
if MarketPosition = 1 and BarsSinceEntry(0) = 5 then begin
Sell("XL") next bar at open;
end;
// Enter Short
if MarketPosition = 0 and maFast crosses under maSlow then begin
SellShort("SE") 1 contract next bar at open;
BarNumberOfEntry = CurrentBar + 1;
end;
// Exit Short
if MarketPosition = -1 and BarsSinceEntry(0) = 5 then begin
BuyToCover("XS") next bar at open;
end;
// Print data
if BarStatus = 2 and barssinceexit(1) = 0 and totaltrades > 0 then begin
Print("#", totaltrades:0:0, "_Open: ", time_s[6]:6:0, " Close: ", time_s:6:0,
" barNumberOfEntry: ", BarNumberOfEntry, " currentBar - entry: ", (currentbar - BarNumberOfEntry));
end;
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
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[0] and Time_s[CurrentBar – Stored bar number] to display the entry and exit time with seconds.
Regards,
Josh