Forex
Google
New signals service!

Go Back   Forex Trading > Discussion Areas > Metatrader 4


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 08-31-2006, 01:05 AM
Junior Member
 
Join Date: Aug 2006
Posts: 21
knili is on a distinguished road
Programming question using Close[]

Hello all,
I’m trying the get the Close price of previous to current candle every min using Close[1] function. The first time through the loop I get the correct value and it will display the same value every time after
This is my sample code
flag =1;
int currentTime=0;
int initTime = 0;
while (flag==1)
{
currentTime = CurTime();
if (initTime + 60 <= currentTime)
{
Alert (Close[1]);
initTime = CurTime();
}

}

Now if the Close[1] value is 1.298 it will display it correctly the first time but each time after that it will display the same number and will not get updated with the correct value.

Is something wrong with my code? Or Close[] can’t be update more than once through the program?

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-31-2006, 01:23 AM
phoenix's Avatar
Senior Member
 
Join Date: May 2006
Posts: 287
phoenix is on a distinguished road
do you want to alert the close[1] everytime a new bar comes

then
//put this code outside the int start(){...}
bool NewBar()
{
static datetime dt = 0;
if (Time[0] != dt)
{
dt = Time[0];
return(true);
}

return(false);
}
//and this inside
if(NewBar())alert(........);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-31-2006, 02:17 AM
Junior Member
 
Join Date: Aug 2006
Posts: 21
knili is on a distinguished road
Thanks, I made some modification to the code. The problem I have is that the first time I get the correct close value of Close[1], However every min after that, I get the same value over and over again, even though the close price changes. How can I modify this code so I can get the new value of Close[1]?

So when you run this code, the first time you get a value 117.21 for USDJPY M1 (of course depends on the market price at the time you run the program). Which is the correct value. A min later the chart shows a new value something like 117.18. However the code below still display the same value as the first time which was 117.21

int start()
{
int flag =1;
while (flag ==1)
{
if(NewBar())Alert(Close[1]);
Sleep (60000);
}
return(0);
}
//+------------------------------------------------------------------+
bool NewBar()
{
static datetime dt = 0;
if (CurTime() != dt)
{
dt = CurTime();
return(true);
}

return(false);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-31-2006, 02:27 AM
phoenix's Avatar
Senior Member
 
Join Date: May 2006
Posts: 287
phoenix is on a distinguished road
i don't know what your "flag" and sleep(60000); are used for ,but try getting rid off them and let only NewBar() function to work you will get the new close[1] everytime new bar comes ... and try modifying your those two parameters again

hope it helps
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-31-2006, 01:59 PM
Junior Member
 
Join Date: Aug 2006
Posts: 21
knili is on a distinguished road
Sorry I’m very new to this MQL lang.
Are you saying the start() function automatically gets called every time there is a new bar?
I tried your code and only got one alert with the price of Close[1] and nothing after that based on M1 interval.

The problem I have with Close[1] is, it always give me the correct first value after I execute the program, but it doesn’t give me the new values as they come. It always gives me the value I got when I first executed the program over and over again.
The reason for flag = 1 is that I want to start a infinite loop (while flag ==1) and the sleep(60) sleep for 60 sec before look for a new value of Close[1].

One other thing that I do is I call the start from init() function
Init()
{
Start();
Return (0);

}

Is this wrong way of doing it? Does start() gets call by itself?

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-31-2006, 02:10 PM
phoenix's Avatar
Senior Member
 
Join Date: May 2006
Posts: 287
phoenix is on a distinguished road
yes close[1] will give you the close price of the last candle before the current one...there is no need to do any loop and any sleep()

and leave this the same way it be would be best buddy

Init Start()
{

Return (0);

}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-31-2006, 06:26 PM
Junior Member
 
Join Date: Mar 2006
Posts: 2
DoctorEvil is on a distinguished road
The reason you aren't getting the updated prices is because you are running a continuous loop and the platform will not update the arrayconstants until you break the loop and the start() is finished processing or you use the RefreshRates().

Here is an effective alternative:

PHP Code:
int bars//global variable
int start(){
   if(
Bars!=bars){
      
bars=Bars;
      
Alert(Symbol()," Close on last bar was ",Close[1]);
   }   
   return(
0);


Quote:
Originally Posted by knili
Thanks, I made some modification to the code. The problem I have is that the first time I get the correct close value of Close[1], However every min after that, I get the same value over and over again, even though the close price changes. How can I modify this code so I can get the new value of Close[1]?

So when you run this code, the first time you get a value 117.21 for USDJPY M1 (of course depends on the market price at the time you run the program). Which is the correct value. A min later the chart shows a new value something like 117.18. However the code below still display the same value as the first time which was 117.21

int start()
{
int flag =1;
while (flag ==1)
{
if(NewBar())Alert(Close[1]);
Sleep (60000);
}
return(0);
}
//+------------------------------------------------------------------+
bool NewBar()
{
static datetime dt = 0;
if (CurTime() != dt)
{
dt = CurTime();
return(true);
}

return(false);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-31-2006, 07:35 PM
Junior Member
 
Join Date: Aug 2006
Posts: 21
knili is on a distinguished road
Got it. Thank you all for your help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming Question... omelette General Discussion 6 12-12-2006 02:29 AM
Question about programming AMA logic into an EA zeroz Metatrader 4 mql 4 - Development course 4 12-09-2006 08:13 PM
Indicator Programming Question cubesteak Indicators - Metatrader 4 5 08-05-2006 09:25 AM
a question on mt4 programming huaxia009 Metatrader 4 3 05-31-2006 03:10 AM


All times are GMT. The time now is 06:54 AM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.