This code snippet demonstrates how to use Multi-Time Frame (MTF) analysis in ProBuilder to open and close trading orders within the same bar. The example uses a higher time frame (1 hour) to decide on opening a position and a lower time frame (1 minute) to close the position, all within the same hour.
Timeframe (1h,UpdateOnClose)
If MyConditions Then
Buy at market
Endif
Timeframe (default)
If LongOnMarket and OpenMinute = 59 Then
Sell At Market
Endif
Explanation of the Code:
- Timeframe (1h,UpdateOnClose): This line sets the script to use a 1-hour time frame for the following conditions. The UpdateOnClose parameter ensures that the trading decisions are based on the closing prices of the 1-hour bars.
- If MyConditions Then: This conditional statement checks if certain predefined conditions (MyConditions) are met. These conditions should be defined elsewhere in your script and could include various technical indicators or price patterns.
- Buy at market: If the conditions are met, this command triggers a market order to buy. This order is executed at the next available price in the market.
- Timeframe (default): After the buy order, the script returns to the default time frame, which is typically the time frame of the chart the script is applied to.
- If LongOnMarket and OpenMinute = 59 Then: This condition checks if there is an open long position and if the current minute is the 59th minute of the hour. The OpenMinute variable should be defined to capture the minute part of the current time.
- Sell At Market: If the above condition is true, it triggers a market order to sell, aiming to close the position at the end of the hour.
This approach allows traders to leverage different time frames to optimize entry and exit points within a single trading session, potentially enhancing trading strategies by taking advantage of more precise timing and price movements.