Issue with arrow drawing when using 2 MTF stochastic indicators on chart.

To add comments, please log in or register
Revazi Tchitanava
69
Revazi Tchitanava  
Hello I have created multitimeframe stochastic indicator. but when I am trying to use 2 indicators on chart it deletes arrow from one sub windows and show only at one sub window. I even tried to use global variables but it get still error. so please if anybody could help me. screenshot
//+------------------------------------------------------------------+
//|                                               MTF Stochastic.mq4 |
//|                              Copyright 2019, R.T. Software Corp. |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, R.T. Software Corp."
#property link      ""
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 14

extern   bool                 Use_Alert         =  false;
extern   string               S_1               =  "Stochastic 1 Settings";
extern   ENUM_TIMEFRAMES      Stoch1_timeframe  =  PERIOD_H4;
extern   int                  Stoch1_Kperiod    =  14;
extern   int                  Stoch1_Dperiod    =  3;
extern   int                  Stoch1_slowing    =  3;
extern   color                Stoch1_Up         =  clrGreen;
extern   color                Stoch1_Dn         =  clrRed;

extern   string               S_2               =  "Stochastic 2 Settings";
extern   ENUM_TIMEFRAMES      Stoch2_timeframe  =  PERIOD_D1;
extern   int                  Stoch2_Kperiod    =  14;
extern   int                  Stoch2_Dperiod    =  3;
extern   int                  Stoch2_slowing    =  3;
extern   color                Stoch2_Up         =  clrCyan;
extern   color                Stoch2_Dn         =  clrDarkCyan;

extern   string               S_3               =  "Stochastic 3 Settings";
extern   ENUM_TIMEFRAMES      Stoch3_timeframe  =  PERIOD_W1;
extern   int                  Stoch3_Kperiod    =  14;
extern   int                  Stoch3_Dperiod    =  3;
extern   int                  Stoch3_slowing    =  3;
extern   color                Stoch3_Up         =  clrWhite;
extern   color                Stoch3_Dn         =  clrDarkGray;

int arrow = 0;

double SSK1[];
double SSD1[];
double SSK2[];
double SSD2[];
double SSK3[];
double SSD3[];


double arrUpSSK1_0[];
double arrDnSSK1_0[];
double arrUpSSK1_1[];
double arrDnSSK1_1[];

double arrUpSSK2[];
double arrDnSSK2[];

double arrUpSSK3[];
double arrDnSSK3[];


bool boolUpSSK_0 = false;
bool boolDnSSK_0 = false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, arrUpSSK1_0);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1, clrNONE);
   SetIndexBuffer(1, arrDnSSK1_0);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 1, clrNONE);
   SetIndexBuffer(2, arrUpSSK1_1);
   SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 1, clrNONE);
   SetIndexBuffer(3, arrDnSSK1_1);
   SetIndexStyle(3, DRAW_LINE, STYLE_SOLID, 1, clrNONE);
   SetIndexBuffer(4, arrUpSSK2);
   SetIndexStyle(4, DRAW_LINE, STYLE_SOLID, 1, clrNONE);
   SetIndexBuffer(5, arrDnSSK2);
   SetIndexStyle(5, DRAW_LINE, STYLE_SOLID, 1, clrNONE);
   SetIndexBuffer(6, arrUpSSK3);
   SetIndexStyle(6, DRAW_LINE, STYLE_SOLID, 1, clrNONE);
   SetIndexBuffer(7, arrDnSSK3);
   SetIndexStyle(7, DRAW_LINE, STYLE_SOLID, 1, clrNONE);
   
   SetIndexBuffer(8, SSK1);
   SetIndexStyle(8, DRAW_LINE, STYLE_SOLID, 1, clrNONE); // clrLightSeaGreen
   SetIndexBuffer(9, SSD1);
   SetIndexStyle(9, DRAW_LINE, STYLE_SOLID, 1, clrNONE);
   SetIndexBuffer(10, SSK2);
   SetIndexStyle(10, DRAW_LINE, STYLE_SOLID, 2, clrNONE); // clrGreen
   SetIndexBuffer(11, SSD2);
   SetIndexStyle(11, DRAW_LINE, STYLE_SOLID, 1, clrNONE);
   SetIndexBuffer(12, SSK3);
   SetIndexStyle(12, DRAW_LINE, STYLE_SOLID, 3, clrNONE); // clrBlue
   SetIndexBuffer(13, SSD3);
   SetIndexStyle(13, DRAW_LINE, STYLE_SOLID, 1, clrNONE);
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("MTFSS");
   
   SetIndexLabel(0, "arrUpSSK1_0");
   SetIndexLabel(1, "arrDnSSK1_0");
   SetIndexLabel(2, "arrUpSSK1_0");
   SetIndexLabel(3, "arrDnSSK1_0");
   SetIndexLabel(4, "arrUpSSK2");
   SetIndexLabel(5, "arrDnSSK2");
   SetIndexLabel(6, "arrUpSSK3");
   SetIndexLabel(7, "arrDnSSK3");
   
   SetIndexLabel(8, "SSK1");
   SetIndexLabel(9, "SSD1");
   SetIndexLabel(10, "SSK2");
   SetIndexLabel(11, "SSD2");
   SetIndexLabel(12, "SSK3");
   SetIndexLabel(13, "SSD3");
