• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 12:41pm
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 12:41pm
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Broker Challenge "NDD/STP" vs. True "ECN" and "Mkt Mkr" broker talk 46 replies

Dealing with "Invalid Price", "Requote", "Server Busy" in MT4 3 replies

Please code "Parabol trendline" or "Curve trendline" for me 0 replies

Looking for Super ADX/Power ADX 1 reply

looking for a "modified" ADX 0 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
Tags: Code for "if ADX (-1) < ADX (-2)"
Cancel

Code for "if ADX (-1) < ADX (-2)"

  • Post #1
  • Quote
  • First Post: Jan 9, 2014 7:14am Jan 9, 2014 7:14am
  •  mntemel
  • | Joined Aug 2011 | Status: Member | 48 Posts
Hi All,

Need some help with coding. I have an assistant EA that I use in my trading.

At the moment the signal is like below;

Inserted Code
 if(use_macd && use_adx)
  {
     if(adx>adx_level &&  macd>overbought) return(sell);
     if(adx>adx_level &&  macd<oversold)   return(buy);
  }

so if MACD is at overbought/sold and ADX is greater than the defined level then it opens a position.

Is it possible to add one more rule and say once candle closes check if ADX is less than previous ADX level. So basically I want it to open the position when ADX starts going down. I guess it should be something like if ADX (-1) < ADX (-2)

Any help would be much appreciated.

cheers,
mem
  • Post #2
  • Quote
  • Jan 9, 2014 7:33am Jan 9, 2014 7:33am
  •  FerruFx
  • Joined May 2007 | Status: MT4/MT5 EAs/Indicators/Alerts coder | 6,428 Posts
Quoting mntemel
Disliked
Hi All, Need some help with coding. I have an assistant EA that I use in my trading. At the moment the signal is like below; if(use_macd && use_adx) { if(adx>adx_level && macd>overbought) return(sell); if(adx>adx_level && macd<oversold) return(buy); } so if MACD is at overbought/sold and ADX is greater than the defined level then it opens a position. Is it possible to add one more rule and say once candle closes check if ADX is less than previous ADX level. So basically I want it to open the position when ADX starts going down. I guess it should...
Ignored
You must check the last candle closed and the candle before :

Inserted Code
double adx_1 = iADX(Symbol(),0,ADXperiod,0,MODE_MAIN,1); //---- That's the last candle closed
double adx_2 = iADX(Symbol(),0,ADXperiod,0,MODE_MAIN,2); //---- That's the candle before the last candle closed

if(adx_1 < adx_2) ... etc ...
MT4/MT5 EAs/Indicators/Alerts coder
 
 
  • Post #3
  • Quote
  • Jan 9, 2014 8:05am Jan 9, 2014 8:05am
  •  mntemel
  • | Joined Aug 2011 | Status: Member | 48 Posts
did the trick, many thanks!
 
 
  • Post #4
  • Quote
  • Jan 9, 2014 10:12am Jan 9, 2014 10:12am
  •  FerruFx
  • Joined May 2007 | Status: MT4/MT5 EAs/Indicators/Alerts coder | 6,428 Posts
Quoting mntemel
Disliked
did the trick, many thanks!
Ignored
Great
MT4/MT5 EAs/Indicators/Alerts coder
 
 
  • Post #5
  • Quote
  • Jan 9, 2014 1:36pm Jan 9, 2014 1:36pm
  •  mntemel
  • | Joined Aug 2011 | Status: Member | 48 Posts
@FerruFx

I tried doing it for MACD as well, I'm not getting a compiling error but doesn't seem like working either. Basically I'm trying to find the direction of MACD by comparing MACD at last candle against MACD at 5 candles back.

Inserted Code
  double macd_1 = iMACD(Symbol(),0,MACD_FastEma,MACD_SlowEma,MACD_SignalPeriod,0,MODE_MAIN,1);
  double macd_2 = iMACD(Symbol(),0,MACD_FastEma,MACD_SlowEma,MACD_SignalPeriod,0,MODE_MAIN,5);

Inserted Code
if(macd>overbought && macd_1<macd_2) return(sell);
if(macd<oversold && macd_1>macd_2)   return(buy);

do you see anything wrong here?
 
 
  • Post #6
  • Quote
  • Jan 9, 2014 7:00pm Jan 9, 2014 7:00pm
  •  FerruFx
  • Joined May 2007 | Status: MT4/MT5 EAs/Indicators/Alerts coder | 6,428 Posts
Quoting mntemel
Disliked
do you see anything wrong here?
Ignored
If your need is to know (ie. SELL) if the last candle closed of the MACD histogram is below of the MACD histogram few candles earlier, it should be fine.

Vice versa for BUY.
MT4/MT5 EAs/Indicators/Alerts coder
 
 
  • Post #7
  • Quote
  • Jan 10, 2014 2:26am Jan 10, 2014 2:26am
  •  mntemel
  • | Joined Aug 2011 | Status: Member | 48 Posts
