Implementing Sequential Event Detection in Trading Strategies

29 Apr 2019
0 comment
0 attachment

This code snippet demonstrates how to implement a sequential event detection system in a trading strategy using the ProBuilder language. The system checks for a series of conditions (events) across different timeframes before executing a trade. Each event has a specific lookback period, after which the condition must be re-evaluated.


TIMEFRAME(Daily,updateonclose)
ONCE LookBackA = 5 //after 5 bars reenable checking event
ONCE barA = 0
IF barA AND (barindex - barA) >= LookBackA THEN
    barA = 0
ENDIF
IF barA = 0 THEN
    eventA = average[10,0](close) CROSSES OVER average[30,0](close)
    IF eventA THEN
        barA = barindex //save the bar number
    ENDIF
ENDIF

// TIMEFRAME(4 hour,updateonclose)
ONCE LookBackB = 5
ONCE barB = 0
IF (barB AND (barindex - barB) >= LookBackB) OR barA = 0 THEN
    barB = 0
ENDIF
IF barB = 0 AND barA THEN
    eventB = rsi[14](close) CROSSES OVER 30
    IF eventB THEN
        barB = barindex
    ENDIF
ENDIF

// TIMEFRAME(1 hour,updateonclose)
ONCE LookBackC = 5
ONCE barC = 0
IF (barC AND (barindex - barC) >= LookBackC) OR barB = 0 THEN
    barC = 0
ENDIF
IF barC = 0 AND barB THEN
    eventC = MACD[12,26,9](close) > 0
    IF eventC THEN
        barC = barindex
    ENDIF
ENDIF

// TIMEFRAME(default)
ONCE LookBackD = 5
ONCE barD = 0
IF (barD AND (barindex - barD) >= LookBackD) OR barC = 0 THEN
    barD = 0
ENDIF
IF barD = 0 AND barC THEN
    eventD = (high = highest[20](high))
    IF eventD THEN
        barD = barindex
    ENDIF
ENDIF

IF eventA AND eventB AND eventC AND eventD AND Not OnMarket THEN
    BUY 1 CONTRACT AT MARKET
    SET STOP PLOSS 20
    SET TARGET PROFIT 60
ENDIF

The code is structured to check for four sequential trading events across different timeframes before placing a trade. Here’s a breakdown of its functionality:

  • Event A: Checks for a crossover of two moving averages on the daily timeframe. If this condition is met within the specified lookback period, it records the bar index.
  • Event B: On the 4-hour timeframe, it checks if the RSI crosses over 30, but only if Event A has previously occurred and is still valid within its lookback period.
  • Event C: On the 1-hour timeframe, it evaluates if the MACD is above zero, contingent on the successful occurrence and validity of Event B.
  • Event D: Finally, on the default timeframe, it checks if the current high is the highest of the last 20 bars, following the successful validation of Event C.
  • If all events are confirmed and the strategy is not already in the market, it executes a buy order with specified stop loss and profit target settings.

This sequential checking ensures that each condition is dependent on the fulfillment of the previous one, making the strategy stringent and potentially more reliable.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/mtf-sequential-setup/#post-102442

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
133
strategy
171

Recent Snippets

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 [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...