Page 1 of 1

Detect when price "cross" a line

Posted: Tue Jan 12, 2010 11:40 am
by leonyde
Hi All,


I was wondering if there would be any function in MQL4 to simply detect when price cross a line (a certain given price). When we trade manually it's visual and simple, price cross the line, we act.

But when coding the only solution I found is to use a set of conditions like that:

Bid > line - x AND price < Bid + x


(line being the price at which we want to trigger something)

Image

- Do any of you know a better way to do this? How do you do?
- If this is the best way to do it, how would you optimize the x?

Posted: Tue Jan 12, 2010 1:13 pm
by blubbb
Use a global or static variable e.g. "old" and store the last known price.

Code: Select all

static double old = 0;
if (old > 0) {
  if ((Close[0] > line) && (old <= line)) {
    // Whatever
  }
}
old = Close[0];


There are several examples in my indis. Have a look at the code.

Edit: This will still be triggered several times if price crosses your line. But you can (A) check the time as well (trigger only once per candle) or (B) only execute your code when no trade is open. So that only one trade can be open at any given time.

Posted: Tue Jan 12, 2010 2:19 pm
by leonyde
Smart solution! Thank you!

For the second issue, the script is only executed if there is no trade open, so no probs.

What is Close[0] ? Bid? I didn't know you can use that...

Posted: Wed Jan 13, 2010 1:58 pm
by newschool
You have many examples of different crosses in this EA I made (various indicators). I give you the link I posted in my TRO EA thread.

http://kreslik.com/forums/download.php? ... 972c955a67

Posted: Thu Jan 14, 2010 9:48 am
by leonyde
Thanks, I saved it to study later. I didn't check the codes in your thread yet (no time) but I will have a look when I will be more free. I think the semaphore one could be very promising, optimized properly... :)

Posted: Thu Jan 14, 2010 10:12 pm
by TheRumpledOne
The TRO_TRENDLINE_ALERT indicator will do that.

It is in one of the MT4 MOTHERLODES.