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)
- 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?
Detect when price "cross" a line
Moderator: moderators
Please add www.kreslik.com to your ad blocker white list.
Thank you for your support.
Thank you for your support.
- blubbb
- rank: 150+ posts
- Posts: 219
- Joined: Mon Sep 29, 2008 8:07 pm
- Reputation: 0
- Location: Europe
- Gender:
Use a global or static variable e.g. "old" and store the last known price.
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.
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.
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
http://kreslik.com/forums/download.php? ... 972c955a67
Please add www.kreslik.com to your ad blocker white list.
Thank you for your support.
Thank you for your support.
- TheRumpledOne
- rank: 10000+ posts
- Posts: 15560
- Joined: Sun May 14, 2006 9:31 pm
- Reputation: 3035
- Location: Oregon
- Real name: Avery T. Horton, Jr.
- Gender:
- Contact:
Please add www.kreslik.com to your ad blocker white list.
Thank you for your support.
Thank you for your support.