3

Okay, there are two things I can't seem to do.

  1. I need to calculate the time between all trades of the strategy (from each individual trade's entry to exit), then out of all of these, average them. Then, convert that output to minutes. Let's call it AverageTradeDurationTime =

  2. Next, similarly, calculate the time between the trades inactive period: Aka, when the trade was "flat" and there weren't any trades going on. Average all of them together, to get an average amount, in days, between all trades. The average. So let's call it TotalDaysBetweenAveraged =

How?

So, from arrays, to i loops, to persistent vars .... nothing I am doing is working here.

#1 - Code Attempt.

FirstTradeTime = strategy.opentrades.entry_time(0)

LastTradeTime = strategy.closedtrades.exit_time(99999) //What do I even put here?

DaysElapsed = (LastTradeTime - FirstTradeTime) / 86400000

DaysConvertedToWholeNumber = math.round((DaysElapsed / 86400000), 2)

plot(DaysConvertedToWholeNumber, "Total Days Between First And Last Trades", color=color.purple)

#2 - Code Attempt.

LastTradeTimeClose = strategy.closedtrades.exit_time(99999) //What do I even put here?

NextTradeTimeEntry = strategy.opentrades.entry_time(0)

DaysBetween = (LastTradeTime - FirstTradeTime) / 86400000

var TotalDaysBetween = 0

if DaysBetween > 0
     TotalDaysBetween := +1

TotalDaysBetweenAveraged = (TotalDaysBetween / DaysBetween)

DaysBetweenConvertedToWholeNumber = math.round((TotalDaysBetweenAveraged / 86400000), 2)

plot(TotalDaysBetweenAveraged, "Total Days Between Trades", color=color.purple)
  • can you provide with the exact datatypes and data structure including relations ? – jmvcollaborator Apr 15 '23 at 01:07
  • Considering I don't even know what this means, I don't think that will be possible lol. This is generalized. Just take any simple strategy and have it do it for the backtest period, so it takes the first entry and last trade exit, then calculates the time, in minutes, of the duration between them. – TheLegendOfA Apr 15 '23 at 02:01
  • @G.Lebret Had a super chaotic week sorry been backlogged. Checking your code first thing and seeing if it worked!! Really appreciate it, as you are truly the only one who answered me and it means the world to me. – TheLegendOfA May 04 '23 at 04:50
  • 1
    @TheLegendOfA thanks for your reply. You upvoted the answer, which is good, but if the answer works for you, you should accept it (click on the little tick at the left of the answer). – G.Lebret May 04 '23 at 05:42
  • @G.Lebret Hi, it's not working. Commented below. – TheLegendOfA May 04 '23 at 17:33

1 Answers1

1

Here is a piece of code I use to do this :

MeanDuratinOfATrade_m is the average duration of the closed trades.
MeanDurationBetweenTrade_m is the average duration between the closed trades : if there are 2 trades, it is the unique duration between the end of the first closed trade and the beginning of the ast closed trade.

var Affichage = table.new(position=position.top_right,  columns=3, rows=25, border_color = color.black, border_width = 1)
if barstate.isrealtime
    // Mean duration of trades
    DurationInTrade_ms = 0
    MeanDuratinOfATrade_m = 0.0
    MeanDurationBetweenTrade_m = 0.0
    if strategy.closedtrades > 0
        for i = 0 to strategy.closedtrades - 1
            DurationInTrade_ms := DurationInTrade_ms + strategy.closedtrades.exit_time(i) - strategy.closedtrades.entry_time(i) // Duration in milliseconds (from UNIX time)
        MeanDuratinOfATrade_m := (DurationInTrade_ms / strategy.closedtrades) / 1000 / 60
        // Mean duration between trades
        if strategy.closedtrades > 1
            TotalTimeBetweenFirstAndLastTrade_ms = strategy.closedtrades.exit_time(strategy.closedtrades-1) - strategy.closedtrades.entry_time(0)
            MeanDurationBetweenTrade_m := (TotalTimeBetweenFirstAndLastTrade_ms - DurationInTrade_ms)/(strategy.closedtrades -1) / 1000 / 60
        // Affichage
        table.cell(Affichage, 0, 0, "Duration of a trade [m]")
        table.cell(Affichage, 1, 0, str.tostring(MeanDuratinOfATrade_m, "#.##"))
        table.cell(Affichage, 0, 1, "Duration Betweentrade [m]")
        table.cell(Affichage, 1, 1, str.tostring(MeanDurationBetweenTrade_m, "#.##"))

On the screen, it will appear once the current bar close : enter image description here

G.Lebret
  • 2,826
  • 2
  • 16
  • 27
  • Hi, this doesn't work. No table appears anywhere on the chart. Thanks for the attempt. – TheLegendOfA May 04 '23 at 17:15
  • 1
    The table appears is barstate.isrealtime : you can see the table on the realtime when a NEW bar appears. It is a problem on pinescript : you will see the chart and must wait for the new bar for the code to be executed and see the table : the problem, if you are on a 1 hour time you have to wait perhaps 1 hour. You can replace barstate.isrealtime by true in the code, it will execute the calcul on each bar (so perhaps it will slow down your chart). – G.Lebret May 05 '23 at 02:52
  • 1
    @TheLegendOfA : you can't say it doesn't work (see my edit with the screenshot) – G.Lebret May 05 '23 at 07:03
  • Dude, you're a freaking genius! Thank you SO MUCH. Literally saved me days of stress. If I could buy you a healthy tea I would. – TheLegendOfA May 05 '23 at 13:33
  • Hi, just noticed one thing: Why didn't you use var to make the variables persistent? It seems to be resetting to 0 at each bar, and therefore, the output only takes into consideration one single duration, then averages that, on the last bar, then outputs. So, you're only getting an average based on one single trade, not all of them. This is not the desirable effect, as it will skew the data. How would you advise? – TheLegendOfA May 05 '23 at 14:07
  • Also, it's super impractical on a chart with a daily timeframe to wait an entire day to populate the data, there must be another way to run this code without the "barstate.isrealtime" ? – TheLegendOfA May 05 '23 at 14:19
  • 1
    you are right, you can try without barstate.isrealtime (it will slow the calculation, but certainly ok) – G.Lebret May 05 '23 at 14:22
  • 1
    But initially, 'if barstate.isrealtime' is here to realise the calcul at the end of the drawing of the chart. No need to use var in this case. – G.Lebret May 05 '23 at 14:25
  • Okay I noticed one inaccuracy: On this code for the inactive, "strategy.closedtrades.entry_time(0)" The 0 will only return the very first trade, but, we need to iterate through the loop and calculate all of the values, to get an average, not just the very first and that's it for the entire loop. Does this make sense? So, how do we fix it? I put "i" in there, it doesn't seem to do anything but return a negative number. – TheLegendOfA May 05 '23 at 19:01