Quoting FerruFx
Disliked
{quote} If your need is to know (ie. SELL) if the last candle closed of the MACD histogram is below of the MACD histogram few candles earlier, it should be fine. Vice versa for BUY.
Ignored
Sorry I forgot to put, I have adx as well in the condition but for some reason it's not doing the job.

Inserted Code
 if(use_macd && use_adx)
  {
     if(adx>30 && adx_2<adx_1 &&  macd>overbought && macd_1<macd_2) return(sell);
     if(adx>30 && adx_2<adx_1 &&  macd<oversold && macd_1>macd_2)   return(buy);
  }

for some reason adx_2<adx_1 is not working this time.. anyway I'll spend a bit more time on it see if I can fix it
 
 
  • Post #8
  • Quote
  • Jan 10, 2014 2:53am Jan 10, 2014 2:53am
  •  cja
  • Joined Feb 2007 | Status: Member | 1,902 Posts
It could be because the BUY / SELL conditions may never occur? if you have something like adx>30 then that is a signal as long as the adx is above 30 however if you have an adx_2<adx_1 that can only signal at that point and your BUY / SELL condition has 2 of those specific signals ( all be it that the macd is 5 bars apart ), so how often do the adx and macd signal conditions occur based on your code? Just throwing this in to the mix and hopefully it helps.
Trade what you see not what you hope
 
 
  • Post #9
  • Quote
  • Jan 10, 2014 4:49am Jan 10, 2014 4:49am
  •  mntemel
  • | Joined Aug 2011 | Status: Member | 48 Posts
Quoting cja
Disliked
It could be because the BUY / SELL conditions may never occur? if you have something like adx>30 then that is a signal as long as the adx is above 30 however if you have an adx_2<adx_1 that can only signal at that point and your BUY / SELL condition has 2 of those specific signals ( all be it that the macd is 5 bars apart ), so how often do the adx and macd signal conditions occur based on your code? Just throwing this in to the mix and hopefully it helps.
Ignored
It actually opens positions but when I look back for instance a positioned was opened even though adx_2 wasn't less than adx_1. For some reason after I added the macd condition adx condition stopped working.
 
 
  • Post #10
  • Quote
  • Jan 10, 2014 5:32am Jan 10, 2014 5:32am
  •  One day!
  • Joined Jul 2009 | Status: ...or there about! | 220 Posts
I'm a hobby coder, so perhaps this isn't the best solution, but if I am testing conditions, I use something along these lines:

Inserted Code
Result = 0; 
if(use_macd && use_adx)
  {
     if(adx > 30.0)        Result += 1000;
     if(adx_2 < adx_1)     Result += 100; 
     if(macd > overbought) Result += 10;
     if(macd_1 < macd_2)   Result += 1;
     Print("Result = ", Result);
  }

So if all conditions are met, Result will = 1111, otherwise you'll see which condition isn't being met.
 
 
  • Post #11
  • Quote
  • Jan 10, 2014 6:40am Jan 10, 2014 6:40am
  •  mntemel
  • | Joined Aug 2011 | Status: Member | 48 Posts
Thanks, I'll give it a try.
 
 
  • Post #12
  • Quote
  • Jan 10, 2014 7:47am Jan 10, 2014 7:47am
  •  fxdaytrader_
  • Joined Jan 2011 | Status: UberTroll | 1,847 Posts
if(use_macd && use_adx) -> and what if both are = FALSE? Does some errorchecking take place then, or will it open position after position ...?

if(adx>30 && adx_2<adx_1 && macd<oversold && macd_1>macd_2) return(buy);: should it not be "adx_2>adx_1"?

you could work with bools also, example (scheme):

