Programming question using Close[]

 
 

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(........);

 

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);

}

 

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

 
 

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);

}

 

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:

int bars; //global variable

int start(){

if(Bars!=bars){

bars=Bars;

Alert(Symbol()," Close on last bar was ",Close[1]);

}

return(0);

}
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);

}
 

Got it. Thank you all for your help.