![]() |
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 |
|
Junior Member
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
|
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.
Adam P.
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
|
Hi Adam,
My plot is set to MACD. That's why I'm stumped. |
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
|
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.
Adam P.
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
|
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. |
|
|
|
|
|
#6 |
|
Junior Member
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
|
When I set the condition to Greater than/Less than (instead of Cross Above/Below) I get the same result
|
|
|
|
|
|
#7 |
|
NinjaTrader Customer Service
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
|
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.
Adam P.
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Junior Member
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
|
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. |
|
|
|
|
|
#9 |
|
NinjaTrader Customer Service
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
|
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.
Adam P.
NinjaTrader Customer Service |
|
|
|
|
|
#10 |
|
Junior Member
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
|
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? |
|
|
|
|
|
#11 |
|
NinjaTrader Customer Service
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
|
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
}
}
Please let me know if I may assist further.
Adam P.
NinjaTrader Customer Service |
|
|
|
|
|
#12 |
|
Junior Member
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#13 |
|
NinjaTrader Customer Service
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
|
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
}
}
Adam P.
NinjaTrader Customer Service
Last edited by NinjaTrader_AdamP; 01-11-2012 at 02:22 PM.
|
|
|
|
|
|
#14 |
|
Junior Member
Join Date: Dec 2011
Posts: 22
Thanks: 0
Thanked 0 times in 0 posts
|
#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 } |
|
|
|
|
|
#15 |
|
NinjaTrader Customer Service
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
|
brojas,
Could you point to which line it is in this example or post the code file (.cs file) here?
Adam P.
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |