Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How can I print a fixed text ONLY when a condition is met

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

    How can I print a fixed text ONLY when a condition is met

    Hello

    To be brief:
    - I know how print and display fixed text into a indicator's textbox, something like a tittle, etc..
    - I know how print and display a variable into a indicator's textbox, through converting it into a string to then be displayed, etc.

    But I'm trying to print a fixed text only when a specific condition is met, but I'm unable to get it print it only when condition takes place.

    For example:
    Code:
    ...
    private int actual_Value;
    private int critical_Value = 100.00;
    ...
    If (actual_Value =< critical_Value)
    {
        // code to print the text "You are out" into an indicator's textbox with some others values already working as should
    }
    else
    {
        // code to print the text "You are in" into an indicator's textbox with some others values already working as should
    }
    ...

    I tried with an example I found in your site, with something like this:
    Code:
    ...
    private int actual_Value;
    private int critical_Value = 100.00;
    ...
    if (actual_Value =< critical_Value)
        Print("You are out");
    else
        Print("You are in");
    ...
    But the problem is this last example doens't print anything because is not specified to be displayed in the textbox, and I tried to adjust the printing process to the way needed for a textbox, like for example some variables an indicator have to print, first converting them to a string, etc, but I get code errors etc. because I think I haven't found the correct adjustment to get the messages displayed.

    Thank you
    Last edited by futurenow; 02-24-2021, 09:12 AM.

    #2
    Hello futurenow,

    Thanks for your post.

    Are you having trouble getting your condition to evaluate when expected, or are you having difficulty drawing text when the condition becomes true, or when the else block is processed?

    If you open the NinjaScript output window, you can see the prints that are displayed and you can use that to confirm that your conditions are becoming true when you expect.

    To have text drawn on the chart and change based on this condition, you can call Draw.TextFixed in the if and else blocks and use the same tag so when one condition becomes true, it replaces the text that was drawn when the condition was false.

    If you are having difficulty writing syntax for Draw.TextFixed, I suggest using the Strategy Builder to generate syntax.

    Strategy Builder 301 (publicly available) - https://www.youtube.com/watch?v=_KQF2Sv27oE

    Drawing on a chart - https://ninjatrader.com/support/help...ToDrawOnAChart

    We look forward to assisting.

    Comment


      #3
      Hello

      Originally posted by NinjaTrader_Jim View Post
      Are you having trouble getting your condition to evaluate when expected, or are you having difficulty drawing text when the condition becomes true, or when the else block is processed?
      The condition has always been working as should in this case, I was having difficulty drawing a specific text once the condition became true, but now it is fixed with what you suggested, with Draw.TextFixed(). .

      To have text drawn on the chart and change based on this condition, you can call Draw.TextFixed in the if and else blocks and use the same tag so when one condition becomes true, it replaces the text that was drawn when the condition was false.
      Thank you, I could solve the situation with this:
      Draw.TextFixed(this,"TextFixed","You are out!", TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Blue, Brushes.Transparent, 0);

      The message get printed right at the moment the condition is met.


      Now what I need is to add 2 more things to be ready:

      - The most important one, that is to get and display the date and time of the alert event, i.e. the very specific time (hh:mm:ss) and date when the condition is met and the event happens. Note, when in Playback then the Playback time and date. Here the idea would be to have the way to print a message like: "You are out, at 13:45:52 on Thr Feb 25, 2021", so printing the time and date next to the fiexed text.

      - The second is to know how can I change the font, font size, font color, but only in this specific alert without take effect in the rest of the displayed text, beause I tried with some combinations trying to get a possitive results like:
      Draw.TextFixed(this, "TextFiexed", "You are out!", Font.Size.20, TextPosition.BottomLeft, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Red, Brushes.Transparent, 0);

      Draw.TextFixed(this, "sampleTextFixed", "You are out!", TextPosition.BottomLeft, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Font.Arial, Brushes.Red, Brushes.Transparent, 0);


      but when I try to compile Ninjascript get errors like "No overload method 'TextFixed' takes 10 arguments". I also tried adding font color, changing the position order etc. but couldn't get it work, it has only worked in the default way the code line comes, only being able to change what it comes, for example changing Brushes.Blue to Brushes.Red, but it is just for a text frame, not for the font itself.

      I have some hours trying to get these 2 things done but C# isn't as friendly as I would like but no problem, I'm here to improve and learn.


      About to get the time and date I've tried with some variations of the next, but no luck to get the time and date, so from here is only working the condition and the fixed text printed when the condition is met:

      Code:
      ...
      private double NetLiquidation;
      private double FailureLevel = 500;      // example of Unrealized Account Balance level that indicates an unsuccessful strategy.
      private time_n_date_ofFailureLevel;   // here, as far as I know, the time and date would need to be a 'DateTime' variable.
      ...
      [B]if (NetLiquidation <= FailureLevel)
      {
          time_n_date_ofFailureLevel = Times[1][0];      [/B]// I don't know if before this line something needs to pre-setted or anything special to do and get the date and time of the event
      
      [B]    string time_n_date_ofFailureLevelstring = time_n_date_ofFailureLevel.ToString;
      
          Draw.TextFixed(this,"TextFixed","You are out!", TextPosition.BottomLeft, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Blue, Brushes.Transparent,0);
      
          Draw.Text(this, "Time_n_Date", time_n_date_ofFailureLevelstring, TextPosition.BottomRight);[/B]
      // it would be better to print the time and date next to the end of previous printed text, after "You are out!", but I'm not able to get it working
      [B]}[/B]
      ...

      Comment


        #4
        Hello futurenow,

        I replied in one of your other threads regarding how you can get the Playback time or current PC time.

        Code:
        if (Account.Connection.Options.Name == "Playback Connection")
            Print(NinjaTrader.Cbi.Connection.PlaybackConnection.Now);
        else
            Print(DateTime.Now);
        Time[0] could also be used, but this would be the timestamp of the bar, not the exact PC/Playback time.

        To create Draw.TextFixed with custom text, you can use an overload that takes a SimpleFont.

        Draw.TextFixed documentation and overloads - https://ninjatrader.com/support/help..._textfixed.htm

        SimpleFont documentation - https://ninjatrader.com/support/help...font_class.htm

        Draw.TextFixed(NinjaScriptBase owner, string tag, string text, TextPosition textPosition, Brush textBrush, SimpleFont font, Brush outlineBrush, Brush areaBrush, int areaOpacity)
        Code:
        Draw.TextFixed(this, "myTextFixed", "Hello world!", TextPosition.BottomRight, Brushes.Black, new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 12) { Size = 50, Bold = true }, Brushes.White, Brushes.Transparent, 0);
        We look forward to assisting.

        Comment


          #5
          Hello Jim

          Thank you for your replay, it really has helped to me.


          Originally posted by NinjaTrader_Jim View Post
          I replied in one of your other threads regarding how you can get the Playback time or current PC time.
          Yes, I know about this, but the problem is I haven't been able to get working the Playback time using what you have provided, and as Playback is a special case I don't know if it needs a special reference, because about the code:
          Code:
          if (Account.Connection.Options.Name == "Playback Connection")
              Print(NinjaTrader.Cbi.Connection.PlaybackConnection.Now);
          When I try to compile it, Ninjascript Editor shows errors messages every time I make any modification or addition trying to get this working, some kind of errors but for example, the last error it got some minutes ago was: " An object reference is required for non-static field, method, or property 'NinjaTrader.Cbi.Account.Connection.get' "


          Code:
          if (Account.Connection.Options.Name == "Playback Connection")
          Print(NinjaTrader.Cbi.Connection.PlaybackConnection.Now);
          else
          Print(DateTime.Now);
          About the 'else' I'm using the next line of code that is more specific for what I need and print directly in the chart:
          Code:
          Draw.TextFixed(this, "Alert2", "You are out, on " + DateTime.Now, TextPosition.Center, Brushes.Red, new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 12) { Size = 14, Bold = true }, Brushes.Transparent, Brushes.Transparent, 0);
          This is to print the time of the event, and yes, finally it is getting printed on the chart (when the 'if' above for the Playback connections out of the code), but with a funny situation because the printed time is the current PC time but it doesn't keep fixed as I need, it continues running in real-time second-by-second, and what I need is the specific time when an event takes place, when a condition is met, because the idea is if I'm not in front the PC, when I return some minutes later, then to be able to know seeing the exact time the event happened, but actually with 'DateTime.Now' what I see is the current time running second-by-second exactly the same as the PC does, hahaha. How can I indicate to see only the specific time of the event, is there anything like "DateTime.AtEvent" or anything similar to get the fixed "frozen" time I need to see?, i.e. if the event happens at 18:05:40, that's the time I need to see, and not it keeps running to 18:05:41, 18:05:42, 18:05:43, 18:05:44 ...

          Time[0] could also be used, but this would be the timestamp of the bar, not the exact PC/Playback time.
          Could Time[0] maybe work to know either the time of the event in the present day (Live, Demo, Sim), and also for the Playback date, for both cases? I told you this because is not necessary the time has scientific accuracy, so if for example Times[1][0] takes date and time from the incoming datafeed then maybe it would display something very similar to DateTime.Now. Well, that's just my guess, since so far I haven't been able to make any success test with 'Times[1][0]'.


          To create Draw.TextFixed with custom text, you can use an overload that takes a SimpleFont.

          Draw.TextFixed documentation and overloads - https://ninjatrader.com/support/help..._textfixed.htm

          SimpleFont documentation - https://ninjatrader.com/support/help...font_class.htm

          Code:
          Draw.TextFixed(this, "myTextFixed", "Hello world!", TextPosition.BottomRight, Brushes.Black, new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 12) { Size = 50, Bold = true }, Brushes.White, Brushes.Transparent, 0);
          Finally, this about the text is now working perfectly thanks to you!
          Last edited by futurenow; 02-26-2021, 04:32 PM.

          Comment


            #6
            Update for my reply posted around 1 hour ago

            What I'm talking about the time is being displayed "in real-time" exactly as shown in the PC is not exactly that, I'm checking that the time get updated to the last second there is any change in the chart or DOM, so every time the price of the instrument moves, so my 'if' condition is getting a kind of loop, something like if every time it checks for an update (account update?) it refresh and updates the time showing the last time updated to the last second it finds an account update.

            Seeing this I tried to move the conditional 'if' to others parts of the code to see if maybe it was happening because the condition was into 'private void OnAccountItemUpdate(object sender, AccountItemEventArgs e)', but when I put the 'if' into others part of the code the indicator keeps doing the same "loop" auto-updating itself every time the account has a change. Could you please show me how to solve this kind of loop? in order to be able to see only the date of the event, so the initial exact date shown when the condition is met.

            As example, I've tried to put the 'if':
            into 'protected override void OnRender(ChartControl chartControl, ChartScale chartScale)'
            into 'protected override void OnStateChange()'

            I tried in around 4-5 different parts of the code and the same result, not showing the fixed initial time of the event, it keeps updating the time

            Thank you

            Comment


              #7
              Hello futurenow,

              If you are hitting an object reference not set to an instance of an object error, you will need to use prints to see which object on that line is null. Please note that Account.Connection requires there is an Account object in the script named Account. You can test the snippet in a strategy and this will work since an Account object is provided. If you have not added an Account object, you will need to do so if you would like to access Account.Connection.

              Checking for null references - https://ninjatrader.com/support/help...references.htm

              Account object - https://ninjatrader.com/support/help...ount_class.htm

              You can use Time[0] instead, just be aware that it reflects the timestamp of the processing bar rather that the current Playback or PC time.

              Please also note, that if you call a Draw method using the same tag parameter, the drawing object will be updated. It sounds like your logic is becoming true to allow the Draw method to be called with the same tag, which then updates the text with the current PC time from DateTime.Now.

              You can check your logic with prints to see when certain conditions are becoming true if you need to adjust your logic.

              Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

              Please also note that NinjaTrader Drawing methods should not be called in OnRender. OnRender should be used for your own custom SharpDX rendering only. Drawing methods should be called from data processing events like OnBarUpdate.

              We look forward to assisting.

              Comment


                #8
                Originally posted by NinjaTrader_Jim View Post
                Hello futurenow,
                You can check your logic with prints to see when certain conditions are becoming true if you need to adjust your logic.

                Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm
                Thank you for your help Jim

                I could solve the situation when the time was keeping auto-updating every time the condition was met just by adding a ‘bool’ as a kind of switch to make the condition run only once.


                Now in other different case, customizing the indicator you made "Position Display Indicator", into 'OnRender(ChartControl chartControl, ChartScale chartScale)' I'm trying to modify the next line of code, to get custom fixed results in both ways, modifying only the font color, or also modifying the font, font size and font color:
                Code:
                DrawTextLayout(RenderTarget, ChartPanel, ChartPanel.X + HorizontalOffset, ChartPanel.Y + VerticalOffset, valuestring, Font, value < 0 ? negativeBrush : positiveBrush);
                Trying with something like this:
                Code:
                DrawTextLayout(RenderTarget, ChartPanel, ChartPanel.X + HorizontalOffset, ChartPanel.Y + VerticalOffset, valuestring, Brushes.Blue, new NinjaTrader.Gui.Tools.SimpleFont("Arial", 12) { Size = 12, Bold = false }, Brushes.Transparent, Brushes.Transparent, 0);
                Just as example I tried with something more simplified like the next, but I've tried with some different ways, adding and changing the order, etc.:
                Code:
                DrawTextLayout(RenderTarget, ChartPanel, ChartPanel.X + HorizontalOffset, ChartPanel.Y + VerticalOffset, valuestring, Font, Brushes.Blue);
                But no luck, the compiler keeps showing errors.

                This part of the code has a predefined way to put color in what is printed 'value < 0 ? negativeBrush : positiveBrush', but I would like to give other fixed colors to some of the variables. I actually know to do that with 'Draw.TextFixed()' but in this specific case I need a list of variable values printed together in a textbox being separated from the chart.

                What would be the way to get the 2 results I need, ideally setted in one single line similar like you can do it in 'Draw.TextFixed()'.

                Comment


                  #9
                  In the other hand I would like to display the values of Position Display Indicator in a kind of table or board, something similar to the image below. How could I do that? specifying the number of rows and columns, cells width, and the text format for every cell.

                  This would be very helpful for a more organized visual presentation

                  Click image for larger version

