2

I'm now coding a very basic moving average crossover trading system on an hourly chart.

What I'd love to know is how to only open and close trades between 7am and 10pm.

I've used the following code but it still shows trades opening outside these hours (like midnight and 1am in the attached picture - I'll be far into dreamland by then!)

TimeWindow=time(timeframe.period,"0700-2200")    
EnterLong = SmallEMA>MedEMA and close>SmallEMA and close>MedEMA and TimeWindow

Can you see if I'm missing anything? (The blue squares in the screenshot are the 7am-10pm trading hours)

vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • What's your ticker id and what's your chart's timezone set to? – vitruvius Dec 22 '22 at 18:39
  • This should work. I suspect you are using a different timezone than your exchange's timezone. – vitruvius Dec 22 '22 at 18:40
  • Hi, it's the USDJPY and I've got the chart set to UK time. I've tried playing around with the time zones but the location of the entries and exits remains the same. Is there a way to code GMT into the chart? Thanks. – Dave_R_Pierce Dec 23 '22 at 17:24

1 Answers1

1

The timezone argument of the time function is set to syminfo.timezone by default. That is the timezone of the exchange of the chart main series.

So, unless you manipulate this parameter, it does not matter what timezone you use on your chart. time() will always return the same value.

Just add your timezone information to your time() call:

TimeWindow=time(timeframe.period,"0700-2200", "GMT")
vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • You are an absolute legend Vitruvius. Adding the "GMT" has worked. Thanks again. – Dave_R_Pierce Dec 23 '22 at 20:55
  • My pleasure :) Protip: You can do something like `bgcolor(TimeWindow ? color.new(color.blue, 85) : na)` to highlight the session. This would visually help you understand what's going on with the timezones. – vitruvius Dec 23 '22 at 21:00
  • Thanks for that. That would be a lot clearer than the indicator I've currently put on the chart! – Dave_R_Pierce Dec 23 '22 at 21:49