View Full Version : MACD Crossover
nybangali
08-14-2007, 10:40 AM
I'm still getting used to this language...
Simple question
How do you detect a MACD crossover ? I saw the example for the simple MA crossover. But not sure what the properties are for the macd line and the signal line.
NinjaTrader_Ray
08-14-2007, 10:47 AM
Most if not all system indicators are well documented which can help you determine available plot values.
MACD - http://www.ninjatrader-support.com/HelpGuideV6/MACD.html
To detect a cross condition you can use either:
CrossAbove() - http://www.ninjatrader-support.com/HelpGuideV6/CrossAbove.html
CrossBelow() - http://www.ninjatrader-support.com/HelpGuideV6/CrossBelow.html
You can then do something like:
if (CrossAbove(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
// Do something here
fragalles
11-07-2007, 03:48 AM
hello
is there a way to setup an alert when an macd crossover happend?
would be nice if this could be setup in the market analyzer as currently i can only check if macd is above or below 0 but not the actuall crossover event.
NinjaTrader_Dierk
11-07-2007, 04:27 AM
You would need to code your own custom indicator which e.g. goes > 0 as the crossover happens and throw that on the market analyzer.
fragalles
11-07-2007, 10:29 PM
You would need to code your own custom indicator which e.g. goes > 0 as the crossover happens and throw that on the market analyzer.
so i would have to make a copy of the @MACD indicator and this lines?:
protected override void OnBarUpdate()
{
double MACDValue=@MACD(Input,Avg,?,period).MACD.Get(Curre ntBar);
}
but how to define MACDValue (above and below 0)?
would it not be more efficient if the market analyzer/(alert system)would have the possibility to cross overs instead of just above and below, this way all indicators could be used in there original state and we would have a powerful market analyzer...
The "@" is unwanted, and make sure you get the parameters correct...
double MACDValue = MACD(Input,Fast,Slow,Smooth)[0];
You can find the default values of Fast, Slow, Smooth by editing the MACD indicator to look at the sources.
Also, the [0] is equivalent to ".Get(CurrentBar)". Note [1] would be ".Get(CurrentBar-1)", etc.
luxurious_04
08-01-2010, 09:44 PM
How about the HMA CrossAbove?Any idea?
NinjaTrader_Josh
08-01-2010, 10:59 PM
luxurious_04,
To do a HMA CrossAbove you would simply use the CrossAbove() method again.
if (CrossAbove(HMA(10), Close, 1))
EnterLong();
luxurious_04
08-01-2010, 11:12 PM
Thanks for the big help NinjaTrader_Josh :)
But there is error in in this code:
iif (CurrentBar==0)
return;
if ((CrossAbove(MACD(12,26,9),MACD(12,26,9)))&& (CrossAbove(HMA(11)[0],HMA(5)[0])))
{
Alert("MyAlert",NinjaTrader.Cbi.Priority.High,"Buy Signal","myalert1",0,Color.Black,Color.Red);
}
else
Alert("MyAlert",NinjaTrader.Cbi.Priority.High,"Sell Signal","myalert2",0,Color.Black,Color.Red);
Error:
Indicator\MACDCrossIsHMACross.cs No overload for method 'CrossAbove' takes '2' arguments CS1501 - click for info 52 9
Indicator\MACDCrossIsHMACross.cs No overload for method 'CrossAbove' takes '2' arguments CS1501 - click for info 52 53
Someone help please.
NinjaTrader_Josh
08-02-2010, 08:13 AM
CrossAbove() method takes 3 parameters. In your code you only have 2 parameters. What you are missing is that last parameter with the # 1. That is the lookback period since it takes two bars to determine a cross over.
if (CrossAbove(someValue, anotherValue, lookBackPeriod))
Also, your first MACD comparison, you are comparing MACD versus the same exact MACD again. This would never yield a cross over because they are always the same values.
luxurious_04
08-02-2010, 06:02 PM
Thanks for the big help
:)
Is if possible to determine weather the crossAbove/Below of zeroLagMacd?
And also the crossAbove/Below of HMA?
Can you give me tips..
Thanks
luxurious_04
08-03-2010, 09:24 PM
Anyone who give me tips in this condition:
if (CrossAbove(MACD(12,26,9),ZeroLagMACD(12,26,9).Avg ,1)== CrossAbove(HMA(11),HMA(5),1))
{
PlaySound(@"C:\Buy_sound.wav");
// Connects the rising plot segment with the other plots
Above.Set(1,Blank[1]);
// Adds the new rising plot line segment to the line
Above.Set(Blank(1)[0]);
Below.Reset();
Neutral.Reset();
}
this Blank in the code what should be the value for that?
NinjaTrader_Bertrand
08-04-2010, 06:36 AM
You would put the value you would want to set for this plot then into this field - http://www.ninjatrader.com/support/forum/showthread.php?t=3227
luxurious_04
08-04-2010, 11:25 PM
In the sample multi-colored plots it is only concern about the Rising,Falling,Flat of SMA.
How about this:
CrossAbove(MACD(Fast,Slow,Smooth),ZeroLagMACD(Fast ,Slow,Smooth) ,1)== CrossAbove(HMA(Period1),HMA(Period2),1)
What will be the basis for the plot?
It consist of 4 indicators? In the example it is only 1 indicator which is SMA?
NinjaTrader_Josh
08-05-2010, 08:39 AM
luxurious_04,
Unfortunately I am not exactly sure what you are asking. Comparing two CrossAbove() methods against each other just checks that the two crosses are happening at the same time, or not at all. The == comparison means that the MACD needs to cross ZeroLagMACD at the exact same time the HMA crosses a different period HMA. The comparison would also be true when there are no crosses from both conditions at the same time.
luxurious_04
08-05-2010, 06:40 PM
what I want is that if this MACD crosses ZeroLagMACD at the same time HMA crosses the HMA with other period then it will create an alarm and then it will the result.If the crosses above then the blue color will plot then if crosses below then red will be added to the plotted blue color.If it is flat then white will also be to the plot.
NinjaTrader_Josh
08-06-2010, 08:32 AM
luxurious_04,
In that case then you would not use ==, but instead use &&.
if (CrossAbove(MACD(Fast,Slow,Smooth),ZeroLagMACD(Fas t ,Slow,Smooth) ,1) && CrossAbove(HMA(Period1),HMA(Period2),1))
luxurious_04
08-08-2010, 10:51 PM
A very big thanks to you.:)
yakito
10-15-2010, 04:11 PM
Hello! I am having a similar problem, maybe someone can help.
When I do:
if (CrossAbove(macdAvg, 0, 1))
{
Print("MACD Above 0");
}
I get an error on the second argument of the CrossAbove, it looks like 0 is not accepted, so how can I check if MACD is above 0?
Thanks a lot!
NinjaTrader_Josh
10-15-2010, 04:44 PM
yakito,
The issue is your first parameter, macdAvg. CrossAbove() is looking for a DataSeries, not a double variable. You need to provide a DataSeries to cross above zero.
For instance...
if (CrossAbove(MACD(12, 26, 9).Avg, 0, 1))
{
Print("MACD Above 0");
}
yakito
10-15-2010, 07:28 PM
Thanks a lot! it is working now.
One more question, do you now if there is any way I can test the "onbarupdate" actions now that the market is closed?
Thanks again one more time.
NinjaTrader_Bertrand
10-16-2010, 09:39 AM
You could backtest on the charts or Strategy Analyzer, or use the Market Replay feature of NinjaTrader -
http://www.ninjatrader-support.com/HelpGuideV6/Overview14.html
XXtrem
10-28-2010, 09:18 PM
I have made the code to proccess buy and sell, on default RSI i have set 30 and 70......
how can i change this to 50 above 50 below instead 30 or 70... here is my broken code yet lool....
if (CrossAbove(EMA(5), EMA(12), 1)
&& RSI(21, 3).Avg[0] > 50) <------ bug 1
EnterLong ();
{
EnterLong(DefaultQuantity, "50");
}
// Condition set 2
if (CrossAbove(EMA(5), EMA(12), 1)
&& RSI(21, 3).Avg[0] < 50 ) <------ bug 2
EnterShort ();
{
EnterShort(DefaultQuantity, "20");
}
}
by the way compiles just fine, but wont function the right way.
hope this is not a funy question lool.
THANK YOU....
NinjaTrader_Bertrand
10-29-2010, 06:32 AM
Welcome to our forums - according to your code the script would trigger on the 50 level for the RSI - do you perhaps refer to the added RSI indicator for display? You would need to change it's oversold and overbought levels (to 50 as well) to reflect what the strategy is doing.
XXtrem
10-29-2010, 02:07 PM
I do it but keep changing back to 30 / 70 again.... looks like i have to create new rsi1 or custom rsa with my default settings... I will try this method see if its working.
Welcome to our forums - according to your code the script would trigger on the 50 level for the RSI - do you perhaps refer to the added RSI indicator for display? You would need to change it's oversold and overbought levels (to 50 as well) to reflect what the strategy is doing.
NinjaTrader_Josh
10-29-2010, 02:11 PM
Please note that after you make code changes you would need to remove the indicator and add a new instance of it to propagate all changes fully.