Important: The time data provided by QuantScript is in the UTC+0 timezone
Let's modify our Hello, World! strategy and add some time constraints to it. We need to replace ourcondition with some code which uses the and/or values of the current bar.
The original strategy looks like this:
We're going to adjust it so it only opens long positions on Mondays, by using theconstant and the equals operator:
Every bar our code executes against has its ownvalue. Only when the current bar falls on a Monday does the strategy open a long position.
Let's add a time constraint between 10:15 and 13:37:
Say we want to trade on Tuesday too, between 12:00 and 12:30. The code would get a bit long for a single line, so we will useparentheses to break it up into multiple lines for readability. The parentheses are also needed to group the statements together so they can be combined with an operator.
Here's what it looks like:
The strategy is still closing positions every chance it gets. Let's apply these time constraints to exiting as well:
A bit redundant, right? Next, we will see how variables can help reduce code repetitiveness.