SchaffTrendCycle_v1

Price Data Components
Indicators Used
Moving average indicator
Miscellaneous
It plays sound alertsIt sends emails
0 Views
0 Downloads
0 Favorites
SchaffTrendCycle_v1
ÿþ//+------------------------------------------------------------------+

//                                              SchaffTrendCycle.mq5 |

//|                                      https://t.me/ForexEaPremium |

//+------------------------------------------------------------------+

#property copyright "https://t.me/ForexEaPremium"

#property link      "https://t.me/ForexEaPremium"

#property version   "1.04"



#property description "Schaff Trend Cycle - Cyclical Stochastic over Stochastic over MACD."

#property description "Falling below 75 is a sell signal."

#property description "Rising above 25 is a buy signal."

#property description "Four kinds of alert: arrows, text, sound, email, and push."



#property indicator_separate_window

#property indicator_buffers 3

#property indicator_plots 1

#property indicator_minimum 0

#property indicator_maximum 100

#property indicator_level1 25

#property indicator_level2 75

#property indicator_width1 2

#property indicator_type1 DRAW_LINE

#property indicator_style1 STYLE_SOLID

#property indicator_color1 clrDarkOrchid

#property indicator_label1 "Schaff Trend Cycle"



//---- Input Parameters

input int MAShort = 23;

input int MALong = 50;

input int Cycle = 10;



input bool ShowArrows = false;

input color UpColor = clrBlue;

input color DownColor = clrRed;

input bool ShowAlerts = false;

input bool SoundAlerts = false;

input bool EmailAlerts = false;

input bool PushAlerts = false;



//---- Global Variables

double Factor = 0.5;

int BarsRequired;

datetime LastAlert = D'1980.01.01';



//---- Buffers

double MACD[];

double ST[];

double ST2[];



//---- MA Buffers

double MAShortBuf[];

double MALongBuf[];



//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

void OnInit()

{

   IndicatorSetString(INDICATOR_SHORTNAME, "STC(" + IntegerToString(MAShort) + "," + IntegerToString(MALong) + "," + IntegerToString(Cycle) + ")");



   SetIndexBuffer(0, ST2, INDICATOR_DATA);

   SetIndexBuffer(1, ST, INDICATOR_CALCULATIONS);

   SetIndexBuffer(2, MACD, INDICATOR_CALCULATIONS);

   

   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, MALong + Cycle * 2);

   IndicatorSetInteger(INDICATOR_DIGITS, 0);

   

   BarsRequired = MALong + Cycle * 2;

}



//+------------------------------------------------------------------+

//| Custom indicator deinitialization function                       |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

   ObjectsDeleteAll(0, "ST_down_");

   ObjectsDeleteAll(0, "ST_up_");

}



//+------------------------------------------------------------------+

//| Schaff Trend Cycle                                               |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &Time[],

                const double &open[],

                const double &High[],

                const double &Low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