Name:	NT8 Chart with custom table.PNG
Views:	589
Size:	82.6 KB
ID:	1145328

                  Comment


                    #10
                    Hello futurenow,

                    Draw.TextFixed and other NinjaTrader Drawing methods should be called outside of OnRender. Only SharpDX code should be used in OnRender.

                    SharpDX code uses SharpDX brushes and does not accept Windows Media Brushes like the NinjaTrader Drawing methods use. Please see below for understanding SharpDX resources. We also suggest following the SampleCustomRender example that comes with NinjaTrader to learn more about using SharpDX with NinjaScript.

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

                    Creating a grid like you mention will require custom SharpDX code. We do not have any pre-written examples that could demonstrate, but I would suggest playing with SharpDX code so you can be more familiar working with RenderTarget.DrawTextLaout() RenderTarget.DrawRect() and also using ChartPanel coordinates.

                    ChartPanel - https://ninjatrader.com/support/help...chartpanel.htm

                    I also see that several threads have been opened regarding this same topic. We ask that you do not open multiple threads/posts regarding the same topic as this affects our ability to assist others in a timely fashion. Please keep this in mind going forward.

                    We look forward to assisting.

                    Comment


                      #11
                      This works:

                      bool condA0 = X;
                      bool condA1= Y;

                      bool condB0 = XOpposite;
                      bool condB1 = YOpposite;


                      string myString0 = "LONG ONLY";
                      string myString1 = "SHORT ONLY";
                      string myString2 = "NO DIRECTION";

                      string mystring3 = (condA0 && condA1) ? myString0 :
                      (condB0 && condB1) ? myString1 : myString2;

                      Draw.TextFixed(this, "longOnly", mystring3, TextPosition.TopRight);​



                      This does not work:

                      bool condA0 = X;
                      bool condA1= Y;

                      bool condB0 = XOpposite;
                      bool condB1 = YOpposite;​

                      if (condA0 && condA1)
                      {
                      Draw.TextFixed(this, "longOnly", "LONG ONLY", TextPosition.TopRight);​
                      }
                      else if (condB0 && condB1)
                      {
                      Draw.TextFixed(this, "shortOnly", "SHORT ONLY", TextPosition.TopRight);​
                      }
                      else
                      {
                      Draw.TextFixed(this, "No Direction", "NO DIRECTION", TextPosition.TopRight);​
                      }
                      Last edited by PaulMohn; 07-05-2024, 06:52 AM.

                      Comment


                        #12
                        Hello PaulMohn,

                        What specifically is not working?

                        With the ternary operator only one tag name is used. With the 'if' branching commands three different tag names are used.

                        Is the issue that the text is being layered on top of each other?
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello NinjaTrader_ChelseaB,

                          Ok I'll test with a single tag asap. Yes the text is superimposed.
                          Thanks

                          Comment


                            #14
                            Hello PaulMohn,

                            Use the same tag name if you want to update the existing object instead of drawing a new object.
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            571 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            331 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            101 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            549 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            550 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X