MA Cross plus 2nd candle alert

 
Hi,

I struggle with programming, (Somewhat dyslexic -- can't remember code.), so I have to resort to cut and paste methods. I'm trying to put together an EA alert system so that I don't have to watch Forex charts for hours.

The script should be fairly simple as it does not need to open orders, set stops, or close orders. Here's what I have managed so far. Any help to straighten it out, would be greatly appreciated.

GB.
======

//+------------------------------------------------------------------+
//| MACross Alert.mq5 |
//| Copyright GB |
//| |
//+------------------------------------------------------------------+
/* Objective:
To create a buy or sell alert on any chart, and any time frame,
to which this EA is attached, subject to the following criteria:

The SELL Alert
1. Short Moving Average (MA1) crosses down through a Long Moving Average (MA2).
2. Then when the second candle below / after the crossover moves below the crossing candle's
(that is the first candle's) closing price, set a marker on the chart plus a sound
and popup alert that says Potential Sell Opportunity!

The Buy Alert
1. Short Moving Average (MA1) crosses up through a Long Moving Average (MA2).
2. Then when the second candle above / after the crossover moves above the crossing candle's
(that is the first candle's) closing price, set a marker on the chart plus a sound
and popup alert that says Potential Buy Opportunity!
*/

//+------------------------------------------------------------------+
#property copyright "GB"
#property link ""
#property version "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+

//[A] ---------------------------------------------------------------

extern int Period_MA_1=20; // Period of MA 1
extern int Period_MA_2=50; // Period of MA 2

bool Work=true; // EA will work. [Redundant???]
string Symb; // Security name [Is this the Currency pair's name? If not, substitute Currency pair.]
//[B] ---------------------------------------------------------------
int start()
{
double
MA_1_t, // Current MA_1 value
MA_2_t; // Current MA_2 value

bool
Opn_B=false, // Criterion for opening Buy
Opn_S=false; // Criterion for opening Sell
//[C] ---------------------------------------------------------------
// Preliminary processing
if(Bars < Period_MA_2) // Not enough bars
{
Alert("Not enough bars in the window. EA doesn't work.");
return; // Exit start()
}
if(Work==false) // Critical error
{
Alert("Critical error. EA doesn't work.");
return; // Exit start()
}
//[D] --------------------------------------------------------------
// Alert criteria
Symb=Symbol(); // Security name [Currency pair?]
MA_1_t=iMA(NULL,0,Period_MA_1,0,MODE_LWMA,PRICE_TYPICAL,0); // МА_1
MA_2_t=iMA(NULL,0,Period_MA_2,0,MODE_LWMA,PRICE_TYPICAL,0); // МА_2

if (MA_1_t > MA_2_t + [2nd candle moves above 1st candle's close]) // Test for a Buy Alert
{
Opn_B=true; // Criterion for Buy Alert
}
if (MA_1_t > MA_2_t - [2nd candle moves below 1st candle's close]) // Test for a Sell Alert
{
Opn_S=true; // Criterion for a Sell Alert
}
// [E] --------------------------------------------------------------
// Alert condition met. Display a popup and a sound Alert

if (Opn_B=true)
{
Alert(Symbol(), ", ", Period(), ") - BUY OPPORTUNITY!!!");
// Print(", Symbol(), ", ", Period(), ") - BUY OPPORTUNITY!!!");
// Comment(", Symbol(), ", ", Period(), ") - BUY OPPORTUNITY!!!");
PlaySound("Alert.wav");
}

if (Opn_S=true)
{
Alert(Symbol(), ", ", Period(), ") - SELL OPPORTUNITY!!!");
// Print(", Symbol(), ", ", Period(), ") - SELL OPPORTUNITY!!!");
// Comment(", Symbol(), ", ", Period(), ") - SELL OPPORTUNITY!!!");
PlaySound("Alert.wav");
}

//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---

}
//+------------------------------------------------------------------+
 
GBTrader:
Hi,

I struggle with programming, (Somewhat dyslexic -- can't remember code.), so I have to resort to cut and paste methods. I'm trying to put together an EA alert system so that I don't have to watch Forex charts for hours.

The script should be fairly simple as it does not need to open orders, set stops, or close orders. Here's what I have managed so far. Any help to straighten it out, would be greatly appreciated.

GB.
======

//+------------------------------------------------------------------+
//| MACross Alert.mq5 |
//| Copyright GB |
//| |
//+------------------------------------------------------------------+
/* Objective:
To create a buy or sell alert on any chart, and any time frame,
to which this EA is attached, subject to the following criteria:

The SELL Alert
1. Short Moving Average (MA1) crosses down through a Long Moving Average (MA2).
2. Then when the second candle below / after the crossover moves below the crossing candle's
(that is the first candle's) closing price, set a marker on the chart plus a sound
and popup alert that says Potential Sell Opportunity!

The Buy Alert
1. Short Moving Average (MA1) crosses up through a Long Moving Average (MA2).
2. Then when the second candle above / after the crossover moves above the crossing candle's
(that is the first candle's) closing price, set a marker on the chart plus a sound
and popup alert that says Potential Buy Opportunity!
*/

//+------------------------------------------------------------------+
#property copyright "GB"
#property link ""
#property version "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+

//[A] ---------------------------------------------------------------

extern int Period_MA_1=20; // Period of MA 1
extern int Period_MA_2=50; // Period of MA 2

bool Work=true; // EA will work. [Redundant???]
string Symb; // Security name [Is this the Currency pair's name? If not, substitute Currency pair.]
//[B] ---------------------------------------------------------------
int start()
{
double
MA_1_t, // Current MA_1 value
MA_2_t; // Current MA_2 value

bool
Opn_B=false, // Criterion for opening Buy
Opn_S=false; // Criterion for opening Sell
//[C] ---------------------------------------------------------------
// Preliminary processing
if(Bars < Period_MA_2) // Not enough bars
{
Alert("Not enough bars in the window. EA doesn't work.");
return; // Exit start()
}
if(Work==false) // Critical error
{
Alert("Critical error. EA doesn't work.");
return; // Exit start()
}
//[D] --------------------------------------------------------------
// Alert criteria
Symb=Symbol(); // Security name [Currency pair?]
MA_1_t=iMA(NULL,0,Period_MA_1,0,MODE_LWMA,PRICE_TYPICAL,0); // МА_1
MA_2_t=iMA(NULL,0,Period_MA_2,0,MODE_LWMA,PRICE_TYPICAL,0); // МА_2

if (MA_1_t > MA_2_t + [2nd candle moves above 1st candle's close]) // Test for a Buy Alert
{
Opn_B=true; // Criterion for Buy Alert
}
if (MA_1_t > MA_2_t - [2nd candle moves below 1st candle's close]) // Test for a Sell Alert
{
Opn_S=true; // Criterion for a Sell Alert
}
// [E] --------------------------------------------------------------
// Alert condition met. Display a popup and a sound Alert

if (Opn_B=true)
{
Alert(Symbol(), ", ", Period(), ") - BUY OPPORTUNITY!!!");
// Print(", Symbol(), ", ", Period(), ") - BUY OPPORTUNITY!!!");
// Comment(", Symbol(), ", ", Period(), ") - BUY OPPORTUNITY!!!");
PlaySound("Alert.wav");
}

if (Opn_S=true)
{
Alert(Symbol(), ", ", Period(), ") - SELL OPPORTUNITY!!!");
// Print(", Symbol(), ", ", Period(), ") - SELL OPPORTUNITY!!!");
// Comment(", Symbol(), ", ", Period(), ") - SELL OPPORTUNITY!!!");
PlaySound("Alert.wav");
}

//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---

}
//+------------------------------------------------------------------+


Hello

Is this code for MT4?

 
GBTrader:
Hi,

I struggle with programming, (Somewhat dyslexic -- can't remember code.), so I have to resort to cut and paste methods. I'm trying to put together an EA alert system so that I don't have to watch Forex charts for hours.

The script should be fairly simple as it does not need to open orders, set stops, or close orders. Here's what I have managed so far. Any help to straighten it out, would be greatly appreciated.


Before posting please read some of the other threads . . . then you would have seen numerous requests like this one:

Please use this to post code . . . it makes it easier to read.

Reason: