Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Activate Indicator Only during certain Timeframes

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

    #16
    Hello tradingnasdaqprueba,

    For an indicator to be used as a signal it needs a plot. You can review the code for any indicator, for example the SMA. That provides a value as a plot.

    Comment


      #17
      Hello Jesse,

      I undertand, but the thing is I dont know what to plot in an Indicator with SharpDX renderization... Here the code:


      Code:
      namespace NinjaTrader.NinjaScript.Indicators
      {
      public class DirectXRenderizationPruebas : Indicator
      {
              // These are WPF Brushes which are pushed and exposed to the UI by default
              // And allow users to configure a custom value of their choice
              // We will later convert the user defined brush from the UI to SharpDX Brushes for rendering purposes
              private System.Windows.Media.Brush    areaBrush;
              private System.Windows.Media.Brush    textBrush;
              private System.Windows.Media.Brush    smallAreaBrush;
              private int                            areaOpacity;
              //private SMA                            mySma;
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                    = @"Enter the description for your new custom Indicator here.";
                      Name                        = "DirectXRenderizationPruebas";
                      Calculate                    = Calculate.OnEachTick;
                      DisplayInDataBox            = false;
                      IsOverlay                    = true;
                      IsChartOnly                    = true;
                      IsSuspendedWhileInactive    = true;
                      ScaleJustification            = ScaleJustification.Right;
                      AreaBrush = System.Windows.Media.Brushes.Crimson;
                      TextBrush = System.Windows.Media.Brushes.Red;
                      SmallAreaBrush = System.Windows.Media.Brushes.White;
                      AreaOpacity = 100;
                      //AddPlot(System.Windows.Media.Brushes.Crimson, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameSampleCustomRender);
                  }
                  else if (State == State.DataLoaded)
                  {
                      //mySma = SMA(20);
                  }
                  else if (State == State.Historical)
                  {
                      SetZOrder(-1); // default here is go below the bars and called in State.Historical
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  //Value[0] = mySma[0];
                  NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 10) { Size = 30, Bold = true };
                  Draw.TextFixed(this, "Text2", "Alert! - Economic News Timeframe!", TextPosition.Center, Brushes.Crimson, myFont, Brushes.Transparent, Brushes.White, 70);
              }
      
          protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
          {
      
              // This sample should be used along side the help guide educational resource on this topic:
              // http://www.ninjatrader.com/support/helpGuides/nt8/en-us/?using_sharpdx_for_custom_chart_rendering.htm
      
              // Default plotting in base class. Uncomment if indicators holds at least one plot
              // in this case we would expect NOT to see the SMA plot we have as well in this sample script
              //base.OnRender(chartControl, chartScale);
      
              // 1.1 - SharpDX Vectors and Charting RenderTarget Coordinates
      
              // The SharpDX SDK uses "Vector2" objects to describe a two-dimensional point of a device (X and Y coordinates)
      
              SharpDX.Vector2 startPoint;
              SharpDX.Vector2 endPoint;
      
              // For our custom script, we need a way to determine the Chart's RenderTarget coordinates to draw our custom shapes
              // This info can be found within the NinjaTrader.Gui.ChartPanel class.
              // You can also use various chartScale and chartControl members to calculate values relative to time and price
              // However, those concepts will not be discussed or used in this sample
              // Notes:  RenderTarget is always the full ChartPanel, so we need to be mindful which sub-ChartPanel we're dealing with
              // Always use ChartPanel X, Y, W, H - as chartScale and chartControl properties WPF units, so they can be drastically different depending on DPI set
      
              startPoint = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y);
              endPoint = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, ChartPanel.Y + ChartPanel.H);
      
              // These Vector2 objects are equivalent with WPF System.Windows.Point and can be used interchangeably depending on your requirements
              // For convenience, NinjaTrader provides a "ToVector2()" extension method to convert from WPF Points to SharpDX.Vector2
      
              SharpDX.Vector2 startPoint1 = new System.Windows.Point(ChartPanel.X, ChartPanel.Y + ChartPanel.H).ToVector2();
              SharpDX.Vector2 endPoint1 = new System.Windows.Point(ChartPanel.X + ChartPanel.W, ChartPanel.Y).ToVector2();
      
              // SharpDX.Vector2 objects contain X/Y properties which are helpful to recalculate new properties based on the initial vector
      
              float width = endPoint.X - startPoint.X;
              float height = endPoint.Y - startPoint.Y;
      
              // Or you can recalculate a new vector from existing vector objects
      
              SharpDX.Vector2 center = (startPoint + endPoint) / 2;
      
              // Tip: This check is simply added to prevent the Indicator dialog menu from opening as a user clicks on the chart
              // The default behavior is to open the Indicator dialog menu if a user double clicks on the indicator
              // (i.e, the indicator falls within the RenderTarget "hit testing")
              // You can remove this check if you want the default behavior implemented
      
              if (!IsInHitTest)
              {
                  // 1.2 - SharpDX Brush Resources
      
                  // RenderTarget commands must use a special brush resource defined in the SharpDX.Direct2D1 namespace
                  // These resources exist just like you will find in the WPF/Windows.System.Media namespace
                  // such as SolidColorBrushes, LienarGraidentBrushes, RadialGradientBrushes, etc.
                  // To begin, we will start with the most basic "Brush" type
                  // Warning:  Brush objects must be disposed of after they have been used
      
                  SharpDX.Direct2D1.Brush areaBrushDx;
                  SharpDX.Direct2D1.Brush smallAreaBrushDx;
                  SharpDX.Direct2D1.Brush textBrushDx;
      
                  // for convenience, you can simply convert a WPF Brush to a DXBrush using the ToDxBrush() extension method provided by NinjaTrader
                  // This is a common approach if you have a Brush property created e.g., on the UI you wish to use in custom rendering routines
      
                  areaBrushDx = areaBrush.ToDxBrush(RenderTarget);
                  smallAreaBrushDx = smallAreaBrush.ToDxBrush(RenderTarget);
                  textBrushDx = textBrush.ToDxBrush(RenderTarget);
      
                  // However - it should be noted that this conversion process can be rather expensive
                  // If you have many brushes being created, and are not tied to WPF resources
                  // You should rather favor creating the SharpDX Brush directly:
                  // Warning:  SolidColorBrush objects must be disposed of after they have been used
      
                  SharpDX.Direct2D1.SolidColorBrush customDXBrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget,
                      SharpDX.Color.Crimson);
      
                  // 1.3 - Using The RenderTarget
                  // before executing chart commands, you have the ability to describe how the RenderTarget should render
                  // for example, we can store the existing RenderTarget AntialiasMode mode
                  // then update the AntialiasMode to be the quality of non-text primitives are rendered
      
                  SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode;
                  RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased;
      
                  // Note: The code above stores the oldAntialiasMode as a best practices
                  // i.e., if you plan on changing a property of the RenderTarget, you should plan to set it back
                  // This is to make sure your requirements to no interfere with the function of another script
                  // Additionally smoothing has some performance impacts
      
                  #region Lines
                  
                  // Once you have defined all the necessary requirements for you object
                  //  You can execute a command on the RenderTarget to draw specific shapes
                  // e.g., we can now use the RenderTarget's DrawLine() command to render a line
                  // using the start/end points and areaBrushDx objects defined before
      
                  RenderTarget.DrawLine(startPoint, endPoint, areaBrushDx, 20);
      
                  // Since rendering occurs in a sequential fashion, after you have executed a command
                  // you can switch a property of the RenderTarget to meet other requirements
                  // For example, we can draw a second line now which uses a different AntialiasMode
                  // and the changes render on the chart for both lines from the time they received commands
      
                  RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.PerPrimitive;
                  RenderTarget.DrawLine(startPoint1, endPoint1, areaBrushDx, 20);
                  
                  #endregion
      
                  // 1.8 - Cleanup
                  // This concludes all of the rendering concepts used in the sample
                  // However - there are some final clean up processes we should always provided before we are done
      
                  // If changed, do not forget to set the AntialiasMode back to the default value as described above as a best practice
      
                  RenderTarget.AntialiasMode = oldAntialiasMode;
      
                  // We also need to make sure to dispose of every device dependent resource on each render pass
                  // Failure to dispose of these resources will eventually result in unnecessary amounts of memory being used on the chart
                  // Although the effects might not be obvious as first, if you see issues related to memory increasing over time
                  // Objects such as these should be inspected first
      
                  areaBrushDx.Dispose();
                  customDXBrush.Dispose();
                  smallAreaBrushDx.Dispose();
                  textBrushDx.Dispose();
      
              }
          }​

      Comment


        #18
        Hello tradingnasdaqprueba,

        If you are only using OnRender then you wouldn't have anything to plot, OnRender is the rendering outlet for the script. That override is not related to processing bar data, that is just used for showing data that has already been processed. You would need to calculate something inside OnBarUpdate to plot it.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        50 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        126 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        69 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X