Implementing a Range Check Function in ProBuilder

23 Oct 2017
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to create and use a custom function to check if a given number falls within a specified range. The function, named `MyRange`, determines whether a value `x` is within a certain distance `z` from a base value `y`. This is useful in trading strategies for validating conditions based on price or other numerical indicators.

Code for the `MyRange` Function:


// MyRange
// indicator to be installed with ProBuilder and be called by strategies
// input:
// x = number to be checked against a range
// y = base to define a range (+/- z)
// z = range within which x will be validated or not
//
// returns:
// 0 = x is NOT within range z
// 1 = x is within above said range
IF x >= (y - z) AND x <= (y + z) THEN
    Result = 1 //X is within range y-z through y+z
ELSE
    Result = 0 //X is not within the above said range
ENDIF
RETURN Result

#### Code for Testing the `MyRange` Function:


// MyStrategy
x = close
y = close + (0.3 * pipsize)
z = 0.5 * pipsize
a = CALL MyRange[x,y,z] // true

x1 = close
y1 = close - (0.6 * pipsize)
z1 = 0.5 * pipsize
a1 = CALL MyRange[x1,y1,z1] // false

x2 = close
y2 = close + (0.9 * pipsize)
z2 = 0.5 * pipsize
a2 = CALL MyRange[x2,y2,z2] // false

GRAPH a
GRAPH a1
GRAPH a2
buy at -close limit

Step-by-Step Explanation:

  • Function Definition: The function `MyRange` takes three parameters: `x` (the number to check), `y` (the base number), and `z` (the range). It returns `1` if `x` is within the range `[y – z, y + z]`, otherwise it returns `0`.
  • Testing the Function: The testing code sets up three scenarios to check if the current closing price (`close`) falls within various ranges defined relative to the closing price itself adjusted by a fraction of `pipsize`.
  • Graphical Output: The results of the function calls (`a`, `a1`, `a2`) are plotted on the graph to visually represent whether the closing price falls within the specified ranges.
  • Trading Command: The last line in the testing code (`buy at -close limit`) is a placeholder for a trading command, demonstrating how the results of the function might be used in a trading strategy.

This example is a practical demonstration of creating reusable functions in ProBuilder to simplify and organize code in trading strategies.

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