//--- set levels
/*
   IndicatorSetInteger(INDICATOR_LEVELS, 5);

   IndicatorSetDouble (INDICATOR_LEVELVALUE, 0, 20);
   IndicatorSetDouble (INDICATOR_LEVELVALUE, 1, 30);
   IndicatorSetDouble (INDICATOR_LEVELVALUE, 2, 50);
   IndicatorSetDouble (INDICATOR_LEVELVALUE, 3, 70);
   IndicatorSetDouble (INDICATOR_LEVELVALUE, 4, 80);
   
   IndicatorSetInteger(INDICATOR_LEVELSTYLE, STYLE_SOLID);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR, clrWhite);
   IndicatorSetInteger(INDICATOR_LEVELWIDTH, 1);
*/
//--- set maximum and minimum for subwindow
   IndicatorSetDouble(INDICATOR_MINIMUM, 0);
   IndicatorSetDouble(INDICATOR_MAXIMUM, 100);
//---
   arrow  = (int) GlobalVariableGet("arrow");
   if(arrow == 0)
   {
      arrow = 1;
      GlobalVariableSet("arrow", arrow);
   }
   else
   {
      arrow = 0;
      GlobalVariableSet("arrow", arrow);
   }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   ObjectsDeleteAll(ChartID(), ChartWindowFind(ChartID(), "MTFSS" + IntegerToString(arrow)), OBJ_ARROW);
//---
   GlobalVariableDel("arrow");
//---
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   if(TimeCurrent() >= D'2019.09.12')
   {
      Alert("Your Trial License Expired! ");
      return -1;
   }
//---
   static datetime Signal;
   bool use_empty = true;
//---
   Stochastick(Stoch1_timeframe, Stoch1_Kperiod, Stoch1_Dperiod, Stoch1_slowing, SSK1, SSD1);
   Stochastick(Stoch2_timeframe, Stoch2_Kperiod, Stoch2_Dperiod, Stoch2_slowing, SSK2, SSD2);
   Stochastick(Stoch3_timeframe, Stoch3_Kperiod, Stoch3_Dperiod, Stoch3_slowing, SSK3, SSD3);
