Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with draw.text

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

    Help with draw.text

    Hello, I am very new to ninjascript but am working to improve my skills and knowledge. The code below works, If trend is 1 it will print that the wave is green in the top right.
    If the wave is red (trend == -1) it will print the text at the specified candle position on the chart. The problem happens when I try to use the Draw.Text for both conditions. If I uncomment the first Draw.Text line (I can leave the Draw.TextFixed line active or comment it out) the indicator no longer works and prints nothing on the chart. I cannot find why this is happening. I have tried using a different Tag, different locations etc but as soon as I have both Draw.Text statements active nothing prints. It compiles fine.

    Any insight on what I am missing?

    Thanks,
    Mark

    if (trend == 1) {
    Draw.TextFixed(this,"drawtag1","Weis Wave is Green", TextPosition.TopRight,Brushes.White, ChartControl.Properties.LabelFont,Brushes.Black,Br ushes.Green,100);
    // Draw.Text(this,"drawtag1",false,"Weis Wave - Green",6,Close[0],-80,Brushes.Green,myFont,TextAlignment.Center,Brush es.Green,Brushes.LightGray,80);}
    else
    if (trend == -1)
    Draw.Text(this,"drawtag1",false,"Weis Wave - Red",5,Close[0],-120,Brushes.Red,myFont,TextAlignment.Center,Brushe s.Red,Brushes.LightGray,80);



    #2
    Hello markbb10,

    Thanks for your post.

    I would suggest keeping the tag names between the draw.TextFixed and the Draw.Text() to be different. If you only change the tag name of Draw.TextFixed then:

    If the trend is 1 then you would see both the Draw.Text Fixed and the green DrawText

    If the trend is -1 then you will see see the green Draw.TextFixed and the red Draw.Text

    Did you want the Draw.TextFixed to change or be removed when trend is -1? If so then you would need to add that to your code block.

    Comment


      #3
      Hi Paul, thanks for the reply. In the end I do not want to use Draw.TextFixed. I want both trend conditions to print using Draw.Text. The problem is I can have one or the other conditions active but not both, if I get rid of the Draw.TextFixed statements and use only the Draw.Text it does not work if both test conditions (trend == 1 or -1) are there.

      Comment


        #4
        Hello markbb10,

        Thanks for your reply.

        I must admit I am confused.

        Are you saying that at all times you want to see both draw.Text() or are you saying you only want to see one or the other, depending on trend?

        If it would help clarify, please feel free to post screenshot that illustrates your goal.

        Comment


          #5
          Sorry Paul, that I am not being clear. Only one Draw.Text will print depending on the value of trend. The problem is if I have both Draw.Text conditions in the code it does not work. If I have one Draw.Text statement in the code it works or if I have one Draw.Text statement and one Draw.TextFixed it works. I cannot have two Draw.Text conditions being evaluated. If I do nothing prints on the chart no matter what the value of trend is.

          Comment


            #6
            Hello markbb10,

            Thanks for your reply and clarification.

            try this:

            if (trend == 1)
            {
            Draw.Text(this,"drawtag1",false,"Weis Wave - Green",6,Close[0],-80,Brushes.Green,myFont,TextAlignment.Center,Brush es.Green,Brushes.LightGray,80);
            }

            if (trend == -1)
            {
            Draw.Text(this,"drawtag1",false,"Weis Wave - Red",5,Close[0],-120,Brushes.Red,myFont,TextAlignment.Center,Brushe s.Red,Brushes.LightGray,80);
            }

            Comment


              #7
              Hi Paul, Exact same behavior. With your statements above it compiles but puts nothing on the chart, including the weis wave in the lower panel. If I comment out
              either Draw.Text line it works as expected

              Comment


                #8
                Hello markbb10,

                Thanks for your reply.

                There must be some other contributing factor here.

                Have you checked the "Log" tab for any error messages when you add the indicator to a chart?

                Comment


                  #9
                  Hi Paul, just ran a test yes there is a log error. Error on calling OnBarUpdate method on bar 5. You are accessing an index with a value that is invalid since it is out of range. I am testing this on a chart though with 3 days of data and at least 50 bars.

                  Comment


                    #10
                    The strange this is the error only appears when both if statements are in force. If either one of them is there it works as expected..

                    Comment


                      #11
                      Hello markbb10,

                      Thanks for your reply.

                      When that error occurs it will cause the indicator to stop so you would not see a display.

                      That type of error is very common and it indicates that something is trying to access a barsago value that does not exist. For your understanding, when a script is loaded, it will process each historical bar starting on the very first bar loaded in your case 3 days ago. So it will execute your code once per bar for each historical bar in order of each bar until the right edge of the chart. What the error message is saying is that on Bar #5 something in your code likely tried to access data more than 5 bars ago. You should review your code for any references to bars ago, such as High[3], Low[6], etc. Or if you have something that is calculating a bars ago you may need to debug using print statements to see what bars ago is actually being calculated.

                      Once you determine the maximum bars ago that would be accessed you can then prevent the error by preventing your code from processing until the script has processed x number of bars. This is done by using a check of the CurrentBar which is the Ninjascript bar counter. for example at the top of OnBarUpdate():

                      if (CurrentBar < 6) return; // do not process below this line until the 6th bar

                      Reference: https://ninjatrader.com/support/help...currentbar.htm

                      Comment


                        #12
                        Thanks Paul, that is helpful, let me see what I can find. The script is very short so it should be obvious.

                        Comment


                          #13
                          Thanks Paul, I found the problem, very much appreciate your help. One last question and I will leave you in peace. In the Draw.Text call in the string field can I just
                          name string type variable there as opposed to a quoted string. My next step is to build a formation multi-line result which I am hoping to do with String.Format but a quick test of just inserting a string variable in that field gives me an error saying The name 'variable' does not exist in the current context

                          Private string StringVariable = " Test String";

                          Draw.Text(this,"drawtagwao,StringVariable,22,Close[0],-20,Brushes.Red,myFont,TextAlignment.Center,Brushes .Red,Brushes.LightGray,80);

                          Comment


                            #14
                            Hello markbb10,

                            Thanks for your reply.

                            Glad to hear of your progress.

                            Yes, you can use string variables.

                            If what you have posted is your actual draw.Text statement then it looks like you are missing the end " for the tag name itself.

                            I am assuming you are using this private string StringVariable = " Test String"; at the class level (above the OnStateChange() method.)

                            Comment


                              #15
                              Thanks Paul, I do not think my approach for what I am doing will work with Draw.Text or Draw.TextFixed but I have learned a ton about both, thank you... I have 4 indicators
                              that I want to correlate against one another. The code I have written can do that now basically for each I know if the status is Long (green) or Short (red). I am trying to print
                              these variables on the screen in a fixed format colored red or green for easy visual reference. The problems I have so far.

                              If I use Draw.TextFixed I can draw one variable at a screen location.
                              I thought I could use String.Format to concatenate the four values into a single string with multi-line formatting and print that at a fixed location but then I lose the ability to color code each status according to its value at the current candle. The color coding is important so I can make a quick visual reference i.e All Red, All Green etc.

                              I thought about Draw.Text and with the offsets I can get that to print pretty well, but I don't like how it is tied to price bars and moves on the screen as the bars update.

                              So now I am thinking can I expose these 4 variables somehow and plot each of them in the market analyzer. This seems like it would be clean, would update each variable with each bar and would be readable. Can you point me to some examples of how to expose a variable within an indicator so that Market Analyzer can see it? This should take up my time for the next week. lol

                              Thanks again, I really appreciate you taking this time, I know how busy you guys are.

                              Mark

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              558 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              324 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
                              545 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              547 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X