This code snippet demonstrates how to implement a moving average crossover trading strategy with an additional distance trigger for order execution in ProBuilder. The strategy uses two moving averages (fast and slow) and places orders based on their crossovers, modified by a distance condition.
defparam cumulateorders=false
fast = average[12,1]
slow = average[36,1]
distance = 30
if (fast>slow and high crosses over level) or (fast
Explanation of the Code:
- Initialization: Orders are not cumulated (defparam cumulateorders=false), which means each new order cancels the previous one.
- Setting Averages: Two moving averages are defined, fast as a 12-period average and slow as a 36-period average.
- Distance Setup: A distance variable is set to 30, which will be used to create a buffer or threshold around the slow moving average.
- Trigger Condition: A trigger is set to 1 if the fast moving average is greater than the slow and the high price crosses over a dynamically defined level, or if the fast is less than the slow and the low price crosses under the level.
- Order Execution: Orders are placed based on the direction of the crossover:
- If the fast average crosses over the slow, and the trigger is active, a buy order is executed at market price. The level is then adjusted to be the slow average plus the product of distance and point size.
- If the fast average crosses under the slow, and the trigger is active, a sell short order is executed at market price. The level is adjusted to be the slow average minus the product of distance and point size.
- Resetting the Trigger: The trigger is reset to 0 after each crossover to ensure it must be set again for new orders.
- Graphical Representation: The code includes commented out lines for graphing the fast and slow averages, high, low, level, and trigger values for visual analysis.
This code snippet is a practical example of combining moving averages with a price level trigger to control order execution, useful for developing automated trading strategies.