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 01-07-2012, 02:15 PM   #1
brojas47
Junior Member
 
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
Default MACD Crossover 0 not firing when it crosses

Hello,

What do I need to do?

I want buy or sell when the MACD to Crosses 0. That's it.

I have the strategy to not Calculate on Bar Close
I've reduced the Chart to 1 min.
I have the lookback to 1 period

The plot of the MACD is set to MACD.

When it fires it's buying and selling 1 to 2 bars late.

What do I need to change?

Thank you
brojas47 is offline  
Reply With Quote
Old 01-07-2012, 02:29 PM   #2
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

brojas,

I am happy to assist you.

You may be selecting the wrong plot. There are three plots in the MACD indicator, so see if one of the plots is crossing when the order is placed. I.e. maybe you need Diff or Avg.

Please let me know if I may assist further.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 01-07-2012, 04:31 PM   #3
brojas47
Junior Member
 
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi Adam,

My plot is set to MACD. That's why I'm stumped.
brojas47 is offline  
Reply With Quote
Old 01-07-2012, 04:47 PM   #4
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

brojas,

The MACD plot is one of the lines, so if you want to have the histogram for example its Diff.

The shorter period line is the MACD plot, the faster period line is the Avg plot and the bars/histogram is the Diff plot.

Please let me know if I may assist further.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 01-07-2012, 05:05 PM   #5
brojas47
Junior Member
 
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi Adam,

I have the plots correct.

You confused me for a second, so I had it buy off the avg (the slower line) and it still buys/sells 1-2 bars late.

Attached is a screenshot of the MACD with vertical lines for when it crosses 0.
Attached Images
File Type: jpg BasicMACD Crossover.jpg (331.2 KB, 47 views)
brojas47 is offline  
Reply With Quote
Old 01-07-2012, 07:02 PM   #6
brojas47
Junior Member
 
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
Default

When I set the condition to Greater than/Less than (instead of Cross Above/Below) I get the same result
brojas47 is offline  
Reply With Quote
Old 01-08-2012, 11:59 AM   #7
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

brojas,

Is this a backtest? Even in backtesting with CalculateOnBarClose = false, it will still act similar to if CalculateOnBarClose is true because of lack of granularity. This behavior does not look abnormal for a backtest. If you want increase intra-bar granularity, you would need to add it to your strategy using the following reference.

http://www.ninjatrader.com/support/f...ead.php?t=6652

Please let me know if I may assist further.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 01-09-2012, 12:24 PM   #8
brojas47
Junior Member
 
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi Adam,

Yes, it is a back-test.
I ran the strategy over the weekend and ran strategy from the chart.

I checked out your sample script. Thanks. Looks useful, but how does it correct this issue?

What time increment can I define less than 1 minute that NT will recognize?

Thanks.
brojas47 is offline  
Reply With Quote
Old 01-09-2012, 01:07 PM   #9
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

brojas,

The example script adds intra-bar granularity, so in other words you will have access to tick bar updates. Essentially, the strategy analyzer attempts to approximate live trading. There will be discrepancies between live trading and backtesting due to this lack of granularity. With intra-bar granularity on the strategy analyzer, you will have order fills sooner after the initial signal as long as you account for it appropriately.

If your data provider offers historical tick data, this is the lowest level NT will be able to plot.

Please let me know if I may assist further.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 01-09-2012, 01:54 PM   #10
brojas47
Junior Member
 
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks. I get cqg data, so I should be good.

Can you point me to an example that I could use with referencing Tick Data with a 1 minute chart?
brojas47 is offline  
Reply With Quote
Old 01-09-2012, 02:10 PM   #11
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

Brojas,

In that link I provided for you, you can change the line :

Add(PeriodType.Minute, 1);

To :

Add(PeriodType.Tick, 1);

Then, in OnBarUpdate() :

Code:
private override void OnBarUpdate()
{

If(BarsInProgress == 0)  //we are on the chart timeseries
{
      Print(Closes[0][0]);  // Prints the current close price for the chart data series
}

if(BarsInProgress == 1)
{
      Print(Closes[1][0];  // Prints the current "close" price for the tick data series
}

}
If you attach the indicator to a 1 minute chart, the chart data series is 1 minute.

Please let me know if I may assist further.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 01-11-2012, 02:11 PM   #12
brojas47
Junior Member
 
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
Default

Hello,

I'm having a hard time following your suggestion.

Do I do this after the existing code under barsinprogress?
Do I leave the other code?

How can I apply this to a MACD crossover 0

Buy when crosses over 0, sell when it crosses below on a 1 minute chart?

Thank you
brojas47 is offline  
Reply With Quote
Old 01-11-2012, 02:19 PM   #13
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

brojas,

It depends on what you are trying to do.

Here is an example with commented out regions of explanation.

Code:
private override void OnBarUpdate()
{

If(BarsInProgress == 0)  //we are on the chart timeseries
{
      //put all the stuff you want done on bar close of the primary chart dataseries in here
}

if(BarsInProgress == 1)    //we are on the tick series
{
     //put all the stuff you want done tick-by-tick in here  e.g.

    if ( CrossAbove(MACD(fast,slow,smooth).Avg,0) )
   {
        EnterLong();
   }

   //other code here
}

}
Please let me know if I may assist further.
Last edited by NinjaTrader_AdamP; 01-11-2012 at 02:22 PM.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 01-18-2012, 10:03 AM   #14
brojas47
Junior Member
 
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
Default Can you tell me what's wrong with this - Error on line 69

#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// MACD Crosses 0
/// </summary>
[Description("MACD Crosses 0")]
public class MACDcrosses0 : Strategy
{
#region Variables
// Wizard generated variables
// User defined variables (add any user defined variables below)
#endregion

/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
Add(PeriodType.Tick, 1);
CalculateOnBarClose = true;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (BarsInProgress == 0)
{
}
if (BarsInProgress == 1)
{
// Condition set 1
if (CrossAbove(MACD(12, 26, 9), 0, 1))
{
EnterLong(DefaultQuantity, "");
}

// Condition set 2
if (CrossBelow(MACD(12, 26, 9), 0, 1))
{
ExitShort("", "");
}
}

else
{
return;
}
}

#region Properties
#endregion
}
brojas47 is offline  
Reply With Quote
Old 01-18-2012, 10:09 AM   #15
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

brojas,

Could you point to which line it is in this example or post the code file (.cs file) here?
NinjaTrader_AdamP 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 alert shanethoene Market Analyzer 20 10-22-2012 08:48 AM
MACD Crossover scartree Automated Trading 18 12-20-2010 08:50 AM
MACD Crossover nybangali General Programming 25 10-29-2010 02:11 PM
MACD with up down arrows on MACD signal line crosses moonriver Indicator Development 1 12-03-2008 10:34 AM
MACD Crossover question rmyrick Strategy Analyzer 7 03-21-2008 01:03 PM


All times are GMT -6. The time now is 10:59 AM.