if (adxLong && macdLong) OrderSend(BUY ...

bool adxLong() {
if (adx1>adx2) return(true);
return(false);
}

or

if (adxdirection==OP_BUY) OrderSend(BUY ...

int adxdirection() {
if (adx1>adx2) return(OP_BUY);
if (adx1<adx29 return(OP_SELL);
return(99);//just some number to let the program know that there is nothing to d
}
PM me with coding requests and I'll probably put you on my ignore list
 
 
  • Post #13
  • Quote
  • Jan 10, 2014 7:53am Jan 10, 2014 7:53am
  •  FerruFx
  • Joined May 2007 | Status: MT4/MT5 EAs/Indicators/Alerts coder | 6,428 Posts
Quoting fxdaytrader_
Disliked
if(use_macd && use_adx) -> and what if both are = FALSE? Does some errorchecking take place then, or will it open position after position ...? if(adx>30 && adx_2<adx_1 && macd<oversold && macd_1>macd_2) return(buy);: should it not be "adx_2>adx_1"? you could work with bools also, example (scheme): if (adxLong && macdLong) OrderSend(BUY ... bool adxLong() { if (adx1>adx2) return(true); return(false); } or if (adxdirection==OP_BUY) OrderSend(BUY ... int adxdirection() { if (adx1>adx2) return(OP_BUY); if (adx1<adx29 return(OP_SELL); return(99);//just...
Ignored
It seems that you don't know how to use ADX. The main line should be up regardless the trade direction (adx1 > adx2).

That's the +DI/-DI which set the direction.
MT4/MT5 EAs/Indicators/Alerts coder
 
 
  • Post #14
  • Quote
  • Jan 10, 2014 8:06am Jan 10, 2014 8:06am
  •  fxdaytrader_
  • Joined Jan 2011 | Status: UberTroll | 1,847 Posts
I know this but I think I overlooked it that he is not referring to +di/-di in the context right here
PM me with coding requests and I'll probably put you on my ignore list
 
 
  • Post #15
  • Quote
  • Jan 10, 2014 10:11am Jan 10, 2014 10:11am
  •  mntemel
  • | Joined Aug 2011 | Status: Member | 48 Posts
Quoting FerruFx
Disliked
{quote} It seems that you don't know how to use ADX. The main line should be up regardless the trade direction (adx1 > adx2). That's the +DI/-DI which set the direction.
Ignored
Well I look at the main ADX line and the moment it starts decreasing I'm taking that as the reversal point.

I thought these two lines below would give me the past two values of the main ADX line. As Fxdaytrader mentioned I'm not interested in +di/-di lines.

Inserted Code
double adx_1 = iADX(Symbol(),0,adx_period,0,MODE_MAIN,1);
double adx_2 = iADX(Symbol(),0,adx_period,0,MODE_MAIN,2);

Is this where I'm going wrong then?
 
 
  • Post #16
  • Quote
  • Jan 10, 2014 10:32am Jan 10, 2014 10:32am
  •  FerruFx
  • Joined May 2007 | Status: MT4/MT5 EAs/Indicators/Alerts coder | 6,428 Posts
Quoting mntemel
Disliked
{quote} Well I look at the main ADX line and the moment it starts decreasing I'm taking that as the reversal point. I thought these two lines below would give me the past two values of the main ADX line. As Fxdaytrader mentioned I'm not interested in +di/-di lines. double adx_1 = iADX(Symbol(),0,adx_period,0,MODE_MAIN,1); double adx_2 = iADX(Symbol(),0,adx_period,0,MODE_MAIN,2); Is this where I'm going wrong then?
Ignored
The MAIN adx line is absolutely not for directional.

Line slopping up means that the price is "trending" (up or down).

Line slopping down means price is "resting" (no momentum).
MT4/MT5 EAs/Indicators/Alerts coder
 
 
  • Post #17
  • Quote
  • Jan 11, 2014 9:37am Jan 11, 2014 9:37am
  •  mntemel
  • | Joined Aug 2011 | Status: Member | 48 Posts
Quoting FerruFx
Disliked
{quote} The MAIN adx line is absolutely not for directional. Line slopping up means that the price is "trending" (up or down). Line slopping down means price is "resting" (no momentum).
Ignored
That's why I have MACD to find the direction, basically I'm looking at points where ADX is greater than 30 lets say for instance and starts descending (means exhaustion to me) and then using MACD to find the overbought/sold levels and direction as well for entries.
 
 
  • Post #18
  • Quote
  • Jan 11, 2014 10:25am Jan 11, 2014 10:25am
  •  fxdaytrader_
  • Joined Jan 2011 | Status: UberTroll | 1,847 Posts
Quoting mntemel
Disliked
I'm looking at points where ADX is greater than 30 lets say for instance and starts descending (means exhaustion to me)
Ignored
And that is the point - Because that is not the way the ADX is used (see FerruFX's posts above or read one of the adx-documentations around).

It is possible that you better should use +di/-di? If you'd like to use the adx 30 level as filter I suggest the following (without macd now for better understanding):

LONG ENTRY:
+di crosses above -di AND adx is > 30

SHORT ENTRY:
-di crosses above +di AND adx is > 30
PM me with coding requests and I'll probably put you on my ignore list
 
 
  • Post #19
  • Quote
  • Last Post: Jan 11, 2014 11:18am Jan 11, 2014 11:18am
  •  mntemel
  • | Joined Aug 2011 | Status: Member | 48 Posts
Quoting fxdaytrader_
Disliked
{quote} And that is the point - Because that is not the way the ADX is used (see FerruFX's posts above or read one of the adx-documentations around). It is possible that you better should use +di/-di? If you'd like to use the adx 30 level as filter I suggest the following (without macd now for better understanding): LONG ENTRY: +di crosses above -di AND adx is > 30 SHORT ENTRY: -di crosses above +di AND adx is > 30
Ignored
To be honest I haven't used ADX in my trading before, someone gave me that idea and I was trying to test it to see how it works, anyway using +di and -di seems more useful. I'll give it a try, many thanks.
 
 
  • Platform Tech
  • /
  • Code for "if ADX (-1) < ADX (-2)"
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023