1

I am attempting to find the very first day of the backtest, and then the very last day of the backtest. This will be determined by some type of method. NOT by a date range input setting, because it changes and sometimes there is no session set, the backtest # of bars determines it. It has to find whatever the backtester has selected as the range, identify the beginning and end of that period, and then output report it, as a date.

So it will read: "9/16/21 To 11/10/22"

FirstDayOfBacktest= //How?
LastDayOfBacktest= //How?

BacktestedPeriodDateOutputted = str.format("{1, date, short} + To ", FirstDay) + str.format("{1, date, short}", LastDay)

1 Answers1

1

If using the strategy tester, you can read the date of the first- and last trade. Reference

var string startDate = na
if strategy.closedtrades == 0 and 0 < strategy.opentrades
    startDate := str.format_time(strategy.opentrades.entry_time(strategy.opentrades - 1), "yyyy-MM-dd HH:mm", syminfo.timezone)
var string endDate = na
if 0 < strategy.closedtrades
    lastEntry = strategy.opentrades.entry_time(strategy.opentrades - 1)
    lastExit = strategy.closedtrades.exit_time(strategy.closedtrades - 1)
    endDate := str.format_time(math.max(lastEntry, lastExit), "yyyy-MM-dd HH:mm", syminfo.timezone)
elod008
  • 1,227
  • 1
  • 5
  • 15
  • Thanks, trying now. As a bonus, do you know how to get all of the days, even on the days no trades occurred, outputted as a total integer, between those two dates? I'd be forever grateful.

    So, `startDate = This
    endDate = This
    TotalDays = startDate to endDate
    str.tostring("Total Days:") str.tonumber(TotalDays, ...) = 215 Days.
    And as a super bonus, how did you get your code block so neat? I can't format, I read the format, it says to put two spaces and to make a new line, I did that. I can't get my code to look as neat as yours.
    – TheLegendOfA Apr 16 '23 at 21:58
  • You just make "math.round((lastExit - startdateInt) / 1000 * 60 * 60 * 24)" to get the number of days in your strategy. NOTE that you'll need to save the start date as an integer to a variable for that, like it's done with lastExit. In a comment you cannot format code so nicely I fear. – elod008 Apr 18 '23 at 10:28
  • Hi, it doesn't work. I get the error " Error at 673:5 Variable 'startDate' was declared with 'simple int' type. Cannot assign it expression of type 'series string' " – TheLegendOfA May 04 '23 at 15:33
  • Yeah, it's not working. :( Even declaring it as a string. Nothing shows up for the values when I try to plot them in a label as a str.tostring(startDate) or endDate – TheLegendOfA May 04 '23 at 16:43
  • You are right, the error's on me. Also I forgot to add the "var" keyword to keep the result you retrieved on the first-last occasions. I updated my answer and tested it myself and it works. To test it you can open up a new strategy (if you will, with a new tradingview boilerplate), add my answer at the end and extend it by: "label.new(bar_index, high, startDate + '\n' + endDate)". It will print the details accordingly. Note that if you want to print only on the last bar you'll need to add to the strategy definition: "calc_on_every_tick = true" otherwise "if barstate.islast" won't work. – elod008 May 06 '23 at 09:41