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

Drawing Naked Weekly OHLC Levels

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

    Drawing Naked Weekly OHLC Levels

    Hello,
    I've been trying to draw the weekly OHLC levels. I am currently trying just to get the weekly Open to plot a horizontal line from the Open and if the price breaks/touches that level during the week it will stop plotting into the future. Every time I change something to fix the error it loops back to another mistake.

    If anyone can help with a possible idea to rectify this I would be greatly appreciative. Thank you for your time.

    James

    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 PreviousWeekOpenTouch : Indicator
    {
    private double previousWeekOpen;
    private List<double> touchedPrices = new List<double>();
    private bool historicalLevelsShown = false;
    private List<DrawnLine> horizontalLines = new List<DrawnLine>();

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Plot horizontal line at previous week open and stop when touched.";
    Name = "Previous Week Open Touch";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    }
    else if (State == State.Configure)
    {
    // Set the secondary data series to the weekly bar interval
    AddDataSeries(Data.BarsPeriodType.Week, 1);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress == 1)
    {
    // Calculate the weekly open value on the secondary data series (weekly bars)
    if (CurrentBar > 0 && Times[1][0].Date.DayOfWeek < Times[1][1].Date.DayOfWeek)
    previousWeekOpen = Open[1];
    }
    else if (BarsInProgress == 0)
    {
    // Check if it's the start of a new week on the primary chart
    if (CurrentBar > 0 && Times[0][0].Date.DayOfWeek < Times[0][1].Date.DayOfWeek)
    {
    // Add touched level to the historical list (if any)
    if (!double.IsNaN(previousWeekOpen) && Close[1] <= previousWeekOpen)
    {
    touchedPrices.Add(previousWeekOpen);
    }
    }

    // Draw the line at the previous week's open
    DrawHorizontalLine("PreviousWeekOpen", previousWeekOpen, Brushes.Blue);

    // Check if the line has been touched by the close of the next week's bar
    if (CurrentBar > 1 && Close[1] <= previousWeekOpen)
    {
    if (!historicalLevelsShown)
    {
    // Add touched level to the historical list (if any)
    touchedPrices.Add(previousWeekOpen);
    }

    historicalLevelsShown = true;
    }
    else
    {
    historicalLevelsShown = false;
    }

    // Plot historical touched levels
    for (int i = 0; i < touchedPrices.Count; i++)
    {
    DrawHorizontalLine("TouchedPriceLine" + i, touchedPrices[i], Brushes.Red, DashStyles.Dot);
    }
    }
    }

    private void DrawHorizontalLine(string tag, double y, System.Windows.Media.Brush brush, System.Windows.Media.DashStyle dashStyle = null)
    {
    string tagUnique = tag + CurrentBar;
    // Remove previous line
    DrawnLine previousLine = horizontalLines.Find(line => line.Tag == tagUnique);
    if (previousLine != null)
    {
    Draw.RemoveObject(this, previousLine.Tag);
    horizontalLines.Remove(previousLine);
    }

    Draw.Line(this, tagUnique, false, 0, y, 1, y, brush, dashStyle ?? DashStyles.Solid);
    horizontalLines.Add(new DrawnLine(tagUnique));
    }

    private class DrawnLine
    {
    public string Tag { get; }

    public DrawnLine(string tag)
    {
    Tag = tag;
    }
    }
    }










    }

    region NinjaScript generated code. Neither change nor remove.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private PreviousWeekOpenTouch[] cachePreviousWeekOpenTouch;
    public PreviousWeekOpenTouch PreviousWeekOpenTouch()
    {
    return PreviousWeekOpenTouch(Input);
    }

    public PreviousWeekOpenTouch PreviousWeekOpenTouch(ISeries<double> input)
    {
    if (cachePreviousWeekOpenTouch != null)
    for (int idx = 0; idx < cachePreviousWeekOpenTouch.Length; idx++)
    if (cachePreviousWeekOpenTouch[idx] != null && cachePreviousWeekOpenTouch[idx].EqualsInput(input))
    return cachePreviousWeekOpenTouch[idx];
    return CacheIndicator<PreviousWeekOpenTouch>(new PreviousWeekOpenTouch(), input, ref cachePreviousWeekOpenTouch);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.PreviousWeekOpenTouch PreviousWeekOpenTouch()
    {
    return indicator.PreviousWeekOpenTouch(Input);
    }

    public Indicators.PreviousWeekOpenTouch PreviousWeekOpenTouch(ISeries<double> input )
    {
    return indicator.PreviousWeekOpenTouch(input);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.PreviousWeekOpenTouch PreviousWeekOpenTouch()
    {
    return indicator.PreviousWeekOpenTouch(Input);
    }

    public Indicators.PreviousWeekOpenTouch PreviousWeekOpenTouch(ISeries<double> input )
    {
    return indicator.PreviousWeekOpenTouch(input);
    }
    }
    }

    #endregion

    #2
    Hello laoshr,

    Thanks for your post.

    I see that you are calling Draw.RemoveObject() in your script but this is not a valid NinjaScript method.

    To remove a draw object from a script the RemoveDrawObject() method should be used.

    See this help guide page for more information about RemoveDrawObject() and sample code: https://ninjatrader.com/support/help...drawobject.htm

    Here is a reference sample from the help guide you could view as well that demonstrates removing draw objects from a chart: https://ninjatrader.com/support/help...s_from_the.htm

    You should also ensure that you are using the correct arguments when calling Draw.Line() in your code. To see the syntax for this method, you could hover your mouse over 'Draw.Line' and view the intellisense information that appears.

    Here is a help guide page for Draw.Line() for more information: https://ninjatrader.com/support/help.../draw_line.htm

    To draw a horizontal line on the chart, you could consider using Draw.HorizontalLine() instead of Draw.Line().

    Draw.HorizontalLine(): https://ninjatrader.com/support/help...zontalline.htm

    Further, if the script is not behaving as expected, it is necessary to add debugging prints to the script to see exactly how your custom logic is evaluating. Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Brandon,
      Thank you for your help. I will work to implement those changes and see what it looks like.
      Thank you again for your time.
      James

      Comment


        #4
        Hey Brandon,
        I was wondering if there is an easy way to change the default OHLC indicator to plot the lines Globally.
        Thank you for your time.
        James

        Comment


          #5
          Hello James,

          Thanks for your notes.

          To make a draw object global, you would need to use the syntax that lets you set the isGlobal argument to true.

          The Draw.HorizontalLine() syntax that lets you set the isGlobal argument is:

          Draw.HorizontalLine(NinjaScriptBase owner, string tag, double y, bool isGlobal, string templateName)

          For the templateName argument in the syntax above, you would need to create a horizontal line drawing object template and specify the name of the template you want to use when calling Draw.HorizontalLine().

          See the help guide documentation below for more information.

          Draw.HorizontalLine(): https://ninjatrader.com/support/help...zontalline.htm
          Creating Drawing Object Templates: https://ninjatrader.com/support/help...bjectTemplates
          Brandon H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Segwin, 05-07-2018, 02:15 PM
          14 responses
          1,789 views
          0 likes
          Last Post aligator  
          Started by Jimmyk, 01-26-2018, 05:19 AM
          6 responses
          837 views
          0 likes
          Last Post emuns
          by emuns
           
          Started by jxs_xrj, 01-12-2020, 09:49 AM
          6 responses
          3,294 views
          1 like
          Last Post jgualdronc  
          Started by Touch-Ups, Today, 10:36 AM
          0 responses
          13 views
          0 likes
          Last Post Touch-Ups  
          Started by geddyisodin, 04-25-2024, 05:20 AM
          11 responses
          63 views
          0 likes
          Last Post halgo_boulder  
          Working...
          X