NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 03-11-2008, 03:23 PM   #1
altrader
Member
 
Join Date: Mar 2008
Posts: 49
Thanks: 0
Thanked 0 times in 0 posts
Default Simple MACD signal crossover question

Hi Guys,

I know this is a simple question, and I've looked in the forum but it's still not 100% clear to me.

How do I code an MACD and signal line crossover?

i.e. if MACD crosses Signal Line
EXIT!!

I guess it it will just use CrossBelow but I'm unsure what the values should be.

Sorry for the stupid question - still learning.
altrader is offline  
Reply With Quote
Old 03-11-2008, 03:31 PM   #2
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

Something like this -

Code:
if (CrossBelow(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
    ExitLong();
NinjaTrader_Ray is offline  
Reply With Quote
Old 03-11-2008, 03:50 PM   #3
altrader
Member
 
Join Date: Mar 2008
Posts: 49
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks Ray.

I might be following up with another stupid question here but ...

is your example below saying
if MACD value crosses below the average of the MACD for previous bar
do something?

Is that going to give me a realtime actual crossover?

I assume it's not specifically a crossover of MACD and signal? Sorry, I'm just trying to understand exactly what I'm doing. I've tried printing out the values to the output window as per below, but I'm not sure it's helped me get it.

if (CrossBelow(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
ExitLong();

The current MACD value [0] is 0.0849033617174939
The current MACD value avg [1] is is 0.110320765678418
The current MACD value [0] is 0.705493932279808
The current MACD value avg [1] is is 0.51412018703849
The current MACD value [0] is 0.0924098265641931
The current MACD value avg [1] is is 0.105237284886233
altrader is offline  
Reply With Quote
Old 03-11-2008, 04:01 PM   #4
altrader
Member
 
Join Date: Mar 2008
Posts: 49
Thanks: 0
Thanked 0 times in 0 posts
Default

Just tried a quick test and it looks to me like that code is exiting me at the lower larger circle in attached image, rather than the cross of MACD and signal.

Is that what you would expect to happen?

Thanks
Attached Images
File Type: jpg Capture1.jpg (15.7 KB, 81 views)
altrader is offline  
Reply With Quote
Old 03-11-2008, 04:18 PM   #5
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

Yes, that is what I coded as an example.

Your options are -

MACD(12, 26, 9) - MACD value
MACD(12, 26, 9).Avg - Average line
MACD(12, 26, 9).Diff - Diff line

You can use any of these value in the CrossBelow() method to achieve what you wish.
NinjaTrader_Ray is offline  
Reply With Quote
Old 03-11-2008, 04:54 PM   #6
altrader
Member
 
Join Date: Mar 2008
Posts: 49
Thanks: 0
Thanked 0 times in 0 posts
Default

Sorry, I think I'm probably suffering from too long staring at some code but I can't make sense of the values I'm printing out.

MACD = 12 period EMA - 26 period EMA
Signal = 9 period EMA of MACD

So I want to do something when MACD crosses Signal, but when I print out timestamps and MACD, AVG, and Diff values to the output window they don't seem to compare to the crosses and timeframes I'm seeing visually on the chart so I'm still not clear on what I need to do.

Am I being an idiot here?
altrader is offline  
Reply With Quote
Old 03-11-2008, 05:53 PM   #7
altrader
Member
 
Join Date: Mar 2008
Posts: 49
Thanks: 0
Thanked 0 times in 0 posts
Default

It would seem I'm an idiot

The penny dropped eventually though.

Many thanks for the help.
Last edited by altrader; 03-11-2008 at 05:54 PM. Reason: Deleted image
altrader is offline  
Reply With Quote
Old 03-12-2008, 12:18 AM   #8
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Glad you got it worked out.
NinjaTrader_Josh is offline  
Reply With Quote
Old 11-12-2008, 05:40 PM   #9
dwt1020
Member
 
Join Date: Jun 2008
Posts: 40
Thanks: 0
Thanked 0 times in 0 posts
Default Help

I am just starting working with Strategy Wizard and thought this would be a great place to start.. I created what I thought was a cross up Go Long and then cross down Go Short.. But when I backtest it takes NO orders at all..

Here is the code generated... can someone help me debug..)

thanks in advance


protected override void OnBarUpdate()
{
// Condition set 1
if (CrossAbove(MACD(Fast, Slow, Smooth), MACD(Fast, Slow, Smooth), 1))
{
EnterLong(DefaultQuantity, "");
}

// Condition set 2
if (CrossBelow(MACD(Fast, Slow, Smooth), MACD(Fast, Slow, Smooth), 1))
{
EnterShort(DefaultQuantity, "");
}
}
dwt1020 is offline  
Reply With Quote
Old 11-12-2008, 05:47 PM   #10
Bogan7
Senior Member
 
Join Date: Aug 2007
Posts: 112
Thanks: 0
Thanked 0 times in 0 posts
Default

How would you incorporate a cross above/below the zero line as the trigger instead of a crossing of the fast and slow?

Regards

Quote:
Originally Posted by dwt1020 View Post
I am just starting working with Strategy Wizard and thought this would be a great place to start.. I created what I thought was a cross up Go Long and then cross down Go Short.. But when I backtest it takes NO orders at all..

Here is the code generated... can someone help me debug..)

thanks in advance


protected override void OnBarUpdate()
{
// Condition set 1
if (CrossAbove(MACD(Fast, Slow, Smooth), MACD(Fast, Slow, Smooth), 1))
{
EnterLong(DefaultQuantity, "");
}

// Condition set 2
if (CrossBelow(MACD(Fast, Slow, Smooth), MACD(Fast, Slow, Smooth), 1))
{
EnterShort(DefaultQuantity, "");
}
}
Bogan7 is offline  
Reply With Quote
Old 11-12-2008, 05:50 PM   #11
Elliott Wave
Senior Member
 
Join Date: Mar 2008
Posts: 731
Thanks: 0
Thanked 1 time in 1 post
Default

Use CrossAbove/CrossBelow in the Condition Builder, choose the MACD plot you want to use and on the right side, choose "Numeric Value" from the Misc section then select 0.
Elliott Wave is offline  
Reply With Quote
Old 11-12-2008, 05:54 PM   #12
Elliott Wave
Senior Member
 
Join Date: Mar 2008
Posts: 731
Thanks: 0
Thanked 1 time in 1 post
Default

It looks to me that no plots have been selected. In the condition builder you'll want the plot parameter on one side to be .MACD and the other to be .Avg. By the looks of your code you are waiting for the MACD line to cross the MACD line, which it never will as they have the same value.

Hope that helps.

Quote:
Originally Posted by dwt1020 View Post
I am just starting working with Strategy Wizard and thought this would be a great place to start.. I created what I thought was a cross up Go Long and then cross down Go Short.. But when I backtest it takes NO orders at all..

Here is the code generated... can someone help me debug..)

thanks in advance


protected override void OnBarUpdate()
{
// Condition set 1
if (CrossAbove(MACD(Fast, Slow, Smooth), MACD(Fast, Slow, Smooth), 1))
{
EnterLong(DefaultQuantity, "");
}

// Condition set 2
if (CrossBelow(MACD(Fast, Slow, Smooth), MACD(Fast, Slow, Smooth), 1))
{
EnterShort(DefaultQuantity, "");
}
}
Elliott Wave is offline  
Reply With Quote
Old 11-12-2008, 05:56 PM   #13
dwt1020
Member
 
Join Date: Jun 2008
Posts: 40
Thanks: 0
Thanked 0 times in 0 posts
Default ??

I know this is a stupid question but which line which regarding AVG & MACD and what is Diff.

thanks in advance

david
dwt1020 is offline  
Reply With Quote
Old 11-12-2008, 06:02 PM   #14
dwt1020
Member
 
Join Date: Jun 2008
Posts: 40
Thanks: 0
Thanked 0 times in 0 posts
Default

LOL.. thank you very much.. TOTALLY over looked that..)

