• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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/

    https://ninjatraderecosystem.com/use...e-drawingtool/

    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.
        https://ninjatrader.com/support/foru...817#post798817


        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.
        https://ninjatrader.com/support/help...g_concepts.htm
        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 embreyw, Today, 05:56 AM
          0 responses
          1 view
          0 likes
          Last Post embreyw
          by embreyw
           
          Started by granitmaleaus, Today, 05:11 AM
          0 responses
          2 views
          0 likes
          Last Post granitmaleaus  
          Started by julianc05, Today, 12:37 AM
          1 response
          9 views
          0 likes
          Last Post Emma1
          by Emma1
           
          Started by fernlicht, Today, 01:02 AM
          1 response
          11 views
          0 likes
          Last Post Emma1
          by Emma1
           
          Started by JellyRoll, Yesterday, 06:57 PM
          1 response
          23 views
          0 likes
          Last Post bltdavid  
          Working...
          X