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

Converting Strategies to Indicators

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

    Converting Strategies to Indicators

    Hi - I am not a Ninja coder but have figured out how to create strategies using the Wizard and then editing the script generated. But creating indicators is a different story as I am not as good when jumping into the code. My strategy basically draws arrows based on other indicators, inputs and variables.

    Is there an easy way to copy the code for the strategy into an indicator editor and then remove or change lines of code and then save it as an indicator?

    Thanks!

    #2
    Hello,

    Thanks for the forum post.

    Glad to see you looking into working with these.

    Yes you can do this. Simply copy everything in between OnBarUpdate() from the strategy into the indicator. Also the variables section as well at the top where you have to expand.

    You will then want to delete everything that has to do with orders or strategies in the indicator file side. As an indicator does not have access to order methods and is for display only.

    Please let me know if you run into problems.

    -Brett

    Comment


      #3
      Thanks Brett - Here is the code I created. But it gives arrows both directions in most cases which means the conditions are not reacting as expected. What have I done wrong? Thx.


      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Gui.Chart;
      #endregion

      // This namespace holds all indicators and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      [Description("Enter the description of your new custom indicator here")]
      public class BullBearBarBreak : Indicator
      {
      #region Variables
      // Wizard generated variables
      private int myInput0 = 1; // Default setting for MyInput0
      // User defined variables (add any user defined variables below)
      #endregion

      /// <summary>
      /// This method is used to configure the indicator and is called once before any bar data is loaded.
      /// </summary>
      protected override void Initialize()
      {
      Add(new Plot(Color.FromKnownColor(KnownColor.MediumSeaGree n), PlotStyle.TriangleUp, "UpTrigger"));
      Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.TriangleDown, "DownTrigger"));
      Overlay = false;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {


      // Condition set 1
      // Finds a bar that closes at the high when the EMA and EMA of MACD are rising
      if (High[0] == Close[0]
      && Rising(EMA(13)) == true
      && Rising(EMA(MACD(50, 300, 80).Diff, 1)) == true)
      return;
      {
      UpTrigger.Set(Close[0]);
      }

      // Condition set 2
      // Finds a bar that closes at the low when the EMA and EMA of MACD are falling
      if (Low[0] == Close[0]
      && Falling(EMA(13)) == true
      && Falling(EMA(MACD(50, 300, 80).Diff, 1)) == true)
      return;
      {
      DownTrigger.Set(Close[0]);
      }
      }

      #region Properties
      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public DataSeries UpTrigger
      {
      get { return Values[0]; }
      }

      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public DataSeries DownTrigger
      {
      get { return Values[1]; }
      }

      [Description("")]
      [GridCategory("Parameters")]
      public int MyInput0
      {
      get { return myInput0; }
      set { myInput0 = Math.Max(1, value); }
      }
      #endregion
      }
      }

      #region NinjaScript generated code. Neither change nor remove.
      // This namespace holds all indicators and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      public partial class Indicator : IndicatorBase
      {
      private BullBearBarBreak[] cacheBullBearBarBreak = null;

      private static BullBearBarBreak checkBullBearBarBreak = new BullBearBarBreak();

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public BullBearBarBreak BullBearBarBreak(int myInput0)
      {
      return BullBearBarBreak(Input, myInput0);
      }

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public BullBearBarBreak BullBearBarBreak(Data.IDataSeries input, int myInput0)
      {
      if (cacheBullBearBarBreak != null)
      for (int idx = 0; idx < cacheBullBearBarBreak.Length; idx++)
      if (cacheBullBearBarBreak[idx].MyInput0 == myInput0 && cacheBullBearBarBreak[idx].EqualsInput(input))
      return cacheBullBearBarBreak[idx];

      lock (checkBullBearBarBreak)
      {
      checkBullBearBarBreak.MyInput0 = myInput0;
      myInput0 = checkBullBearBarBreak.MyInput0;

      if (cacheBullBearBarBreak != null)
      for (int idx = 0; idx < cacheBullBearBarBreak.Length; idx++)
      if (cacheBullBearBarBreak[idx].MyInput0 == myInput0 && cacheBullBearBarBreak[idx].EqualsInput(input))
      return cacheBullBearBarBreak[idx];

      BullBearBarBreak indicator = new BullBearBarBreak();
      indicator.BarsRequired = BarsRequired;
      indicator.CalculateOnBarClose = CalculateOnBarClose;
      #if NT7
      indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
      indicator.MaximumBarsLookBack = MaximumBarsLookBack;
      #endif
      indicator.Input = input;
      indicator.MyInput0 = myInput0;
      Indicators.Add(indicator);
      indicator.SetUp();

      BullBearBarBreak[] tmp = new BullBearBarBreak[cacheBullBearBarBreak == null ? 1 : cacheBullBearBarBreak.Length + 1];
      if (cacheBullBearBarBreak != null)
      cacheBullBearBarBreak.CopyTo(tmp, 0);
      tmp[tmp.Length - 1] = indicator;
      cacheBullBearBarBreak = tmp;
      return indicator;
      }
      }
      }
      }

      // This namespace holds all market analyzer column definitions and is required. Do not change it.
      namespace NinjaTrader.MarketAnalyzer
      {
      public partial class Column : ColumnBase
      {
      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.BullBearBarBreak BullBearBarBreak(int myInput0)
      {
      return _indicator.BullBearBarBreak(Input, myInput0);
      }

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public Indicator.BullBearBarBreak BullBearBarBreak(Data.IDataSeries input, int myInput0)
      {
      return _indicator.BullBearBarBreak(input, myInput0);
      }
      }
      }

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      public partial class Strategy : StrategyBase
      {
      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.BullBearBarBreak BullBearBarBreak(int myInput0)
      {
      return _indicator.BullBearBarBreak(Input, myInput0);
      }

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public Indicator.BullBearBarBreak BullBearBarBreak(Data.IDataSeries input, int myInput0)
      {
      if (InInitialize && input == null)
      throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

      return _indicator.BullBearBarBreak(input, myInput0);
      }
      }
      }
      #endregion

      Comment


        #4
        Hello,

        I can assist if you have a compile error. However logic issues normally take looking more in depth into the code and do whats called debugging. Please follow this guide on how to do this.



        Let me know if I can be of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Max238, Today, 01:28 AM
        5 responses
        40 views
        0 likes
        Last Post Max238
        by Max238
         
        Started by giulyko00, Yesterday, 12:03 PM
        3 responses
        12 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by habeebft, Today, 07:27 AM
        1 response
        14 views
        0 likes
        Last Post NinjaTrader_ChristopherS  
        Started by AveryFlynn, Today, 04:57 AM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_Erick  
        Started by r68cervera, Today, 05:29 AM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X