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
33
indicator
132
strategy
171

Recent Snippets

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) [...]
Calculating Break-Even Points in ProBuilder
strategy
This ProBuilder code snippet demonstrates how to calculate the break-even point for a trading position. The break-even [...]
Implementing Time and Day Restrictions in Trading Algorithms
strategy
This code snippet demonstrates how to set restrictions on trading activities based on specific days of the week and [...]
Implementing Partial Position Closure Based on Price Retracement in ProBuilder
strategy
This code snippet demonstrates how to partially close a trading position when the price retraces to a certain level in [...]
Logo Logo
Loading...