![]() |
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
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Mar 2008
Posts: 49
Thanks: 0
Thanked 0 times in 0 posts
|
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. |
|
|
|
|
|
#2 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
Something like this -
Code:
if (CrossBelow(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
ExitLong();
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Member
Join Date: Mar 2008
Posts: 49
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#4 |
|
Member
Join Date: Mar 2008
Posts: 49
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#5 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#6 |
|
Member
Join Date: Mar 2008
Posts: 49
Thanks: 0
Thanked 0 times in 0 posts
|
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?
|
|
|
|
|
|
#7 |
|
Member
Join Date: Mar 2008
Posts: 49
Thanks: 0
Thanked 0 times in 0 posts
|
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
|
|
|
|
|
|
#8 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
Glad you got it worked out.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
Member
Join Date: Jun 2008
Posts: 40
Thanks: 0
Thanked 0 times in 0 posts
|
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, ""); } } |
|
|
|
|
|
#10 | |
|
Senior Member
Join Date: Aug 2007
Posts: 112
Thanks: 0
Thanked 0 times in 0 posts
|
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:
|
|
|
|
|
|
|
#11 |
|
Senior Member
Join Date: Mar 2008
Posts: 731
Thanks: 0
Thanked 1 time in 1 post
|
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.
|
|
|
|
|
|
#12 | |
|
Senior Member
Join Date: Mar 2008
Posts: 731
Thanks: 0
Thanked 1 time in 1 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. ![]() Quote:
|
|
|
|
|
|
|
#13 |
|
Member
Join Date: Jun 2008
Posts: 40
Thanks: 0
Thanked 0 times in 0 posts
|
I know this is a stupid question but which line which regarding AVG & MACD and what is Diff.
thanks in advance david |
|
|
|
|
|
#14 | |
|
Member
Join Date: Jun 2008
Posts: 40
Thanks: 0
Thanked 0 times in 0 posts
|
LOL.. thank you very much.. TOTALLY over looked that..
)David Quote:
|
|
|
|
|
|
|
#15 |
|
Senior Member
Join Date: Mar 2008
Posts: 731
Thanks: 0
Thanked 1 time in 1 post
|
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.
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |