Script to buy or sell at the opening of a new candel

 

Hi ; i couldn t find any script that can open an order straight when the next candel open or when the current candel close ..

does it exist ? i mean, i put this script during the current candel if i want to open an order direct when it close .. the stop loss will be juste under( over ) this current candel ..

in advance thank you .

 
Try studying the Time[0] and iTime() functions. They'll solve your problem. Just FYI, scripts are not suitable (unless using infinite loops) for these types of solutions.
 

Thank you for your answer ; but i did not understand why exactly scripts are not suitable for this ..

i only want the script run once .. that s all .. i put the script less than 10seconds before the cloture if i see i will have the desire signal and it open the order straight when the candel open with the desire stop loss ..

if a do that manually,it takes too much time to enter the stop loss value in a 1Mn chart ( for example if the trend is strong ) ... And an Expert Advisor won t be the solution too ( i decide to enter or not .. and if i want to open an other order a few minutes later ; i just have to put the script one more time on the chart )..

I understand that i have to use Time[0] and iTime() functions .. thank you ; but i m not really good in MQL4 and i thought it was something many person used already .. that s why i asked for .

 

but i did not understand why exactly scripts are not suitable for this

Because time of the bar opening is represented in Seconds. So you'll have to be there the second the new bar forms because Scripts do-not wait on the charts they execute and exit within seconds (Unless you force them to stay with contentious loops).

i put the script less than 10seconds before the cloture

Use an EA instead, same things you can put into Scripts, you can put into EA. However only put the EA on the chart 10-Seconds before the bar closes (if you decided to take the trade). Something like is TimeCurrent()==Time[0] ... then Buy should do the trick.

 

Thank you very much giving me your time..

I understand now why i can t use script for this and i will try with TimeCurrent ..

 
The Script will have to wait until the current candle closes
int start(){
    datetime startTime = Time[0];
    while( !(IsTesting() || IsStopped()) ){
        sleep(1000);    // One second
        RefreshRates(); // Update Time[0]
        if (startTime != Time[0]) break;
    }
    :
 

That s Great !! I ll try it .

Thank you both of you

Reason: