Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Who can help me to make Price channel

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

    Who can help me to make Price channel


    "I am trying to create an indicator that connects the highest and lowest prices for each candle with lines, either 20 or 50 of them. It's called Price Channel in Thinkorswim. I don't have much coding experience, and I tried to create it, but I keep encountering errors. Is there anyone who can help me?"​

    -----------------------
    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 PriceChannel : Indicator
    {
    private double[] priorHigh20;
    private double[] priorLow20;
    private double[] priorHigh50;
    private double[] priorLow50;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Price Channel based on each bar's High and Low (20, 50)";
    Name = "PriceChannel";
    IsOverlay = true;
    IsSuspendedWhileInactive = true;

    AddPlot(Brushes.Orange, "Upper20");
    AddPlot(Brushes.Orange, "Lower20");
    AddPlot(Brushes.Blue, "Upper50");
    AddPlot(Brushes.Blue, "Lower50");
    }
    else if (State == State.DataLoaded)
    {
    priorHigh20 = new double[20];
    priorLow20 = new double[20];
    priorHigh50 = new double[50];
    priorLow50 = new double[50];
    }
    }

    protected override void OnBarUpdate()
    {
    UpdatePriorValues(High, Low, ref priorHigh20, ref priorLow20, 20);
    UpdatePriorValues(High, Low, ref priorHigh50, ref priorLow50, 50);

    Upper20[0] = priorHigh20[0];
    Lower20[0] = priorLow20[0];
    Upper50[0] = priorHigh50[0];
    Lower50[0] = priorLow50[0];
    }

    private void UpdatePriorValues(Series<double> highs, Series<double> lows, ref double[] priorHigh, ref double[] priorLow, int period)
    {
    double currentHigh = highs[0];
    double currentLow = lows[0];

    for (int i = 0; i < period - 1; i++)
    {
    priorHigh[i] = priorHigh[i + 1];
    priorLow[i] = priorLow[i + 1];
    }

    priorHigh[period - 1] = currentHigh;
    priorLow[period - 1] = currentLow;
    }

    region Properties
    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> Upper20
    {
    get { return Values[0]; }
    }

    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> Lower20
    {
    get { return Values[1]; }
    }

    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> Upper50
    {
    get { return Values[2]; }
    }

    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> Lower50
    {
    get { return Values[3]; }
    }
    #endregion
    }
    }

    #2
    Hello sarihwa,

    Thank you for your post and welcome to the NinjaTrader forum community.

    Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services or one-on-one educational support in our NinjaScript Support department. This is so that we can maintain a high level of service for all of our clients as well as our associates.

    That said, through email or on the forum we are happy to answer any questions you may have about NinjaScript if you decide to code this yourself. We are also happy to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior. If you would like to move forward with debugging assistance, please describe the kinds of errors you have encountered and/or the behavior you are expecting from certain lines of code vs. the behavior you are experiencing in testing.

    Otherwise, this thread will remain open in case a member of the forum community would like to assist you with this script. You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team to follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one-on-one educational services.

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

    Comment


      #3
      If you provide more information of what exactly you need I can help. Cheers!

      Comment


        #4
        adtsystems,

        apologies for messaging you here, but I don't know how to pm you.

        I just downloaded your Rectangle 50 mid Line and labels: https://ninjatraderecosystem.com/user-app-share-download/rectangle-50-mid-line-and-labels/

        I was wondering if you would consider adding a Ray Rectangle? where you click on the high and low of a bar and it projects indefinitely into the future from that specific bar.

        Just a suggestion, thanks again!

        Comment


          #5
          Hi, why not simply use Region Highlight Y - just a suggestion.
          I've find something you can try, not mine.
          You can join adts discord support server to explore more tools if you like https://discord.gg/gB75nGrzZx


          Attached Files

          Comment


            #6
            Originally posted by adtsystems View Post
            Hi, why not simply use Region Highlight Y - just a suggestion.
            I've find something you can try, not mine.
            You can join adts discord support server to explore more tools if you like https://discord.gg/gB75nGrzZx

            I tried it and it didn't work for me, but I appreciate the reply. I'll check out your discord.

            Why not simply use Region? Because I want to mark an important bar and project price forward from that bar.
            If it's the whole screen you don't immediately see which bar it's based on and it's distracting for the previous bars.
            Last edited by davydhnz; 12-14-2023, 06:23 AM.

            Comment


              #7
              I worked out how it works now. I'll try and take the necessary code and add it into Rectangle. Thanks.

              Comment


                #8
                Hi All, on the same topic if anyone can please give a hand , I was trying to do the same(price channel) but at the same time added a displace (like in TOS). This is the code but I'm also getting a hard time: Im getting an error in the ehigligthed line cannot convert double to sbyte:

                namespace NinjaTrader.NinjaScript.Indicators
                {
                public class PriceChannel : Indicator
                {
                private double upperBand;
                private double lowerBand;

                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Description = @"Enter the description for your new custom Indicator here.";
                Name = "PriceChannel";
                Calculate = Calculate.OnBarClose;
                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;
                Length = 33;
                Displace = 54;
                AddPlot(Brushes.SpringGreen, "UpperBand");
                AddPlot(Brushes.Red, "LowerBand");
                }
                else if (State == State.Configure)
                {
                }
                }

                protected override void OnBarUpdate()
                {
                // Calculate the highest high and lowest low within the variable 'Length' periods
                // Calculate the highest high and lowest low within the variable 'Length' periods
                if (CurrentBar < Length + Displace - 1)
                {
                Values[0][0] = upperBand;
                Values[1][0] = lowerBand;
                return;
                }

                upperBand = double.MinValue;
                lowerBand = double.MaxValue;

                for (int i = 0; i < Length; i++)
                {
                int barIndex = CurrentBar - i - Displace;

                if (barIndex >= 0)
                {
                upperBand = Math.Max(upperBand, Highs[barIndex]);
                lowerBand = Math.Min(lowerBand, Lows[barIndex]);

                }
                }
                }

                Comment


                  #9
                  here it is
                  Attached Files

                  Comment


                    #10
                    Originally posted by Lyubomir Kratinov View Post
                    here it is
                    Thank you this looks so much better already. Still don't have the horizontal displacement, i did this mod but is moving the channel upwards instead of moving it to the right.

                    if ( CurrentBar < Length ) return;


                    int highestBar = HighestBar(High, Length);
                    int lowestBar = LowestBar(Low, Length);

                    UpperBand[0] = High[highestBar] + Displace * TickSize ;
                    LowerBand[0] = Low[lowestBar] - Displace * TickSize;

                    // Displace horizontally
                    UpperBand[0] = UpperBand[0] + Displace;
                    LowerBand[0] = LowerBand[0] + Displace;




                    Thanks again for the help!

                    Comment


                      #11
                      Sorry, I thought you mean price displacement. To displace ( plot shift ) by time use this :
                      NinjaTrader 8

                      protected override void OnStateChange()
                      {
                      if (State == State.SetDefaults)
                      {
                      Displacement = 2; // Plots the indicator value from 2 bars ago on the current bar
                      AddPlot(Brushes.Orange, "SMA");
                      }
                      }​​

                      Comment


                        #12
                        Originally posted by Lyubomir Kratinov View Post
                        Sorry, I thought you mean price displacement. To displace ( plot shift ) by time use this :
                        NinjaTrader 8

                        protected override void OnStateChange()
                        {
                        if (State == State.SetDefaults)
                        {
                        Displacement = 2; // Plots the indicator value from 2 bars ago on the current bar
                        AddPlot(Brushes.Orange, "SMA");
                        }
                        }ââÂÂ
                        Great!! TKYSM!!

                        Comment


                          #13
                          Originally posted by JPBForum View Post

                          Great!! TKYSM!!
                          Adding the Displacement like this will work as a regular indicator, Changing only how you plot it and not the results being moved to another "time." Can you please help me incorporate this idea into the indicator? TKY!

                          Comment


                            #14
                            I will, if you share what the idea is first place. Without information I can't guess what exactly you want. Please provide hand drawing, chart, something that will make your idea more clear. "" (like in TOS). "" speaks nothing to me. We use Ninja Trader. Thanks.

                            Comment


                              #15
                              Thank you again.
                              What we have so far works fine, but just as plot, is shown correctly in the chart as a channel with a horizontal displacement. If you can help me including this displacement as a parameter and not just a visual (not sure about this solution), so there is an actual calculation for the present period of "X" periods ago. That calculation will be used later for a strategy.

                              I was mentioning TOS , because over there the displacement in the chart seems to work fine for the calculation and i do it like this:

                              "input displace_price = -33;
                              input length = 33;

                              plot LowerBand = Lowest(low[-displace_price + 1], length);​
                              ​"​


                              Click image for larger version

Name:	image.png
Views:	113
Size:	108.6 KB
ID:	1283363
                              as you can see when i include this indicator, there is no way to adjust the displacement:

                              Click image for larger version

Name:	image.png
Views:	113
Size:	53.5 KB
ID:	1283365

                              Click image for larger version

Name:	image.png
Views:	115
Size:	24.2 KB
ID:	1283364
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              45 responses
                              3,992 views
                              3 likes
                              Last Post johntraderuser2  
                              Started by TAJTrades, Today, 09:46 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post TAJTrades  
                              Started by rhyminkevin, Yesterday, 04:58 PM
                              5 responses
                              62 views
                              0 likes
                              Last Post dp8282
                              by dp8282
                               
                              Started by realblubb, Today, 09:28 AM
                              0 responses
                              8 views
                              0 likes
                              Last Post realblubb  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              1 response
                              19 views
                              0 likes
                              Last Post Rikazkhan007  
                              Working...
                              X