David


Quote:
Originally Posted by Elliott Wave View Post
It looks to me that no plots have been selected. In the condition builder you'll want the plot parameter on one side to be .MACD and the other to be .Avg. By the looks of your code you are waiting for the MACD line to cross the MACD line, which it never will as they have the same value.

Hope that helps.
dwt1020 is offline  
Reply With Quote
Old 11-12-2008, 06:04 PM   #15
Elliott Wave
Senior Member
 
Join Date: Mar 2008
Posts: 731
Thanks: 0
Thanked 1 time in 1 post
Default

The Diff is the histogram (bars). Instead of doing MACD CrossAbove Avg etc, you could use:

If .Diff CrossAbove 0
Go Long
If .Diff CrossBelow 0
Go Short

Its exactly the same as the diff is merely the difference between the two lines.

You might want to give the PPO indicator a try. Its almost the same as MACD but uses %, I personally find it better. The QQE is also very good, but its not included by default so you'd need to grab it from the file sharing section.
Last edited by Elliott Wave; 11-12-2008 at 06:06 PM.
Elliott Wave is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MACD Crossover nybangali General Programming 25 10-29-2010 02:11 PM
Is MACD exp. mov. avg. or simple? dandxg Charting 10 02-18-2010 09:35 AM
simple charting question z32000 Charting 3 11-30-2007 01:07 AM
A simple question from a new NT programmer elemento-portador Indicator Development 1 11-05-2007 08:00 AM


All times are GMT -6. The time now is 01:41 PM.