If you can fix this indicator it will be something special!

To add comments, please log in or register
Ernest Klokow
1200
Ernest Klokow  

I have written an indicator but it is not performing the way it should.

The problem is that the indicator does not take into consideration the higher time frame (H1) when posting the BUY and SELL arrows on the lower time frame (M15) chart.

If you can fix this we will have a fantastic indicator!

Here is the code:

//+------------------------------------------------------------------+
//|                                         Indicator: Technical.mq4 |
//|                                         Created by Ernest Klokow |
//|                                      http://forex-training.co.za |
//+------------------------------------------------------------------+
#property copyright "Ernest Klokow"
#property link      "http://www.forex-training.co.za"
#property version   "1.00"
#property description ""

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 2
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 2
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern int _K_periodH1 = 20;
extern int SlowingH1 = 10;
extern int Period1H1 = 14;
extern int Fast_EMAH1 = 8;
extern int Slow_EMAH1 = 20;
extern int _K_periodM15 = 20;
extern int SlowingM15 = 10;
extern int Period1M15 = 14;
extern int CCPeriod = 30;
extern int ATRPeriod = 13;
extern int Fast_EMAM15 = 8;
extern int Slow_EMAM15 = 20;
datetime time_alert; //used when sending alert
extern bool Audible_Alerts = true;
extern bool Push_Notifications = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | Technical @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      Print(type+" | Technical @ "+Symbol()+","+Period()+" | "+message);
      if(Audible_Alerts) Alert(type+" | Technical @ "+Symbol()+","+Period()+" | "+message);
      if(Push_Notifications) SendNotification(type+" | Technical @ "+Symbol()+","+Period()+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      //Indicator Buffer 1
      if(iStochastic(NULL, PERIOD_H1, _K_periodH1, 3, SlowingH1, MODE_LWMA, 1, MODE_MAIN, 1+i) > iStochastic(NULL, PERIOD_H1, _K_periodH1, 3, SlowingH1, MODE_LWMA, 1, MODE_SIGNAL, 1+i) //Stochastic Oscillator > Stochastic Oscillator
      && iRSI(NULL, PERIOD_H1, Period1H1, PRICE_CLOSE, 1+i) > 50 //Relative Strength Index > fixed value
      && iMACD(NULL, PERIOD_H1, Fast_EMAH1, Slow_EMAH1, 9, PRICE_CLOSE, MODE_MAIN, 1+i) > 0 //MACD > fixed value
      && iStochastic(NULL, PERIOD_M15, _K_periodM15, 3, SlowingM15, MODE_LWMA, 1, MODE_MAIN, 1+i) > iStochastic(NULL, PERIOD_M15, _K_periodM15, 3, SlowingM15, MODE_LWMA, 1, MODE_SIGNAL, 1+i) //Stochastic Oscillator > Stochastic Oscillator
      && iRSI(NULL, PERIOD_M15, Period1M15, PRICE_CLOSE, 1+i) > 51 //Relative Strength Index > fixed value
      && iCustom(NULL, PERIOD_H1, "trend-magic", CCPeriod, ATRPeriod, 0, 1+i) != 0 && iCustom(NULL, PERIOD_H1, "trend-magic", CCPeriod, ATRPeriod, 0, 1+i) != EMPTY_VALUE //trend-magic is not equal to fixed value
      && iCustom(NULL, PERIOD_H1, "trend-magic", CCPeriod, ATRPeriod, 0, 1+i) != 2147483647 //trend-magic is not equal to fixed value
      && iCustom(NULL, PERIOD_M15, "trend-magic", CCPeriod, ATRPeriod, 0, 1+i) != 0 && iCustom(NULL, PERIOD_M15, "trend-magic", CCPeriod, ATRPeriod, 0, 1+i) != EMPTY_VALUE //trend-magic is not equal to fixed value
      && iCustom(NULL, PERIOD_M15, "trend-magic", CCPeriod, ATRPeriod, 0, 1+i) != 2147483647 //trend-magic is not equal to fixed value
      && iMACD(NULL, PERIOD_M15, Fast_EMAM15, Slow_EMAM15, 9, PRICE_CLOSE, MODE_MAIN, 1+i) > 0.002 //MACD > fixed value
      )
        {
         Buffer1[i] = iLow(NULL, PERIOD_M15, 1+i); //Set indicator value at Candlestick Low
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Buy"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer1[i] = 0;
        }
      //Indicator Buffer 2
      if(iStochastic(NULL, PERIOD_H1, _K_periodH1, 3, SlowingH1, MODE_LWMA, 1, MODE_MAIN, 1+i) < iStochastic(NULL, PERIOD_H1, _K_periodH1, 3, SlowingH1, MODE_LWMA, 1, MODE_SIGNAL, 1+i) //Stochastic Oscillator < Stochastic Oscillator
      && iRSI(NULL, PERIOD_H1, Period1H1, PRICE_CLOSE, 1+i) < 50 //Relative Strength Index < fixed value
      && iMACD(NULL, PERIOD_H1, Fast_EMAH1, Slow_EMAH1, 9, PRICE_CLOSE, MODE_MAIN, 1+i) < 0 //MACD < fixed value
      && iStochastic(NULL, PERIOD_M15, _K_periodM15, 3, SlowingM15, MODE_LWMA, 1, MODE_MAIN, 1+i) < iStochastic(NULL, PERIOD_M15, _K_periodM15, 3, SlowingM15, MODE_LWMA, 1, MODE_SIGNAL, 1+i) //Stochastic Oscillator < Stochastic Oscillator
      && iRSI(NULL, PERIOD_M15, Period1M15, PRICE_CLOSE, 1+i) < 49 //Relative Strength Index < fixed value
      && iCustom(NULL, PERIOD_H1, "trend-magic", CCPeriod, ATRPeriod, 1, 1+i) != 0 && iCustom(NULL, PERIOD_H1, "trend-magic", CCPeriod, ATRPeriod, 1, 1+i) != EMPTY_VALUE //trend-magic is not equal to fixed value
      && iCustom(NULL, PERIOD_H1, "trend-magic", CCPeriod, ATRPeriod, 1, 1+i) != 2147483647 //trend-magic is not equal to fixed value
      && iCustom(NULL, PERIOD_M15, "trend-magic", CCPeriod, ATRPeriod, 1, 1+i) != 0 && iCustom(NULL, PERIOD_M15, "trend-magic", CCPeriod, ATRPeriod, 1, 1+i) != EMPTY_VALUE //trend-magic is not equal to fixed value
      && iCustom(NULL, PERIOD_M15, "trend-magic", CCPeriod, ATRPeriod, 1, 1+i) != 2147483647 //trend-magic is not equal to fixed value
      && iMACD(NULL, PERIOD_M15, Fast_EMAM15, Slow_EMAM15, 9, PRICE_CLOSE, MODE_MAIN, 1+i) < 0 //MACD < fixed value
      )
        {
         Buffer2[i] = iHigh(NULL, PERIOD_M15, 1+i); //Set indicator value at Candlestick High
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Sell"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer2[i] = 0;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
William Roeder
20210
William Roeder  
  1. if (i >= MathMin(5000-1, rates_total-1-50)) continu
    Drop this (copied) BS and learn how to do your lookbacks correctly.

  2.  && iMACD(NULL, PERIOD_H1, 
          && iStochastic(NULL, PERIOD_M15,
    On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors.
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum

  3.  for(int i = limit-1; i >= 0; i--){
    
          if(iStochastic(NULL, PERIOD_H1, _K_periodH1, 3, SlowingH1, MODE_LWMA, 1, MODE_MAIN, 1+i) 
    You are mixing apples and oranges.
Ernest Klokow
1200
Ernest Klokow  
whroeder1:
  1. Drop this (copied) BS and learn how to do your lookbacks correctly.

  2. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors.
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. You are mixing apples and oranges.

Thank you very much for that response. Not sure yet how will this help me to resolve the problem. Guess my knowledge level is too low. Think I will rather write an EA and be satisfied with some alerts.

To add comments, please log in or register