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

Indicator based on MACD outputs

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

    Indicator based on MACD outputs

    Hello,

    I am trying to put together an indicator based on the MACD. The Indicator will use the same inputs as MACD as well as the calcs, but I want the output (osilator) to be based on a point system.

    Value[0] = macd;
    if slope Value[0] is positive input to point system = 1
    if slope Value[0] is = 0 input to point system = 0
    if slope Value[0] is negative input to point system = -1

    Avg[0] = macdAvg;
    if slope Avg[0] is positive input to point system = 1
    if slope Avg[0] is = 0 input to point system = 0
    if slope Avg[0] is negative input to point system = -1


    Diff[0] = macd - macdAvg;
    if slope Diff[0] is positive input to point system = 1
    if slope Diff[0] is = 0 input to point system = 0
    ​​​​​​​ if slope Diff[0] is negative input to point system = -1

    I have been trying to put together this osilator and have come up short. I do not want to ruin the MACD indicator, but when I use the Indicator generator it does not give me the flexibility to call other indicators.
    ​​​​​​​any assistance would be appreciated.

    #2
    Hello GTBrooks,

    Thanks for your post.

    By point system, do you mean you want each plot value from your custom indicator to show a 1, 0 or -1?

    If you are struggling with the syntax, I may suggest designing the logic in the Strategy Builder. You could create each condition in the Strategy Builder and then under Actions, add a print for 1, 0 or -1, and also note what value the 1, 0 or -1 represents with your print. This can help just to design the logic and get it working.

    From there, you can create an indicator that adds three plots. Information on creating plots and assigning values can be found below. I suggest assigning each plot value to Close[0], High[0], and Low[0] as a simple test to confirm that the plots are added and values assigned.

    AddPlot() - https://ninjatrader.com/support/help...8/?addplot.htm

    Next, you can implement the code generated by the Strategy Builder into the indicator. Click View Code in the Strategy Builder to see the resulting syntax.

    With the logic implemented in the indicator, you can then replace the prints in your logic with assigning the indicator plots your 1, 0, or -1 values.

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim,

      By point system, I mean you want to plot value from my evaluation of the MACD variables to have a range between 3 to -3 displayed as a plot similarly to an oscillating indicator under the graphed data.

      I used the Strategy Builder to help with syntax.
      the logic is designed and checks out. i was able to render this in TradeStation and was trying to constitute a NinjaTrader version of the indicator.
      I would prefer to fully transition into NinjaTrader.

      Under what section of Strategy Builder (New Indicator) should I put the logic that; evaluates the slope gives each variable a value between 1 to -1, and adds these variable together giving me a value range of 3 to -3? the options are (Welcome, General, Default Properties, Additional Data, Additional Event Methods, Input parameters, Plots and Lines, Finish)

      this is what I have so far.

      Code:
      #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 GTBderivativeMACD : Indicator
          {
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"MACD = Value[0], Signal = Avg[0], Difference = Diff[0] ... Each variable will be evaluated by slope and position. 1, 0, -1 will be assigned to each condition then added to gether for a posable 6 to -6 value. value apraised in plot under graph data.";
                      Name                                        = "GTBderivativeMACD";
                      Calculate                                    = Calculate.OnEachTick;
                      IsOverlay                                    = true;
                      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;
                      MACDfastGTB                    = 3;
                      MACDslowGTB                    = 4;
                      MACDsmoothGTB                    = 2;
                      AddPlot(Brushes.Lime, "GTBderivativeMACDplot");
                  }
                  else if (State == State.Configure)
                  {
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  //Add your custom indicator logic here.
              }
      
              #region Properties
              [NinjaScriptProperty]
              [Range(3, int.MaxValue)]
              [Display(Name="MACDfastGTB", Order=1, GroupName="Parameters")]
              public int MACDfastGTB
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(4, int.MaxValue)]
              [Display(Name="MACDslowGTB", Order=2, GroupName="Parameters")]
              public int MACDslowGTB
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(2, int.MaxValue)]
              [Display(Name="MACDsmoothGTB", Order=3, GroupName="Parameters")]
              public int MACDsmoothGTB
              { get; set; }
      
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> GTBderivativeMACDplot
              {
                  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 GTBderivativeMACD[] cacheGTBderivativeMACD;
              public GTBderivativeMACD GTBderivativeMACD(int mACDfastGTB, int mACDslowGTB, int mACDsmoothGTB)
              {
                  return GTBderivativeMACD(Input, mACDfastGTB, mACDslowGTB, mACDsmoothGTB);
              }
      
              public GTBderivativeMACD GTBderivativeMACD(ISeries<double> input, int mACDfastGTB, int mACDslowGTB, int mACDsmoothGTB)
              {
                  if (cacheGTBderivativeMACD != null)
                      for (int idx = 0; idx < cacheGTBderivativeMACD.Length; idx++)
                          if (cacheGTBderivativeMACD[idx] != null && cacheGTBderivativeMACD[idx].MACDfastGTB == mACDfastGTB && cacheGTBderivativeMACD[idx].MACDslowGTB == mACDslowGTB && cacheGTBderivativeMACD[idx].MACDsmoothGTB == mACDsmoothGTB && cacheGTBderivativeMACD[idx].EqualsInput(input))
                              return cacheGTBderivativeMACD[idx];
                  return CacheIndicator<GTBderivativeMACD>(new GTBderivativeMACD(){ MACDfastGTB = mACDfastGTB, MACDslowGTB = mACDslowGTB, MACDsmoothGTB = mACDsmoothGTB }, input, ref cacheGTBderivativeMACD);
              }
          }
      }
      
      namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
      {
          public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
          {
              public Indicators.GTBderivativeMACD GTBderivativeMACD(int mACDfastGTB, int mACDslowGTB, int mACDsmoothGTB)
              {
                  return indicator.GTBderivativeMACD(Input, mACDfastGTB, mACDslowGTB, mACDsmoothGTB);
              }
      
              public Indicators.GTBderivativeMACD GTBderivativeMACD(ISeries<double> input , int mACDfastGTB, int mACDslowGTB, int mACDsmoothGTB)
              {
                  return indicator.GTBderivativeMACD(input, mACDfastGTB, mACDslowGTB, mACDsmoothGTB);
              }
          }
      }
      
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
          {
              public Indicators.GTBderivativeMACD GTBderivativeMACD(int mACDfastGTB, int mACDslowGTB, int mACDsmoothGTB)
              {
                  return indicator.GTBderivativeMACD(Input, mACDfastGTB, mACDslowGTB, mACDsmoothGTB);
              }
      
              public Indicators.GTBderivativeMACD GTBderivativeMACD(ISeries<double> input , int mACDfastGTB, int mACDslowGTB, int mACDsmoothGTB)
              {
                  return indicator.GTBderivativeMACD(input, mACDfastGTB, mACDslowGTB, mACDsmoothGTB);
              }
          }
      }
      
      #endregion
      the code I am trying to intergrate into the code above
      Code:
      if (CurrentBar == 0)
      {
                              GTBslopeMACD [0]                                          = 0;
                              GTBslopeSignal [0]                                          = 0;
                              GTBslopeDifferential[0]                                   = 0;
                              GTBderivativeMACDplot                                = 0;
      }
      else
      {
                              //Evaluation of Variables to be used in calculation for plot
      if (Value[0] > Value[1])
      GTBslopeMACD = 1;
      else if (Value[0] < Value[1])
      GTBslopeMACD = -1;
      else if (Value[0] = Value[1])
                                                                              GTBslopeMACD = 0;
      if (Avg[0] > Avg[1])
      GTBslopeSignal = 1;
      else if (Avg[0] < Avg[1])
      GTBslopeSignal = -1;
      else if (Avg[0] = Avg[1])
                              GTBslopeSignal = 0;
      if (Diff[0] > Diff[1])
      GTBslopeDifferential = 1;
      else if (Diff [0] < Diff[1])
      GTBslopeDifferential = -1;
      else if (Diff[0] = Diff[1])
                                                                              GTBslopeDifferential = 0;
      
       double GTBderivativeMACDplot                                           = (GTBslopeMACD +GTBpositionMACD +GTBslopeSignal +GTBpositionSignal +GTBslopeDifferential + GTBpositionDifferential);
      Last edited by GTBrooks; 06-12-2020, 03:42 PM. Reason: updating code I am trying to integrate

      Comment


        #4
        Hello GTBrooks,

        Thanks for clarifying.

        The Strategy Builder allows for creating logic, but the New Indicator wizard does not. You can use the View Code button in the Strategy Builder to see how the code can be written, and then you can implement that code in the indicator. This requires programming the indicator by hand.

        If you are trying to -normalize- the plot values to a range of -3 to 3, you could consider the following:

        I have a number range -100 to 100. I'm trying to Normalize it to 0 to 3 with decimal places. -100 = 0 -50 = 0.75 0 = 1.5 50 = 2.25 100 = 3 I want to create a program function to normalize...


        The resource above is a publicly available article on normalizing values with C#.

        The indicator can then add a MACD indicator, add three plots, and then normalize the MACD plot values before assigning them to your custom indicator's plot values. The Strategy Builder can help to see how the MACD indicator can be added and have the MACD plots referenced.

        We look forward to assisting.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by agclub, 04-21-2024, 08:57 PM
        5 responses
        32 views
        0 likes
        Last Post agclub
        by agclub
         
        Started by ESHunter, Today, 08:06 PM
        2 responses
        12 views
        0 likes
        Last Post ESHunter  
        Started by ETFVoyageur, 05-07-2024, 07:05 PM
        19 responses
        149 views
        0 likes
        Last Post ETFVoyageur  
        Started by ETFVoyageur, Yesterday, 10:13 PM
        3 responses
        26 views
        0 likes
        Last Post ETFVoyageur  
        Started by ETFVoyageur, Yesterday, 12:52 AM
        3 responses
        33 views
        0 likes
        Last Post ETFVoyageur  
        Working...
        X