Multi-Timeframe Stochastic and MACD Screener Implementation

18 Jan 2020
0 comment
0 attachment

This code snippet demonstrates how to implement a screener using both the Stochastic Oscillator and MACD (Moving Average Convergence Divergence) indicators across multiple timeframes (weekly, daily, 4-hour, and 1-hour) in the ProBuilder language. The screener evaluates whether both indicators are moving upwards, downwards, or are neutral, and assigns a specific code for each scenario and timeframe.

// Constants (can be used throughout TF's with the same name)
Stoc1 = 4 //4
Stoc2 = 4 //4
Stoc3 = 4 //4
Macd1 = 12 //12
Macd2 = 26 //26
Macd3 = 9 //9

// Variables for each timeframe: W = weekly, D = Daily, 4 = 4-hour, 1 = 1-hour
// Weekly Timeframe
TIMEFRAME(Weekly)
StocKW = Stochastic[Stoc1,Stoc2](close)
StocDW = Average[Stoc3](StocKW)
StocUPW = StocKW > StocDW
StocDNW = StocKW < StocDW
MacdValW = MACD[Macd1,Macd2,Macd3](close)
MacdUPW = MacdValW > MacdValW[1]
MacdDNW = MacdValW < MacdValW[1]
IF StocUPW AND MacdUPW THEN
    xW = 1000
ELSIF StocDNW AND MacdDNW THEN
    xW = 2000
ELSE
    xW = 9000
ENDIF

// Daily Timeframe
TIMEFRAME(Daily)
StocKD = Stochastic[Stoc1,Stoc2](close)
StocDD = Average[Stoc3](StocKD)
StocUPD = StocKD > StocDD
StocDND = StocKD < StocDD
MacdValD = MACD[Macd1,Macd2,Macd3](close)
MacdUPD = MacdValD > MacdValD[1]
MacdDND = MacdValD < MacdValD[1]
IF StocUPD AND MacdUPD THEN
    xD = 100
ELSIF StocDND AND MacdDND THEN
    xD = 200
ELSE
    xD = 900
ENDIF

// 4-hour Timeframe
TIMEFRAME(4 hours)
StocK4 = Stochastic[Stoc1,Stoc2](close)
StocD4 = Average[Stoc3](StocK4)
StocUP4 = StocK4 > StocD4
StocDN4 = StocK4 < StocD4
MacdVal4 = MACD[Macd1,Macd2,Macd3](close)
MacdUP4 = MacdVal4 > MacdVal4[1]
MacdDN4 = MacdVal4 < MacdVal4[1]
IF StocUP4 AND MacdUP4 THEN
    x4 = 10
ELSIF StocDN4 AND MacdDN4 THEN
    x4 = 20
ELSE
    x4 = 90
ENDIF

// 1-hour Timeframe
TIMEFRAME(1 hour)
StocK1 = Stochastic[Stoc1,Stoc2](close)
StocD1 = Average[Stoc3](StocK1)
StocUP1 = StocK1 > StocD1
StocDN1 = StocK1 < StocD1
MacdVal1 = MACD[Macd1,Macd2,Macd3](close)
MacdUP1 = MacdVal1 > MacdVal1[1]
MacdDN1 = MacdVal1 < MacdVal1[1]
IF StocUP1 AND MacdUP1 THEN
    x1 = 1
ELSIF StocDN1 AND MacdDN1 THEN
    x1 = 2
ELSE
    x1 = 9
ENDIF

// Combine results from all timeframes
TIMEFRAME(DEFAULT)
Result = xW + xD + x4 + x1
IF Result = 9999 THEN
    Result = 0
ENDIF
SCREENER[Result] (Result AS "wd41")

Explanation of the Code:

  • Constants and Variables: The code starts by defining constants for the Stochastic and MACD parameters, which are reused across different timeframes. Variables specific to each timeframe are also defined.
  • Timeframe-specific Calculations: For each timeframe (weekly, daily, 4-hour, 1-hour), the Stochastic and MACD values are calculated. The code checks if these indicators are trending upwards or downwards by comparing current values to their previous states or averages.
  • Decision Logic: Based on the direction of the indicators, a specific code (e.g., 1000 for weekly upwards) is assigned. This helps in identifying the trend direction across multiple timeframes.
  • Result Combination and Screening: The results from all timeframes are summed up and passed to a screener function, which can be used to filter instruments based on these combined trend indicators.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/multi-timeframe-screener-needed/#post-85538

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...