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

Help with strategy

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

    Help with strategy

    Hi, I have put together this strategy, but there are far too many entries and exits.
    Meaning that my logic isn't working as expected.

    Simply, the strategy uses 2 MA crossovers to enter a position and one to exit.
    Only one trade allowed at a time.
    So the BP should be above the fast, and the medium should cross above the slow to initiate a long position. Exit when the medium crosses below the slow. ( this would be "on bar close"). The opposite for a short trade.

    I would appreciate if someone can examine my code and advise me where I may have gone wrong and suggest how I might correct it. I am a novice coder and have done some online courses, successfully coded a few simple indicators, but am struggling with this.
    Please be nice
    Thanks, Ken.
    Attached Files
    Last edited by KennyK; 05-01-2017, 12:06 AM. Reason: forgot upload

    #2
    Hello KennyK,

    Thanks for your post.

    In your description you are stating, "Simply, the strategy uses 2 MA crossovers to enter a position and one to exit....So the BP should be above the fast, and the medium should cross above the slow to initiate a long position. Exit when the medium crosses below the slow." Your code does not reflect crossovers merely that one MA is greater than or less than another. While the MA above below conditions remain true, your strategy will enter and exit.

    For example:

    if (this.BPAboveEmaFast && EmaMediumAboveEmaSlow)
    EnterLong();

    Enter a position, then on the next onbarUpdate:

    if (this.IsLong && EmaMediumAboveEmaSlow);
    ExitLong();

    which is the same condition but now when the bool isLong becomes true the strategy will exit.

    If you want to work with crossover conditions then please see the helpguide sections listed here for your convenience:

    http://ninjatrader.com/support/helpG...crossabove.htm
    http://ninjatrader.com/support/helpG...crossbelow.htm

    Also, so that you can debug your code/strategy it will help for you to use the techniques listed here: http://ninjatrader.com/support/forum...ead.php?t=3418
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Ok, fixed that, next.

      Thanks Paul for that help, I have got that part sorted now.
      I have another small issue that I need help with though.
      In the strategy I use an SMA. I want to define the SMA input series property as "Typical", but can't find out how to do that. I thought it would be simply SMA.typical.
      But that causes an error, so I don't know what to do here, can you please give me a hand.
      Thanks,
      Ken.

      Comment


        #4
        Hello KennyK,

        Thanks for your post.

        The SMA() has a syntax of SMA(data series, period). Typical is one of the acceptable data series (List is Close, Open, High, Low, median, typical or weighted) Reference: http://ninjatrader.com/support/helpG...simple_sma.htm

        Example: SMA(Typical, 14)[0]; // Current bar value of the 14 period SMA of typical price type.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Ok, that's sorted as well, next.

          Thanks Paul, that was easily sorted, would be good if that info regarding the SMA was in the manual.

          Now that I have that MultiMACrossOver strategy working, I am wanting to add to it.
          I have copied it and given it a new name and added an MACD & Stochastic indicators and conditions.
          However I am needing help with this.
          I have attached the strategy here, I have done what I can and it is compiling but no trades are entered when run in the strategy analyser. I haven't yet added the stochastic conditions, as I added the MACD first and tested it, finding it not working as expected.
          The strategy worked before the MACD & stochastic were added.
          So basically I need help to set up the MACD & stochastics properly.
          The conditions I wish to add are:
          For a long entry MACD to be above the zero line, (opposite for short).
          For a long entry Stochastics to be below 50, (opposite for short).
          I have set user input parameters for both and added them to the chart.
          Please inspect my coding and let me know where I have gone wrong and what to do to rectify.
          Thanks again for your help.
          Ken.
          Attached Files

          Comment


            #6
            Hello KennyK,

            Thanks for your reply.

            In state == state.configure you have:

            double macdValue = macd[0];
            double stochValue = stoch[0];


            These will not update in State.Configure and will cause an error that shows up in the NinjaTrader control center's "Log" tab referencing an index that is invalid. Please remove the statements as you don't need them.

            In these statements:
            bool macdAboveZero { get { return macdValue > 0; } }
            bool macdBelowZero { get { return macdValue < 0; } }


            Please change them to:

            bool macdAboveZero { get { return macd[0] > 0; } }
            bool macdBelowZero { get { return macd[0] < 0; } }


            This assigns the value of the MACD indicator that you created with macd = MACD(MACDFast, MACDSlow, MACDSmooth);

            For the stochastics, if you are looking at the "K" line being over/below 50 then you would be using stoch.K[0] in your similar style bool statements.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              MA Plots not the right colour??

              Thanks very much Paul, I have that sorted now.
              Can you please tell me why the SMA & EMA plots don't appear in the analysis chart in the colour coded into the strategy. They are all in the default colour only.
              Thanks again.
              Ken.

              Comment


                #8
                Hi Ken,

                Thanks for your reply.

                Using AddPlot() would only work if were setting those plots with actual values in the OnBarUpdate() and I don't believe this was your intent so you should remove the addPlot statements.

                You have added to the chart a number of indicators using the appropriate method of AddChartIndicators(). As you have observed they will default to their own default colors. You can change the colors of the added indicators plots using ChartIndicators[] collection:

                ChartIndicators[0].Plots[0].Brush = Brushes.Yellow;

                The above would apply to the first added indicator (in your code this would be smaBP). To change the 4th added indicator (emaMedium) would be:

                ChartIndicators[3].Plots[0].Brush = Brushes.DarkOrange;

                Yes, you can also change the plot line widths:

                ChartIndicators[0].Plots[0].Width = 2;

                If an indicator has more than one plot (MACD for example) you would need to know the order that the plots are added within the MACD indicator and then you can change each plots color or other plot characteristics, for example:

                ChartIndicators[5].Plots[0].Brush = Brushes.Gold; // MACD plot line
                ChartIndicators[5].Plots[1].Brush = Brushes.Green; // MACD average line
                ChartIndicators[5].Plots[2].Brush = Brushes.Red; // MACD diff bars
                ChartIndicators[5].Plots[2].Width = 6; // make MACD diff bars w i d e

                Reference: http://ninjatrader.com/support/helpG...indicators.htm
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Location in the code?

                  Thanks Paul, for all your help, i'm learning a lot from you.
                  I did as you instructed in your last reply, however I am not sure I placed the following in the correct location within the code,
                  ChartIndicators[0].Plots[0].Brush = Brushes.Yellow;
                  ChartIndicators[2].Plots[0].Brush = Brushes.Magenta;
                  ChartIndicators[3].Plots[0].Brush = Brushes.Aqua;
                  ChartIndicators[4].Plots[0].Brush = Brushes.DarkOrange;
                  ChartIndicators[5].Plots[0].Brush = Brushes.Firebrick;.
                  I first put it in the OnStateChange() / (State == State.Configure) section, but it caused an error.
                  So I moved it to the OnBarUpdate() section, where it works and there are no errors.
                  But looking at the info here, http://ninjatrader.com/support/helpG...indicators.htm, it would appear it should be placed in a (State == State.DataLoaded) section, as should the AddChartIndicator() methods.
                  Is that right? If so where should that be incorporated into my code? Would it be within the OnStateChange() method?

                  Currently I have:
                  OnStateChange()
                  if (State == State.SetDefaults)
                  else if (State == State.Configure)

                  If I was to add (State == State.DataLoaded), would it be added following, else if (State == State.Configure), as such:
                  else if (State == State.DataLoaded)?

                  Thanks again,
                  Ken.

                  Comment


                    #10
                    Hello Ken,

                    Thanks for your reply.

                    Yes, move the AddChartIndicator() statements into State.DataLoaded. Place the ChartIndicators[] statments in the same State and below the AddChartIndicator() statements.

                    Yes, State.Dataloaded check should be in the OnStateChange().

                    Something like:

                    else if (State == State.Configure)
                    {
                    smaBP = SMA(Typical, BalancePoint);
                    emaSuper = EMA(Super);
                    emaFast = EMA(Fast);

                    etc. etc.
                    }
                    else if (State == State.DataLoaded)
                    {
                    AddChartIndicator(smaBP);
                    AddChartIndicator(emaSuper);
                    AddChartIndicator(emaFast);

                    etc. etc.
                    ChartIndicators[0].Plots[0].Brush = Brushes.Yellow;
                    etc. etc.
                    }
                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by terofs, Yesterday, 04:18 PM
                    1 response
                    21 views
                    0 likes
                    Last Post terofs
                    by terofs
                     
                    Started by CommonWhale, Today, 09:55 AM
                    1 response
                    3 views
                    0 likes
                    Last Post NinjaTrader_Erick  
                    Started by Gerik, Today, 09:40 AM
                    2 responses
                    7 views
                    0 likes
                    Last Post Gerik
                    by Gerik
                     
                    Started by RookieTrader, Today, 09:37 AM
                    2 responses
                    13 views
                    0 likes
                    Last Post RookieTrader  
                    Started by alifarahani, Today, 09:40 AM
                    1 response
                    7 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X