Price Crosses MA
Download scriptThis tool is designed to provide visual and audible signals on a trading chart by identifying specific interactions between the market price and a Moving Average (MA). Instead of just showing where the price sits relative to the average, it focuses on clear "breakout" moments to help traders identify potential trend changes.
Core Functionality
The script continuously monitors two primary pieces of data: the current price movement and a calculated Moving Average line. It looks for specific instances where the market price moves far enough away from this average to be considered a significant crossover.
How the Logic Works
- The Moving Average Baseline: The script calculates a smooth average of recent price action (the "MA"). This serves as the baseline or "neutral" zone for the market.
- The Buffer Zone (Crossed_Pips): To prevent "false signals" caused by minor price fluctuations that just touch the line, the tool uses a specific buffer. It only triggers a signal if the price moves a certain distance beyond the Moving Average.
- Visual Indicators:
- When the price breaks significantly above the Moving Average, the script places a visual arrow on the chart to signal an upward movement.
- When the price breaks significantly below the Moving Average, it places a different arrow to signal downward pressure.
- Real-Time Alerts: In addition to the visual arrows, the tool monitors the very latest live candle. If the current price crosses these thresholds in real-time, the script will trigger an automated alert on your screen, notifying you immediately of the "UP" or "DOWN" movement.
Key Behavior Features
- State Memory: The logic is designed to be smart about repeating signals. It tracks the current trend state so that it doesn't spam multiple alerts for the same movement; it waits for a distinct change in direction before notifying you again.
- Historical Analysis: The tool can look back at a specific number of past bars (history) to draw these arrows and lines, allowing you to see how the price has behaved historically in relation to the Moving Average.
- Customizable Sensitivity: Because users have different trading styles, the distance required for a "cross" and the speed of the Moving Average can be adjusted to make the tool more or less sensitive to market moves.
//+------------------------------------------------------------------+
//| MA Cross Arrows.mq4 |
//| Copyright © 2006 Scorpion@fxfisherman.com |
//+------------------------------------------------------------------+
#property copyright "FxFisherman.com"
#property link "http://www.fxfisherman.com"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 White
#property indicator_color3 Red
extern int Crossed_Pips = 10;
extern int MA_Period = 34;
extern int MA_Type = 0;
extern int Shift_Bars=0;
extern int Bars_Count= 1000;
int state;
//---- buffers
double v1[];
double v2[];
double v3[];
int init()
{
IndicatorBuffers(3);
SetIndexArrow(0,217);
SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,1);
SetIndexDrawBegin(1,MA_Period * 2);
SetIndexBuffer(0, v1);
SetIndexLabel(0,"Buy");
SetIndexArrow(1,218);
SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,1);
SetIndexDrawBegin(1,MA_Period * 2);
SetIndexBuffer(1, v2);
SetIndexLabel(1,"Sell");
SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1);
SetIndexDrawBegin(1,MA_Period * 2);
SetIndexBuffer(2, v3);
SetIndexLabel(2,"MA");
watermark();
return(0);
}
int start()
{
double ma;
int previous;
int i;
int shift;
bool crossed_up, crossed_down;
int totalBars = Bars - (MA_Period * 2);
if (Bars_Count > 0 && Bars_Count <= totalBars)
{
i = Bars_Count;
}else if(totalBars <= 0 ) {
return(0);
}else{
i = totalBars;
}
while(i>=0)
{
shift = i + Shift_Bars;
ma = iMA(Symbol(), Period(), MA_Period, 0, MA_Type, PRICE_CLOSE, shift);
crossed_up = High[shift] >= (ma + (Crossed_Pips * Point));
crossed_down = Low[shift] <= (ma - (Crossed_Pips * Point));
v1[i] = NULL;
v2[i] = NULL;
v3[i] = ma;
if (crossed_up && previous != 1) {
v1[i] = ma + (Crossed_Pips * Point);
previous = 1;
}else if(crossed_down && previous != 2){
v2[i] = ma - (Crossed_Pips * Point);
previous = 2;
}
i--;
}
ma = iMA(Symbol(), Period(), MA_Period, 0, MA_Type, PRICE_CLOSE, 0);
if (Close[0] >= ma + (Crossed_Pips * Point) && state != 1) {
state = 1;
Alert(Symbol() + " - UP - Price has crossed up the moving average.");
}else if (Close[0] <= ma - (Crossed_Pips * Point) && state != -1) {
state = -1;
Alert(Symbol() + " - DOWN - Price has crossed down the moving average.");
}
return(0);
}
//+------------------------------------------------------------------+
void watermark()
{
ObjectCreate("fxfisherman", OBJ_LABEL, 0, 0, 0);
ObjectSetText("fxfisherman", "fxfisherman.com", 11, "Lucida Handwriting", RoyalBlue);
ObjectSet("fxfisherman", OBJPROP_CORNER, 2);
ObjectSet("fxfisherman", OBJPROP_XDISTANCE, 5);
ObjectSet("fxfisherman", OBJPROP_YDISTANCE, 10);
return(0);
}
Comments