stopreversal_v1
Download scriptThis script is designed as a visual indicator for the MetaTrader 4 platform, specifically engineered to identify potential market reversal points. It automates the logic of tracking price movement relative to specific "stop levels" to help traders visualize where the trend might shift from bullish to bearish, or vice versa.
Core Logic and Behavior
The script functions by scanning historical price data (the "bars" on your chart) and calculating a dynamic threshold based on price momentum. Here is a breakdown of how that logic works:
1. Tracking Price Momentum The tool looks at the closing price of each candle to determine the direction of the market. It compares the current price against the previous identified stop level to see if the market is moving upward, downward, or staying stagnant.
2. Dynamic Stop Level Calculation Unlike a fixed line, this script calculates a "Stop Level" based on four distinct scenarios:
- Stability: If the price remains unchanged, it holds the previous level.
- Downward Movement: If the price is consistently falling, it identifies a new lower boundary (the current price plus a small buffer of "Pips").
- Upward Movement: If the price is consistently rising, it identifies a new higher boundary (the current price minus a small buffer of "Pips").
- Trend Changes: If the price moves in the opposite direction of the previous bar, it creates a new threshold immediately to account for the sudden shift in volatility.
3. Identifying Reversal Signals The primary goal of the script is to spot the exact moment price "breaks" these calculated levels.
- Buy Signals: The script looks for instances where the price was previously below a level but suddenly closes above it. When this happens, it marks a specific point on your chart (represented as a green arrow).
- Sell Signals: Conversely, if the price was previously above a level but suddenly drops below it, the script marks that point (represented as a red arrow).
Summary for the User
In simple terms, this tool acts as a "trend-follower" that automatically moves its boundaries as the market moves. It filters out minor price fluctuations and only alerts you (via visual icons on the chart) when the price makes a significant enough move to break through its current boundary, signaling a potential change in market direction. The "nPips" setting allows you to control how much "breathing room" the script gives the price before it decides a new level has been set.
//+------------------------------------------------------------------
//| Stopreversal Mt4.mq4
//|
//|
//| Conversion only Dr. Gaines
//| dr_richard_gaines@yahoo.com
//|
//+------------------------------------------------------------------
#property copyright " Dominic."
#property link " http://www.metaquotes.net/"
#property indicator_chart_window
#include <stdlib.mqh>
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_width1 2
#property indicator_color2 Red
#property indicator_width2 2
//+------------------------------------------------------------------
//| Common External variables
//+------------------------------------------------------------------
//+------------------------------------------------------------------
//| External variables
//+------------------------------------------------------------------
extern double nPips = 0.004;
//+------------------------------------------------------------------
//| Special Convertion Functions
//+------------------------------------------------------------------
int LastTradeTime;
double ExtHistoBuffer[];
double ExtHistoBuffer2[];
void SetLoopCount(int loops)
{
}
void SetIndexValue(int shift, double value)
{
ExtHistoBuffer[shift] = value;
}
void SetIndexValue2(int shift, double value)
{
ExtHistoBuffer2[shift] = value;
}
//+------------------------------------------------------------------
//| End
//+------------------------------------------------------------------
//+------------------------------------------------------------------
//| Initialization
//+------------------------------------------------------------------
int init()
{
SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID);
SetIndexArrow(0, 233);
SetIndexBuffer(0, ExtHistoBuffer);
SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID);
SetIndexArrow(1, 234);
SetIndexBuffer(0, ExtHistoBuffer);
SetIndexBuffer(1, ExtHistoBuffer2);
return(0);
}
int start()
{
//+------------------------------------------------------------------
//| Local variables
//+------------------------------------------------------------------
int shift = 0;
double cnt = 0;
double TrStopLevel = 0;
double PREV = 0;
double pass = 0;
SetLoopCount(0);
// loop from first bar to current bar (with shift=0)
for(shift=Bars-2;shift>=0 ;shift--){
if( (Close[shift] == PREV) )
{
TrStopLevel=PREV;
}
else
{
if( (Close[shift+1])<PREV && (Close[shift]<PREV) )
{
TrStopLevel=MathMin(PREV,Close[shift]*(1+nPips));
}
else
{
if( ((Close[shift+1])>PREV) && (Close[shift]>PREV) )
{
TrStopLevel=MathMax(PREV,Close[shift]*(1-nPips));
}
else
{
if( (Close[shift]>PREV) )
{
TrStopLevel=Close[shift]*(1-nPips);
}
else TrStopLevel=Close[shift]*
(1+nPips);
}
}
}
if( Close[shift] > TrStopLevel && Close[shift+1]<PREV && PREV !=
0 )
{
//SetOrder(OP_BUY,1,ask,2,0,ask+TakeProfit*Point,blue);
//Alert("buy");
SetIndexValue(shift, TrStopLevel);
}
if( Close[shift] < TrStopLevel && Close[shift+1]>PREV && PREV !=
0 )
{
//SetOrder(OP_SELL,1,bid,2,0,bid-TakeProfit*Point,Red);
SetIndexValue2(shift, TrStopLevel);
//Alert("Sell");
}
PREV=TrStopLevel;
//Alert(TrStopLevel);
}
return(0);
}
//+------------------------------------------------------------------+
Comments