//+------------------------------------------------------------------+ //| MyMA_ind_with_ChartEvent.mq5 | //| http://www.mql5.com | //+------------------------------------------------------------------+ // Естественный обработчик - внутри индикатора #property link "http://www.mql5.com" #property indicator_chart_window //--- indicator settings #property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 1 #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 clrBlue,clrRed #property indicator_width1 3 //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum Indicator_ { open=0, napravl=1 }; input Indicator_ ind_type=napravl; // индекс #define KEY_UP 38 #define KEY_DOWN 40 #define KEY_NUMLOCK_DOWN 98 #define KEY_NUMLOCK_UP 104 #define KEY_TAB 9 //--- input parameters /// Mthmt: теперь внешних нет, они просто глобальные и соответствуют ГПТ int MA_Period = 13; int MA_Shift = 0; ENUM_MA_METHOD MA_Method = 1; int Updated=0; /// показывает, обновился ли индикатор после изменения параметров //--- indicator buffers double LineBuffer[]; double LineColors[]; double tmp[1]; double _price[]; /// Mthmt: массив ценовых данных, который для принудительного обновления //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ /// индикатора нужно обновлять "вручную" //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,LineBuffer,INDICATOR_DATA); SetIndexBuffer(1,LineColors,INDICATOR_COLOR_INDEX); //--- set accuracy IndicatorSetInteger(INDICATOR_DIGITS,Digits()+1); //--- sets first bar from what index will be drawn PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,MA_Period); //---- line shifts when drawing PlotIndexSetInteger(0,PLOT_SHIFT,MA_Shift); //--- name for DataWindow if(MA_Method==MODE_EMA) IndicatorSetString(INDICATOR_SHORTNAME, "EMA("+string(MA_Period)+")"); if(MA_Method==MODE_LWMA) IndicatorSetString(INDICATOR_SHORTNAME, "LWMA("+string(MA_Period)+")"); if(MA_Method==MODE_SMA) IndicatorSetString(INDICATOR_SHORTNAME, "SMA("+string(MA_Period)+")"); if(MA_Method==MODE_SMMA) IndicatorSetString(INDICATOR_SHORTNAME, "SMMA("+string(MA_Period)+")"); //---- sets drawing line empty value-- PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); if(!GlobalVariableCheck("MA_Period")) GlobalVariableSet("MA_Period",MA_Period); else MA_Period=(int)GlobalVariableGet("MA_Period"); if(!GlobalVariableCheck("MA_Shift")) GlobalVariableSet("MA_Shift",MA_Shift); else MA_Shift=(int)GlobalVariableGet("MA_Shift"); if(!GlobalVariableCheck("MA_Method")) GlobalVariableSet("MA_Method",MA_Method); else MA_Method=(ENUM_MA_METHOD)GlobalVariableGet("MA_Method"); GlobalVariableSet("Updated",0); ArraySetAsSeries(LineColors,true); ArraySetAsSeries(LineBuffer,true); //---- initialization done } //+------------------------------------------------------------------+ //| Moving Average | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, /// Mathemat: пересчет полный! const int begin, /// Mathemat: пересчет полный! const double &price[]) { if(ArraySize(LineBuffer)<=0){Print("ArraySize(LineBuffer)<=0");return(0);} ArrayCopy(_price,price,0,0,rates_total); //--- check for bars count if(rates_total=0;i--)// non-first calculation if(ind_type==1) { if(LineBuffer[i+1/*>=ArraySize(LineBuffer)?ArraySize(LineBuffer)-1:i+1*/]>LineBuffer[i]) LineColors[i]=1; else LineColors[i]=0; } else if(ind_type==0) { if(LineBuffer[i+1]>LineBuffer[i]) LineColors[i]=1; else LineColors[i]=0; } //--- return value of prev_calculated for next call return(rates_total); } //--- //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ void changeTerminalGlobalVar(string name,int dir=0) { int var=(int)GlobalVariableGet(name); int newparam=var+dir; if(name=="MA_Period") { if(newparam>0) /// возможный период валиден для мувинга { GlobalVariableSet(name,newparam); MA_Period=newparam; /// не забываем сменить глобальную переменную } else /// период не меняем, т.к. период МА равен 1 минимум { GlobalVariableSet(name,1); MA_Period=1; /// не забываем сменить глобальную переменную } } if(name=="MA_Method") /// тут dir при вызове всегда равен 1, значение dir не важно { newparam=(var+1)%4; GlobalVariableSet(name,newparam); MA_Method=(ENUM_MA_METHOD)newparam; } if(name=="MA_Shift") { GlobalVariableSet(name,newparam); MA_Shift=newparam; } Comment("MA_Period = "+(string)MA_Period+"\n"+ "MA_Method = "+(string)MA_Method+"\n"+ "MA_Shift = "+(string)MA_Shift+"\n" ); return; }//+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { if(id==CHARTEVENT_KEYDOWN) switch((int)lparam) { case(KEY_TAB): changeTerminalGlobalVar("MA_Method",1); GlobalVariableSet("Updated",0); Updated=0; break; case(KEY_UP): changeTerminalGlobalVar("MA_Period",1); GlobalVariableSet("Updated",0); Updated=0; break; case(KEY_DOWN): changeTerminalGlobalVar("MA_Period",-1); GlobalVariableSet("Updated",0); Updated=0; break; case(KEY_NUMLOCK_UP): changeTerminalGlobalVar("MA_Shift",1); GlobalVariableSet("Updated",0); Updated=0; break; case(KEY_NUMLOCK_DOWN): changeTerminalGlobalVar("MA_Shift",-1); GlobalVariableSet("Updated",0); Updated=0; break; } OnCalculate_Void(); ChartRedraw(); return; }//+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int OnCalculate_Void() { const int rates_total=Bars(_Symbol,PERIOD_CURRENT); //CopyOpen( _Symbol, PERIOD_CURRENT, 0, rates_total, _price ); OnCalculate(rates_total,0,0,_price); //OnInit(); return(1); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Simple moving average on price array | //+------------------------------------------------------------------+ int SimpleMAOnBuffer(const int rates_total,const int prev_calculated,const int begin, const int period,const double &price[],double &buffer[]) { int i,limit; //--- check for data if(period<=1 || rates_total-begin