Serving Traders Since 2005
|
|
|
|||||||
| MetaTrader Indicators Post and discuss the MetaTrader indicators here. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
||||
|
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. //+------------------------------------------------------------------+ //| 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_Mod e,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,MABuff er); //--- 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. //+------------------------------------------------------------------+ //| 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_IN DEX); IndicatorSetString(INDICATOR_SHORTNAME,"MA["+MA_Period+"]"); //--- ma_handle=iMA(Symbol(),0,MA_Period,MA_Shift,MA_Mod e,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,MABuff er); //--- 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 fxwalb; 19th December 2009 at 13:49. |
|
||||
|
Quote:
Yes this code is working properly, thanks. I tried to plot the MA value on the chart with ObjectSetString (0, "myMA_now_1", OBJPROP_TEXT,"MA ["+IntegerToString(MA_Period)+"] @ "+DoubleToString(MATempBuffer[0],4)); Unfortunatly the value is way off the actual reading. What can I do? Last edited by fxwalb; 21st December 2009 at 16:11. |
|
||||
|
Which modification?
__________________
Please, read the Forum Rules and the Signature Rules to avoid termination of your account. |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Indicator - MACD with colors and Sound ALERT | ContraTrader | MetaTrader Indicators | 0 | 22nd June 2009 15:50 |