Hi to all!
Is there anybody around who can help me with my code for MT5 platform. I tried to code a MA with two colors for up and down direction.
But there is still only the updirection on my chart. Must be a bug somewhere.
Here is my code so far.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
Another version of the indi is this, which draws a line without changing colors.
Is there anybody around who can help me with my code for MT5 platform. I tried to code a MA with two colors for up and down direction.
But there is still only the updirection on my chart. Must be a bug somewhere.
Here is my code so far.
MQL5:
//+------------------------------------------------------------------+ //| TestCopyBuffer1.mq5 | //| Copyright 2009, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2009, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 1 //---- plot MA #property indicator_label1 "MA" #property indicator_type1 DRAW_LINE #property indicator_color1 Lime, Red #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- input parameters input int MA_Period=15; input ENUM_MA_METHOD MA_Mode=MODE_EMA; input ENUM_APPLIED_PRICE MA_Price=PRICE_CLOSE; input int MA_Shift=0; //--- indicator buffers double MABuffer[]; double dUpMABuffer[]; double dDownMABuffer[]; int ma_handle; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,dUpMABuffer,INDICATOR_DATA); SetIndexBuffer(1,dDownMABuffer,INDICATOR_DATA); IndicatorSetString(INDICATOR_SHORTNAME,"MA["+MA_Period+"]"); //--- ma_handle=iMA(Symbol(),0,MA_Period,MA_Shift,MA_Mode,MA_Price); return(0); } //+------------------------------------------------------------------+ //| 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[]) { //--- Copy the values of the moving average in the buffer MABuffer int copied=CopyBuffer(ma_handle,0,0,rates_total,MABuffer); //--- for (int i = rates_total; i > 1; i--) { dUpMABuffer[rates_total - i + 1] = 0; dDownMABuffer[rates_total - i + 1] = 0; double myMA_now = MABuffer[rates_total - i+1]; double myMA_previous = MABuffer[rates_total - i]; //MA one bar ago if (myMA_now >= myMA_previous) { dUpMABuffer[rates_total - i + 1] = myMA_now; if(dUpMABuffer[rates_total - i ]==0.0) dUpMABuffer[rates_total - i ]=myMA_previous; } if (myMA_now < myMA_previous) { dDownMABuffer[rates_total - i + 1] = myMA_now; if(dDownMABuffer[rates_total - i]==0.0) dDownMABuffer[rates_total - i ]=myMA_previous; } } //---- //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
Another version of the indi is this, which draws a line without changing colors.
MQL5:
//+------------------------------------------------------------------+ //| TestCopyBuffer1.mq5 | //| Copyright 2009, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2009, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 3 #property indicator_plots 1 //---- plot MA #property indicator_label1 "MA" #property indicator_type1 DRAW_LINE #property indicator_color1 Lime, Red #property indicator_style1 STYLE_SOLID #property indicator_width1 2 //--- input parameters input int MA_Period=15; input ENUM_MA_METHOD MA_Mode=MODE_EMA; input ENUM_APPLIED_PRICE MA_Price=PRICE_CLOSE; input int MA_Shift=0; //--- indicator buffers double MABuffer[]; double ExtColorBuffer[]; int ma_handle; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,MABuffer,INDICATOR_DATA); SetIndexBuffer(1,ExtColorBuffer,INDICATOR_COLOR_INDEX); IndicatorSetString(INDICATOR_SHORTNAME,"MA["+MA_Period+"]"); //--- ma_handle=iMA(Symbol(),0,MA_Period,MA_Shift,MA_Mode,MA_Price); return(0); } //+------------------------------------------------------------------+ //| 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[]) { //--- Copy the values of the moving average in the buffer MABuffer int copied=CopyBuffer(ma_handle,0,0,rates_total,MABuffer); //--- for (int i = rates_total; i > 1; i--) { if (MABuffer[rates_total-i] >= MABuffer[rates_total-i+1]) ExtColorBuffer[rates_total-i+1]=0.0; // set color Lime else ExtColorBuffer[rates_total-i+1]=1.0; // set color Red } //---- //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
Last edited by a moderator: