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

Can I convert or change AddPlot into Draw.Line?

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

    Can I convert or change AddPlot into Draw.Line?

    Hi, I would like to change the "AddPlot" to "Draw.Line" because I don't like Dash, Line due to the noise every new open week, day, etc and I don't like Solid, Hash because there is much noise in the chart.

    I sent 2 pictures for examples and the indicator code in the CurrentWeekOHL.cs file.
    Especially I would like to CurrenWeekOpen Line, but if it is possible in the 3 lines is better

    The goal is to draw a line at the opening of each week.

    The indicator is inside Indicators/MyIndicators

    Thank you very much for your help!!!

    This is the 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.MyIndicators
    {
    public class CurrentWeekOHL : Indicator
    {
    private DayOfWeek startWeekFromDay = DayOfWeek.Monday;
    private DateTime currentDate = Core.Globals.MinDate;
    private DateTime lastDate = Core.Globals.MinDate;
    private double currentWeekOpen = 0;
    private double currentWeekHigh = 0;
    private double currentWeekLow = 0;
    private Data.SessionIterator sessionIterator;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"CurrentWeekOHL based on CurrentDayOHL indicator";
    Name = "CurrentWeekOHL";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    IsAutoScale = false;
    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;
    // Parameters
    ShowCurrentWeekOpen = true;
    ShowCurrentWeekHigh = true;
    ShowCurrentWeekLow = true;
    StartWeekFromDay = DayOfWeek.Monday;
    // Plots
    AddPlot(new Stroke(Brushes.DarkGoldenrod, DashStyleHelper.Dot, 3), PlotStyle.Square, "CurrentWeekOpen");
    AddPlot(new Stroke(Brushes.DarkGreen, DashStyleHelper.Dot, 3), PlotStyle.Square, "CurrentWeekHigh");
    AddPlot(new Stroke(Brushes.IndianRed, DashStyleHelper.Dot, 3), PlotStyle.Square, "CurrentWeekLow");

    }
    else if (State == State.Configure)
    {
    currentDate = Core.Globals.MinDate;
    currentWeekOpen = double.MinValue;
    currentWeekHigh = double.MinValue;
    currentWeekLow = double.MaxValue;
    sessionIterator = null;
    }
    else if (State == State.DataLoaded)
    {
    sessionIterator = new Data.SessionIterator(Bars);
    }
    }

    protected override void OnBarUpdate()
    {
    if (!Bars.BarsType.IsIntraday) return;
    lastDate = currentDate;
    if (sessionIterator.GetTradingDay(Time[0]).DayOfWeek == startWeekFromDay) {
    currentDate = sessionIterator.GetTradingDay(Time[0]);
    }
    if (lastDate != currentDate || currentWeekOpen == double.MinValue){
    currentWeekOpen = Open[0];
    currentWeekHigh = High[0];
    currentWeekLow = Low[0];
    }
    currentWeekHigh = Math.Max(currentWeekHigh, High[0]);
    currentWeekLow = Math.Min(currentWeekLow, Low[0]);
    if (ShowCurrentWeekOpen) { CurrentWeekOpen[0] = currentWeekOpen; }
    if (ShowCurrentWeekHigh) { CurrentWeekHigh[0] = currentWeekHigh; }
    if (ShowCurrentWeekLow) { CurrentWeekLow[0] = currentWeekLow; }
    }

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

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

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

    [NinjaScriptProperty]
    [Display(Name="ShowCurrentWeekOpen", Order=1, GroupName="Parameters")]
    public bool ShowCurrentWeekOpen
    { get; set; }

    [NinjaScriptProperty]
    [Display(Name="ShowCurrentWeekHigh", Order=2, GroupName="Parameters")]
    public bool ShowCurrentWeekHigh
    { get; set; }

    [NinjaScriptProperty]
    [Display(Name="ShowCurrentWeekLow", Order=3, GroupName="Parameters")]
    public bool ShowCurrentWeekLow
    { get; set; }

    [NinjaScriptProperty]
    [Display(Name="StartWeekFromDay", Order=5, GroupName="Parameters")]
    public DayOfWeek StartWeekFromDay
    {
    get { return startWeekFromDay; }
    set { startWeekFromDay = value; }
    }
    #endregion
    }
    }
    Attached Files
    Last edited by joselube001; 12-04-2021, 08:54 PM.

    #2
    Hello joselube001,

    Lines can be drawn with Draw.Line() in OnBarUpdate. These will need a start and end bar, as well as a start and end price.

    Below is a link to the help guide.


    I am also including a link to a forum post with helpful information about getting started with NinjaScript and C#.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello joselube001,

      Lines can be drawn with Draw.Line() in OnBarUpdate. These will need a start and end bar, as well as a start and end price.

      Below is a link to the help guide.


      I am also including a link to a forum post with helpful information about getting started with NinjaScript and C#.
      https://ninjatrader.com/support/foru...040#post786040
      Hello Chelsea! Thank you for your answer!

      I made a small code that works but there is a small problem, I have to use +5 extra minutes if I use this in a 5-minute chart. The same if I want to use it in a 1-minute chart, I need to add +1 minute extra, if I want to use it in 30-minute chart I have to add +30-minute extra!!

      This is not working, nothing happens, but is what I want to use it with the same time (18:00) for in different charts:
      if (Time[0].DayOfWeek == DayOfWeek.Sunday)
      if (ToTime(Time[0]) == ToTime(18, 00, 00))
      {Draw.Line(this, "AperturaDomingo" + Time[0], false, Time[0], Open[0], Time[0].AddDays(7.0), Open[0], Brushes.Red, DashStyleHelper.Dash, 2);}

      Is there any way to configure the line to start with the next bar opened? I ask for this because I see that the problem is that the current bar isn't correct, if the indicator can take the next bar, I would use the same hour (18:00) for all charts.

      Do you know how can I solve this problem?


      Examples:
      This work but I have to use 18:05 in a 5-minute chart
      if (Time[0].DayOfWeek == DayOfWeek.Sunday)
      if (ToTime(Time[0]) == ToTime(18, 05, 00))
      {Draw.Line(this, "AperturaDomingo" + Time[0], false, Time[0], Open[0], Time[0].AddDays(7.0), Open[0], Brushes.Red, DashStyleHelper.Dash, 2);}

      This work but I have to use 18:30 in a 30-minute chart
      if (Time[0].DayOfWeek == DayOfWeek.Sunday)
      if (ToTime(Time[0]) == ToTime(18, 30, 00))
      {Draw.Line(this, "AperturaDomingo" + Time[0], false, Time[0], Open[0], Time[0].AddDays(7.0), Open[0], Brushes.Red, DashStyleHelper.Dash, 2);}

      Thank you very much for your help!!

      Click image for larger version

