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

Color in Indicator causing issue

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

    Color in Indicator causing issue

    Hello,


    I am trying to implement a Point of Control value into a custom strategy. There has been an indicator created by another forum called SimplePOC. This indicator functions fine as an indicator, however, if I try to reference it in a strategy it throws an error, saying the color is not defined and also that the color is acting as a variable. Is there any way I can resolve this?


    Code below for the original SimplePOC indicator.



    Code:
    #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;
     using System.Collections;
     using System.Collections.Generic;
     using System.Linq;
    #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 SimplePOC : Indicator
        {
            private Color clr=Color.Blue;
            private Dictionary<double, double> vol_price = new Dictionary<double, double>();
               private Dictionary<Int32, double> poc_list = new Dictionary<Int32, double>();
            
            public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
            {
                for (int i=ChartControl.FirstBarPainted;i<=ChartControl.LastBarPainted;i++)
                {
                    if (i>=0 && i<=BarsArray[0].Count-1)
                    {
                        int x=ChartControl.GetXByBarIdx(BarsArray[0],i)-ChartControl.BarWidth;
                        int y=ChartControl.GetYByValue(BarsArray[0],poc_list[i]);
                        graphics.DrawLine(new Pen(new SolidBrush(clr),2),x-4,y,x+6+ChartControl.BarWidth*2,y);
                    }
                }
            }
    
    
            protected override void Initialize()
            {
                Overlay                             = true;
                CalculateOnBarClose=false;
                Add(PeriodType.Tick,1);
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress==1)
                {
                    if (!vol_price.ContainsKey(Closes[1][0]))
                        vol_price.Add(Closes[1][0],Volumes[1][0]);
                    else
                        vol_price[Closes[1][0]]+=Volumes[1][0];
                }
                
                
                
                if (BarsInProgress==0)
                {
                    if (FirstTickOfBar)
                    {
                        double poc_price=0;
                        double poc_vol=0;
                        foreach(KeyValuePair<double, double> kvp in vol_price.OrderByDescending(key => key.Value))
                        {    
                            poc_price=kvp.Key;
                            poc_vol=kvp.Value;
                            break;
                        }
                        
                        poc_list.Add(CurrentBar,poc_price);
                        vol_price.Clear();
                    }
                            
                    if (!Historical)
                     {
                        double poc_price2=0;
                        double poc_vol2=0;
                        foreach(KeyValuePair<double, double> kvp in vol_price.OrderByDescending(key => key.Value))
                        {    
                            poc_price2=kvp.Key;
                            poc_vol2=kvp.Value;
                            break;
                        }
                        poc_list[CurrentBar]=poc_price2;
                    }
                }        
            }
    
            #region Properties
            [Gui.Design.DisplayName("Color for POC line")]
            [GridCategory("Parameters")]
            public Color Clr
            {
                get { return clr ; }
                set { clr  = value; }
            }
            [Browsable(false)]
            public string clrSerialize
            {
                get { return NinjaTrader.Gui.Design.SerializableColor.ToString(clr ); }
                set { clr  = NinjaTrader.Gui.Design.SerializableColor.FromString(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 SimplePOC[] cacheSimplePOC = null;
    
            private static SimplePOC checkSimplePOC = new SimplePOC();
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public SimplePOC SimplePOC(Color clr)
            {
                return SimplePOC(Input, clr);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public SimplePOC SimplePOC(Data.IDataSeries input, Color clr)
            {
                if (cacheSimplePOC != null)
                    for (int idx = 0; idx < cacheSimplePOC.Length; idx++)
                        if (cacheSimplePOC[idx].Clr == clr && cacheSimplePOC[idx].EqualsInput(input))
                            return cacheSimplePOC[idx];
    
                lock (checkSimplePOC)
                {
                    checkSimplePOC.Clr = clr;
                    clr = checkSimplePOC.Clr;
    
                    if (cacheSimplePOC != null)
                        for (int idx = 0; idx < cacheSimplePOC.Length; idx++)
                            if (cacheSimplePOC[idx].Clr == clr && cacheSimplePOC[idx].EqualsInput(input))
                                return cacheSimplePOC[idx];
    
                    SimplePOC indicator = new SimplePOC();
                    indicator.BarsRequired = BarsRequired;
                    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
                    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
                    indicator.Input = input;
                    indicator.Clr = clr;
                    Indicators.Add(indicator);
                    indicator.SetUp();
    
                    SimplePOC[] tmp = new SimplePOC[cacheSimplePOC == null ? 1 : cacheSimplePOC.Length + 1];
                    if (cacheSimplePOC != null)
                        cacheSimplePOC.CopyTo(tmp, 0);
                    tmp[tmp.Length - 1] = indicator;
                    cacheSimplePOC = 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.SimplePOC SimplePOC(Color clr)
            {
                return _indicator.SimplePOC(Input, clr);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public Indicator.SimplePOC SimplePOC(Data.IDataSeries input, Color clr)
            {
                return _indicator.SimplePOC(input, clr);
            }
        }
    }
    
    // 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.SimplePOC SimplePOC(Color clr)
            {
                return _indicator.SimplePOC(Input, clr);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public Indicator.SimplePOC SimplePOC(Data.IDataSeries input, Color clr)
            {
                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.SimplePOC(input, clr);
            }
        }
    }
    #endregion



    thanks,


    Nick

    #2
    Hello bobajob78,

    Thank you for your note.

    So I may test on my end would you please provide a copy of the strategy which is having the issue?

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi - thanks! But I think I have solved this. I used the wizard to create a simple strategy. I noticed that the color was predefined in the indicator, and in the strategy wizard I could modify this to another color. Once I did this the wizard was happy. And now I can look at the code to see how it modifies the strategy correctly, and can incorporate it into my complex strategy.

      Really great you have the wizard feature to allow us to cheat at writing code!

      Comment


        #4
        OK - so I am scratching my head on this one.


        I have this SimplPOC indicator that finds the price in a bar that has the max accumulated volume - known as the Point of Control. When I create a new 15 minute chart and apply this indicator I get sensible results.


        However, when I add it to a strategy, the POC results on the 15 minute bars are not the same. The 15 minute bars are a secondary series in my strategy. Is there anything happening that would prevent the summation of the volume correctly in the strategy?


        Thanks.


        simplePOC code is in the first post. My indicator code is below:


        Code:
        #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.Indicator;
        using NinjaTrader.Gui.Chart;
        using NinjaTrader.Strategy;
        #endregion
        
        // This namespace holds all strategies and is required. Do not change it.
        namespace NinjaTrader.Strategy
        {
            /// <summary>
            /// First setup of code for 15min MP testing
            /// </summary>
            [Description("First setup of code for 15min MP testing")]
            public class min15VolST1 : Strategy
            {
                #region Variables
                // Wizard generated variables
                private int profit = 10; // Default setting for Profit
                private int stop = 5; // Default setting for Stop
                private int zonePos = 1; // Default setting for ZonePos
                private int zoneNeg = 1; // Default setting for ZoneNeg
                private int savedBar = 0; // to prevent multiple triggered trades
                private int counter = 0; // counter to reset every 15 min
                private int bias = 0; // bias of open versus previoius 15min bar
                // User defined variables (add any user defined variables below)
                #endregion
        
                /// <summary>
                /// This method is used to configure the strategy and is called once before any strategy method is called.
                /// </summary>
                protected override void Initialize()
                {
                    SetProfitTarget("", CalculationMode.Ticks, Profit);
                    SetStopLoss("", CalculationMode.Ticks, Stop, false);
                    
                    // Add a 5 minute Bars object to the strategy
                    Add(PeriodType.Minute, 15);            
                    //Add(PeriodType.Minute, 1);
                    Add(PeriodType.Tick, 1);
                    EntriesPerDirection = 1;
                       EntryHandling = EntryHandling.AllEntries; 
                    Add(SimplePOC(Color.Blue));
        
                    CalculateOnBarClose = true;
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
                    // Ignore bar update events for the supplementary Bars object added above
                       if (BarsInProgress == 0)
                    {
                        counter = counter + 1;
                        //Print("Counter " + counter);
                        Print(" POC 15min " + SimplePOC(BarsArray[1], (Color.Blue))[0]);
                    }
                    if (BarsInProgress == 1)
                    {
                        counter = 0;
                        Print("Counter " + counter + " Time " + ToTime(Time[0]));
                        Print("Closes " + Closes[2][0] + " High " + Highs[1][0] + " Lows " + Lows[1][0] + " POC 15min " + SimplePOC(BarsArray[1], (Color.Blue))[0]);
                    }
                    if (BarsInProgress == 2)
                    {    
                        if (Position.MarketPosition != MarketPosition.Flat)
                        return;
                        // Condition set 1
                        //if (CurrentBars[0] != savedBar && Closes[2][0] <= Lows[1][0] + ZonePos * TickSize
                        //&& Closes[2][0] >= Lows[1][0] - ZonePos * TickSize
                        if (CurrentBars[0] != savedBar && Closes[2][0] <= SimplePOC(BarsArray[1], (Color.Blue))[0] + ZonePos * TickSize
                        //&& Closes[2][0] >= Lows[1][0] - ZonePos * TickSize
                        && ToTime(Time[0]) >= ToTime(8, 45, 0)
                        && ToTime(Time[0]) <= ToTime(10, 30, 0))
                        {
                        Print("LowTrade " + ToTime(Time[0]) + " POC 15min " + SimplePOC(BarsArray[1], (Color.Blue))[0] + " Trigger long " + Closes[2][0]);
                        EnterLong(DefaultQuantity, "");
                        savedBar = CurrentBars[0]; // save the bar number of the chart bar (higher time frame)
                        }
                    
                        // Condition set 2
                        //if (CurrentBars[0] != savedBar && Closes[2][0] >= Highs[1][0] - ZoneNeg * TickSize
                        //&& Closes[2][0] <= Highs[1][0] + ZoneNeg * TickSize
                        if (CurrentBars[0] != savedBar && Closes[2][0] >= SimplePOC(BarsArray[1], (Color.Blue))[0] - ZoneNeg * TickSize
                        //&& Closes[2][0] <= Highs[1][0] + ZoneNeg * TickSize
                        && ToTime(Time[0]) >= ToTime(8, 45, 0)
                        && ToTime(Time[0]) <= ToTime(10, 30, 0))
                        {
                        Print("HighTrade " + ToTime(Time[0]) + " POC 15min " + SimplePOC(BarsArray[1], (Color.Blue))[0] + " Trigger short " + Closes[2][0]);
                        EnterShort(DefaultQuantity, "");
                        savedBar = CurrentBars[0]; // save the bar number of the chart bar (higher time frame)
                        }
                    }
                }
        
                #region Properties
                [Description("Target")]
                [GridCategory("Parameters")]
                public int Profit
                {
                    get { return profit; }
                    set { profit = Math.Max(1, value); }
                }
        
                [Description("Stop")]
                [GridCategory("Parameters")]
                public int Stop
                {
                    get { return stop; }
                    set { stop = Math.Max(1, value); }
                }
        
                [Description("Distance from trigger Long")]
                [GridCategory("Parameters")]
                public int ZonePos
                {
                    get { return zonePos; }
                    set { zonePos = Math.Max(0, value); }
                }
        
                [Description("Distance from trigger Short")]
                [GridCategory("Parameters")]
                public int ZoneNeg
                {
                    get { return zoneNeg; }
                    set { zoneNeg = Math.Max(-5, value); }
                }
                #endregion
            }
        }

        Comment


          #5
          Also - if I create a super simple strategy using 15 minute bars, I get very odd results. It looks like the POC value is just equal to the bar close in the instance:


          Code:
          #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.Indicator;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Strategy;
          #endregion
          
          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
              /// <summary>
              /// Enter the description of your strategy here
              /// </summary>
              [Description("Enter the description of your strategy here")]
              public class POCstrategy : Strategy
              {
                  #region Variables
                  // Wizard generated variables
                  private int stop = 5; // Default setting for Stop
                  private int profit = 10; // Default setting for Profit
                  // User defined variables (add any user defined variables below)
                  #endregion
          
                  /// <summary>
                  /// This method is used to configure the strategy and is called once before any strategy method is called.
                  /// </summary>
                  protected override void Initialize()
                  {
                      SetProfitTarget("", CalculationMode.Ticks, Profit);
                      SetStopLoss("", CalculationMode.Ticks, Stop, false);
          
                      CalculateOnBarClose = true;
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                      // Condition set 1
                      if (CrossAbove(High, SimplePOC(Color.Blue), 1))
                      {
                          EnterShort(DefaultQuantity, "");
                          //Print("Closes " + Closes[2][0] + " High " + Highs[1][0] + " Lows " + Lows[1][0] + " POC 15min " + SimplePOC(BarsArray[1], (Color.Blue))[0]);
                          Print("Time " + ToTime(Time[0]) + " Close " + Close[0] + " High " + High[0] + " Lows " + Low[0] + " POC 15min " + SimplePOC((Color.Blue))[0]);
                      }
          
                      // Condition set 2
                      if (CrossBelow(Low, SimplePOC(Color.Blue), 1))
                      {
                          EnterLong(DefaultQuantity, "");
                          Print("Time " + ToTime(Time[0]) + " Close " + Close[0] + " High " + High[0] + " Lows " + Low[0] + " POC 15min " + SimplePOC((Color.Blue))[0]);
                      }
                  }
          
                  #region Properties
                  [Description("stop loss")]
                  [GridCategory("Parameters")]
                  public int Stop
                  {
                      get { return stop; }
                      set { stop = Math.Max(1, value); }
                  }
          
                  [Description("profit target")]
                  [GridCategory("Parameters")]
                  public int Profit
                  {
                      get { return profit; }
                      set { profit = Math.Max(1, value); }
                  }
                  #endregion
              }
          }

          Output of the window is:


          Time 143000 Close 2746.75 High 2748.25 Lows 2746.75 POC 15min 2746.75
          Time 144500 Close 2747.25 High 2747.5 Lows 2746.25 POC 15min 2747.25
          Time 91500 Close 2750 High 2752.5 Lows 2748.75 POC 15min 2750
          Time 123000 Close 2747 High 2747.75 Lows 2746.25 POC 15min 2747
          Time 141500 Close 2747.5 High 2750.75 Lows 2747.5 POC 15min 2747.5
          Time 143000 Close 2747.25 High 2748.5 Lows 2746.25 POC 15min 2747.25

          Comment


            #6
            Hello bobajob78,

            Thank you for your response.

            Can you export and attach the indicator to your response?

            You can attach your indicator to your response by going to File > Utilities > Export NinjaScript > Export selected source files > select your Indicator > select the right arrow > Export. The file will be located under (My) Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript.

            I look forward to assisting you further.

            Comment


              #7
              Here you go.


              Thanks!


              Attached is the SimplePOC indicator and a very simple strategy trying to use it.
              Attached Files

              Comment


                #8
                Hello bobajob78,

                I apply the strategy to a 15 minute aussie dollar chart and see that sometimes the POC value is the close of the bar, sometimes not. I also see the strategy makes trades.

                Could you please provide more detail on what the issue is?
                Attached Files
                Alan P.NinjaTrader Customer Service

                Comment


                  #9
                  Can you compare the output of the POC in a strategy, from the output of the Indicator POC on a 15 minute chart?


                  From what I have done they don't give the same values.

                  Comment


                    #10
                    Hello bobajob78,

                    In the support department at NinjaTrader we do not create, debug, or modify code for our clients. This is so that we can maintain a high level of service for all of our clients.

                    I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:
                    Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


                    I’ve provided a link covering debugging which you may find helpful.
                    Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

                    You can also contact a professional NinjaScript Consultants who would be eager to create or modify a script at your request or assist you with your script. Please let me know if you would like our business development follow up with you with a list of professional NinjaScript Consultants who would be happy to create this script or any others at your request.

                    Please let us know if you need further assistance.
                    Alan P.NinjaTrader Customer Service

                    Comment


                      #11
                      No problem at all. Was just wondering if it was a problem I had with the Indicator referencing a BarsArray object and a strategy having a different BarsArray object.


                      I'll work it out.


                      Thanks.

                      Comment


                        #12
                        Going back to this example - I have a simple question. How do I reference the values of the indicator SimplePOC in a Strategy? If I try it just reference the close of the dataset, not the values of the indicator. Any help appreciated.

                        Comment


                          #13
                          To clarify - I reference the SimplePOC indicator in a strategy and instead a actually getting the POC numbers is just get reference bar close numbers. Maybe I am referencing the indicator wrong? I notice in the Databox, that if I apply an SMA indicator this is shown in the Databox, however if I apply the SimplePOC it does NOT show in the Databox as an indicator.

                          Comment


                            #14
                            Hello bobajob78,

                            An indicator plot will be a property of that indicator. For example to call the Avg plot of the MACD I would call Print(MACD(12, 26, 9).Avg[0]);.

                            Whether the values for an indicator appear in the in the DataBox will depend if the programmer properly developed the script and if 'Display in DataBox' is true in the indicator parameters.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks for the reply. I notice that the Databox contains only DUMMY, and N/A. So it seems it is not correctly setup. I find it hard to understand why the indicator will plot correctly, but when referenced in the way above all I get is bar closing values. Is it possible that the values or NOT being stored into an array under the SimplePOC indicator array? Surely if they are showing on a chart it must be possible to retrieve those values somehow?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by GLFX005, Today, 03:23 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post GLFX005
                              by GLFX005
                               
                              Started by XXtrader, Yesterday, 11:30 PM
                              2 responses
                              11 views
                              0 likes
                              Last Post XXtrader  
                              Started by Waxavi, Today, 02:10 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by TradeForge, Today, 02:09 AM
                              0 responses
                              14 views
                              0 likes
                              Last Post TradeForge  
                              Started by Waxavi, Today, 02:00 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Working...
                              X