1

Almost every day at 14.30 CET European indices and futures change substantial because data from the US become available. Also when US markets open at 16.00 CET European markets react again and keep following US markets afterwards.

I noticed that around these times quite a number of losing trades occur while backtesting because no indicator can really predict what happens at those times. I would like to test a strategy that would close open positions a few minutes before those events occur in order not to 'gamble' on those two (and maybe other ??) events and look if this makes a difference.

So on a 1 minute timeframe I would like to close positions at 14.28 and at 15.58. Of course I would like to make this general instead of hardcoding the times. How can I check if a bars time is X minutes before a (user) specified time?

SoftwareTester
  • 1,048
  • 1
  • 10
  • 25

1 Answers1

0

The simplest way is to hardcode it like you say. If you want to customize it, it's still easy but somewhat more code. So the 2 options for you that should work:

//@version=5
indicator("My script", overlay = true)

// option 1: customized solution possibility
// you could do something like this or just input the hour and minute
// reference from TV website
i_time_diff = input.int(120, "Diff minutes")
i_date = input.time(timestamp("30 Oct 2022 22:00:00"), "Date") // @link https://www.tradingview.com/pine-script-reference/v5/#fun_input{dot}time

if i_date - time <= i_time_diff * 60 * 1000 // compare milliseconds
    label.new(bar_index, high, str.tostring('Foo'))
    // close strategy logic

// option 2: very simple solution on 1 minute
// you could also customize it with inputting your minute or hour
if minute == 28 and hour == 14
    label.new(bar_index, high, 'Bar')
    // close strategy logic
elod008
  • 1,227
  • 1
  • 5
  • 15