mql4 make a function that constantly run

 

Hello, I'm trying to make my EA check every second if the price reached a certain price and if it does change it's T.P and S.L.
The problem is I managed to make the EA do that every new candle and it doesn't work properly this way.
I've tried to make an infinite while that exit when a new candle data arrive and it didn't work.
Can anyone help me?

        static datetime dtBarCurrent=WRONG_VALUE;
        datetime dtBarPrevious=dtBarCurrent;
        dtBarCurrent=(datetime) SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
        bool NewBarFlag=(dtBarCurrent!=dtBarPrevious);
        while(buyOrdersTotal() != 0){
        if(Bid >= (newSL+15*pips)){
            newTP = newSL+55*pips;
            for (int i=OrdersTotal()-1; i>=0;i--){
                OrderSelect (i, SELECT_BY_POS, MODE_TRADES);
                if(OrderType() == OP_BUY && OrderMagicNumber() == EAMagicNumber){
                    bool res =OrderModify(OrderTicket(),OrderOpenPrice(),newSL,newTP,0,Red);
                }
            }
            newSL = newSL+20*pips;
        }
        if(NewBarFlag){
            break;
        }
     }


Thanks in advance

 

You should read the documentation for Bid

Bid

The latest known buyer's price (offer price, bid price) of the current symbol. The RefreshRates() function must be used to update.



It is a bad idea to use "endless" loops.

Bid will only change when there is a new tick, so do your checks on a new tick, not in a loop.


Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford:

You should read the documentation for Bid

It is a bad idea to use "endless" loops.

Bid will only change when there is a new tick, so do your checks on a new tick, not in a loop.


Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

Thank you very much for the quick reply and for moving my question to the right place.
That makes alot of sense that the bid only change when there is a new tick, will RefreshRates fix it? Or is there any other idea to get it done other than endless loop.
 
nivcohen11: I'm trying to make my EA check every second if the price reached a certain price

No need for that; nothing is changing. Wait for a tick to arrive and test.

 
William Roeder:

No need for that; nothing is changing. Wait for a tick to arrive and test.

If the code is placed inside the OnTick() function, the modification happens every new candle, and I want to modify the price during the candle move because sometimes the conditions wont apply when a new candle gets opened and they do get triggered during the candle move. So I'm trying to make it ask the conditions every second or something like that in order to make it work.
If you have any idea how to get it done it would be much appriciated.

Thank you very much for your comment and time.

 
nivcohen11: If the code is placed inside the OnTick() function, the modification happens every new candle, and I want to modify the price during the candle move because sometimes the conditions wont apply when a new candle gets opened and they do get triggered during the candle move. So I'm trying to make it ask the conditions every second or something like that in order to make it work.

Please note that the function is called "OnTick". It is not called "OnBar" or "OnCandle". It is called on every tick.

If your code is only reacting on a new candle, then that is because you are using a test for a new bar/candle and only processing it when that happens. You most probably are using part of another persons code, from which you build on with your own code.

EDT: I just noticed that you did post your code and that you are using my own code for the Bar detection.

static datetime dtBarCurrent=WRONG_VALUE;
       datetime dtBarPrevious=dtBarCurrent;
       dtBarCurrent=(datetime) SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
       bool NewBarFlag=(dtBarCurrent!=dtBarPrevious);

So, identify and study the code that is filtering for only the opening of the bar, and place your new code instead in place where it will react to every call of OnTick instead.

If this is confusing for you, then at least show part of your code, especially in regard to the OnTick event handler, so that we can offer better guidance.

EDIT: From your code posting, it seems that you are returning from the OnTick event when you detect a new bar/candle, so that any code after that will not be executed.

if(NewBarFlag){ break; }
You don't seem to be using my code properly in your code, and since you did not show more of your code, I don't really understand what you are trying to achieve. If none of your code depends on a new bar detection, then don't use my filter! Use it when you only need to run code on the opening of a new bar.
 
Fernando Carreiro:

Please note that the function is called "OnTick". It is not called "OnBar" or "OnCandle". It is called on every tick.

If your code is only reacting on a new candle, then that is because you are using a test for a new bar/candle and only processing it when that happens. You most probably are using part of another persons code, from which you build on with your own code.

EDT: I just noticed that you did post your code and that you are using my own code for the Bar detection.

So, identify and study the code that is filtering for only the opening of the bar, and place your new code instead in place where it will react to every call of OnTick instead.

If this is confusing for you, then at least show part of your code, especially in regard to the OnTick event handler, so that we can offer better guidance.

EDIT: From your code posting, it seems that you are returning from the OnTick event when you detect a new bar/candle, so that any code after that will not be executed.

You don't seem to be using my code properly in your code, and since you did not show more of your code, I don't really understand what you are trying to achieve. If none of your code depends on a new bar detection, then don't use my filter! Use it when you only need to run code on the opening of a new bar.
Because I'm using an endless while loop I want it to exit and start the OnTick() functions when a new bar recieved and that is why I'm using the new bar detection to break the while and recheck the OnTick functions.
By the way, thank you very much for the great code which fits perfectly with my code.
If you know how can I fix my while loop the achieve what I'm looking for that would be much appriciated ( I want to exit the while loop when all of the orders got closed - means 0 buy orders or when a new bar is triggered).
Thanks in advance
 
nivcohen11: Because I'm using an endless while loop I want it to exit and start the OnTick() functions when a new bar recieved and that is why I'm using the new bar detection to break the while and recheck the OnTick functions. By the way, thank you very much for the great code which fits perfectly with my code. If you know how can I fix my while loop the achieve what I'm looking for that would be much appriciated ( I want to exit the while loop when all of the orders got closed - means 0 buy orders or when a new bar is triggered).

You should not be using an endless loop! You should return and exit the OnTick event handler as quickly as possible after processing the data.

I repeat, don't use an endless loop. If you want to react to new ticks, then exit the event handler as soon as possible.

Build your code so that it reacts to the event each time a new tick arrives and OnTick is called. Don't hang on to it via endless loop or else you will be blocking other subsequent events.

 
nivcohen11:

Hello, I'm trying to make my EA check every second if the price reached a certain price and if it does change it's T.P and S.L.
The problem is I managed to make the EA do that every new candle and it doesn't work properly this way.
I've tried to make an infinite while that exit when a new candle data arrive and it didn't work.
Can anyone help me?


Thanks in advance

if u want to check ur conditions on everytick you should remove your first steps for chechking new candle status

if u wanna check at a special time u can use onTimer function

here is ur code with 1second timer

Files:
just_buy.mq4  2 kb
 
Mohsen Bjp:

if u want to check ur conditions on everytick you should remove your first steps for chechking new candle status

if u wanna check at a special time u can use onTimer function

here is ur code with 1second timer

Thank you very much for your time writing the code.
I've tried to use this code and it seems like the "if(Default==true)" never gets triggered because the T.P and S.L never changes.
If you have any idea what could cause that problem that would be much appriciated.
 
nivcohen11:
Thank you very much for your time writing the code.
I've tried to use this code and it seems like the "if(Default==true)" never gets triggered because the T.P and S.L never changes.
If you have any idea what could cause that problem that would be much appriciated.

here i set Defualt variable

if u run my expert on ur chart

u will see showing "true" and "false" and switch between every 5 second

u can attach ur code instead of my #comment part

and ur sl & tp will Check every 5 second instead

Files:
just_buy.mq4  1 kb
Reason: