Indicators Used
Miscellaneous
0
Views
0
Downloads
0
Favorites
MT4-LevelStop-Reverse-vB0-4_Alert_mtf
//+------------------------------------------------------------------+
//| MT4-LevelStop-Reverse |
//| Version Beta 0.4 |
//| Copyright © 2007, Bruce Hellstrom (brucehvn) |
//| bhweb@speakeasy.net |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
/////////////////////////////////////////////////////////////////////
// Version 0.4 Beta
//
// This is a port of the VTTrader VT-LevelStop-Reverse trading system.
// This is ported as an MT4 indicator only and perhaps can be evolved
// into an EA later.
//
// This is a beta version
//
/////////////////////////////////////////////////////////////////////
/*
This is a combination of two VT Trader trading systems. The first is the default
VT-LevelStop-Reverse and the second is one that was modified to allow customizing
the ATR settings for calculating the stop line. I've tried to combine these 2
versions into a single MT4 indicator.
The default VT version allows you to use two modes, optimized, and manual.
Optimized mode calculates the stop line by using a 14 period EMA smoothed
ATR(14) multiplied by a fixed multiplier of 2.824. In manual mode, you set a
fixed number of pips you want the stop line to be drawn. In my MT4 version,
there are two modes:
1. ATR mode (customizable ATR period, multiplier, and smoothing)
2. Fixed stop mode (customizable fixed stop)
The input parameters are as follows:
* UseATRMode - This calculates the stop line based on ATR using customizable period, multiplier and smoothing.
* NonATRStopPips - If "UseATRMode" is false, then this value is the number of fixed pips to place the stop line.
* ATRPeriod - If "UseATRMode" is true, then this sets the ATR period.
* ATRMultiplier - If "UseATRMode" is true, then the ATR value will be multiplied by this value when calculating the stop line.
* ATRSmoothing - If "UseATRMode" is true, then this will smooth the selected ATR with an EMA of this smoothing period.
* UpArrowColor - The color the Up arrows will display in.
* DnArrowColor - The color the Down arrows will display in.
* ArrowDistance - This can adjust the distance away from the stop line that the arrows appear. By default, the arrows appear directly above or below the stop line. A positive number here will move the arrows further away from the price. A negative number will move it closer to the price.
For the default VT-LevelStop-Reverse behavior, set the following:
UseATRMode = true
ATRPeriod = 14
ATRMultiplier = 2.824
ATRSmoothing = 14
To use this indicator, copy it to your <MetaTrader Folder>\experts\indicators folder. Then restart MT4. It will appear in the custom indicators list.
This is version Beta 0.4. As I get feedback, I will release newer versions as needed.
Revision History
Version Beta 0.2
* Minor bug fixes.
* Remove extra "UseVTDefault" option.
* Add smoothing option for compatibility with default VT version.
Version Beta 0.3
* Delete objects at startup.
* Use a more unique object name prefix.
* Change ATRBuffer and SmoothBuffer to be non-indicator buffers.
* No need for UpSignal and DnSignal to be buffers.
* Change arrows to display at the stop line.
* Fix bug on current bar drawing that would cause multiple arrows to appear.
Version Beta 0.4
* Fix bug in non-indicator buffers that was causing erroneous data in the ATR buffer and smoothing buffer.
*/
#property copyright "Copyright © 2007, Bruce Hellstrom (brucehvn)"
#property link "http: //www.metaquotes.net/"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 DeepPink
#property indicator_style1 STYLE_DOT
#define INDICATOR_VERSION "vB0.4"
#define MAX_OBJECT_NUM 32768
//#define VTS_OBJECT_PREFIX "vtsbh2483-"
//---- input parameters
extern int NonATRStopPips = 40;
extern bool UseATRMode = true;
extern int ATRPeriod = 9;
extern double ATRMultiplier = 3.0;
extern int ATRSmoothing = 0;
extern color UpArrowColor = DodgerBlue;
extern color DnArrowColor = OrangeRed;
extern int ArrowSize = 1;
extern int ArrowDistance = 0;
extern bool alertsOn = true;
extern bool alertsMessage = true;
extern bool alertsSound = false;
extern bool alertsEmail = false;
extern int TimeFrame = 0;
string IndicatorFileName;
string VTS_OBJECT_PREFIX ;
//---- buffers
double TrStopLevel[];
//---- variables
double ATRBuffer[];
double SmoothBuffer[];
string ShortName;
string UniqueName;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
int DrawBegin = 0;
if ( UseATRMode ) {
DrawBegin = ATRPeriod;
}
IndicatorBuffers( 1 );
SetIndexStyle( 0, DRAW_LINE, STYLE_DOT, 1 );
SetIndexBuffer( 0, TrStopLevel );
SetIndexDrawBegin( 0, DrawBegin );
VTS_OBJECT_PREFIX = "vtmt4 "+UniqueName;
ShortName = "MT4-LevelStop-Reverse-" + INDICATOR_VERSION + "(";
if ( UseATRMode ) {
ShortName = StringConcatenate( ShortName, "ATRMode ", ATRPeriod, ", ",
ATRMultiplier, ", ", ATRSmoothing, " )" );
}
else {
ShortName = StringConcatenate( ShortName, "Manual Mode Stop = ", NonATRStopPips, " )" );
}
IndicatorShortName( ShortName );
SetIndexLabel( 0, ShortName );
Print( ShortName );
Print( "Copyright (c) 2007 - Bruce Hellstrom, bhweb@speakeasy.net" );
MathSrand( TimeLocal() );
// Cleanup any leftover objects from previous runs
DeleteAllArrowObjects();
IndicatorFileName = WindowExpertName();
TimeFrame = MathMax(TimeFrame,Period());
UniqueName = MakeUniqueName("","");
return( 0 );
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
DeleteAllArrowObjects();
return( 0 );
}
//+------------------------------------------------------------------+
//| Function run on every tick |
//+------------------------------------------------------------------+
int start() {
bool savedDn = false;
bool savedUp = false;
int ictr;
int counted_bars = IndicatorCounted();
// Check for errors
if ( counted_bars < 0 ) {
return( -1 );
}
// Last bar will be recounted
if ( counted_bars > 0 ) {
counted_bars--;
}
int i,limit = Bars - counted_bars;
if (TimeFrame != Period())
{
limit = MathMax(limit,TimeFrame/Period());
datetime TimeArray[];
ArrayCopySeries(TimeArray ,MODE_TIME ,NULL,TimeFrame);
for(i=0,int y=0; i<limit; i++)
{
if(Time[i]<TimeArray[y]) y++;
TrStopLevel[i] = iCustom(NULL,TimeFrame,IndicatorFileName,
NonATRStopPips,UseATRMode, ATRPeriod, ATRMultiplier,
ATRSmoothing, UpArrowColor,DnArrowColor, ArrowSize, ArrowDistance,
alertsOn,alertsMessage,alertsSound,alertsEmail,0,y);
}
return(0);
}
ictr = limit - 1;
if ( UseATRMode && Bars < ATRPeriod ) {
return( 0 );
}
// Make sure buffers are sized correctly
int buff_size = ArraySize( TrStopLevel );
if ( ArraySize( ATRBuffer ) != buff_size ) {
ArraySetAsSeries( ATRBuffer, false );
ArrayResize( ATRBuffer, buff_size );
ArraySetAsSeries( ATRBuffer, true );
ArraySetAsSeries( SmoothBuffer, false );
ArrayResize( SmoothBuffer, buff_size );
ArraySetAsSeries( SmoothBuffer, true );
}
int xctr;
if ( UseATRMode ) {
// First calculate the ATR
for ( xctr = 0; xctr < limit; xctr++ ) {
ATRBuffer[xctr] = iATR( NULL, 0, ATRPeriod, xctr );
}
// Smooth the ATR if necessary
if ( ATRSmoothing > 0 ) {
for ( xctr = 0; xctr < limit; xctr++ ) {
SmoothBuffer[xctr] = Wilders( ATRBuffer, ATRSmoothing, xctr );
}
}
}
for ( xctr = ictr; xctr >= 0; xctr-- ) {
// Calculate the stop amount
double DeltaStop = NonATRStopPips * Point;
// Calculate our stop value based on ATR if required
if ( UseATRMode ) {
if ( ATRSmoothing > 0 ) {
DeltaStop = NormalizeDouble( SmoothBuffer[xctr] * ATRMultiplier, 4 );
}
else {
DeltaStop = NormalizeDouble( ATRBuffer[xctr] * ATRMultiplier, 4 );
}
}
// Figure out where the current bar's stop level should be
double NewStopLevel;
double PrevStop = TrStopLevel[xctr + 1];
if ( Close[xctr] == PrevStop ) {
NewStopLevel = PrevStop;
}
else {
if ( Close[xctr + 1] <= PrevStop && Close[xctr] < PrevStop ) {
NewStopLevel = MathMin( PrevStop, ( Close[xctr] + DeltaStop ) );
}
else {
if ( Close[xctr + 1] >= PrevStop && Close[xctr] > PrevStop ) {
NewStopLevel = MathMax( PrevStop, ( Close[xctr] - DeltaStop ) );
}
else {
if ( Close[xctr] > PrevStop ) {
NewStopLevel = Close[xctr] - DeltaStop;
}
else {
NewStopLevel = Close[xctr] + DeltaStop;
}
}
}
}
TrStopLevel[xctr] = NewStopLevel;
// Can't do the arrows until the bar closes
if ( xctr > 0 ) {
// Figure out the up/down arrows
bool Up = false;
bool Dn = false;
if ( Close[xctr] > TrStopLevel[xctr] && Close[xctr + 1] <= TrStopLevel[xctr + 1] ) {
Up = true;
double UpSignal = TrStopLevel[xctr] - ( ArrowDistance * Point );
string ObjName = GetNextObjectName();
ObjectCreate( ObjName, OBJ_ARROW, 0, Time[xctr], UpSignal );
ObjectSet( ObjName, OBJPROP_COLOR, UpArrowColor );
ObjectSet( ObjName, OBJPROP_ARROWCODE, 233 );
ObjectSet( ObjName, OBJPROP_WIDTH, ArrowSize );
}
if ( Close[xctr] < TrStopLevel[xctr] && Close[xctr + 1] >= TrStopLevel[xctr + 1] ) {
Dn = true;
double DnSignal = TrStopLevel[xctr] + ( 2 * Point ) + ( ArrowDistance * Point );
// ObjName = MakeUniqueName("","");
ObjName = GetNextObjectName();
ObjectCreate( ObjName, OBJ_ARROW, 0, Time[xctr], DnSignal );
ObjectSet( ObjName, OBJPROP_COLOR, DnArrowColor );
ObjectSet( ObjName, OBJPROP_ARROWCODE, 234 );
ObjectSet( ObjName, OBJPROP_WIDTH, ArrowSize );
}
// Reverse TrStopLevel According to Up and Down Signals
if ( Up ) {
TrStopLevel[xctr] = Close[xctr] - DeltaStop;
if (xctr==1) savedUp=Up;
}
else {
if ( Dn ) {
TrStopLevel[xctr] = Close[xctr] + DeltaStop;
if (xctr==1) savedDn=Dn;
}
}
}
}
//
//
//
//
//
if (alertsOn)
{
if (savedDn) doAlert("down signal");
if (savedUp) doAlert("up signal");
}
//
//
//
//
//
return( 0 );
}
//+------------------------------------------------------------------+
//| Gets the next object index so they can be deleted later |
//+------------------------------------------------------------------+
string GetNextObjectName() {
int rand_val = MathRand() + 1;
string retval = VTS_OBJECT_PREFIX + rand_val;
return( retval );
}
//+------------------------------------------------------------------+
//| Wilders Calculation |
//+------------------------------------------------------------------+
double Wilders( double& indBuffer[], int Periods, int shift ) {
double retval = 0.0;
retval = iMAOnArray( indBuffer, 0, ( Periods * 2 ) - 1, 0, MODE_EMA, shift );
return( retval );
}
//+------------------------------------------------------------------+
//| Delete all the arrow objects |
//+------------------------------------------------------------------+
void DeleteAllArrowObjects() {
for ( int ictr = 0; ictr < MAX_OBJECT_NUM; ictr++ ) {
string ObjName = VTS_OBJECT_PREFIX + ( ictr + 1 );
ObjectDelete( ObjName );
}
return;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
void doAlert(string doWhat)
{
static string previousAlert="nothing";
static datetime previousTime;
string message;
if (previousAlert != doWhat || previousTime != Time[0]) {
previousAlert = doWhat;
previousTime = Time[0];
//
//
//
//
//
message = StringConcatenate(Symbol()+" - "+TimeFrameToString(Period())," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Level stop reverse ",doWhat);
if (alertsMessage) Alert(message);
if (alertsEmail) SendMail(StringConcatenate(Symbol()," Level stop reverse trend change"),message);
if (alertsSound) PlaySound("alert2.wav");
}
}
string TimeFrameToString(int tf)
{
string tfs="";
switch(tf) {
case PERIOD_M1: tfs="M1" ; break;
case PERIOD_M5: tfs="M5" ; break;
case PERIOD_M15: tfs="M15" ; break;
case PERIOD_M30: tfs="M30" ; break;
case PERIOD_H1: tfs="H1" ; break;
case PERIOD_H4: tfs="H4" ; break;
case PERIOD_D1: tfs="D1" ; break;
case PERIOD_W1: tfs="W1" ; break;
case PERIOD_MN1: tfs="MN1";
}
return(tfs);
}
string MakeUniqueName(string first, string rest)
{
string result = first+(MathRand()%1001)+rest;
while (WindowFind(result)> 0)
result = first+(MathRand()%1001)+rest;
return(result);
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---