Name:	Issue with draw line in open market 5minute.png
Views:	252
Size:	45.2 KB
ID:	1180569Click image for larger version

Name:	Issue with draw line in open market 30minute.png
Views:	258
Size:	45.4 KB
ID:	1180568
      Attached Files

      Comment


        #4
        Hello joselube001,

        You could use barsAgo values instead. One with 0, the other with Bars.GetBar() to specify a bar with the time.


        When you say it doesn't work, are you not seeing the drawn object in the Drawing objects window? Or does your line not have the times you are expecting?
        What specifically isn't working?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello joselube001,

          You could use barsAgo values instead. One with 0, the other with Bars.GetBar() to specify a bar with the time.


          When you say it doesn't work, are you not seeing the drawn object in the Drawing objects window? Or does your line not have the times you are expecting?
          What specifically isn't working?
          When I say it doesn't work, I'm not seeing the drawn object in the Drawing objects window, and in the chart the line disappears.
          Click image for larger version

Name:	18.00.png
Views:	257
Size:	54.2 KB
ID:	1180574
          Click image for larger version

Name:	18.05.png
Views:	248
Size:	59.6 KB
ID:	1180576
          Attached Files

          Comment


            #6
            Hello joselube001,

            May we have a screenshot of the Drawing objects window to confirm no drawing objects are appearing?

            The tag name of the object will start with "AperturaDomingo" followed by the time.

            If the object is not appearing in the Drawing objects window list, this would imply that the Draw method was not called.

            Use prints to find out why the condition has not evaluated as true.

            Below is a link to a forum post that demonstrates using prints to understand behavior.


            In the screenshots you have posted, what is this showing? I see some window is open and 18:05 is highlighted, but what is this? If this is not the drawing objects window is this the Indicators window with some indicator that has a time input?

            Why have you highlighted 18:05?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello again!
              I will send you a couple of pictures with the Drawing objects window...

              The opened window showing 18:05 is from the indicator window that I'm writing to show configurations like Opening time, Color, DashStyle, Width, etc.

              I will send you the indicator too.

              I don't know how to say to the indicator than read the next open candle and draw a line from this open candle

              In this way, I think that the indicator will work in all charts with the same hour configured because right now if I change from 30 minutes to 60 minutes the drawn lines disappear, and if I change from 30 minutes to 5 minutes the drawn lines is not drawn where it should be.

              Thank you very much!
              Click image for larger version

Name:	Is not showing drawn lines (18.00).png
Views:	280
Size:	69.4 KB
ID:	1180683Click image for larger version

Name:	Is showing drawn lines (18.30).png
Views:	241
Size:	80.4 KB
ID:	1180684

              Comment


                #8
                Hello joselube001,

                The screenshot you have provided is showing ApeturaDomingo5/12/2021 18:3...

                This seems to imply that the object is being drawn. You have previously stated that the drawing object does not appear in the Drawing Objects window, is this still the case?

                Where you have mentioned:
                I don't know how to say to the indicator than read the next open candle and draw a line from this open candle
                You can use a bool to trigger an action on the next bar. When the condition is true, set the bool to true. The next time OnBarUpdate() updates check the bool is true, draw the object, and set the bool back to false for the next time the condition is true.

                If you want to draw a line from the current bar back to the open of the session, you can save the CurrentBar number to a variable when Bars.IsFirstBarOfSession is true.
                Code:
                int savedFirstBarOfSession;
                (edited)
                if (Bars.FirstBarOfSesssion)
                savedFirstBarOfSession = CurrentBar;
                
                //int barsAgoFromSessionOpen = CurrentBar - savedFirstBarOfSession;
                
                Draw.Line(this, "myLine", CurrentBar - savedFirstBarOfSession, 100, 0, 100, Brushes.Blue);
                Last edited by NinjaTrader_ChelseaB; 12-06-2021, 04:46 PM.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  You have mentioned:
                  The screenshot you have provided is showing ApeturaDomingo5/12/2021 18:3...

                  This seems to imply that the object is being drawn. You have previously stated that the drawing object does not appear in the Drawing Objects window, is this still the case?
                  Is still the case if I configure the indicator in 18:00 for any chart and that should be the correct time parameter to winter time.

                  You have mentioned:
                  You can use a bool to trigger an action on the next bar. When the condition is true, set the bool to true. The next time OnBarUpdate() updates check the bool is true, draw the object, and set the bool back to false for the next time the condition is true.
                  I don't know how to do this, excuse me please.

                  You have mentioned this code:
                  Code:
                  int savedFirstBarOfSession; if (Bars.IsFirstBarOfSesssion) savedFirstBarOfSession = CurrentBar; //int barsAgoFromSessionOpen = CurrentBar - savedFirstBarOfSession; Draw.Line(this, "myLine", CurrentBar - savedFirstBarOfSession, 100, 0, 100, Brushes.Blue);
                  I load this code and show an error, error code CS1061: 'NinjaTrader.Data.Bars' does not contain a definition of 'IsFirstBarOfSesssion'...

                  If it is possible you can import the indicator and load it on a 30 minute chart Candlestick and press "Apply", you will see the drawn lines, after in indicator Properties configure the first parameter "Hora de apertura del mercado" (Market opening time) from 18:30 to 18:00 and you see what happens (All drawn lines by the indicator will disappear), please check it.

                  Now what I want is that when you set "Hora de apertura del mercado" (Market opening time) to 18:00 it does what it does when it is set to 18:30

                  I hope you understand this explanation. thanks for your patience and understanding!

                  Simplifying, what I want as a base is that regardless of the chart time, draw a line from the market opening to the market closing.


                  Something similar to this code, but this code with that time isn't drawing the line right now:
                  Code:
                  if (Time[0].DayOfWeek == DayOfWeek.Sunday)
                  if (ToTime(Time[0]) == ToTime([B]18, 00, 00[/B]))
                  {Draw.Line(this, "AperturaDomingo" + Time[0], false, Time[0], Open[0], Time[0].AddDays(1.0), Open[0], Brushes.Red, DashStyleHelper.Dash, 2);}

                  Comment


                    #10
                    Hello joselube001,

                    Do any drawing objects with the tag name starting with ApeturaDomingo appear in the Drawing Objects window? You have previously stated they do not.

                    As an example of a bool used to trigger an action on the next bar.

                    In the scope of the class:
                    private bool myTriggerBool;

                    In OnBarUpdate():

                    if (myTriggerBool == true)
                    {
                    Draw.Line(this, "MyLine", 0, 0, 0, 0, Brushes.Blue);
                    myTriggerBool = false;
                    }

                    if (Close[0] > Open[0])
                    {
                    myTriggerBool = true;
                    }

                    Apologies, that should be Bars.FirstTickOfBar as defined in the help guide page I have linked.

                    As far as drawing a line from 18:00 to the current bar, there are multiple ways to acheive this.

                    You can use Bars.GetBar() to get a bar on the same day at 18:00, if there is a bar that closes at 18:00. Otherwise it will be the first bar after 18:00 if there is no bar that closes at exactly 18:00.

                    int startBar = Bars.GetBar(new DateTime(Core.Globals.Now.Year, Core.Globals.Now.Month, Core.Globals.Now.Day, 18, 0, 0));
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Chelsea!
                      Thank you for your replies!

                      Finally, I found the correct code that I was searching, based on SessionIterator:
                      https://ninjatrader.com/support/help...oniterator.htm

                      I found this from your answer to another post to one person with the same needs:
                      https://ninjatrader.com/support/foru...reference-line

                      You have mentioned in one answer of the post mentioned early:
                      Are you wanting to get the time of a session open from a SessionIterator?
                      And after I was looking at this code in the Help Guide, and I found the answer.

                      Below I attached the compiled indicator and an example picture.

                      Here is the complete base code if someone more like me want to programing the same or something similar:
                      The red text is the code added to show properly every opening day.

                      Code:
                      //This namespace holds Indicators in this folder and is required. Do not change it.
                      namespace NinjaTrader.NinjaScript.Indicators
                      {
                      public class DailyOpening : Indicator
                      {
                      [B][COLOR=#e74c3c]private SessionIterator sessionIterator;[/COLOR][/B]
                      
                      protected override void OnStateChange()
                      {
                      
                      [B][COLOR=#e74c3c]if (State == State.DataLoaded)
                      {
                      sessionIterator = new SessionIterator(Bars);
                      }[/COLOR][/B]
                      
                      if (State == State.SetDefaults)
                      {
                      Description = @"";
                      Name = "Daily Opening";
                      Calculate = Calculate.OnBarClose;
                      IsOverlay = true;
                      IsAutoScale = false;
                      DisplayInDataBox = false;
                      DrawOnPricePanel = true;
                      DrawHorizontalGridLines = true;
                      DrawVerticalGridLines = true;
                      PaintPriceMarkers = false;
                      ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                      IsSuspendedWhileInactive = true;
                      }
                      }
                      
                      protected override void OnBarUpdate()
                      [B][COLOR=#e74c3c]{
                      if (Bars.IsFirstBarOfSession)
                      {
                      sessionIterator.GetNextSession(Time[0], true);[/COLOR][/B]
                      {Draw.Line(this, "OpenLine" + Time[0], false, Time[0], Open[0], Time[0].AddDays(1.0), Open[0], Brushes.Red, DashStyleHelper.Dash, 2);}
                      }
                      }
                      }
                      }
                      If someone wants everyday separate need to add the next code before every Draw.Line:

                      Code:
                      [B][COLOR=#e74c3c]if (Time[0].DayOfWeek == DayOfWeek.Sunday)[/COLOR][/B]
                      For example: If I only want to see the Open Line on Sunday and Monday and change the name of the line I need to add this code:
                      Code:
                      [B][COLOR=#e74c3c]if (Time[0].DayOfWeek == DayOfWeek.Sunday)[/COLOR][/B]
                      {Draw.Line(this, "[COLOR=#e74c3c][B]OpenLineSunday[/B][/COLOR]" + Time[0], false, Time[0], Open[0], Time[0].AddDays(1.0), Open[0], Brushes.Red, DashStyleHelper.Dash, 2);}
                      [B][COLOR=#e74c3c]if (Time[0].DayOfWeek == DayOfWeek.Monday)[/COLOR][/B]
                      {Draw.Line(this, "[B][COLOR=#e74c3c]OpenLineMonday[/COLOR][/B]" + Time[0], false, Time[0], Open[0], Time[0].AddDays(1.0), Open[0], Brushes.Red, DashStyleHelper.Dash, 2);}
                      I hope this helps others.
                      Click image for larger version  Name:	DailyOpening Line drawn.png Views:	0 Size:	48.8 KB ID:	1180965
                      Last edited by joselube001; 12-08-2021, 11:45 AM.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by algospoke, Yesterday, 06:40 PM
                      2 responses
                      23 views
                      0 likes
                      Last Post algospoke  
                      Started by ghoul, Today, 06:02 PM
                      3 responses
                      14 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by jeronymite, 04-12-2024, 04:26 PM
                      3 responses
                      45 views
                      0 likes
                      Last Post jeronymite  
                      Started by Barry Milan, Yesterday, 10:35 PM
                      7 responses
                      22 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by AttiM, 02-14-2024, 05:20 PM
                      10 responses
                      181 views
                      0 likes
                      Last Post jeronymite  
                      Working...
                      X