Implementing Conditional Trading Based on Moving Average Crossovers

09 Jun 2018
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to implement a trading strategy based on the crossover of two moving averages, specifically the 20-period and 200-period moving averages. The strategy initiates a buy order when specific conditions are met regarding the time and the gap between these averages.


starttime = 000000
gapneeded = 20
ma20 = average[20](close)
ma200 = average[200](close)
magap = ma20 - ma200

if opentime = starttime then
  crossflag = 0
  gapflag = 0
endif

if ma20 crosses over ma200 then
  crossflag = 1
endif

if crossflag and magap > gapneeded then
  gapflag = 1
endif

if ma20 crosses under ma200 then
  crossflag = 0
  gapflag = 0
endif

if crossflag and gapflag then
  buy at ma20 limit
endif

This code snippet is structured to check conditions at the start of a trading session and to monitor the relationship between two moving averages for trading signals. Here’s a step-by-step breakdown:

  • Initialization: Sets the start time for trading and the minimum required gap between the moving averages.
  • Calculation of Moving Averages: Computes the 20-period and 200-period moving averages of the closing prices.
  • Gap Calculation: Determines the difference between the two moving averages.
  • Session Start Check: At the start of the trading session, resets the flags used to track the crossover and gap conditions.
  • Crossover Detection: Checks if the 20-period moving average crosses over the 200-period moving average and sets a flag if true.
  • Gap Condition Check: If a crossover has occurred and the gap between the moving averages is greater than the specified minimum, a second flag is set.
  • Reverse Crossover Detection: Resets the flags if the 20-period moving average crosses back under the 200-period moving average.
  • Trade Execution: If both the crossover and gap conditions are met, a buy order is placed at the level of the 20-period moving average.

This example is useful for understanding conditional statements and real-time trading logic in financial markets using ProBuilder.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/first-hit-of-a-moving-average/#post-75105

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.
Vonasi Master
V-oyaging ON A S-mall I-ncome
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...