Forex Forum - EarnForex
Serving Traders Since 2005
 

Go Back   Forex Forum - EarnForex > MetaTrader > MetaTrader Indicators

MetaTrader Indicators Post and discuss the MetaTrader indicators here.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 19th December 2009, 06:26
Default Avatar
Member
 
Join Date: Dec 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default MA with two colors

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.
Reply With Quote
  #2 (permalink)  
Old 21st December 2009, 09:36
Enivid's Avatar
Administrator
 
Join Date: Nov 2008
Posts: 1,446
Thanks: 6
Thanked 8 Times in 7 Posts
Default

The main reason for not drawing in color is that you've set the type of the plot to DRAW_LINE instead of DRAW_COLOR_LINE. But there are also some other minor errors in the code. Please, have a look at the attached Color_MA that is based on your code, but has no errors.

By the way, now it's possible to attach .mq5 files to the posts, so no need to list the code inside the forum messages.
Attached Files
File Type: mq5 Color_MA.mq5 (2.6 KB, 27 views)
Reply With Quote
  #3 (permalink)  
Old 21st December 2009, 10:15
Default Avatar
Member
 
Join Date: Dec 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Enivid View Post
The main reason for not drawing in color is that you've set the type of the plot to DRAW_LINE instead of DRAW_COLOR_LINE. But there are also some other minor errors in the code. Please, have a look at the attached Color_MA that is based on your code, but has no errors.

By the way, now it's possible to attach .mq5 files to the posts, so no need to list the code inside the forum messages.

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.
Reply With Quote
  #4 (permalink)  
Old 23rd December 2009, 09:38
Enivid's Avatar
Administrator
 
Join Date: Nov 2008
Posts: 1,446
Thanks: 6
Thanked 8 Times in 7 Posts
Default

MATempBuffer[0] contains the value of MA for the oldest bar (if timeseries isn't set to true on the buffer, and in my Color_MA indicator it's not). You have to use MATempBuffer[rate_total-1] in this case.
Reply With Quote
  #5 (permalink)  
Old 23rd December 2009, 14:46
Default Avatar
Member
 
Join Date: Dec 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Enivid View Post
MATempBuffer[0] contains the value of MA for the oldest bar (if timeseries isn't set to true on the buffer, and in my Color_MA indicator it's not). You have to use MATempBuffer[rate_total-1] in this case.
Thank you.
Reply With Quote
  #6 (permalink)  
Old 1st July 2011, 17:34
Default Avatar
xx2 xx2 is offline
Junior Member
 
Join Date: Jul 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

if you do not want only 200 bar length but the full windows, then use this little modification
Reply With Quote
  #7 (permalink)  
Old 4th July 2011, 08:45
Enivid's Avatar
Administrator
 
Join Date: Nov 2008
Posts: 1,446
Thanks: 6
Thanked 8 Times in 7 Posts
Default

Which modification?
__________________
Please, read the Forum Rules and the Signature Rules to avoid termination of your account.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT. The time now is 22:32.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Inactive Reminders By Icora Web Design

SEO by vBSEO 3.3.2