• 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

Draw.Text from Indicator used by Strategy - fail to draw text on chart

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

    Draw.Text from Indicator used by Strategy - fail to draw text on chart

    Hello.

    I am using the latest NT8.0.22 version.

    I have written an indicator that part of easier debug prints the Bar index on top of each bar and print Z1, Z2, Z3, etc. based on some algorithem.
    It also generates two data series to be used by the strategy that will use it

    The indicator itself draw text with no problem on the Chart (marked in Arrows two type of text drawing i do). However when i include this indicator i made to a strategy it will not draw any text from the strategy itself. Also it looks as if i am unable to extract the data from its two data series that the indicator generates (not 100% sure about this part).


    The function and variables in the indicator responsible to draw the text is:


    // ----------------- Text Fonts size & color -----------------
    private Brush textBrush = Brushes.DimGray;
    private Brush DrawText_ZigZagWeightBrush = Brushes.OrangeRed;
    private Gui.Tools.SimpleFont BarNumFont = new Gui.Tools.SimpleFont() { Size = 10 };
    private Gui.Tools.SimpleFont DrawText_ZigZagWeightFont = new Gui.Tools.SimpleFont() { Size = 14 };



    //================================================== =========
    // Draw text on Chart
    //================================================== =========
    private void Draw_BarNum(string text, int barsAgo, double y_Axis, int yOffset)
    {
    Draw.Text(this, text + CurrentBar, false, text, barsAgo, y_Axis, yOffset, textBrush, BarNumFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
    }




    code snippets from my strategy
    =======================
    public class DualZigZagReversalEK : Strategy
    {
    private NinjaTrader.NinjaScript.Indicators.EK.DualZigZag _DualZigZag;

    .........

    protected override void OnStateChange()
    {
    .....
    .....
    ....
    else if (State == State.DataLoaded)
    {
    _DualZigZag = DualZigZag(Close, 10, 20);



    When i try to debug it using Visual Studio i get the following when the indicator is calling my function "Draw_BarNum()" to text draw the bar.
    I press F11 in the debugger and enter into Draw.Text method.The method will exit on txt=null (not my text some internal vars).

    Any idea what is the problem?



    public static partial class Draw
    {
    private static Text TextCore(NinjaScriptBase owner, string tag, bool autoScale, string text,
    int barsAgo, DateTime time, double y, int? yPixelOffset, Brush textBrush, TextAlignment? textAlignment,
    Gui.Tools.SimpleFont font, Brush outlineBrush, Brush areaBrush, int? areaOpacity, bool isGlobal, string templateName,
    DashStyleHelper outlineDashStyle, int outlineWidth)
    {
    if (barsAgo == int.MinValue && time == Core.Globals.MinDate)
    throw new ArgumentException("Text: Bad barsAgo/time parameters");

    if (string.IsNullOrWhiteSpace(tag))
    throw new ArgumentException(@"tag cant be null or empty", "tag");

    if (isGlobal && tag[0] != GlobalDrawingToolManager.GlobalDrawingToolTagPrefi x)
    tag = GlobalDrawingToolManager.GlobalDrawingToolTagPrefi x + tag;

    Text txt = DrawingTool.GetByTagOrNew(owner, typeof(Text), tag, templateName) as Text;
    if (txt == null)
    return null; <----- txt is NULL so the method exit and dont continue to draw.



    #2
    Hello ezrakoper,

    At that moment the drawing object is draw from the strategy, are there any errors?

    After it is drawn, try looping through all of the drawing objects to see if the object you are expecting is in there.
    https://ninjatrader.com/support/help...rawobjects.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello.

      I have attached in zip file stripped versions of the indicator+strategy. The indicator does nothing beside using Draw.Text to show bar index on chart. And the Strategy does nothing beside loading the indicator which is supposed to print the bar index. However it will not do so (see 2nd image screenshot i had attached)

      Overall there is zero code involved. So why the strategy doesn't Draw.Text on chart??


      ================== Striped down version of the strategy. It ONLY load the indicator that all is doing is print bar index on top ===========
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class testdualzigzagdraw : Strategy
      {
      private NinjaTrader.NinjaScript.Indicators.EK.DualZigZagPr intOnlyBarIndex PrintBarIndex;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "testdualzigzagdraw";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      }
      else if (State == State.Configure)
      {
      }
      else if (State == State.DataLoaded)
      {
      PrintBarIndex = DualZigZagPrintOnlyBarIndex(Close, 10, 20);
      }
      }

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 10)
      return;

      // Do nothing. We need only that the indicator will print bar index on top of each candle bar
      }
      }
      }



      ====================== INDICATOR that only print bar index =================================
      namespace NinjaTrader.NinjaScript.Indicators.EK
      {
      public class DualZigZagPrintOnlyBarIndex : Indicator
      {
      // ----------------- Text Fonts size & color -----------------
      private Brush textBrush = Brushes.DimGray;
      private Gui.Tools.SimpleFont BarNumFont = new Gui.Tools.SimpleFont() { Size = 10 }; // font size used to print bar index above candle

      #region parameters_region


      //================================================== ===========
      // Input Parameters
      //================================================== ===========
      [Range(0, int.MaxValue), NinjaScriptProperty] // can be optimized
      [Display(ResourceType = typeof(Custom.Resource), Name = "Fast Deviation", GroupName = "ZigZag Parameters", Order = 1)]
      public double FastZigZag_Deviation
      { get; set; }

      [Range(0, int.MaxValue), NinjaScriptProperty] // can be optimized
      [Display(ResourceType = typeof(Custom.Resource), Name = "Slow Deviation", GroupName = "ZigZag Parameters", Order = 2)]
      public double SlowZigZag_Deviation
      { get; set; }

      #endregion //parameters_region

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"DualZigZagPrintOnlyBarIndex";
      Name = "DualZigZagPrintOnlyBarIndex";
      Calculate = Calculate.OnBarClose;
      IsOverlay = false; // true = draw on the graph itself. False = draw bellow the chart on a dedicated area
      IsAutoScale = true;
      }
      }

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0) // An index value of the current Bars. In a single Bars script this property will always return an index value of 0.
      return;
      if (CurrentBar < 20) // Lets some peace of the chart be made 1st before we start processing it
      return;

      Draw_BarNum("" + CurrentBar.ToString(), 0, High[0], +100); // Draw Bar index number above the Bar.
      }


      private void Draw_BarNum(string text, int barsAgo, double y_Axis, int yOffset)
      {
      Draw.Text(this, text + CurrentBar, false, text, barsAgo, y_Axis, yOffset, textBrush, BarNumFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
      }
      }
      }

      Attached Files

      Comment


        #4
        Hello.

        I have attached updated stripped down versions of indicator + strategy.

        I find out that if the strategy does nothing with the indicator then the Indicator OnBarUpdate isnt called at all. So i just added a series to the indicator that is updated with Bar Index. Also i changed the strategy that it will read the series from the indicator and print it on the Terminal window.

        As a result now the Indicator OnBarUpdate is called on every bar close.

        However still the Draw.Text() from the indicator to print for debug reasons the bar index on top of each candle bar doesn't print anything.
        Headless to say that my indicator fail also to print more meaningful info on the chart.

        Attached Files

        Comment


          #5
          Hello.

          It looks as if any drawing made from inside an indicator on a chart is totally not viable when a strategy is using the indicator.

          To prove this i did the following:
          I used Strategy builder ONLY to create a strategy using NT ZigZag indicator, printing "Hello" any time its series is greater then 1.
          When i run this test strategy on a chart, nothing is shown on the chart. I mean NO ZIGZAG lines that the indicator adds to chart when i add manually the indicator to the chart.

          This leads me to the conclusion that any drawing made by an indicator on a chart will not be viable when same indicator is used from within a strategy.

          Here is the test strategy i made with strategy builder to test it:

          namespace NinjaTrader.NinjaScript.Strategies
          {
          public class TestZigZag : Strategy
          {
          private ZigZag ZigZag1;

          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "TestZigZag";
          Calculate = Calculate.OnBarClose;
          EntriesPerDirection = 1;
          EntryHandling = EntryHandling.AllEntries;
          IsExitOnSessionCloseStrategy = true;
          ExitOnSessionCloseSeconds = 30;
          IsFillLimitOnTouch = false;
          MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
          OrderFillResolution = OrderFillResolution.Standard;
          Slippage = 0;
          StartBehavior = StartBehavior.WaitUntilFlat;
          TimeInForce = TimeInForce.Gtc;
          TraceOrders = false;
          RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
          StopTargetHandling = StopTargetHandling.PerEntryExecution;
          BarsRequiredToTrade = 20;
          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          }
          else if (State == State.Configure)
          {
          }
          else if (State == State.DataLoaded)
          {
          ZigZag1 = ZigZag(Close, DeviationType.Points, 0.5, false);
          }
          }

          protected override void OnBarUpdate()
          {
          if (BarsInProgress != 0)
          return;

          if (CurrentBars[0] < 0)
          return;

          // Set 1
          if (ZigZag1.ZigZagHigh[0] >= 1)
          {
          Print(@"hello");
          }

          }
          }
          }

          Comment


            #6
            Hello ezrakoper,

            Thanks for your replies.

            Correct, an added indicator to a strategy will not display on the chart, this is expected behavior.

            You would add an indicator to a strategy when you want to use that indicator in your strategy, for example using an EMA. However this only provides the value of the EMA to the strategy it would not add it to the display,

            To have the indicator display on the chart you would need to use the AddChartIndicator() method. Please see the help guide here: https://ninjatrader.com/support/help...tindicator.htm

            In addition, please see:

            https://ninjatrader.com/support/help...indicators.htm
            https://ninjatrader.com/support/help..._strategie.htm

            If you want to use an indicator in your strategy and have it display, you can create a private instance to use in the strategy and also use AddChartIndicator() to display that indicator. You can see this code generated by the strategy builder by selecting the "Plot On chart" option. I've created/attached an example that helps to illustrate this.

            Click image for larger version

Name:	ezrakoper-1.PNG
Views:	42
Size:	136.0 KB
ID:	1108895
            Paul H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by WHICKED, Today, 01:01 PM
            1 response
            3 views
            0 likes
            Last Post NinjaTrader_Kate  
            Started by polo0202, Today, 12:15 PM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_BrendanC  
            Started by GARZONJ, Today, 11:27 AM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by NtFan, Today, 11:08 AM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Kate  
            Started by anamaria0589, Today, 09:39 AM
            1 response
            14 views
            0 likes
            Last Post NinjaTrader_Jermaine  
            Working...
            X