Detect when price "cross" a line

Moderator: moderators

User avatar
leonyde
rank: <50 posts
rank: <50 posts
Posts: 49
Joined: Wed Jun 24, 2009 1:11 pm
Reputation: 0
Location: Here
Gender: None specified

Detect when price "cross" a line

Postby leonyde » Tue Jan 12, 2010 11:40 am

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?

Please add www.kreslik.com to your ad blocker white list.
Thank you for your support.

User avatar
blubbb
rank: 150+ posts
rank: 150+ posts
Posts: 219
Joined: Mon Sep 29, 2008 8:07 pm
Reputation: 0
Location: Europe
Gender: Male

Postby blubbb » Tue Jan 12, 2010 1:13 pm

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.

User avatar
leonyde
rank: <50 posts
rank: <50 posts
Posts: 49
Joined: Wed Jun 24, 2009 1:11 pm
Reputation: 0
Location: Here
Gender: None specified

Postby leonyde » Tue Jan 12, 2010 2:19 pm

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...

User avatar
newschool
rank: 150+ posts
rank: 150+ posts
Posts: 489
Joined: Fri Aug 21, 2009 2:26 am
Reputation: 0
Gender: Male

Postby newschool » Wed Jan 13, 2010 1:58 pm

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

User avatar
leonyde
rank: <50 posts
rank: <50 posts
Posts: 49
Joined: Wed Jun 24, 2009 1:11 pm
Reputation: 0
Location: Here
Gender: None specified

Postby leonyde » Thu Jan 14, 2010 9:48 am

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... :)

Please add www.kreslik.com to your ad blocker white list.
Thank you for your support.

User avatar
TheRumpledOne
rank: 10000+ posts
rank: 10000+ posts
Posts: 11440
Joined: Sun May 14, 2006 9:31 pm
Reputation: 45
Location: Oregon
Real name: Avery T. Horton, Jr.
Gender: None specified
Contact:

Postby TheRumpledOne » Thu Jan 14, 2010 10:12 pm

The TRO_TRENDLINE_ALERT indicator will do that.

It is in one of the MT4 MOTHERLODES.
IT'S NOT WHAT YOU TRADE, IT'S HOW YOU TRADE IT!

Please do NOT PM me with trading or coding questions, post them in a thread.

Please add www.kreslik.com to your ad blocker white list.
Thank you for your support.


Return to “MetaTrader”