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

Calling Direction from DPOC Indicator

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

    Calling Direction from DPOC Indicator

    Hi,

    I found the POC indicator on Ninjatrader User App Share, and I would like to call the direction if the line color is green to be buy and if the line color is red, direction to be sell. What change do I need to make in the code to be able to create the if statement or could I use the current code for the if statement?



    Thank you!

    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 _dPOC : Indicator
    
    {
    
            #region Propertis
    
    
    
    
            private Dictionary<double, long> cacheDictionary = new Dictionary<double, long>();
    
    
    
    
            #endregion
    
    
    
    
            protected override void OnStateChange()
    
    {
    
    if (State == State.SetDefaults)
    
    {
    
    Description = @"";
    
    Name = "_dPOC";
    
    Calculate = Calculate.OnEachTick;
    
    IsOverlay = true;
    
    DisplayInDataBox = true;
    
    DrawOnPricePanel = true;
    
    DrawHorizontalGridLines = false;
    
    DrawVerticalGridLines = false;
    
    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(new Stroke(Brushes.Transparent, 1), PlotStyle.Line, "DPOC_Line");
    
    UpPoc = Brushes.Red;
    
    DnPoc = Brushes.Green;
    
    }
    
    else if (State == State.Configure)
    
    {
    
    }
    
    }
    
    
    
    
    protected override void OnMarketData(MarketDataEventArgs e)
    
    {
    
                if (Bars.Count <= 0)
    
                    return;
    
    
    
    
                if (e.MarketDataType == MarketDataType.Last && e.Price != 0)
    
                {
    
    
    
    
                    double price = e.Price;
    
                    long volume = e.Volume;
    
    
    
    
                    if (!cacheDictionary.ContainsKey(price))
    
                    {
    
                        cacheDictionary[price] = volume;
    
                    }
    
                    else
    
                    {
    
                        cacheDictionary[price] += volume;
    
                    }
    
    
    
    
    
    
                }
    
    
    
    
            }
    
    
    
    
    protected override void OnBarUpdate()
    
    {
    
                if (Bars.IsFirstBarOfSession)
    
                {
    
                   cacheDictionary.Clear();
    
                  //Plots[0].Brush = Brushes.Transparent;
    
    return;
    
                }
    
    
    
    
    
    
    
    
    
    
                using (var enumerator = cacheDictionary.GetEnumerator())
    
                {
    
                    KeyValuePair<double, long> max = enumerator.Current;
    
                    while (enumerator.MoveNext())
    
                    {
    
                        var kv = enumerator.Current;
    
                        if (kv.Value > max.Value)
    
                            max = kv;
    
                    }
    
                    Values[0][0] = max.Key;
    
    
    
                }
    
    
    
    if(Close[0] < Values[0][0])
    
    {
    
    PlotBrushes[0][0] = UpPoc;
    
    }
    
    else if(Close[0] > Values[0][0])
    
    {
    
    PlotBrushes[0][0] = DnPoc;
    
    }
    
    
    
    }
    
    
    
    #region Properties
    
    
    
    [NinjaScriptProperty]
    
    [XmlIgnore, Display(Name = "Brush from Un Poc", GroupName = "Color", Order = 1)]
    
    [Browsable(true)]
    
    public Brush UpPoc
    
    {
    
    get; set;
    
    }
    
    
    
    [Browsable(false)]
    
    public string UpPocSerialize
    
    {
    
    get{return Serialize.BrushToString(UpPoc);}
    
    set{UpPoc = Serialize.StringToBrush(value);}
    
    
    
    }
    
    
    
    [NinjaScriptProperty]
    
    [XmlIgnore, Display(Name = "Brush from Dn Poc", GroupName = "Color", Order = 2)]
    
    [Browsable(true)]
    
    public Brush DnPoc
    
    {
    
    get; set;
    
    }
    
    
    
    [Browsable(false)]
    
    public string DnPocSerialize
    
    {
    
    get{return Serialize.BrushToString(DnPoc);}
    
    set{DnPoc = Serialize.StringToBrush(value);}
    
    
    
    }
    
    
    
    
    
    #endregion
    
    
    
    }
    
    
    
    
    
    }
    
    
    
    
    #region NinjaScript generated code. Neither change nor remove.
    
    
    
    
    namespace NinjaTrader.NinjaScript.Indicators
    
    {
    
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    
    {
    
    private _dPOC[] cache_dPOC;
    
    public _dPOC _dPOC(Brush upPoc, Brush dnPoc)
    
    {
    
    return _dPOC(Input, upPoc, dnPoc);
    
    }
    
    
    
    
    public _dPOC _dPOC(ISeries<double> input, Brush upPoc, Brush dnPoc)
    
    {
    
    if (cache_dPOC != null)
    
    for (int idx = 0; idx < cache_dPOC.Length; idx++)
    
    if (cache_dPOC[idx] != null && cache_dPOC[idx].UpPoc == upPoc && cache_dPOC[idx].DnPoc == dnPoc && cache_dPOC[idx].EqualsInput(input))
    
    return cache_dPOC[idx];
    
    return CacheIndicator<_dPOC>(new _dPOC(){ UpPoc = upPoc, DnPoc = dnPoc }, input, ref cache_dPOC);
    
    }
    
    }
    
    }
    
    
    
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    
    {
    
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    
    {
    
    public Indicators._dPOC _dPOC(Brush upPoc, Brush dnPoc)
    
    {
    
    return indicator._dPOC(Input, upPoc, dnPoc);
    
    }
    
    
    
    
    public Indicators._dPOC _dPOC(ISeries<double> input , Brush upPoc, Brush dnPoc)
    
    {
    
    return indicator._dPOC(input, upPoc, dnPoc);
    
    }
    
    }
    
    }
    
    
    
    
    namespace NinjaTrader.NinjaScript.Strategies
    
    {
    
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    
    {
    
    public Indicators._dPOC _dPOC(Brush upPoc, Brush dnPoc)
    
    {
    
    return indicator._dPOC(Input, upPoc, dnPoc);
    
    }
    
    
    
    
    public Indicators._dPOC _dPOC(ISeries<double> input , Brush upPoc, Brush dnPoc)
    
    {
    
    return indicator._dPOC(input, upPoc, dnPoc);
    
    }
    
    }
    
    }
    
    
    
    
    #endregion
    ​

    #2
    Hello AgriTrdr,

    Thank you for your post.

    Since this is a script from the User App Share and not a script that comes with NinjaTrader by default, we are limited on what support we can offer. You mentioned that, "I would like to call the direction if the line color is green to be buy and if the line color is red, direction to be sell" - what do you mean to "call" the directions buy and sell? Where are you wanting to call them this? Please clarify so we may better understand and try to assist you.

    I look forward to your reply.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hi Emily,

      I was going to call this indicator in a strategy, and wanted to know the if statement? Below is an example, but I don't know how to call if the dPOC line color is green it should enter long and if the dPOC line color is red, it should enter short.

      if (_dPOC1[0] > 0)
      EnterLong();

      if (_dPOC1[0] < 0)
      EnterShort();

      I hope this is a bit more clearer.

      Thank you!


      Comment


        #4
        Hello AgriTrdr,

        Thank you for your reply.

        Based on the following lines of the indicator, the PlotBrushes are changed to the UpPoc color (Brushes.Red) or the DnPoc color (Brushes.Green) based on if the Close price is above or below the plot value:

        Code:
        UpPoc = Brushes.Red;
        
        DnPoc = Brushes.Green;
        Code:
        if(Close[0] < Values[0][0])
        
        {
        
        PlotBrushes[0][0] = UpPoc;
        
        }
        
        else if(Close[0] > Values[0][0])
        
        {
        
        PlotBrushes[0][0] = DnPoc;
        
        }
        With that in mind, you could add a check to see either if the Close is above/below the value of the indicator, or you could add a check for the PlotBrushes color:
        Code:
        if (Close[0] < _dPOC[0])
        // plot is red, enter short
        EnterShort();
        
        if (Close[0] > _dPOC[0])
        // plot is green, enter long
        EnterLong();
        or:

        Code:
        // check if the PlotBrushes value for the current bar is green
        if (_dPOC().PlotBrushes[0][0] == Brushes.Green)
        // plot is green, enter long
        EnterLong();
        
        // check if the PlotBrushes value for the current bar is red
        if (_DPOC().PlotBrushes[0][0] == Brushes.Red)
        // plot is red, enter short
        EnterShort();
        ​​

        For more information about PlotBrushes:


        Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Thank you very much! This is exactly what I needed.

          Have a great Thanksgiving!

          Comment


            #6
            Hi,

            I added the below script and received an error code CS1501. Please see the attached screenshot. How do I resolve this?

            (_dPOC().PlotBrushes[0][0] == Brushes.Green)


            Attached Files

            Comment


              #7
              Hello AgriTrdr,

              Thank you for your reply.

              The error is likely due to calling the indicator with only _dPOC() - it requires certain arguments in the parenthesis and the intelliprompt should be able to walk you through the information after typing the open parenthesis. For example, once you type in _dPOC( the prompt will pop up with what arguments are required, then you can still include the rest of the snippet after you finish with the arguments and closed parenthesis. My snippet was merely an example and you will need to fill in whichever info is required for the indicator.

              Thank you for your time and patience.
              Emily C.NinjaTrader Customer Service

              Comment


                #8
                Once the intelliprompt opens up, attached is the screenshot that I get. I tried typing in "Brush dnPOC" in the parentheses, but it's still not working.

                Attached Files

                Comment


                  #9
                  Hello AgriTrdr,

                  Thank you for your reply.

                  You will need to use brush values, such as Brushes.Red or Brushes.Green that are used as the default values for upPoc and dnPoc. We have a different script example that has user-definable color inputs found in the help guide here:


                  For more info about working with brushes:


                  Please let us know if we may be of further assistance.
                  Emily C.NinjaTrader Customer Service

                  Comment


                    #10
                    So essentially, add what's in the Properties of the indicator. Do I need to add the plots of the indicator to the strategy as well?

                    Code:
                    [NinjaScriptProperty]
                    
                    [XmlIgnore, Display(Name = "Brush from Un Poc", GroupName = "Color", Order = 1)]
                    
                    [Browsable(true)]
                    
                    public Brush UpPoc
                    
                    {
                    
                    get; set;
                    
                    }
                    
                    
                    
                    [Browsable(false)]
                    
                    public string UpPocSerialize
                    
                    {
                    
                    get{return Serialize.BrushToString(UpPoc);}
                    
                    set{UpPoc = Serialize.StringToBrush(value);}
                    
                    
                    
                    }
                    
                    
                    
                    [NinjaScriptProperty]
                    
                    [XmlIgnore, Display(Name = "Brush from Dn Poc", GroupName = "Color", Order = 2)]
                    
                    [Browsable(true)]
                    
                    public Brush DnPoc
                    
                    {
                    
                    get; set;
                    
                    }
                    
                    
                    
                    [Browsable(false)]
                    
                    public string DnPocSerialize
                    
                    {
                    
                    get{return Serialize.BrushToString(DnPoc);}
                    
                    set{DnPoc = Serialize.StringToBrush(value);}
                    
                    
                    
                    }
                    ​

                    Comment


                      #11
                      Hello AgriTrdr,

                      Thank you for your reply.

                      You do not need to add the properties or the plots from the indicator. You simply need to define the desired brush colors for upPoc and dnPoc when calling the indicator method for _dPOC(). Previously you had mentioned "if (_dPOC1[0] < 0)" and I am not sure if you already had something saved for _dPOC1. Here is a modified example from part of my snippet earlier that sets upPoc brush to Brushes.Red and dnPoc brush to Brushes.Green:

                      Code:
                      // check if the PlotBrushes value for the current bar is red
                      if (_DPOC(Brushes.Red, Brushes.Green).PlotBrushes[0][0] == Brushes.Red)
                      // plot is red, enter short
                      EnterShort();
                      Thank you for your time and patience.​
                      Emily C.NinjaTrader Customer Service

                      Comment


                        #12
                        That worked. Thank you!

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by futtrader, 04-21-2024, 01:50 AM
                        4 responses
                        41 views
                        0 likes
                        Last Post futtrader  
                        Started by Option Whisperer, Today, 09:55 AM
                        1 response
                        12 views
                        0 likes
                        Last Post bltdavid  
                        Started by port119, Today, 02:43 PM
                        0 responses
                        8 views
                        0 likes
                        Last Post port119
                        by port119
                         
                        Started by Philippe56140, Today, 02:35 PM
                        0 responses
                        7 views
                        0 likes
                        Last Post Philippe56140  
                        Started by 00nevest, Today, 02:27 PM
                        0 responses
                        7 views
                        0 likes
                        Last Post 00nevest  
                        Working...
                        X