Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Newbie question -- custom drawing tool, print to output window

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

    Newbie question -- custom drawing tool, print to output window

    This is my very first attempt at creating a drawing tool so I thought I'd start with something very basic. I just want to print to the output window where a user has clicked on the chart -- in other words, the chart anchor. From there, I can start building, but I cannot get this to work! I tried the code snippet in the documentation but that doesn't seem to work for me.

    I'm probably missing something very basic, but I assumed this would work as follows: Select drawing tool (shows up fine) > click on the chart > see anchor time displayed in output window. None of that is happening, it's like my script isn't executing at all. Btw, no compile errors. Here's the code I'm using. ANY help would be appreciated since this is really basic and I'm just trying to learn how everything fits together. Thanks in advance :-)

    Code:
    //This namespace holds Drawing tools in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.DrawingTools
    {
    public class HRay : DrawingTool
    {
    public ChartAnchor myAnchor;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Draw horizontal ray";
    Name = "HRay";
    }
    else if (State == State.Configure)
    {
    }
    }
    
    public override Cursor GetCursor(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, Point point)
    {
    return Cursors.Arrow; // add your custom cursor logic here
    }
    
    public override Point[] GetSelectionPoints(ChartControl chartControl, ChartScale chartScale)
    {
    return new Point[0]; // add your custom selection point logic here
    }
    
    public override void OnCalculateMinMax()
    {
    // It is important to set MinValue and MaxValue to the min/max Y values your drawing tool uses if you want it to support auto scale
    }
    
    public override void OnMouseDown(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, ChartAnchor dataPoint)
    {
    NinjaTrader.Code.Output.Process("dataPoint: " + dataPoint, PrintTo.OutputTab1);
    Point myPoint = dataPoint.GetPoint(chartControl, chartPanel, chartScale);
    myAnchor.UpdateFromPoint(myPoint, chartControl, chartScale);
    NinjaTrader.Code.Output.Process("myAnchor: " + myAnchor.Time, PrintTo.OutputTab1);
    }
    
    public override void OnMouseMove(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, ChartAnchor dataPoint)
    {
    // add any logic for when the mouse is moved here
    }
    
    public override void OnMouseUp(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, ChartAnchor dataPoint)
    {
    // add any logic for when the mouse left button is released here
    }
    
    public override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    NinjaTrader.Code.Output.Process("myAnchor: " + myAnchor.Time, PrintTo.OutputTab1);
    }
    
    }
    }

    #2
    Hello asifbacchus,

    Thank you for the post.

    Creating a tool from scratch and then checking each override would be very difficult, I would suggest to just duplicate one of the existing tools so you have a starting point that is already working. You would need to do a lot of work initially to get anchors and other items that you would need in order to see the object on the chart, because of that using an existing tool is a better way to start.

    You could duplicate one of the tools which is similar to what your goal will be. For example you could start with the Line class from the Line tool as one starting point. There are more features to those objects but they already work and will be able to be added to a chart. An empty class like you have shown lacks anchors so it won't do anything beyond adding a menu item. You would really need the anchors logic and rendering logic in place before you can test the script on a chart.

    If you take a look at the Line class, line 98 in the Lines.cs file you can see where the starting of the anchors happens. That is missing from what you provided and is a core part of how anchors are defined.

    I look forward to being of further assistance.
    Last edited by NinjaTrader_Jesse; 12-02-2020, 08:15 AM.

    Comment


      #3
      Thanks for the quick response! Yes, seems I totally forgot to instantiate the anchor in the OnStateChage() section. Once I did that, I started getting output from Print().

      I've taken a look at Lines.cs and it has helped a little, however, it is overly complex for learning from scratch. I can figure out what it's doing, but there is so much going on (i.e. alerts, DirectX, etc.) that it makes it difficult to track what directly affects what and what order things need to be defined in. I have found that starting from scratch is helping me understand things a lot better but you are very right that it takes much more time.

      I really wish there was a short tutorial type series on just the basics -- not meant to generate a 'full-fledged' tool. For example Lesson 1: getting and setting anchors, Lesson 2: editing anchors, Lesson 3: checking for min/max, Lesson 4: drawing a line, etc. That way understanding could be built on the basics and creating drawing tools would be much simpler to understand -- that is what I'm trying to do by starting from scratch. These things exist for indicators, but nothing seems to exist to just help someone get started with drawing tools. It's just assumed you already know you need to instantiate anchors instead of using points directly, for example.

      Anyway, I appreciate the help. I'm sure I'll have more questions as my journey continues! Thanks again!

      Comment


        #4
        Hello asifbacchus,

        Yes with the drawing tools it is assumed that you will learn from the existing tools so there is no tutorial or other help content at this point.

        Taking one of the existing tools and reducing it is another option. The DirectX parts are going to be required if you want to see it because that's the only way to visualize a tool but you could reduce that to something much more simple. You can also remove a lot of the other features it has if you are just trying to understand the base framework.

        A good way to do what you were trying would be to duplicate an existing tool and leave its code alone aside from the renaming needed to compile and see it in the menu. Then add prints so that you can see how a working tools overrides and other areas function. That's a little better way to approach drawing objects because they heavily involve the mouse events and states which the tool is in, that lets you complete the logical loop the tool is expecting so you can get a more complete picture of how everything is involved.


        Please let me know if I may be of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        599 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        345 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        558 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        558 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X