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

Value Plots

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

    Value Plots



    MACD CODE:
    Code:
    //
    // Copyright (C) 2021, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    #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.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
    {
    /// <summary>
    /// The MACD (Moving Average Convergence/Divergence) is a trend following momentum indicator
    /// that shows the relationship between two moving averages of prices.
    /// </summary>
    public class MACD : Indicator
    {
    private Series<double> fastEma;
    private Series<double> slowEma;
    private double constant1;
    private double constant2;
    private double constant3;
    private double constant4;
    private double constant5;
    private double constant6;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionMACD;
    Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meMACD;
    Fast = 12;
    IsSuspendedWhileInactive = true;
    Slow = 26;
    Smooth = 9;
    
    AddPlot(Brushes.DarkCyan, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meMACD);
    AddPlot(Brushes.Crimson, NinjaTrader.Custom.Resource.NinjaScriptIndicatorAv g);
    AddPlot(new Stroke(Brushes.DodgerBlue, 2), PlotStyle.Bar, NinjaTrader.Custom.Resource.NinjaScriptIndicatorDi ff);
    AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZe roLine);
    }
    else if (State == State.Configure)
    {
    constant1 = 2.0 / (1 + Fast);
    constant2 = (1 - (2.0 / (1 + Fast)));
    constant3 = 2.0 / (1 + Slow);
    constant4 = (1 - (2.0 / (1 + Slow)));
    constant5 = 2.0 / (1 + Smooth);
    constant6 = (1 - (2.0 / (1 + Smooth)));
    }
    else if (State == State.DataLoaded)
    {
    fastEma = new Series<double>(this);
    slowEma = new Series<double>(this);
    }
    }
    
    protected override void OnBarUpdate()
    {
    double input0 = Input[0];
    
    if (CurrentBar == 0)
    {
    fastEma[0] = input0;
    slowEma[0] = input0;
    Value[0] = 0;
    Avg[0] = 0;
    Diff[0] = 0;
    }
    else
    {
    double fastEma0 = constant1 * input0 + constant2 * fastEma[1];
    double slowEma0 = constant3 * input0 + constant4 * slowEma[1];
    double macd = fastEma0 - slowEma0;
    double macdAvg = constant5 * macd + constant6 * Avg[1];
    
    fastEma[0] = fastEma0;
    slowEma[0] = slowEma0;
    Value[0] = macd;
    Avg[0] = macdAvg;
    Diff[0] = macd - macdAvg;
    }
    }
    
    #region Properties
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Avg
    {
    get { return Values[1]; }
    }
    
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Default
    {
    get { return Values[0]; }
    }
    
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Diff
    {
    get { return Values[2]; }
    }
    
    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptParameters", Order = 0)]
    public int Fast
    { get; set; }
    
    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptParameters", Order = 1)]
    public int Slow
    { get; set; }
    
    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Smooth", GroupName = "NinjaScriptParameters", Order = 2)]
    public int Smooth
    { get; set; }
    #endregion
    }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private MACD[] cacheMACD;
    public MACD MACD(int fast, int slow, int smooth)
    {
    return MACD(Input, fast, slow, smooth);
    }
    
    public MACD MACD(ISeries<double> input, int fast, int slow, int smooth)
    {
    if (cacheMACD != null)
    for (int idx = 0; idx < cacheMACD.Length; idx++)
    if (cacheMACD[idx] != null && cacheMACD[idx].Fast == fast && cacheMACD[idx].Slow == slow && cacheMACD[idx].Smooth == smooth && cacheMACD[idx].EqualsInput(input))
    return cacheMACD[idx];
    return CacheIndicator<MACD>(new MACD(){ Fast = fast, Slow = slow, Smooth = smooth }, input, ref cacheMACD);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.MACD MACD(int fast, int slow, int smooth)
    {
    return indicator.MACD(Input, fast, slow, smooth);
    }
    
    public Indicators.MACD MACD(ISeries<double> input , int fast, int slow, int smooth)
    {
    return indicator.MACD(input, fast, slow, smooth);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.MACD MACD(int fast, int slow, int smooth)
    {
    return indicator.MACD(Input, fast, slow, smooth);
    }
    
    public Indicators.MACD MACD(ISeries<double> input , int fast, int slow, int smooth)
    {
    return indicator.MACD(input, fast, slow, smooth);
    }
    }
    }
    
    #endregion


    Click image for larger version

