Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get EMA 4 and EMA 13 on 5 Day : 15 Min chart

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

    How to get EMA 4 and EMA 13 on 5 Day : 15 Min chart

    Hello all,

    I am New to Ninja Trader and starting working with it to automate some of my trades.

    I was wondering if it is possible to programmatically retrieve crossover or values for EMA4 and EMA13 on 5 Day : 15 Minute Chart and possibly execute trades at that instant.

    Also would like to know about 5 Day : 5 Min Chart as well.

    Thanks in advance for any advices.

    -Manu.

    #2
    Hello miamitrader, and thank you for your question.

    I am including a sample NinjaScript to help me go over some concepts. In this sample script, I go over each of the things you mentioned.

    First, since you may want to be able to adjust your fast and slow periods for the EMA, I went ahead and made these parameters. I did that with this code :

    Code:
    [FONT=Courier New]    // Wizard generated variables
        private int fastPeriod = 4;  // fast EMA period
        private int slowPeriod = 13; // slow EMA period
        private int lookBackPeriod = 5; // how long to look for a crossover
    [/FONT]
    and this code (repeated twice more for slowPeriod and lookBackPeriod),

    Code:
    [FONT=Courier New]    [Description("")]
        [GridCategory("Parameters")]
        public int FastPeriod
        {
          get { return fastPeriod; }
          set { fastPeriod = Math.Max(1, value); }
        }
    [/FONT]
    You will notice I added a variable you did not mention, "lookBackPeriod". This is to let you control how many bars back you look for crossover conditions with.

    I went ahead and also decided, as a convenience, to draw these on your chart with this code,

    Code:
    [FONT=Courier New]      Add(EMA(fastPeriod));
          Add(EMA(slowPeriod));
    [/FONT]
    Finally, I demonstrated how to place trades based on a crossover between your fast and slow EMA with this code,

    Code:
    [FONT=Courier New]        if (CrossAbove(EMA(fastPeriod), EMA(slowPeriod), lookBackPeriod))
            {
              EnterLong();
            }
    [/FONT]
    Please let us know if there are any other ways we can help.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      JessicaP,

      Can you explain how one would use DrawTextFixed() in this code if I wanted to display the values of the EMA4[0] and EMA13[0] based on 15m bars when viewing the 5m bar chart.

      Where in the Strategy script could the draw text function be inserted besides OnBarUpdate(). I was looking at indicator "CustomPlotSample" script which uses a Plot(), but saw no actual place where it was called. Is Plot() method called periodically by NT similar to OnBarUpdate()?

      Comment


        #4
        Hello Borland, and thank you for your question.

        In NinjaTrader, Plot objects are created during Initialize and passed in to the Add() method to draw plots on your chart. They are configured by setting their 0'th bar during OnBarUpdate. If you don't configure them, they'll just draw a horizontal line starting at 0.

        If you created a plot like this,

        Code:
        [FONT=Courier New]Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "[B]Plot0[/B]"));[/FONT]
        you will want to modify it like so

        Code:
        [FONT=Courier New][B]Plot0.[/B]Set(Close[0]);[/FONT]
        The reason you want to call everything we will discuss here from OnBarUpdate, is because other methods do not offer strong guarantees that the Indicator or Strategy is in a state where you are able to draw.

        If you would like to access 15 minute data from a 5 minute chart, you will want to first initialize a 15 minute data feed, like this,

        Code:
        [FONT=Courier New]Add(PeriodType.Minute, 15);[/FONT]
        For more information on multiple time series, please see this section of the help guide.



        Once we have a 15 minute data series, we can access its price data like so (assuming that the 15 minute data series was the first call to Add() )

        Code:
        [FONT=Courier New]Closes[1][0][/FONT]
        Finally we have everything we need to draw the lines you would like. Except, that is, for a quick check to make sure our 15 minute bars are populated.

        Code:
        [FONT=Courier New]
        protected override void OnBarUpdate()
        {
            if (CurrentBar < 4)
            {
                return;
            }[/FONT]
        [FONT=Courier New][FONT=Courier New]
            DrawHorizontalLine("My Line", EMA([B]BarsArray[1][/B], 3)[0], Color.ForestGreen);[/FONT][/FONT][FONT=Courier New][FONT=Courier New][FONT=Courier New]
            DrawHorizontalLine("My Other Line", EMA([B]BarsArray[1][/B], 14)[0], Color.Aqua);[/FONT][/FONT]}[/FONT]
        Please let us know if there are any other ways we can help.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Sounds good. For drawing a text block in Strategy script on 5m chart showing EMA(13)[0] 15m bar value, do I need to initialize the text block with the Add() statement similar to the your line plot?

          Code:
          protected override void OnBarUpdate() 
          {
          
               // Draws a text object
               ITextFixed textFixed = DrawTextFixed("tag1", "Text to draw", TextPosition.TopRight);
          
           
          
               // Print the text drawn on the chart
          
               Print(textFixed.Text);
          }

          Comment


            #6
            That's correct, this data would then be available through BarsArray[1], Closes[1], Highs[1], etc.
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              So this should work?

              Code:
               Initialize() {
              
              Add(PeriodType.Minute,15);
              Add ( ITextFixed textFixed = DrawTextFixed("tag1",
                   "EMA " + EMA(BarsArray[1], 13)[0], TextPosition.TopRight));
              }
              with this..

              Code:
              OnBarUpdate() {
              
              if (CurrentBar < 14)
              return;
              
              Print(textFixed);
              }

              Comment


                #8
                Hello borland,

                The DrawTextFixed() method would automatically draw the text on your chart. You wouldn't want to pass textFixed to Print(), nor would you want to pass this into an Add() method.

                You'll want to call DrawTextFixed() from within OnBarUpdate().

                More information about DrawTextFixed() can be found here: https://ninjatrader.com/support/help...wtextfixed.htm

                Please, let us know if we may be of further assistance.
                Zachary G.NinjaTrader Customer Service

                Comment


                  #9
                  I tried that without initialization() in my Strategy, complies but it doesn't draw the fixed text box.

                  Code:
                  if (CurrentBar < 14)
                       return;
                  else
                       DrawTextFixed("tag1", "10m EMA(13) value: " +
                         EMA(BarsArray[2],13)[0], TextPosition.TopRight);
                  Got any example code?

                  Comment


                    #10
                    Hello borland,

                    I have created an example indicator below that will draw the EMA(13) value of an added 15 minute series:

                    Code:
                    protected override void Initialize()
                    {
                         Add(PeriodType.Minute, 15);
                         Overlay = true;
                    }
                    
                    protected override void OnBarUpdate()
                    {
                         if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
                              return;
                    
                         DrawTextFixed("tag1", "15 minute EMA(13) value: " + EMA(BarsArray[1], 13)[0], TextPosition.TopRight);
                    }
                    Zachary G.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks that works
                      Last edited by borland; 07-13-2016, 09:58 PM.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by helpwanted, Today, 03:06 AM
                      1 response
                      12 views
                      0 likes
                      Last Post sarafuenonly123  
                      Started by Brevo, Today, 01:45 AM
                      0 responses
                      9 views
                      0 likes
                      Last Post Brevo
                      by Brevo
                       
                      Started by aussugardefender, Today, 01:07 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post aussugardefender  
                      Started by pvincent, 06-23-2022, 12:53 PM
                      14 responses
                      242 views
                      0 likes
                      Last Post Nyman
                      by Nyman
                       
                      Started by TraderG23, 12-08-2023, 07:56 AM
                      9 responses
                      387 views
                      1 like
                      Last Post Gavini
                      by Gavini
                       
                      Working...
                      X