Planet
Download scriptThis script functions as a technical analysis tool designed to identify market trends and specific entry signals by monitoring multiple financial indicators simultaneously. It works by analyzing "higher timeframe" data to determine the overall direction of the market and then looking for specific "triggers" on lower timeframes to suggest potential actions.
Core Logic Overview
The logic is divided into two primary layers: Trend Identification and Trigger Detection.
1. Trend Identification (High-Level Analysis)
The script constantly monitors a higher timeframe (the 4-Hour chart) to establish the "big picture" trend. It looks for a specific convergence of three different conditions:
- Moving Average Crossover: It tracks two moving averages. When the shorter average crosses above the longer one, it signals upward momentum; when it crosses below, it signals downward momentum.
- Relative Strength Index (RSI): It checks if the RSI is above or below a middle threshold (50) to confirm the strength of the current price movement.
- Stochastic Oscillator: It evaluates the relationship between the main and signal lines of a Stochastic indicator to ensure the momentum aligns with the trend.
If all three conditions align in a positive direction, the script identifies an Up Trend. If they align negatively, it identifies a Down Trend. This "TrendON" status serves as the foundation for all subsequent signals.
2. Trigger Detection (Actionable Signals)
The script features a specific set of logic for identifying precise moments to enter a trade (specifically tailored for the GBPUSD pair on a 30-minute timeframe). It looks for a "perfect storm" where four distinct indicators align:
- Moving Average Crossover: A quick crossover on the current chart.
- RSI Position: Confirmation that the price is in the correct territory (above or below 50).
- Stochastic Change: Ensuring the oscillators are moving in the desired direction.
- MACD Momentum: Checking the Moving Average Convergence Divergence to confirm the velocity of the move.
For a signal to be valid, these four conditions must meet the criteria at once. Furthermore, the script can be configured to only show these triggers if they align with the "High-Level" trend identified in the first step, ensuring that short-term signals aren't traded against the long-term market direction.
Visual and Notification Features
- Visual Aids: The script draws lines on the chart to visually represent whether the current trend is Up or Down. It also places arrows on the chart to mark specific "Triggers."
- Customization for GBPUSD: The script contains unique logic that adjusts its visual style (labels and line thickness) specifically when viewing the GBPUSD currency pair on a 30-minute timeframe.
- Automated Alerts: It includes an optional email notification system. When a valid trigger occurs, the script can compile all current technical data—including the exact values of the Moving Averages, RSI, Stochastic, and MACD—into a detailed summary and send it via email.
//+------------------------------------------------------------------+
//| Planet |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red
extern int IndicatorDisplacement = 15;
extern bool Show_Trend = true;
extern bool Only_Show_Valid_Triggers= true;
extern bool Send_Mail = false;
//---- buffers
double UPTrend[];
double DNTrend[];
double UPTrigger[];
double DNTrigger[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- additional buffers are used for counting
IndicatorBuffers(4);
//---- indicators
SetIndexLabel(2,NULL);
SetIndexLabel(3,NULL);
if((Symbol()=="GBPUSD")&&(Period()==PERIOD_M30))
{
SetIndexLabel(0,NULL);
SetIndexLabel(1,NULL);
SetIndexStyle(0,DRAW_LINE,STYLE_DOT,1,Teal);
SetIndexStyle(1,DRAW_LINE,STYLE_DOT,1,Teal);
}
else
{
SetIndexLabel(0,"UP TREND");
SetIndexLabel(1,"DOWN TREND");
SetIndexStyle(0,DRAW_LINE,0,3,Teal);
SetIndexStyle(1,DRAW_LINE,0,3,Teal);
}
SetIndexStyle(2,DRAW_ARROW,EMPTY,3,Teal);
SetIndexStyle(3,DRAW_ARROW,EMPTY,3,Teal);
SetIndexArrow(2,SYMBOL_ARROWUP);
SetIndexArrow(3,SYMBOL_ARROWDOWN);
SetIndexBuffer(0,UPTrend);
SetIndexBuffer(1,DNTrend);
SetIndexBuffer(2,UPTrigger);
SetIndexBuffer(3,DNTrigger);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int maCross,StochCHANGE,maCross4H,StochCHANGE4H,MACDChange,i,counted_bars=IndicatorCounted();
double ma5now,ma10now,ma5prev,ma10prev,ma5now4H,ma10now4H,ma5prev4H,ma10prev4H;
double RSI,Stoch1,Stoch2,Signal1,Signal2,MACDChange1,MACDChange2,RSI4H,Stoch4H1,Stoch4H2,Signal4H1,Signal4H2;
int Trigger,i4h;
static int TrendON;
bool GBP15;
string text,CRLF;
CRLF = CharToStr(13) + CharToStr(10);
GBP15=((Symbol()=="GBPUSD")&&(Period()==PERIOD_M30));
if(Bars<=100) return(0);
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- the last counted bar will be recounted
if(counted_bars>0) counted_bars--;
i=Bars-counted_bars;
while(i>=0)
{
//---- TREND4H
i4h=iBarShift(NULL,PERIOD_H4,iTime(NULL,0,i+1),true)+1;
ma5now4H=iMA(NULL,PERIOD_H4,5,0,MODE_EMA,PRICE_CLOSE,i4h);
ma10now4H=iMA(NULL,PERIOD_H4,10,0,MODE_EMA,PRICE_CLOSE,i4h);
ma5prev4H=iMA(NULL,PERIOD_H4,5,0,MODE_EMA,PRICE_CLOSE,i4h+1);
ma10prev4H=iMA(NULL,PERIOD_H4,10,0,MODE_EMA,PRICE_CLOSE,i4h+1);
if(ma5now4H > ma5prev4H && ma10now4H > ma10prev4H && ma5prev4H < ma10prev4H && ma5now4H > ma10now4H) maCross4H=1;
else if(ma5now4H < ma5prev4H && ma10now4H < ma10prev4H && ma5prev4H > ma10prev4H && ma5now4H < ma10now4H) maCross4H=2;
else maCross4H=0;
RSI4H= iRSI(NULL,PERIOD_H4,9,PRICE_CLOSE,i4h);
Stoch4H1= iStochastic(NULL,PERIOD_H4,10,3,3,MODE_SMA,0,MODE_MAIN,i4h);
Stoch4H2= iStochastic(NULL,PERIOD_H4,10,3,3,MODE_SMA,0,MODE_MAIN,i4h+1);
Signal4H1= iStochastic(NULL,PERIOD_H4,10,3,3,MODE_SMA,0,MODE_SIGNAL,i4h);
Signal4H2= iStochastic(NULL,PERIOD_H4,10,3,3,MODE_SMA,0,MODE_SIGNAL,i4h+1);
if(Stoch4H1>Stoch4H2 && Signal4H1>Signal4H2) StochCHANGE4H=1;
else if(Stoch4H1<Stoch4H2 && Signal4H1<Signal4H2) StochCHANGE4H=2;
else StochCHANGE4H=0;
if((maCross4H==1) && (RSI4H>50) && (StochCHANGE4H==1)) TrendON=1;
if((maCross4H==2) && (RSI4H<50) && (StochCHANGE4H==2)) TrendON=-1;
if(Show_Trend)
{
if(TrendON==1)
{
UPTrend[i]= High[i]+IndicatorDisplacement*Point;
SetIndexLabel(0,"UP TREND");
SetIndexLabel(1,NULL);
}
else if(TrendON==-1)
{
DNTrend[i]= Low[i]-IndicatorDisplacement*Point;
SetIndexLabel(0,NULL);
SetIndexLabel(1,"DOWN TREND");
}
}
if(GBP15)
{
//---- TRIGGERS
ma5now=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,i+1);
ma10now=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,i+1);
ma5prev=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,i+2);
ma10prev=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,i+2);
if(ma5now > ma5prev && ma10now > ma10prev && ma5prev < ma10prev && ma5now > ma10now) maCross=1;
else if(ma5now < ma5prev && ma10now < ma10prev && ma5prev > ma10prev && ma5now < ma10now) maCross=2;
else maCross=0;
RSI= iRSI(NULL,0,9,PRICE_CLOSE,i+1);
Stoch1= iStochastic(NULL,0,10,3,3,MODE_SMA,0,MODE_MAIN,i+1);
Stoch2= iStochastic(NULL,0,10,3,3,MODE_SMA,0,MODE_MAIN,i+2);
Signal1= iStochastic(NULL,0,10,3,3,MODE_SMA,0,MODE_SIGNAL,i+1);
Signal2= iStochastic(NULL,0,10,3,3,MODE_SMA,0,MODE_SIGNAL,i+2);
if(Stoch1>Stoch2 && Signal1>Signal2) StochCHANGE=1;
else if(Stoch1<Stoch2 && Signal1<Signal2) StochCHANGE=2;
else StochCHANGE=0;
MACDChange1=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i+1);
MACDChange2=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i+2);
if(MACDChange2<0 && MACDChange2<MACDChange1) MACDChange=1;
else if(MACDChange2>0 && MACDChange2>MACDChange1) MACDChange=2;
else MACDChange=0;
if((maCross==1) && (RSI>50) && (StochCHANGE==1) && (MACDChange==1) && ((TrendON==1)||(Only_Show_Valid_Triggers==false))) Trigger=1;
if((maCross==2) && (RSI<50) && (StochCHANGE==2) && (MACDChange==2) && ((TrendON==-1)||(Only_Show_Valid_Triggers==false))) Trigger=2;
if((UPTrigger[i+1]!=0x7fffffff)||(DNTrigger[i+1]!=0x7fffffff)) Trigger=0;
if(Trigger==1) UPTrigger[i+1]= High[i+1]+10*Point;
if(Trigger==2) DNTrigger[i+1]= Low[i+1]-10*Point;
if((Trigger)&&(Send_Mail) && (TimeYear(Time[i])>=Year())&&(TimeMonth(Time[i])>=Month())&&(TimeDay(Time[i])>=Day())&&(TimeHour(Time[i])>=Hour())&&(TimeMinute(Time[i])>=Minute()))
{
if(Trigger==1) text="BUY ";
else text="SELL ";
text=text + "trigger occurred on " +TimeDay(Time[i+1])+"/"+TimeMonth(Time[i+1])+"/"+TimeYear(Time[i+1])+" at "+TimeHour(Time[i+1])+":"+TimeMinute(Time[i+1]) + " (server time)" + CRLF;
text=text + CRLF + CRLF;
text=text + "Trend is ";
if(TrendON==1) text=text + "UP" + CRLF + CRLF;
else text=text + "DOWN" + CRLF + CRLF;
text = text +"MA5 = " + ma5now + CRLF;
text = text +"MA10 = " + ma10now + CRLF;
text = text +"MA5 Previous Bar = " + ma5prev + CRLF;
text = text +"MA10 Previous Bar = " + ma10prev + CRLF;
text = text +"RSI = " + RSI + CRLF;
text = text +"STOCHASTIC MAIN = " + Stoch1 + CRLF;
text = text +"STOCHASTIC SIGNAL = " + Signal1 + CRLF;
text = text +"STOCHASTIC MAIN Previous Bar = " + Stoch2 + CRLF;
text = text +"STOCHASTIC SIGNAL Previous Bar = " + Signal2 + CRLF;
text = text +"MACD = " + MACDChange1 + CRLF;
text = text +"MACD Previous Bar = " + MACDChange2 + CRLF;
SendMail("Planet Telling you",text);
Print(text);
}
}
Trigger=0;
i--;
}
return(0);
}
//+-----------------------------------------------------------------+
Comments