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

Modify Plot

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

    Modify Plot

    Below is the NT8 code for the Swing Indicator. Can anyone guide me how to modify it so the plot drawn stays on the chart like a ray instead of ending on a new swing?
    Thank you.

    //
    // Copyright (C) 2018, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    #region Using declarations
    using System;
    using System.Collections;
    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
    {
    /// <summary>
    /// The Swing indicator plots lines that represents the swing high and low points.
    /// </summary>
    public class Swing : Indicator
    {
    private int constant;
    private double currentSwingHigh;
    private double currentSwingLow;
    private ArrayList lastHighCache;
    private double lastSwingHighValue;
    private ArrayList lastLowCache;
    private double lastSwingLowValue;
    private int saveCurrentBar;

    private Series<double> swingHighSeries;
    private Series<double> swingHighSwings;
    private Series<double> swingLowSeries;
    private Series<double> swingLowSwings;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionSwing;
    Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meSwing;
    DisplayInDataBox = false;
    PaintPriceMarkers = false;
    IsSuspendedWhileInactive = true;
    IsOverlay = true;
    Strength = 5;

    AddPlot(new Stroke(Brushes.DarkCyan, 2), PlotStyle.Dot, NinjaTrader.Custom.Resource.SwingHigh);
    AddPlot(new Stroke(Brushes.Goldenrod, 2), PlotStyle.Dot, NinjaTrader.Custom.Resource.SwingLow);
    }

    else if (State == State.Configure)
    {
    currentSwingHigh = 0;
    currentSwingLow = 0;
    lastSwingHighValue = 0;
    lastSwingLowValue = 0;
    saveCurrentBar = -1;
    constant = (2 * Strength) + 1;
    }
    else if (State == State.DataLoaded)
    {
    lastHighCache = new ArrayList();
    lastLowCache = new ArrayList();

    swingHighSeries = new Series<double>(this);
    swingHighSwings = new Series<double>(this);
    swingLowSeries = new Series<double>(this);
    swingLowSwings = new Series<double>(this);
    }
    }

    protected override void OnBarUpdate()
    {
    double high0 = (Input is Indicator || Input is Series<double>) ? Input[0] : High[0];
    double low0 = (Input is Indicator || Input is Series<double>) ? Input[0] : Low[0];
    double close0 = (Input is Indicator || Input is Series<double>) ? Input[0] : Close[0];
    double highStrength = High[Math.Min(Strength, CurrentBar)];
    double lowStrength = Low[Math.Min(Strength, CurrentBar)];

    if (BarsArray[0].BarsType.IsRemoveLastBarSupported && CurrentBar < saveCurrentBar)
    {
    currentSwingHigh = SwingHighPlot.IsValidDataPoint(0) ? SwingHighPlot[0] : 0;
    currentSwingLow = SwingLowPlot.IsValidDataPoint(0) ? SwingLowPlot[0] : 0;
    lastSwingHighValue = swingHighSeries[0];
    lastSwingLowValue = swingLowSeries[0];
    swingHighSeries[Strength] = 0;
    swingLowSeries[Strength] = 0;

    lastHighCache.Clear();
    lastLowCache.Clear();
    for (int barsBack = Math.Min(CurrentBar, constant) - 1; barsBack >= 0; barsBack--)
    {
    lastHighCache.Add((Input is Indicator || Input is Series<double>) ? Input[barsBack] : High[barsBack]);
    lastLowCache.Add((Input is Indicator || Input is Series<double>) ? Input[barsBack] : Low[barsBack]);
    }
    saveCurrentBar = CurrentBar;
    return;
    }

    if (saveCurrentBar != CurrentBar)
    {
    swingHighSwings[0] = 0; // initializing important internal
    swingLowSwings[0] = 0; // initializing important internal

    swingHighSeries[0] = 0; // initializing important internal
    swingLowSeries[0] = 0; // initializing important internal

    lastHighCache.Add(high0);
    if (lastHighCache.Count > constant)
    lastHighCache.RemoveAt(0);
    lastLowCache.Add(low0);
    if (lastLowCache.Count > constant)
    lastLowCache.RemoveAt(0);

    if (lastHighCache.Count == constant)
    {
    bool isSwingHigh = true;
    double swingHighCandidateValue = (double) lastHighCache[Strength];
    for (int i=0; i < Strength; i++)
    if (((double) lastHighCache[i]).ApproxCompare(swingHighCandidateValue) >= 0)
    isSwingHigh = false;

    for (int i=Strength+1; i < lastHighCache.Count; i++)
    if (((double) lastHighCache[i]).ApproxCompare(swingHighCandidateValue) > 0)
    isSwingHigh = false;

    swingHighSwings[Strength] = isSwingHigh ? swingHighCandidateValue : 0.0;
    if (isSwingHigh)
    lastSwingHighValue = swingHighCandidateValue;

    if (isSwingHigh)
    {
    currentSwingHigh = swingHighCandidateValue;
    for (int i=0; i <= Strength; i++)
    SwingHighPlot[i] = currentSwingHigh;
    }
    else if (high0 > currentSwingHigh || currentSwingHigh.ApproxCompare(0.0) == 0)
    {
    currentSwingHigh = 0.0;
    SwingHighPlot[0] = close0;
    SwingHighPlot.Reset();
    }
    else
    SwingHighPlot[0] = currentSwingHigh;

    if (isSwingHigh)
    {
    for (int i=0; i<=Strength; i++)
    swingHighSeries[i] = lastSwingHighValue;
    }
    else
    {
    swingHighSeries[0] = lastSwingHighValue;
    }
    }

    if (lastLowCache.Count == constant)
    {
    bool isSwingLow = true;
    double swingLowCandidateValue = (double) lastLowCache[Strength];
    for (int i=0; i < Strength; i++)
    if (((double) lastLowCache[i]).ApproxCompare(swingLowCandidateValue) <= 0)
    isSwingLow = false;

    for (int i=Strength+1; i < lastLowCache.Count; i++)
    if (((double) lastLowCache[i]).ApproxCompare(swingLowCandidateValue) < 0)
    isSwingLow = false;

    swingLowSwings[Strength] = isSwingLow ? swingLowCandidateValue : 0.0;
    if (isSwingLow)
    lastSwingLowValue = swingLowCandidateValue;

    if (isSwingLow)
    {
    currentSwingLow = swingLowCandidateValue;
    for (int i=0; i <= Strength; i++)
    SwingLowPlot[i] = currentSwingLow;
    }
    else if (low0 < currentSwingLow || currentSwingLow.ApproxCompare(0.0) == 0)
    {
    currentSwingLow = double.MaxValue;
    SwingLowPlot[0] = close0;
    SwingLowPlot.Reset();
    }
    else
    SwingLowPlot[0] = currentSwingLow;

    if (isSwingLow)
    {
    for (int i=0; i<=Strength; i++)
    swingLowSeries[i] = lastSwingLowValue;
    }
    else
    {
    swingLowSeries[0] = lastSwingLowValue;
    }
    }

    saveCurrentBar = CurrentBar;
    }
    else if (CurrentBar >= constant - 1)
    {
    if (high0 > highStrength && swingHighSwings[Strength] > 0.0)
    {
    swingHighSwings[Strength] = 0.0;
    for (int i = 0; i <= Strength; i++)
    {
    SwingHighPlot[i] = close0;
    SwingHighPlot.Reset(i);
    currentSwingHigh = 0.0;
    }
    }
    else if (high0 > highStrength && currentSwingHigh.ApproxCompare(0.0) != 0)
    {
    SwingHighPlot[0] = close0;
    SwingHighPlot.Reset();
    currentSwingHigh = 0.0;
    }
    else if (high0 <= currentSwingHigh)
    SwingHighPlot[0] = currentSwingHigh;

    if (low0 < lowStrength && swingLowSwings[Strength] > 0.0)
    {
    swingLowSwings[Strength] = 0.0;
    for (int i = 0; i <= Strength; i++)
    {
    SwingLowPlot[i] = close0;
    SwingLowPlot.Reset(i);
    currentSwingLow = double.MaxValue;
    }
    }
    else if (low0 < lowStrength && currentSwingLow.ApproxCompare(double.MaxValue) != 0)
    {
    SwingLowPlot.Reset();
    currentSwingLow = double.MaxValue;
    }
    else if (low0 >= currentSwingLow)
    SwingLowPlot[0] = currentSwingLow;
    }

    #2
    Hello, Trader17.

    Thanks for the post.

    Please see this example where I modified the swing indicator to plot only one dot at the beginning of a new swing.



    Please see these instructions for importing this script:


    You may modify this script further using Draw.Ray to draw a ray where the single dot is being drawn.



    Please let us know if we may be of any further assistance.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thank you so much. So where do I replace the Draw.Ray so a ray will extend out all the time at every swing instead of a single dot or dots till a new swing is formed? I am not into coding. Sorry for the bother.
      Thanks.

      Comment


        #4
        Hello Trader17,

        Thanks for the reply.

        For educational purposes, I have made a version of this. Please see attached.

        I modified the previously posted SingleDotSwing to draw rays on lines 154 for the high swing and line 200 for the low swing.

        Please let us know if you have any questions.
        Attached Files
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thank you very much. So to modify an indicator you copy the original one and then go to Strategy Wizard to make changes?
          Thank you once again.

          Comment


            #6
            Hello Trader17,

            Thanks for the follow-up.

            You must edit all default strategies in the NinjaScript editor. All of the default indicators and strategies code is unlocked, meaning it can not be edited in the Strategy Builder (only strategies may be edited in a strategy builder). To make a new copy of any indicator for editing, right click on the Indicators/Strategies code and select "Save as".

            Please let me know if you have any further questions.
            Last edited by NinjaTrader_ChrisL; 05-21-2018, 07:38 AM.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Do you have an educational video showing how to make a new indicator or modify an existing one?
              Thanks.

              Comment


                #8
                Originally posted by NinjaTrader_ChrisL View Post
                Hello Trader17,

                Thanks for the reply.

                For educational purposes, I have made a version of this. Please see attached.

                I modified the previously posted SingleDotSwing to draw rays on lines 154 for the high swing and line 200 for the low swing.

                Please let us know if you have any questions.
                I want to change the colors of the rays. Can I do it directly in this version you made or am I supposed to make a copy of it and modify i the copy?
                Thank you.

                Comment


                  #9
                  Originally posted by Trader17 View Post
                  Do you have an educational video showing how to make a new indicator or modify an existing one?
                  Thanks.
                  Not a video, but take a look starting here,
                  Support for the development of custom automated trading strategies using NinjaScript.


                  The steps are pretty much the same, regardless NT7 or NT8, strategy or indicator ...

                  Comment


                    #10
                    Hello Trader17,

                    You can edit the indicator that I posted. If something goes awry, you can always download the original that I posted here.
                    Chris L.NinjaTrader Customer Service

                    Comment


                      #11
                      hey everyone,

                      maybe anybody can help me out here. I´m looking for a swing indicator which is drawing a line on the LOW of the highest candle and the HIGH of the lowest candle in n bars.

                      Thank you

                      Comment


                        #12
                        Hello eisi83,

                        Thank you for your reply.

                        I'm not aware of a particular existing indicator that would do this out of the box, however, I'd encourage you to search around on our publicly available User App Share to see if there's anything you see with that functionality:



                        That being said, it would be relatively simple to create an indicator yourself that would draw lines at the points you specify.

                        You could use the MIN() and MAX() indicators in a script to get the lowest low and highest high prices and use Draw.HorizontalLine to draw the lines you wish:





                        If you don't see a script that meets your needs on the user app share, and don't want to code a custom indicator yourself, 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 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 to you.

                        The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
                        Kate W.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by rdtdale, Today, 01:02 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post rdtdale
                        by rdtdale
                         
                        Started by alifarahani, Today, 09:40 AM
                        3 responses
                        15 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by RookieTrader, Today, 09:37 AM
                        4 responses
                        18 views
                        0 likes
                        Last Post RookieTrader  
                        Started by PaulMohn, Today, 12:36 PM
                        0 responses
                        8 views
                        0 likes
                        Last Post PaulMohn  
                        Started by love2code2trade, 04-17-2024, 01:45 PM
                        4 responses
                        41 views
                        0 likes
                        Last Post love2code2trade  
                        Working...
                        X