[Solved] Displaying EntryTime and ExitTime with seconds

Questions about MultiCharts and user contributed studies.
User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

[Solved] Displaying EntryTime and ExitTime with seconds

Postby JoshM » 15 Jun 2011

Solved: see post below.

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;
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[0] and Time_s[CurrentBar – Stored bar number] to display the entry and exit time with seconds.
Thanks in advance for any thoughts/comments/suggestions,

Regards,
Josh
Attachments
barNumberProblem.PNG
(25.9 KiB) Downloaded 300 times
Last edited by JoshM on 17 Jun 2011, edited 1 time in total.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Displaying EntryTime and ExitTime with seconds: how?

Postby JoshM » 17 Jun 2011

Though I still don't know where I went wrong with the above bar numbers code, that's not relevant anymore, because I've found a solution to display the entry time and exit time in seconds. And guess what, it's easier than I've guessed (well, in hindsight at least). ;)

For those interested, here's my working code now:

Code: Select all

value89 = totaltrades;

if (BarsSinceExit(1) = 0 or BarsSinceExit(1) = 1) and (value89[1] < TotalTrades) and (EntryDate(1) > 0) then begin

value45 = BarsSinceEntry(1);
value80 = BarsSinceExit(1);

myTestString = Text("#", NumToStr(totaltrades, 0), "__", date:6:0, "_",
"EntryTime: ", FormatTime("HH:mm:ss", el_timetodatetime_s(time_s[value45])),
" ExitTime: ", FormatTime("HH:mm:ss", el_timetodatetime_s(time_s[value80]))
);
Print(myTestString);
end;
Which results in the following output:

Code: Select all

#1__1110104_EntryTime: 11:02:01 ExitTime: 12:13:55
#2__1110104_EntryTime: 12:13:55 ExitTime: 14:45:45
#3__1110104_EntryTime: 14:45:45 ExitTime: 16:51:01
#4__1110105_EntryTime: 16:51:52 ExitTime: 21:59:51
#5__1110105_EntryTime: 08:18:17 ExitTime: 08:21:45
#6__1110105_EntryTime: 08:21:45 ExitTime: 10:05:07
#7__1110105_EntryTime: 10:09:39 ExitTime: 10:45:31
#8__1110105_EntryTime: 11:55:40 ExitTime: 14:12:52
#9__1110105_EntryTime: 14:15:09 ExitTime: 15:38:47
#10__1110106_EntryTime: 08:30:30 ExitTime: 09:27:58
#11__1110106_EntryTime: 09:27:58 ExitTime: 10:50:20
#12__1110106_EntryTime: 10:50:20 ExitTime: 11:09:46
#13__1110106_EntryTime: 11:11:48 ExitTime: 11:29:56
#14__1110106_EntryTime: 12:01:15 ExitTime: 13:35:00
Which is in agreement with the Performance Report and the trades on the graph.

Because my original if entry statement missed trades, I turned to the more complicated use of the bar numbers. Here I went wrong, and in hindsight it would have paid off to let the problem rest instead of trying to create ever more "creative" and complex "solutions" for the problem.

Just thought I should share this solution and what I did wrong with you guys. Perhaps someone finds it useful.

Regards,
Josh

User avatar
Stan Bokov
Posts: 963
Joined: 18 Dec 2009
Has thanked: 367 times
Been thanked: 302 times

Re: [Solved] Displaying EntryTime and ExitTime with seconds

Postby Stan Bokov » 20 Jun 2011

Thanks for the update Josh. It really helps when people post updates with solutions.


Return to “MultiCharts”