This code snippet demonstrates how to implement a simple trading strategy using the Moving Average Convergence Divergence (MACD) indicator across multiple timeframes in ProBuilder. The strategy initiates a buy order when the MACD values on both 10-minute and 5-minute charts are positive.
defparam cumulateorders=false
timeframe(10 minutes,updateonclose)
macd10 = MACD[12,26,9](close)
timeframe(5 minutes,updateonclose)
macd5 = MACD[12,26,9](close)
condition = macd10>0 and macd5>0
if not longonmarket and condition then
buy at market
endif
Explanation of the Code:
- defparam cumulateorders=false: This line ensures that multiple orders are not accumulated; only one order can be active at a time.
- timeframe(10 minutes, updateonclose): Sets the timeframe for the following lines of code to 10 minutes, and updates the indicator values at the close of each 10-minute period.
- macd10 = MACD[12,26,9](close): Calculates the MACD for the 10-minute timeframe using the standard settings (12, 26, 9) based on the close prices.
- timeframe(5 minutes, updateonclose): Changes the timeframe to 5 minutes, applying to subsequent lines of code.
- macd5 = MACD[12,26,9](close): Computes the MACD for the 5-minute timeframe.
- condition = macd10>0 and macd5>0: Defines the condition for entering a trade. A buy order is triggered only if both MACD values are positive, indicating upward momentum on both timeframes.
- if not longonmarket and condition then buy at market endif: This conditional statement checks if there is no existing long position (not longonmarket) and if the defined condition (both MACD values positive) is true. If both conditions are met, a buy order is executed at the market price.
This example is useful for understanding how to implement and manage trading conditions across different timeframes, a common technique in algorithmic trading to confirm trends and signals across multiple observations.