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 line until end of canvas in mouse clicked event

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

    Drawing line until end of canvas in mouse clicked event

    ˇ Good morning!
    I want to draw a line in a chart from the point I click with the mouse until the end of the right side of the chart while the mouse button is clicked.

    I have this code written for the mouse event.

    Code:
    protected void MouseClicked(object sender, MouseButtonEventArgs e)
    {
        try
        {
    
            // convert e.GetPosition for different dpi settings
            clickPoint.X = ChartingExtensions.ConvertToHorizontalPixels(e.Get Position(ChartPanel as IInputElement).X, ChartControl.PresentationSource);
            clickPoint.Y = ChartingExtensions.ConvertToVerticalPixels(e.GetPo sition(ChartPanel as IInputElement).Y, ChartControl.PresentationSource);
    
            double precioenClick = Instrument.MasterInstrument.RoundToTickSize(chartS cale.GetValueByY((float)clickPoint.Y));
    
            Point punto2 = new Point(ChartControl.CanvasRight, chartScale.GetYByValue(precioenClick));
            RenderTarget.DrawLine(clickPoint.ToVector2(), punto2.ToVector2(), Brushes.Aqua.ToDxBrush(RenderTarget));​
    
            e.Handled = true;
        }
        catch (Exception exception)
        {
             // In case the indicator has already been Terminated, you can safely ignore errors
             if (State >= State.Terminated)
                  return;
    
             /* With our caught exception we are able to generate log entries that go to the Control Center logs and also print more detailed information
             about the error to the Output Window. */
    
            // Submits an entry into the Control Center logs to inform the user of an error
            Log(Name+ " MOUSECLICKED Error: Please check your indicator for errors.", NinjaTrader.Cbi.LogLevel.Warning);
    
            // Prints the caught exception in the Output Window
            Print(Name+ " ONMOUSE CLICK " +Time[0] + " " + exception.ToString()+exception.StackTrace);
        }
    
    }​
    The first tiem I click nothing happens. And the second time I get the error

    AaGranVolumenBAK ONMOUSE CLICK 07/11/2023 0:01:00 System.ArgumentNullException: Value cannot be null.
    ​ Parameter name: renderTarget
    ​ at NinjaTrader.Gui.DxExtensions.ToDxBrush(Brush brush, RenderTarget renderTarget, Single opacity)
    What am I doind wrong ?

    Thanks

    #2
    Hello hborlorin,

    Thanks for your notes.

    Please see this help guide page for information on how to use ToDxBrush in a script that uses SharpDX

    https://ninjatrader.com/support/help..._todxbrush.htm

    You could first define the WPF Brush in your script and then convert the Brush from a WPF to a SharpDXBrush in OnRender() to use with RenderTarget.DrawLine(). For example, the code might look something like this:

    //class level variable
    private Brush dxBrush;

    //OnStateChange() State.SetDefaults
    dxBrush = System.Windows.Media.Brushes.Aqua;

    //OnRender()
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    // convert user WPF selection to a DX brush
    SharpDX.Direct2D1.Brush dxBrush = TextBrush.ToDxBrush(RenderTarget);

    //...
    }


    Further, I do not see where you are properly defining the Vector2 variables in the script.

    You must use SharpDX.Vector2 to create the Vector2 arguments in the script to use for RenderTarget.DrawLine().

    For example:

    SharpDX.Vector2 startPoint = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y);
    SharpDX.Vector2 endPoint = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, ChartPanel.Y + ChartPanel.H);


    See the 'SharpDX Lines and Shapes' section of this help guide page for more information and sample code: https://ninjatrader.com/support/help...ing.htm​

    RenderTarget.DrawLine(): https://ninjatrader.com/support/help...t_drawline.htm
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by StockTrader88, 03-06-2021, 08:58 AM
    45 responses
    3,992 views
    3 likes
    Last Post johntraderuser2  
    Started by TAJTrades, Today, 09:46 AM
    0 responses
    7 views
    0 likes
    Last Post TAJTrades  
    Started by rhyminkevin, Yesterday, 04:58 PM
    5 responses
    62 views
    0 likes
    Last Post dp8282
    by dp8282
     
    Started by realblubb, Today, 09:28 AM
    0 responses
    8 views
    0 likes
    Last Post realblubb  
    Started by AaronKoRn, Yesterday, 09:49 PM
    1 response
    19 views
    0 likes
    Last Post Rikazkhan007  
    Working...
    X