Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

MACD Crossover 0 not firing when it crosses

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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

    #2
    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

    Comment


      #3
      Hi Adam,

      My plot is set to MACD. That's why I'm stumped.

      Comment


        #4
        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

        Comment


          #5
          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 Files

          Comment


            #6
            When I set the condition to Greater than/Less than (instead of Cross Above/Below) I get the same result

            Comment


              #7
              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.

              You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


              Please let me know if I may assist further.
              Adam P.NinjaTrader Customer Service

              Comment


                #8
                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.

                Comment


                  #9
                  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

                  Comment


                    #10
                    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?

                    Comment


                      #11
                      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.
                      Adam P.NinjaTrader Customer Service

                      Comment


                        #12
                        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

                        Comment


                          #13
                          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, 03:22 PM.
                          Adam P.NinjaTrader Customer Service

                          Comment


                            #14
                            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
                            }

                            Comment


                              #15
                              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

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by elirion, Yesterday, 09:32 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post elirion
                              by elirion
                               
                              Started by cre8able, Yesterday, 09:15 PM
                              1 response
                              8 views
                              0 likes
                              Last Post bltdavid  
                              Started by cummish, Yesterday, 08:43 PM
                              0 responses
                              16 views
                              0 likes
                              Last Post cummish
                              by cummish
                               
                              Started by Option Whisperer, Yesterday, 07:58 PM
                              4 responses
                              21 views
                              0 likes
                              Last Post Option Whisperer  
                              Started by ETFVoyageur, 05-07-2024, 07:05 PM
                              13 responses
                              87 views
                              0 likes
                              Last Post ETFVoyageur  
                              Working...
                              X