Implementing Multi-Timeframe MACD Conditions for Trading Strategies

31 Jul 2023
0 comment
0 attachment

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.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/echelle-de-temps-multiples/#post-82039

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Nicolas Legend
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
135
strategy
171

Recent Snippets

A multi indicator Dashboard for ProRealTime
indicator
This snippet creates a compact on-chart dashboard that aggregates multiple indicator states into a single, readable [...]
How to Track Up/Down Cumulative Volume Inside Each Candle (Tick Volume Delta)
indicator
This snippet separates the real-time, intra-candle volume into up-volume and down-volume based on whether the last [...]
How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
Logo Logo
Loading...