Name:	macd.png
Views:	475
Size:	50.6 KB
ID:	1176784


    What in this MACD code allows the strategy builder to be able to work with these MACD value plots?
    I cant figure it out?

    I've tried:

    protected override void OnStateChange()
    {
    MyValuePlot[0] = true;

    Tried adding this to my public class:
    private Series<bool> ValuePlot;
    private double exposedValuePlot;


    Do I need to add it to properties?
    Its so easy to do in ToS! I'm not getting it. Thanks













    #2
    Also tried adding a visual plot:
    AddPlot(new Stroke(Brushes.Green, 3), PlotStyle.Line, "ValuePlot");

    Comment


      #3
      ahhhhh
      That was it.
      I just needed to add it to the properties!


      public Series<double> ValuePlot //
      {
      get { return Values[1]; }
      }

      Comment


        #4
        How do I do it with it being a boolean instead of a series?
        I'm just trying to return to the builder
        If its bullish == true or bearish;

        Comment


          #5
          I tried:
          public bool bull; at the top of my class

          putting bull == true; in my if statement

          and this in my properties:
          public bool Bull //
          {
          get { return bull; }
          }

          Comment


            #6
            Hello ezrollin,

            Yes, the public access modifier with a Series<double> declaration using the XmlIgnore and Browsable(false) attributes that return Values plot series allows the plot to appear in the Strategy Builder or Market Analyzer. These would be in the Properties region near the bottom of the class.

            Unfortunately, only plot series added with AddPlot() can be used in the Strategy Builder. This means you would need to set the value to a 1 or 0 to represent true or false.
            Below is a link to an example.
            https://ninjatrader.com/support/foru...238#post812238
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              So I tried doing it on the Volume up/down indicator to tell me if the volume is green or red and now it doesnt plot anything at all.
              Can you tell me whats wrong?




              Code:
              //
              // Copyright (C) 2021, NinjaTrader LLC <www.ninjatrader.com>.
              // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
              //
              #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.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 MyVolumeUpDown : Indicator
              {
              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Calculate = Calculate.OnBarClose;
              Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionVolumeUpDown;
              DrawOnPricePanel = false;
              IsOverlay = false;
              IsSuspendedWhileInactive = true;
              Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meVolumeUpDown;
              
              AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.Bar, NinjaTrader.Custom.Resource.VolumeUp);
              AddPlot(new Stroke(Brushes.Crimson, 2), PlotStyle.Bar, NinjaTrader.Custom.Resource.VolumeDown);
              AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZe roLine);
              
              [B]AddPlot(Brushes.Green, "Volume Color"); // I addded this **** 4th plot[/B]
              
              
              
              
              
              
              }
              else if (State == State.Historical)
              {
              if (Calculate == Calculate.OnPriceChange)
              {
              Draw.TextFixed(this, "NinjaScriptInfo", string.Format(NinjaTrader.Custom.Resource.NinjaScr iptOnPriceChangeError, Name), TextPosition.BottomRight);
              Log(string.Format(NinjaTrader.Custom.Resource.Ninj aScriptOnPriceChangeError, Name), LogLevel.Error);
              }
              }
              }
              
              protected override void OnBarUpdate()
              {
              if (Close[0] >= Open[0])
              {
              UpVolume[0] = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
              DownVolume.Reset();
              
              [B]Values[4][0] = 1; //added this to show the VolumeColor plot should be 1==green
              Draw.VerticalLine(this, @"Volume is green", 0, Brushes.Yellow, DashStyleHelper.Dot, 2);[/B]
              
              
              
              }
              else
              {
              UpVolume.Reset();
              DownVolume[0] = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
              Values[4][0] = 2; //added this to show the VolumeColor plot should be 2==red
              }
              }
              
              #region Properties
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> DownVolume
              {
              get { return Values[1]; }
              }
              
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> UpVolume
              {
              get { return Values[0]; }
              }
              
              [B][Browsable(false)]
              [XmlIgnore]
              public Series<double> VolumeColor
              {
              get { return Values[4]; } //to work with the 4th plot[/B]
              }
              
              
              
              
              #endregion
              }
              }

              Comment


                #8
                Hello ezrollin,

                Are you getting an error on the Log tab of the Control Center?

                If so, what is the error?

                I see AddPlot() 3 times.

                VolumeUp is Values[0]
                VolumeDown is Values[1]

                VolumeColor would be Values[2]

                There are no plots added for Values[3] or Values[4] that I can see in the code you have provided.
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by sightcareclickhere, Today, 01:55 PM
                0 responses
                1 view
                0 likes
                Last Post sightcareclickhere  
                Started by Mindset, 05-06-2023, 09:03 PM
                9 responses
                258 views
                0 likes
                Last Post ender_wiggum  
                Started by Mizzouman1, Today, 07:35 AM
                4 responses
                18 views
                0 likes
                Last Post Mizzouman1  
                Started by philmg, Today, 01:17 PM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_ChristopherJ  
                Started by cre8able, Today, 01:01 PM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Working...
                X