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

MTF Indicator

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

    MTF Indicator

    Hi Need some help coding MTF Indicator

    Using your @sampleMultiTimeFrame.cs as a base

    I've added the following lines to protected override void Initialize()

    Add(PeriodType.Day,1);
    Add(SMA(8));
    Add(SMA(21));

    After compiling I get the following error twice

    The best overloaded method match for NT.Indicator.IndicatorBase.Add(nt.gui.chart.line)'
    Argument '1'; cannot convert from NT.Indicator.SMA to nt.gui.chart.line

    Any ideas?

    Also your example uses

    if (BarsInProgress != 0) return;

    and i already use

    if (CurrentBar < 2) return;

    can you tell me what the difference is and should i be able to use either?

    Thanks

    #2
    I am happy to assist

    The problem here :

    Add(PeriodType.Day,1);
    Add(SMA(8));
    Add(SMA(21));
    Is that you cannot add SMA to an indicator this way, however it will work in a strategy. The indicator base and strategy base do not always work the same. You will want to add your SMA in your OnBarUpdate() section.

    Please see the following for a helpful link : http://www.ninjatrader.com/support/h...ndicators2.htm

    As far as your other question, BarsInProgress is not the same thing as CurrentBar. BarsInProgress tells you which data-series is currently being updated. It will cycle between values every update and identify which data-series is currently being updated.

    For example, if we have the following

    protected override void Initialize()
    {
    Add(PeriodType.Minute, 5);
    Add(PeriodType.Minute, 15);
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    //rest of code here
    }
    When BarsInProgress = 0, it is updating the chart data-series.
    When BarsInProgress = 1, it is updating the 5 minute data-series.
    When BarsInProgress = 2, it is updating the 15 minute data-series

    In this case we don't care about the chart data-series since we care only about 5 minute and 15 minute data, so we skip it.

    Please let me know if I can be of further assistance.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      I want to build an indicator not a strategy

      Do i add them in the same way

      protected override void OnBarUpdate()
      {
      Add(PeriodType.Day,1);
      Add(SMA(8));
      Add(SMA(21));

      if (CurrentBar < 2)
      return;

      if

      (
      //this would check the the 8 period sma is greater that the 21 period sma on the daily chart
      (SMA(BarsArray[1],8)[0])> SMA(BarsArray[1],21)[0])
      do something
      }
      }
      Last edited by ryebank; 09-14-2011, 02:04 PM.

      Comment


        #4
        Happy to assist you.

        For indicators, you want to initialize plot items in the Initialize() segment. It is best to set these plots up by using the indicator wizard in Tools > New NinjaScript > Indicator before you add custom code.

        For example :

        protected override void Initialize()
        {
        Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "sma1"));
        Add(new Plot(Color.FromKnownColor(KnownColor.Blue), PlotStyle.Line, "sma2"));
        }
        Then, you update their value in the OnBarUpdate() segment.

        protected override void OnBarUpdate()
        {
        sma1.Set(SMA(8));
        sma2.Set(SMA(21));
        }
        Should you require a walkthrough on creating a custom indicator, please see the following link :

        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Hi RyeBank and Adam,

          I think what he is trying to do is add a daily timeframe, to a different timeframe chart.

          So in initialize add the daily bars:
          Add(PeriodType.Day,1);

          And then in OnBarUpdate() you don't "add" the SMAs, just do this:

          Code:
          [COLOR=blue][COLOR=blue]if[/COLOR] (CurrentBars[1] < [COLOR=purple]2[/COLOR]) [COLOR=blue]return[/COLOR];[/COLOR]
          [COLOR=blue][COLOR=blue]if[/COLOR] (SMA(BarsArray[[COLOR=purple]1[/COLOR]], [COLOR=purple]8[/COLOR])[[COLOR=purple]0[/COLOR]] > SMA(BarsArray[[COLOR=purple]1[/COLOR]], [COLOR=purple]21[/COLOR])[[COLOR=purple]0[/COLOR]])[/COLOR]
          [COLOR=blue]{[/COLOR]
          [COLOR=blue] //Do something[/COLOR]
          [COLOR=blue]}[/COLOR]
          edit: CurrentBar should be CurrentBars[1] sorry about that.
          and you may also need some BarsInProgress logic, depending on what you're trying to do.

          Hope this helps.
          VT
          Last edited by VTtrader; 09-14-2011, 03:13 PM.

          Comment


            #6
            Yes this is exactly what i'm trying to do

            and there will be the other side as well (down)

            if (CurrentBar < 2) return;
            if (SMA(BarsArray[1], 8)[0] > SMA(BarsArray[1], 21)[0])
            {
            //Do something
            }
            else if (SMA(BarsArray[1], 8)[0] < SMA(BarsArray[1], 21)[0])
            {
            //Do something
            }

            Comment


              #7
              RyeBank,
              I edited my post while you gave your last response, so be sure to check the edit.

              VT

              Comment


                #8
                Thank you for the clarification VTtrader.

                Ryebank, please let us know if you require additional assistance.
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  Yep i've noticed the change

                  I'll be working on this tomorrow, getting late in London and probably have some more questions

                  Thanks for your help so far, much appreciated

                  Rye

                  Comment


                    #10
                    Can i not do this....?

                    if (CurrentBars[1] < 2) return;
                    if
                    {
                    (SMA(BarsArray[1], 8)[0] > SMA(BarsArray[1], 21)[0])
                    && (code for inside bar/outside bar)
                    ||
                    (SMA(BarsArray[1], 8)[0] < SMA(BarsArray[1], 21)[0])
                    && (code for inside bar/outside bar)
                    }
                    {
                    \\send email
                    \\set up bar color and down bar color
                    insidebar.Set(1);
                    }
                    else
                    insidebar.Set(0);
                    }

                    The problem I have is I can't program to save my life, but i have a script which i use as a template and trying to edit so I can include a condition based on the daily chart. Checking the long term trend if you like....
                    Last edited by ryebank; 09-14-2011, 03:48 PM.

                    Comment


                      #11
                      Not how you've written it.

                      A couple things come to mind, first the basic syntax of a conditional "if" statement.
                      "if" is followed by () which contain the condition(s) to evaluate. The {} are for the block of code to execute if the condition is met. You can chain the conditions together using && for condition1 AND condition2, or you can use || for condition1 OR condition2.

                      So you could use:
                      Code:
                      if ((<condition1> && <condition2>) || (<condition3> && <condition4>))
                      {
                           // do something
                      }
                      else
                      {
                           //do something else
                      }
                      That would be if "condition1 AND condition2" is true, OR "condition3 AND condition4" is true...do something.

                      The other thing that wasn't clear in your example was how you were working with 8sma > 21sma || 8sma < 21sma...it won't matter if the 8sma > 21sma or not. But that could be that I just got lost in what you were trying to do.

                      Hope this helps.
                      VT
                      Last edited by VTtrader; 09-14-2011, 04:26 PM.

                      Comment


                        #12
                        Just to round this out (and might be what you were intending).

                        Would be something like this:
                        Code:
                        if (<condition1> && <condition2>) 
                        {
                             // do something
                        }
                        else if (<condition3> && <condition4>)
                        {
                             //do something else
                        }
                        else
                        // continue with the rest of the code
                        to use your example in the first "if" <condition1> would be 8sma > 21sma AND <condition2> would be your inside/outside bar logic, then do something.

                        if that condition wasn't met then "else if" <condition3> would be 8sma < 21sma AND <condition4> is your inside/outside logic, then it would do something else

                        And if neither condition was met, then it would continue on.

                        VT

                        Comment


                          #13
                          Thanks VT

                          I was making a basic mistake with regards to the () and {} which was causing syntax errors. I've now ironed these out given your explanation....

                          This code compiles, but when I run the indicator on a 60 minute chart i don't see IB being highlighted even though the conditions on the daily chart are passed (SMA 8 > SMA 21)

                          This is how i've coded it. I think my logic is wrong. What i'm trying to achieve is if the SMA 8 > SMA 12 plot IB's, if SMA 8 < SMA 12 plot IB's

                          protected override void Initialize()
                          {
                          //Adding multiple time fram data
                          Add(PeriodType.Day,1);

                          }

                          protected override void OnBarUpdate()
                          {

                          if (CurrentBar < 2)
                          return;


                          if (
                          (
                          ((SMA(BarsArray[1],8)[0] > SMA(BarsArray[1],12)[0]) && (High[0] < High[1] && Low[0] > Low[1]))
                          )
                          ||
                          (
                          ((SMA(BarsArray[1],8)[0] < SMA(BarsArray[1],12)[0]) && (High[0] < High[1] && Low[0] > Low[1]))
                          )
                          )

                          {
                          do something
                          insidebar.Set(1);

                          }
                          else
                          {
                          insidebar.Set(0);
                          }

                          }

                          Comment


                            #14
                            what does this mean

                            15/09/2011 20:45:51|3|4|Error on calling 'OnBarUpdate' method for indicator 'InsideBar' on bar 3: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                            Comment


                              #15
                              ryebank,

                              This issue is described here:


                              Anytime you index a value with [ ], you have to take into account that for the first so many bars, this value is not available.

                              For a multiseries script, use something like this to give it enough bars before making calculations. BarsRequired default is 20.

                              if (CurrentBar < BarsRequired || CurrentBars[1] < BarsRequired) return;
                              Ryan M.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by tierraany, Today, 01:06 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post tierraany  
                              Started by Wilmarobyi, Today, 12:48 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Wilmarobyi  
                              Started by BackToTheFutures, 08-17-2021, 03:15 PM
                              8 responses
                              732 views
                              0 likes
                              Last Post joehanus  
                              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  
                              Working...
                              X