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

EMA of a division between two series is not plotting

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

    EMA of a division between two series is not plotting

    Hi everyone. I'm trying to write a simple indicator that includes an EMA of a division between two series, but is not plotting the result of the EMA, when I plot only the result of the division it plots fine. I don't have any idea how to solve this, I'm not getting any error while compiling. This is the indicator I'm trying to write: http://www.fxcorporate.com/help/MS/NOTFIFO/i_OBOS.html

    And here is the code I'm using (the division is the variable rk5_a and the EMA rk6_a):
    Thanks for any help

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class ensayo : Indicator
    {
    private Series<double> Ys1;
    private EMA rk3_a;
    private StdDev rk4_a;
    private Series<double> nom;
    private Series<double> den;
    private Series<double> rk5_a;
    private EMA rk6_a;
    private EMA up_a;
    private EMA down_a;
    //private Series<double> Ys1;
    //private Series<double> rk3_a;
    //private double rk3_a;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "ensayo";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    AddPlot(Brushes.Orange, "Ys1_a");
    }
    else if (State == State.Configure)
    {
    Ys1 = new Series<double>(this);
    rk3_a = EMA(Ys1,50);
    rk4_a = StdDev(rk3_a,50);
    nom = new Series<double>(this);
    den = new Series<double>(this);
    rk5_a = new Series<double>(this);
    rk6_a = EMA(rk5_a,50);
    }
    }

    protected override void OnBarUpdate()
    {

    //Add your custom indicator logic here.
    Ys1[0] = (High[0]+Low[0]+(Close[0]*2.0))/4.0;
    //rk5_a[0] = (((Ys1[0]-rk3_a[0])*100) / (rk4_a[0]));
    nom[0] = (Ys1[0]-rk3_a[0])*100;
    den[0] = rk4_a[0];
    rk5_a[0] = (nom[0])/(den[0]);
    Ys1_a[0] = rk6_a[0];

    }

    #region Properties

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Ys1_a
    {
    get { return Values[0]; }
    }
    #endregion

    }
    }

    #region NinjaScript generated code. Neither change nor remove.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private ensayo[] cacheensayo;
    public ensayo ensayo()
    {
    return ensayo(Input);
    }

    public ensayo ensayo(ISeries<double> input)
    {
    if (cacheensayo != null)
    for (int idx = 0; idx < cacheensayo.Length; idx++)
    if (cacheensayo[idx] != null && cacheensayo[idx].EqualsInput(input))
    return cacheensayo[idx];
    return CacheIndicator<ensayo>(new ensayo(), input, ref cacheensayo);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.ensayo ensayo()
    {
    return indicator.ensayo(Input);
    }

    public Indicators.ensayo ensayo(ISeries<double> input )
    {
    return indicator.ensayo(input);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.ensayo ensayo()
    {
    return indicator.ensayo(Input);
    }

    public Indicators.ensayo ensayo(ISeries<double> input )
    {
    return indicator.ensayo(input);
    }
    }
    }

    #endregion

    #2
    Hello jdmonto0,

    Welcome to the NinjaTrader forums!

    As a tip, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
    1. Click Tools -> Export -> NinjaScript...
    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
    3. Click the 'Export' button
    4. Enter a unique name for the file in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message
    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
    Below is a link to the help guide on Exporting NinjaScripts.


    Are there any errors appearing when running the script?

    The rk3_a is the variable storing the SMA indicator in question, correct?
    And this is using the Ys1 as the input series?

    This rk3_a is not added to the chart. This is not a strategy so AddChartIndicator() cannot be used.

    From an indicator you will need to add a plot with AddPlot().


    Then assign the Value[0] of the plot to the value of rk3_a[0].



    Try printing the values for Ys1[0] after this is assigned a value, and rk3_a[0] to ensure these have values.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chelsea for your answer.

      Here is the zip file with the script. I'm using AddPlot.
      The variable that holds the final result and that I'm using to plot is Ys1_a (AddPlot line 58).
      The line 81 calculates the ratio between the two series (rk5_a[0] = (nom[0])/(den[0[])), and when I put that result in Ys1_a (commented line 82), the ratio line is plotted without issues, which means that everything is working as expected till that point. However, when I try to calculate the EMA of that ratio (line 68 rk6_a = EMA(rk5_a,50)) and put that result in Ys1_a (line 83), I don't see any line plotted and I'm not getting any error while compiling.
      Attached Files

      Comment


        #4
        Hello jdmonto0,

        AddPlot() adds a Value series where you can set the values for the plot.

        Assign the Value[0] of the plot to the value of rk3_a[0].

        See post #2 which includes links to the help guide on Value and Values.

        Use prints to ensure the values are being set.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea

          I followed your suggestions. I assigned Value[0] = rk3_a[0] and values where printed and plotted. Then Value[0] = rk4_a[0], it worked too as rk3_a. Then Value[0] = rk5_a[0], it worked too. But when I assigned Value[0] = rk6_a[0], which calculates the EMA of the ratio, nothing was displayed and no values where printed

          Comment


            #6
            Hello jdmonto0,

            If you want 3 plots, you will need to call AddPlot() 3 times and used Values[0][0], Values[1][0], and Values[2][0].

            rk6_a appears to have rk5_a as the input series.

            Does rk5_a have a value for every bar?
            Is the calculation causing a NaN (not a number) value?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,
              I only want to plot the variable rk6_a, the other variables are intermediate steps to get the final result in rk6_a and rk5_a is the input serie to calculate the EMA.
              rk5_a has value for every bar, but rk6_a is causing NaN with this error at the end "Error on calling 'CalculateMinMax' method on bar 2687: The calculation results in unrenderable values".

              Comment


                #8
                Solved. I added an if condition for CurrentBar == 0 and that was the solution.
                Thanks for your help

                Comment


                  #9
                  hey jdmonto0, would you be so kind to upload the final working indicator? I would also like to chart the EMA of a division and don't find an indicator for it! All the best


                  Last edited by hansintakt; 07-25-2023, 09:56 AM.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by rtwave, 04-12-2024, 09:30 AM
                  4 responses
                  30 views
                  0 likes
                  Last Post rtwave
                  by rtwave
                   
                  Started by yertle, Yesterday, 08:38 AM
                  7 responses
                  29 views
                  0 likes
                  Last Post yertle
                  by yertle
                   
                  Started by bmartz, 03-12-2024, 06:12 AM
                  2 responses
                  22 views
                  0 likes
                  Last Post bmartz
                  by bmartz
                   
                  Started by funk10101, Today, 12:02 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post funk10101  
                  Started by gravdigaz6, Yesterday, 11:40 PM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Working...
                  X