//---
   int counted_bars = IndicatorCounted();
   if(counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   for(int i = 0; i < limit; i++)
   {
//=============================================================================================================================
// Trend State
//=============================================================================================================================
//=============================================================================================================================
// Trend Trigger Alert
//=============================================================================================================================   

   
   //--- The line colour magenta is only when SSK1 > SSD1 AND ((SSK2 > SSK2) AND SSK3 > SSD3))
   if(SSK1[i] > SSD1[i] &&
      SSK2[i] > SSD2[i] &&
      SSK3[i] > SSD3[i] )
   {
      //--- Trend State = Bullish1
      boolUpSSK_0 = true;
      boolDnSSK_0 = false;
      //---
      if( (SSK1[i + 1] < SSD1[i + 1] && SSK1[i] > SSD1[i]) ||
          (SSK2[i + 1] < SSD2[i + 1] && SSK2[i] > SSD2[i]) ||
          (SSK3[i + 1] < SSD3[i + 1] && SSK3[i] > SSD3[i]) )
      {
         //---
         if(Use_Alert && Time[0] != Signal)
         {
            Alert(_Symbol, " Bullish trigger ", IntegerToString(_Period), " ", Time[i]);
            Signal = Time[0];
         }
         //---
         if(ObjectFind(ChartID(), IntegerToString(arrow) + "Arrow SSD1 " + IntegerToString(i)) == - 1)
         {
            ArrowCreate(ChartID(), IntegerToString(arrow) + "Arrow SSD1 " + IntegerToString(i), ChartWindowFind(ChartID(), "MTFSS"), Time[i], arrUpSSK1_0[i], 241, ANCHOR_BOTTOM, clrGreen);
         }
         //---
      }
   }
   //--- The line colour DARKmagenta is only when SSK1 < SSD1 AND ((SSK2 < SSK2) AND SSK3 < SSD3))
   if(SSK1[i] < SSD1[i] &&
      SSK2[i] < SSD2[i] &&
      SSK3[i] < SSD3[i] )
   {
      //--- Trend State = Bearish
      boolUpSSK_0 = false;
      boolDnSSK_0 = true;
      //---
      if( (SSK1[i + 1] > SSD1[i + 1] && SSK1[i] < SSD1[i]) ||
          (SSK2[i + 1] > SSD2[i + 1] && SSK2[i] < SSD2[i]) ||
          (SSK3[i + 1] > SSD3[i + 1] && SSK3[i] < SSD3[i]) )
      {
         //---
         if(Use_Alert && Time[0] != Signal)
         {
            Alert(_Symbol, " Bearish trigger ", IntegerToString(_Period), " ", Time[i]);
            Signal = Time[0];
         }
         //---
         if(ObjectFind(ChartID(), IntegerToString(arrow) + "Arrow SSK1 " + IntegerToString(i)) == - 1)
         {
            ArrowCreate(ChartID(), IntegerToString(arrow) + "Arrow SSK1 " + IntegerToString(i), ChartWindowFind(ChartID(), "MTFSS"), Time[i], arrDnSSK1_0[i], 242, ANCHOR_BOTTOM, clrRed);
         }
         //---
      }
   }
   //--- The line colour green is only when SSK1 > SSD1 AND ((SSK2 < SSK2) or SSK3 < SSD3))
   if(SSK1[i] > SSD1[i] &&
      (SSK2[i] < SSD2[i] ||
       SSK3[i] < SSD3[i] ) )
   {
      //---
      arrUpSSK1_1[i] = SSK1[i];
      if(arrUpSSK1_1[i + 1] == EMPTY_VALUE && use_empty)
      {
         arrUpSSK1_1[i + 1] = SSK1[i];
      }
      SetIndexStyle (2, DRAW_LINE, STYLE_SOLID, 5, Stoch1_Up);
   }
   //--- The line colour red is only when SSK1 < SSD1 AND ((SSK2 > SSK2) or SSK3 > SSD3))
   if(SSK1[i] < SSD1[i] &&
      (SSK2[i] > SSD2[i] ||
       SSK3[i] > SSD3[i] ) )
   {
      //---
      arrDnSSK1_1[i] = SSK1[i];
      if(arrDnSSK1_1[i + 1] == EMPTY_VALUE && use_empty)
      {
         arrDnSSK1_1[i + 1] = SSK1[i];
      }
      SetIndexStyle (3, DRAW_LINE, STYLE_SOLID, 5, Stoch1_Dn);
   }
   //---
   if(SSK2[i] > SSD2[i])
   {
      //---
      arrUpSSK2[i] = SSK2[i];
      if(arrUpSSK2[i + 1] == EMPTY_VALUE && use_empty)
      {
         arrUpSSK2[i + 1] = SSK2[i];
      }
      SetIndexStyle (4, DRAW_LINE, STYLE_SOLID, 5, Stoch2_Up);
   }
   else
   {
      //---
      arrDnSSK2[i] = SSK2[i];
      if(arrDnSSK2[i + 1] == EMPTY_VALUE && use_empty)
      {
         arrDnSSK2[i + 1] = SSK2[i];
      }
      SetIndexStyle (5, DRAW_LINE, STYLE_SOLID, 5, Stoch2_Dn);
   }
   //---
   if(SSK3[i] > SSD3[i])
   {
      //---
      arrUpSSK3[i] = SSK3[i];
      if(arrUpSSK3[i + 1] == EMPTY_VALUE && use_empty)
      {
         arrUpSSK3[i + 1] = SSK3[i];
      }
      SetIndexStyle (6, DRAW_LINE, STYLE_SOLID, 5, Stoch3_Up);
   }
   else
   {
      //---
      arrDnSSK3[i] = SSK3[i];
      if(arrDnSSK3[i + 1] == EMPTY_VALUE && use_empty)
      {
         arrDnSSK3[i + 1] = SSK3[i];
      }
      SetIndexStyle (7, DRAW_LINE, STYLE_SOLID, 5, Stoch3_Dn);
   }
   //---
   if(boolUpSSK_0)
   {
      //---
      arrUpSSK1_0[i] = SSK1[i];
      if(arrUpSSK1_0[i + 1] == EMPTY_VALUE && use_empty)
      {
         arrUpSSK1_0[i + 1] = SSK1[i];
      }
      SetIndexStyle (0, DRAW_LINE, STYLE_SOLID, 5, clrMagenta);
   }
   //---
   if(boolDnSSK_0)
   {
      arrDnSSK1_0[i] = SSK1[i];
      if(arrDnSSK1_0[i + 1] == EMPTY_VALUE && use_empty)
      {
         arrDnSSK1_0[i + 1] = SSK1[i];
      }
      SetIndexStyle (1, DRAW_LINE, STYLE_SOLID, 5, clrDarkMagenta);
   }
   //---
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void Stochastick(ENUM_TIMEFRAMES      Stoch_timeframe,
                 int                  Stoch_Kperiod,
                 int                  Stoch_Dperiod,
                 int                  Stoch_slowing,
                 double&              SSK[],
                 double&              SSD[])
{
   int counted_bars = IndicatorCounted();
   if(Stoch_timeframe < _Period) Stoch_timeframe = _Period;
   if(counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   for(int i = 0; i < limit; i++)
   {
      SSK[i] = iStochastic(_Symbol, Stoch_timeframe, Stoch_Kperiod, Stoch_Dperiod, Stoch_slowing, MODE_SMA, 0, MODE_MAIN,
                    iBarShift(_Symbol, Stoch_timeframe, Time[i], false));
      SSD[i] = iStochastic(_Symbol, Stoch_timeframe, Stoch_Kperiod, Stoch_Dperiod, Stoch_slowing, MODE_SMA, 0, MODE_SIGNAL,
                    iBarShift(_Symbol, Stoch_timeframe, Time[i], false));
   }
}
//+------------------------------------------------------------------+ 
//| Create the arrow                                                 | 
//+------------------------------------------------------------------+ 
bool ArrowCreate(const long              chart_ID=0,           // chart's ID 
                 const string            name="Arrow",         // arrow name 
                 const int               sub_window=0,         // subwindow index 
                 datetime                time=0,               // anchor point time 
                 double                  price=0,              // anchor point price 
                 const uchar             arrow_code=252,       // arrow code 
                 const ENUM_ARROW_ANCHOR anchor=ANCHOR_BOTTOM, // anchor point position 
                 const color             clr=clrRed,           // arrow color 
                 const ENUM_LINE_STYLE   style=STYLE_SOLID,    // border line style 
                 const int               width=3,              // arrow size 
                 const bool              back=false,           // in the background 
                 const bool              selection=false,      // highlight to move 
                 const bool              hidden=false,          // hidden in the object list 
                 const long              z_order=0)            // priority for mouse click 
  { 
//--- reset the error value 
   ResetLastError(); 
//--- create an arrow 
   if(!ObjectCreate(chart_ID,name,OBJ_ARROW,sub_window,time,price)) 
     { 
      Print(__FUNCTION__, 
            ": failed to create an arrow! Error code = ",GetLastError()); 
      return(false); 
     } 
//--- set the arrow code 
   ObjectSetInteger(chart_ID,name,OBJPROP_ARROWCODE,arrow_code); 
//--- set anchor type 
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor); 
//--- set the arrow color 
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); 
//--- set the border line style 
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style); 
//--- set the arrow's size 
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width); 
//--- display in the foreground (false) or background (true) 
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); 
//--- enable (true) or disable (false) the mode of moving the arrow by mouse 
//--- when creating a graphical object using ObjectCreate function, the object cannot be 
//--- highlighted and moved by default. Inside this method, selection parameter 
//--- is true by default making it possible to highlight and move the object 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); 
//--- hide (true) or display (false) graphical object name in the object list 
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); 
//--- set the priority for receiving the event of a mouse click in the chart 
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); 
//--- successful execution 
   return(true); 
  } 
//+------------------------------------------------------------------+
Ernst Van Der Merwe
6365
Ernst Van Der Merwe  
long chartID;
int window;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   chartID=ChartID();
   window=(int)ChartWindowFind();
//---

//---
   if(ObjectFind(chartID, IntegerToString(arrow) + (string)window + "Arrow SSD1 " + IntegerToString(i)) == - 1)
   {
      ArrowCreate(chartID, IntegerToString(arrow) + (string)window + "Arrow SSD1 " + IntegerToString(i), window , Time[i], arrUpSSK1_0[i], 241, ANCHOR_BOTTOM, clrGreen);
   }
//---   
To add comments, please log in or register