Forex
Google

Go Back   Forex Trading > Programming > Metatrader Programming
Forex Forum Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-10-2007, 07:59 PM
ontherun ontherun is offline
Junior Member
 
Join Date: Dec 2005
Posts: 13
ontherun is on a distinguished road
Buff MA - help to code(translate) it

Hi,

as I'm not well-versed in MT4 coding I need help with a simple formula called Buff MA; I've the corresponding formula in Metastock language as the following one:


BUFF:=Sum(V*C, 32) / (Cum(V) - Ref( Cum(V), -32));
BUFF

If someone could compile it for MT4 I'd be glad.

Buff MA helps in determining the most probable side to trade... when price is above Buff and aligned with it* only long positions are allowed... viceversa for short ones.

*(that's if Buff and price both are upward it's OK to be long... viceversa for shorting)

Thanks a lot

ontherun

ps: I've been looking for the Sum() and Cum() corresponding functions in the MetaEditor but I've not found anything related to those functions...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-10-2007, 09:06 PM
ralph.ronnquist's Avatar
ralph.ronnquist ralph.ronnquist is offline
Senior Member
 
Join Date: Oct 2006
Posts: 280
ralph.ronnquist is on a distinguished road
Someone familiar with Metastock may correct me, but by the looks of it, you have a "BUFF" function that looks at the 32 most recent bars, adds up the volume times the close price for them, and divides that by the total volume for those bars. It could look like the following in MT4:
PHP Code:
double buff()
{
    
double sum 0;
    
double v 0;
    for ( 
int i 032i++ ) {
        
sum += Volume] * Close];
        
+= Volume];
    }
    return( 
sum );

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-11-2007, 08:32 AM
ontherun ontherun is offline
Junior Member
 
Join Date: Dec 2005
Posts: 13
ontherun is on a distinguished road
Ralph,

thanks for taking time to answer me.

Your understanding of the formula I've posted is right and actually it could be written simpler, that's: BUFF:= Sum(V*C, 32) / Sum(V,32);

Anyway I've created a new indicator with your code but even if it compiled without errors it didn't plot anything.

below is the whole script I've written for the Buff MA:

PHP Code:
//+------------------------------------------------------------------+
//|                                                      Buff MA.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 1

#property indicator_color1 Blue

double BUFF[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
string short_name;
  
  
IndicatorBuffers(1);
  
//---- indicator lines
  
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,Blue);
  
SetIndexBuffer(0,BUFF);
  
SetIndexLabel(0,"Buff MA");
  
//---- name for DataWindow and indicator subwindow label
   
short_name="Buff MA";
   
IndicatorShortName(short_name);
   
//----
   
SetIndexDrawBegin(0,100);     
  
//----
   
return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   
return(0);
  }
//+------------------------------------------------------------------+
//| Buff MA                                                          |
//+------------------------------------------------------------------+
int start()

  { 
    
double sum=0
    
double v=0
    for (
int i=0i<32i++) { 
        
sum+=Volume[i]*Close[i]; 
        
v+=Volume[i];      
    } 
    return(
sum/v); 
}  

//+------------------------------------------------------------------+ 

Last edited by ontherun : 09-11-2007 at 08:34 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-11-2007, 09:35 AM
ralph.ronnquist's Avatar
ralph.ronnquist ralph.ronnquist is offline
Senior Member
 
Join Date: Oct 2006
Posts: 280
ralph.ronnquist is on a distinguished road
no, right; it will need to compute the value relative to prior bars, and assign BUFF with the values.... like
PHP Code:
int start()
{
    for ( 
int bar Bars IndicatorCounted(); bar >= 0bar-- ) {
        
indicatebar );
    }
}

void indicate(int bar)
{
    
double sum=0
    
double v=0
    for ( 
int i=0i<32i++ ) {
        
sum += Volumebar ] * Closebar ]; 
        
+= Volumebar ];
    }
    
BUFFbar ] = sum/v;

Just replace your start() function with the above two functions, and it should do the trick.

Last edited by ralph.ronnquist : 09-11-2007 at 09:53 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-12-2007, 07:12 PM
ontherun ontherun is offline
Junior Member
 
Join Date: Dec 2005
Posts: 13
ontherun is on a distinguished road
Ralph,

as I ve not yet got it working, can you attach the formula as .mq4 file

Thanks

ontherun
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-12-2007, 08:04 PM
Michel Michel is offline
Senior Member
 
Join Date: Feb 2006
Posts: 502
Michel is on a distinguished road
You can try :
PHP Code:
int start()
{
    for ( 
int bar Bars-32-IndicatorCounted(); bar >= 0bar-- ) {
        
indicatebar );
    }

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-12-2007, 09:24 PM
ontherun ontherun is offline
Junior Member
 
Join Date: Dec 2005
Posts: 13
ontherun is on a distinguished road
Michel,

thanks for answering.

The Buff formula with your modification plots correct as for the calculated values (I compare them to those plotted by the Buff in Metastock) but it doesn't update... that's it stops plotting just at the bar developing the moment I attach it to the chart.

ontherun
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 09-12-2007, 11:59 PM
Michel Michel is offline
Senior Member
 
Join Date: Feb 2006
Posts: 502
Michel is on a distinguished road
You are right, so please try this code which should be the right way to code this kind of indic (I am not able to check it myself now):
PHP Code:
int start()
  {
   
int i,counted_bars=IndicatorCounted();
   if(
Bars<=32) return(0);
   if(
counted_bars<1) for(i=1;i<=32;i++) BUFF[Bars-i]=EMPTY_VALUE;
   
i=Bars-33;
   if(
counted_bars>=32i=Bars-counted_bars-1;
   while(
i>=0)
     {
      
indicate(i);
      
i--;
     }
   return(
0);
  } 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 09-14-2007, 04:23 PM
ontherun ontherun is offline
Junior Member
 
Join Date: Dec 2005
Posts: 13
ontherun is on a distinguished road
Great work!
Thanks, Michel.

Actually I didn't think coding this was so demanding comparing to Metastock language.

Thanks again and thanks to Ralph too.

Have a great weekend.

ontherun
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
translate easyLanguage in MT3 or MT4 gbolla Metatrader 3 30 07-03-2008 08:21 PM
What does this code mean? matrixebiz Expert Advisors - Metatrader 4 3 09-01-2007 10:38 AM
How to code this? iscuba11 Metatrader 4 mql 4 - Development course 1 08-03-2007 04:22 PM
Please translate my idea Indonesian to english version harryhid Expert Advisors - Metatrader 4 4 10-18-2006 09:59 AM


All times are GMT. The time now is 03:16 AM.