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:
This example is a practical demonstration of creating reusable functions in ProBuilder to simplify and organize code in trading strategies.