Forex
Google

Go Back   Forex Trading > Metatrader Training > Metatrader 4 mql 4 - Development course > Questions
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 12-02-2005, 02:13 PM
murky waters murky waters is offline
Junior Member
 
Join Date: Dec 2005
Posts: 2
murky waters is on a distinguished road
quick question

question: how do you write an "if" function that triggers code at the END of each bar on the chart (which works independent of chart timeframe)?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-02-2005, 04:13 PM
codersguru's Avatar
codersguru codersguru is offline
Senior Member
 
Join Date: Oct 2005
Posts: 987
codersguru has a spectacular aura aboutcodersguru has a spectacular aura aboutcodersguru has a spectacular aura about
Exclamation Welcome!

Quote:
Originally Posted by murky waters
question: how do you write an "if" function that triggers code at the END of each bar on the chart (which works independent of chart timeframe)?
murky waters,
Welcome to our forum!

Test this code:

PHP Code:
bool NewBar() //this function will return true if there's a new bar.
   
{
      
      static 
bool first_call true;
      static 
int start_bar 0;
      if(
first_call)
      {
         
start_bar=Bars;
         
first_call=false;
      }
      if(
Bars == (start_bar+1))
      {
         
first_call=true;
         return (
true);
      }
      else
      {
         return (
false);
      }
   } 

int start() //<-- in your start function you may use code like this
{
    if (
NewBar)
    {
       
//do something
    
}
    return (
0);

__________________
Hope it helps !
Coders' Guru
Senior MQL programmer:
www.xpworx.com/custom.htm
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-02-2005, 04:22 PM
amarnath's Avatar
amarnath amarnath is offline
Senior Member
 
Join Date: Oct 2005
Location: salem
Posts: 135
amarnath is on a distinguished road
Thumbs up Really great job

Hi coderguru,

Your are really doing a gr8 job here . Keep up good works
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-05-2005, 12:59 PM
murky waters murky waters is offline
Junior Member
 
Join Date: Dec 2005
Posts: 2
murky waters is on a distinguished road
codersguru,

Thank you for the quick reply. However when testing your code in the meta editor, I recieve the error NewBar: variable not defined. For the code:

PHP Code:
//+------------------------------------------------------------------+
//|                                                    test_bars.mq4 |
//|                                                      ©2005,  S T |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "©2005, S T"
#property link      ""

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

   return(
0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {

   return(
0);
  }

bool NewBar() //this function will return true if there's a new bar. 
   

       
      static 
bool first_call true
      static 
int start_bar 0
      if(
first_call
      { 
         
start_bar=Bars
         
first_call=false
      } 
      if(
Bars == (start_bar+1)) 
      { 
         
first_call=true
         return (
true); 
      } 
      else 
      { 
         return (
false); 
      } 
   } 

int start() //<-- in your start function you may use code like this 

    if (
NewBar
    { 
       
//do something 
    

    return (
0); 

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-05-2005, 05:09 PM
codersguru's Avatar
codersguru codersguru is offline
Senior Member
 
Join Date: Oct 2005
Posts: 987
codersguru has a spectacular aura aboutcodersguru has a spectacular aura aboutcodersguru has a spectacular aura about
Unhappy Missed ()

murky ,

I'm sorry, missed () in the line:
if (NewBar())

Try this code and tell me what do you think:

PHP Code:
//+------------------------------------------------------------------+
//|                                                    test_bars.mq4 |
//|                                                      ©2005,  S T |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "©2005, S T"
#property link      ""

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

   return(
0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {

   return(
0);
  }

bool NewBar() //this function will return true if there's a new bar. 
   

       
      static 
bool first_call true
      static 
int start_bar 0
      if(
first_call
      { 
         
start_bar=Bars
         
first_call=false
      } 
      if(
Bars == (start_bar+1)) 
      { 
         
first_call=true
         return (
true); 
      } 
      else 
      { 
         return (
false); 
      } 
   } 

int start() //<-- in your start function you may use code like this 

    if (
NewBar()) 
    { 
       
//do something 
    

    return (
0); 

__________________
Hope it helps !
Coders' Guru
Senior MQL programmer:
www.xpworx.com/custom.htm
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-02-2006, 03:40 AM
cardio cardio is offline
Senior Member
 
Join Date: Sep 2005
Location: St Louis, MO, USA
Posts: 176
cardio is an unknown quantity at this point
Great Code

Thanks Codersguru
You help me greatly
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-30-2006, 01:08 PM
intelligent_14's Avatar
intelligent_14 intelligent_14 is offline
Junior Member
 
Join Date: Mar 2006
Location: Tehran/Iran
Posts: 24
intelligent_14 is on a distinguished road
Another Way

I prefer to use this solution for finding new Bars

HTML Code:
bool NewBar()
  {
  static datetime LastTime;
  if (LastTime != Time[0])
    {
    LastTime = Time[0];
    return(true);
    }
  return(false);
  }
it's seems to be shorter
__________________
there wil be nothig for a person exept his efforts (Emam Ali Alayhesalam)
M.A.Gh
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-30-2006, 02:02 PM
codersguru's Avatar
codersguru codersguru is offline
Senior Member
 
Join Date: Oct 2005
Posts: 987
codersguru has a spectacular aura aboutcodersguru has a spectacular aura aboutcodersguru has a spectacular aura about
Arrow

Quote:
Originally Posted by intelligent_14
I prefer to use this solution for finding new Bars

HTML Code:
bool NewBar()
  {
  static datetime LastTime;
  if (LastTime != Time[0])
    {
    LastTime = Time[0];
    return(true);
    }
  return(false);
  }
it's seems to be shorter
Yes this is good one!
__________________
Hope it helps !
Coders' Guru
Senior MQL programmer:
www.xpworx.com/custom.htm
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-30-2006, 06:27 PM
intelligent_14's Avatar
intelligent_14 intelligent_14 is offline
Junior Member
 
Join Date: Mar 2006
Location: Tehran/Iran
Posts: 24
intelligent_14 is on a distinguished road
Quote:
Originally Posted by codersguru
Yes this is good one!
thank u codersguru, i had read all of your MQL4 tutorial, its very good, i finished it 2 weeks ago

thanks a lot
__________________
there wil be nothig for a person exept his efforts (Emam Ali Alayhesalam)
M.A.Gh
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
question evgeni1980 Questions 4 11-27-2006 01:32 PM
Two quick questions regards Profit Factor and .ex4 files JJLL Metatrader 4 3 10-01-2006 07:54 PM
really quick DUMB question Eaglehawk Questions 1 07-11-2006 12:52 AM
quick dummy question increase Questions 4 06-13-2006 08:23 AM
Just a quick question... bluefish Metatrader 4 1 05-30-2006 05:26 PM


All times are GMT. The time now is 08:24 AM.