Implementing Multi-Timeframe Heikin Ashi Strategy in ProBuilder

25 Aug 2023
0 comment
0 attachment

This code snippet demonstrates how to implement a simple trading strategy using Heikin Ashi candlesticks calculated from a higher timeframe (1 hour) and executed on a lower timeframe (1 minute) using the ProBuilder programming language.


TIMEFRAME(1 hour,updateonclose) // Heikin-Ashii candle setup
if barindex > 0 then
    xClose = (open+close+low+high)/4
    xOpen = (xOpen[1]+xClose[1])/2
    //haHigh = Max(xOpen, xClose)
    //haLow = Min(xOpen, xClose)
    //xHigh = Max(High,haHigh)
    //xLow = Min(Low,haLow)
else
    xClose = (open+close+low+high)/4
    xOpen = (Open[1]+Close[1])/2
    //haHigh = Max(xOpen, xClose)
    //haLow = Min(xOpen, xClose)
    //xHigh = Max(High,haHigh)
    //xLow = Min(Low,haLow)
endif
Cond1 = xOpen > xClose //BEARish HA candlestick
TIMEFRAME(1 minute,updateonclose)
Cond2 = close < average[5,0](close) //current price < SMA5
TIMEFRAME(default)
IF Cond1 AND Cond2 AND Not OnMarket THEN
    SELLSHORT 1 CONTRACT AT MARKET
ENDIF
SET TARGET pPROFIT 50
SET STOP pLOSS 20

Explanation of the Code:

  • The code starts by setting the timeframe to 1 hour to calculate the Heikin Ashi (HA) candlesticks. Heikin Ashi candles are a type of candlestick chart that averages price data to create a smoother appearance.
  • Conditional Logic: If it's not the first bar (barindex > 0), the HA close and open prices are calculated based on the formulae provided. If it is the first bar, the HA open is calculated differently to handle the edge case.
  • The code then checks if the HA candlestick is bearish (xOpen > xClose), which is stored in Cond1.
  • The timeframe is switched to 1 minute to check the current price against a 5-period simple moving average (SMA). This condition (close < average[5,0](close)) is stored in Cond2.
  • If both conditions (Cond1 and Cond2) are true and the strategy is not already in the market (Not OnMarket), it triggers a short sell of 1 contract at the market price.
  • Finally, the code sets a profit target of 50 points and a stop loss of 20 points to manage the trade.

This snippet is a practical example of how to implement a multi-timeframe strategy in ProBuilder, utilizing different timeframes for signal confirmation and trade execution.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/strategy-in-two-different-timeframes/#post-91072

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.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
Author’s Profile

Comments

Search Snippets

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

Snippets Categories

global
35
indicator
134
strategy
171

Recent Snippets

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 [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Logo Logo
Loading...