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't get Started: Line Drawing

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

    Can't get Started: Line Drawing

    I can't seem to get started with simple drawing tools (2 weeks of searching has only left me frustrated).
    While the final script will be more complicated, for the sake of this discussion, I simply want to write a script to draw a line from point A to point B on a chart.
    Is there a tutorial or other resource that presents:
    1) What is the sequence of events that occurs when I select my drawing tool?
    2) Once selected, how to I set my anchors?
    3) Once anchors are set, what is called that actually draws the line?

    I'm an experienced coder with C# experience but new to NinjaScript. I started with the new drawing tool wizard, tried tracing several existing line drawing tool scripts but I just can't figure out how this all plays together.

    Thanks for any help.

    #2


    https://ninjatraderecosystem.com/use...-mahtoolbar-2/

    This script was written with the idea of speeding up the process of drawing lines during my day trading. This drawing tool can do pretty much everything directly from the context menu now. The only thing it cannot do is set/unset drawing to global. Feature request SFT-948 is already in place for this. I am […]


    IDrawingTool is for drawing points on chart.
    The various states of Drawing : public override void OnMouseDown(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, Point point)
    {
    switch(DrawingState)
    {
    case DrawingState.Normal:
    DrawingState = DrawingState.Editing; // set state to allow editing
    break;
    case DrawingState.Editing:
    // do your edits here
    break;
    case DrawingState.Moving:
    return; // don't allow move when editing
    }

    }
    anchors

    //move the chart anchors x and y values
    MyAnchor.MoveAnchor(lastPoint, newPoint, chartControl, chartPanel, chartScale, this);

    To MoveAnchor x and MoveAnchor y is under ChartAnchor() in the guide with various elements explained in the NinjaTrader 8 Guide.


    Draw Line: Drawline(). or using
    SharpDX.Direct2D1.RenderTarget.DrawLine() Previous pageReturn to chapter overviewNext page
    TheBeans236 , i hope this can help
    Last edited by Emma1; 08-01-2020, 08:00 PM.

    Comment


      #3
      Thank you, Emma1.

      It does help, however I fear my problem appears to be more basic.

      OnMouseDown is never called. In fact, none of my routines are called except OnStateChange with State = SetDefaults when I select the tool. Here's my complete list of functions. In each is a print statement for tracing. None of these routines are called (with the exception of OnStateChange).
      . OnStateChange
      . GetCursor
      . GetSelectionPoints
      . OnCalculateMinMax
      . OnMouseDown
      . OnMouseMove
      . OnMouseUp
      . OnRender

      There's something very basic I'm missing, but I just can't find it.
      Here's the code:

      Code:
      namespace NinjaTrader.NinjaScript.DrawingTools
      {
          /* -----------------------------------------------------------------------------------
           *    Define logLine Drawing Tool
           *----------------------------------------------------------------------------------*/
          [EditorBrowsable(EditorBrowsableState.Always)]
          public class LogLines : DrawingTool
          {
              public bool gbTraceFunct = true;
      
              // Create the Anchor Collection
              public IEnumerable<ChartAnchor> Anchors { get { return new[] { StartAnchor, EndAnchor }; } }
              [Display(Order = 1)]
              public ChartAnchor    StartAnchor        { get; set; }
              [Display(Order = 2)]
              public ChartAnchor    EndAnchor        { get; set; }
      
              protected override void OnStateChange()
              {
                  if (gbTraceFunct) Print ("LogLines OnStateChage called. State = " + State);
                  if (State == State.SetDefaults)
                  {
                      Description                    = @"LogLines Drawing Tool";
                      Name                        = "LogLines";
                  }
                  else if (State == State.Configure)
                  {
                  }
              }
      
              public override Cursor GetCursor(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, Point point)
              {
                  if (gbTraceFunct) Print ("Log GetCursor Called. Drawing State = " + DrawingState);
                  switch (DrawingState)
                  {
                      case DrawingState.Building:    return Cursors.Pen;
                      case DrawingState.Moving:    return Cursors.SizeAll;
                      default:                    return Cursors.Pen;
                  }
              }
      
              public override Point[] GetSelectionPoints(ChartControl chartControl, ChartScale chartScale)
              {
                  if (gbTraceFunct) Print ("GetSelectionPoints " + DrawingState);
                  return new Point[0]; // add your custom selection point logic here
              }
      
              public override void OnCalculateMinMax()
              {
                  Print ("LogLines: OnCalculateMinMax " + DrawingState);
                  // 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)
              {
                  if (gbTraceFunct) Print ("OnMouseDown " + DrawingState);
                  switch (DrawingState)
                  {
                      case DrawingState.Building:            break;
                      case DrawingState.Editing:            break;
                      case DrawingState.Moving:            // Don't allow move while editing
                          return;
                          break;
                      case DrawingState.Normal:
                          DrawingState = DrawingState.Editing;
                          break;
                      default:                        break;
                  }
                  return;
                  // add any logic for when the mouse is left clicked here
              }
      
              public override void OnMouseMove(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, ChartAnchor dataPoint)
              {
                  if (gbTraceFunct) Print ("OnMouseMove " + DrawingState);
                  // add any logic for when the mouse is moved here
              }
      
              public override void OnMouseUp(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, ChartAnchor dataPoint)
              {
                  if (gbTraceFunct) Print ("OnMouseUp " + DrawingState);
                  // add any logic for when the mouse left button is released here
              }
      
              /* ---------------------------------------------------------------------------------------------
               *    Function:    OnRender
               *    Purpose:    Update where the object (line) is drawn
               *                This is called whenever the chart redisplays (e.g. new bar)
               *    Inputs:        Chart Control     : X
               *                Chart Scale        : Y
               *    Process:    Move lines to new position on chart
               *    Outputs:    None
               *    Notes:
               *--------------------------------------------------------------------------------------------*/
              public override void OnRender(ChartControl chartControl, ChartScale chartScale)
              {
                  if (gbTraceFunct) Print ("OnRender");
                  foreach (ChartAnchor anchor in Anchors)
                  {
                      Print (anchor.DisplayName + "  Slot Index: " + anchor.SlotIndex);
                  }
              }
      
              public override object Icon            { get { return Gui.Tools.Icons.DrawLineTool; } }
      
      
              #region Properties
              [NinjaScriptProperty]
              [Range(3, int.MaxValue)]
              [Display(Name="NumLines", Description="# of Lines to Draw", Order=1, GroupName="Parameters")]
              public int NumLines
              { get; set; }
              #endregion
      
          }
      }
      Thanks!
      Bean (TheBean236)

      Comment


        #4
        Hello TheBean236,

        There are not any tutorials on creating drawing objects, or any other advanced script.

        The @Lines.cs within the DrawingTools is an example.
        I've made a version of this that separates this class from the other classes in that file.



        When you state the public override void OnMouseDown() is not being run when clicking the chart, is this because an error is occuring and appearing on the Log tab of the Control Center?

        How do you know this is not running, have you added a print?

        This is running in the example script, so we know that the method does run when implemented correctly.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you Chelsea

          I examined your examples and wasn't able to locate my issue. Yes, I do have Prints in each of the modules.

          I know it works when done correctly, but after 2 weeks or searching, I just can't find the problem. And honestly, drawing a simple line between two points on a chart shouldn't be this complicated.

          I'm giving up for now, but sincerely, thank you for the effort.

          Bean

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by yertle, Yesterday, 08:38 AM
          7 responses
          28 views
          0 likes
          Last Post yertle
          by yertle
           
          Started by bmartz, 03-12-2024, 06:12 AM
          2 responses
          21 views
          0 likes
          Last Post bmartz
          by bmartz
           
          Started by funk10101, Today, 12:02 AM
          0 responses
          5 views
          0 likes
          Last Post funk10101  
          Started by gravdigaz6, Yesterday, 11:40 PM
          1 response
          9 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by MarianApalaghiei, Yesterday, 10:49 PM
          3 responses
          11 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X