{

   if (rates_total <= BarsRequired) return(rates_total);

   

   int counted_bars = prev_calculated;

   

   double LLV = 0, HHV = 0;

   int shift, n = 1, i, myMA;

   // Static variables are used to flag that we already have calculated curves from the previous indicator run.

   static bool st1_pass = false;

   static bool st2_pass = false;

   int st1_count = 0;

   bool check_st1 = false, check_st2 = false;

   

   if (counted_bars < BarsRequired)

   {

      for (i = 0; i < BarsRequired; i++) ST2[i] = 0;

      for (i = 0; i < BarsRequired; i++) ST[i] = 0;

   }



   if (counted_bars > 0) counted_bars--;



   shift = counted_bars - BarsRequired + MALong - 1;

   

   if (shift < 0) shift = 0;



   myMA = iMA(NULL, 0, MAShort, 0, MODE_EMA, PRICE_CLOSE);

   if  (CopyBuffer(myMA, 0, 0, rates_total, MAShortBuf) != rates_total) return(0);

   

   myMA = iMA(NULL, 0, MALong, 0, MODE_EMA, PRICE_CLOSE);

   if  (CopyBuffer(myMA, 0, 0, rates_total, MALongBuf) != rates_total) return(0);



   while (shift < rates_total)

   {

	   MACD[shift] = MAShortBuf[shift] - MALongBuf[shift];

	   

      if (n >= Cycle) check_st1 = true;

      else n++;

	

      if (check_st1)  

      {

         // Finding Max and Min on Cycle of MA differences (MACD).

         for (i = 0; i < Cycle; i++)

         {	

            if (i == 0)

            {

               LLV = MACD[shift - i];

               HHV = MACD[shift - i];

            }

            else

            {

               if (LLV > MACD[shift - i]) LLV = MACD[shift - i];

               if (HHV < MACD[shift - i]) HHV = MACD[shift - i];

            }

         }

         // Calculating first Stochastic.

         if (HHV - LLV != 0) ST[shift] = ((MACD[shift] - LLV) / (HHV - LLV)) * 100;

         else ST[shift] = ST[shift - 1];

         

         // Smoothing first Stochastic

         if (st1_pass) ST[shift] = Factor * (ST[shift] - ST[shift - 1]) + ST[shift - 1];

         st1_pass = true;

                  

         // Have enough elements of first Stochastic to proceed to second.

         if (st1_count >= Cycle) check_st2 = true;

         else st1_count++;

         

         if (check_st2)

         {

            // Finding Max and Min on Cycle of first smoothed Stochastic.

            for (i = 0; i < Cycle; i++)

            {	

               if (i == 0)

               {

                  LLV = ST[shift - i];

                  HHV = ST[shift - i];

               }

               else

               {

                  if (LLV > ST[shift - i]) LLV = ST[shift - i];

                  if (HHV < ST[shift - i]) HHV = ST[shift - i];

               }

            }

            // Calculating second Stochastic.

            if (HHV - LLV != 0) ST2[shift] = ((ST[shift] - LLV) / (HHV - LLV)) * 100;

            else ST2[shift] = ST2[shift - 1];

            

            // Smoothing second Stochastic.

            if (st2_pass) ST2[shift] = Factor * (ST2[shift] - ST2[shift - 1]) + ST2[shift - 1];

            st2_pass = true;

         }

      }

      

      if (shift > 0)

      {

      	if ((ST2[shift] < 75) && (ST2[shift - 1] >= 75))

      	{

      		if (ShowArrows)

      		{

	      		string name = "ST_down_" + TimeToString(Time[shift]);

	      		double offset = (High[shift] - Low[shift]) / 2;

	      		ObjectCreate(0, name, OBJ_ARROW, 0, Time[shift], High[shift] + offset + spread[shift] * Point());

	      		ObjectSetInteger(0, name, OBJPROP_ARROWCODE, 234);

	      		ObjectSetInteger(0, name, OBJPROP_COLOR, DownColor);

	      		ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);

	      	}

		      if ((shift == rates_total - 2) && (LastAlert != Time[rates_total - 1]))

		      {

		      	if (ShowAlerts) Alert("Bearish signal on " + Symbol() + ".");

		      	if (SoundAlerts) PlaySound("alert.wav");

		      	if (EmailAlerts) SendMail("Schaff Trend Cycle Alert", "Bearish signal on " + TimeToString(Time[rates_total - 1]) + " on " + Symbol() + ".");

		      	if (PushAlerts) SendNotification("STC Alert: Bearish signal on " + TimeToString(Time[rates_total - 1]) + " on " + Symbol() + ".");

		      	LastAlert = Time[rates_total - 1];

				}

      	}

      	else if ((ST2[shift] > 25) && (ST2[shift - 1] <= 25))

      	{

      		if (ShowArrows)

      		{

	      		string name = "ST_up_" + TimeToString(Time[shift]);

	      		double offset = (High[shift] - Low[shift]) / 2;

	      		ObjectCreate(0, name, OBJ_ARROW, 0, Time[shift], Low[shift] - offset);

	      		ObjectSetInteger(0, name, OBJPROP_ARROWCODE, 233);

	      		ObjectSetInteger(0, name, OBJPROP_COLOR, UpColor);

	      		ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);

	      	}

		      if ((shift == rates_total - 2) && (LastAlert != Time[rates_total - 1]))

		      {

		      	if (ShowAlerts) Alert("Bullish signal on " + Symbol() + ".");

		      	if (SoundAlerts) PlaySound("alert.wav");

		      	if (EmailAlerts) SendMail("Schaff Trend Cycle Alert", "Bullish signal on " + TimeToString(Time[rates_total - 1]) + " on " + Symbol() + ".");

		      	if (PushAlerts) SendNotification("STC Alert: Bullish signal on " + TimeToString(Time[rates_total - 1]) + " on " + Symbol() + ".");

		      	LastAlert = Time[rates_total - 1];

				}

      	}

     	}

      shift++;

   }



   return(rates_total);

}

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---