Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help to fix coding error Cannot Convert String to NijaTrader.Gui.Tools.Simplefont

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

    Help to fix coding error Cannot Convert String to NijaTrader.Gui.Tools.Simplefont

    I am a newbie coding C# and/or NinjaScript.

    Looking for some other codes and reading some documentation I tried to assemble the code attached but I am not able to get what I aimed for:

    1. I want these variables to be accessible to end users to define values as it can be done with other indicators when adding them to a chart
    • ShowBullish (bool)
    • ShowBearish (bool)
    • ShowStrong (bool)
    • ShowNeutral (bool)
    • ShowWeak (bool)
    • TextColor (brush)
    • TextSize (int)
    • TextOffset (int)
    • TextFont (string)

    2. Depending on the selection done for the boolean variables listed before, I pretend this code to print above the candles the text "SB", "NB" or "WB" (text characteristics defined by the "Text..." variables listed before) in case of:
    • "SB" if the close of the candle is in the highest third of the candle range (for bulling candle) or in the lowest third of the candle range (for bearish candle)
    • "NB" if the close of the candle is in the middle third of the candle range
    • "WB" if the close of the candle is in the lowest third of the candle range (for bulling candle) or in the upper highest of the candle range(for bearish candle)
    When compiling the code I got error messages pointing to Draw.Text lines.

    I appreciate any help in fixing these errors.

    Thank you


    Code: BarThirds.cs
    Attached Files
    Last edited by rafo05; 07-27-2022, 05:36 PM.

    #2
    Hello rafo05,

    Thanks for your post.

    I see in the script that you shared that you do not have a SimpleFont object created to use for the Draw.Text() method.

    To draw text on a chart with a customized font, you would need to create and pass in a 'SimpleFont font' argument as seen in the Draw.Text() syntax in the help guide page.

    Draw.Text(NinjaScriptBase owner, string tag, bool isAutoScale, string text, int barsAgo, double y, int yPixelOffset, Brush textBrush, SimpleFont font, TextAlignment alignment, Brush outlineBrush, Brush areaBrush, int areaOpacity)

    See this help guide page for more information about using Draw.Text(): https://ninjatrader.com/support/help.../draw_text.htm

    See this help guide page for more information about SimpleFont and sample code: https://ninjatrader.com/support/help...font_class.htm

    Let me know if I may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Originally posted by NinjaTrader_BrandonH View Post
      Hello rafo05,

      Thanks for your post.

      I see in the script that you shared that you do not have a SimpleFont object created to use for the Draw.Text() method.

      To draw text on a chart with a customized font, you would need to create and pass in a 'SimpleFont font' argument as seen in the Draw.Text() syntax in the help guide page.

      Draw.Text(NinjaScriptBase owner, string tag, bool isAutoScale, string text, int barsAgo, double y, int yPixelOffset, Brush textBrush, SimpleFont font, TextAlignment alignment, Brush outlineBrush, Brush areaBrush, int areaOpacity)

      See this help guide page for more information about using Draw.Text(): https://ninjatrader.com/support/help.../draw_text.htm

      See this help guide page for more information about SimpleFont and sample code: https://ninjatrader.com/support/help...font_class.htm

      Let me know if I may assist further.
      Hi NinjaTrader_BrandonH

      Thank you for your reply.

      I assume if I had C# experience this hint "you do not have a SimpleFont object created to use for the Draw.Text() method. To draw text on a chart with a customized font, you would need to create and pass in a 'SimpleFont font' argument as seen in the Draw.Text() syntax in the help guide page" would had been valuable.

      Before I asked for help in the forum I had read the links you shared and those were too generic.

      Finally, I got direct-to-the-point help from aBarTimer.cs in https://ninjatrader.com/support/foru...ize-of-my-font.

      This is how I got what I needed

      Code:
      [FONT=Courier New][COLOR=black]if (State == State.SetDefaults)[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]{[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]Description = @"Indicator that...";[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]Name = "IndicatorName";[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]:[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]:[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]TextFont = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 14);[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]:[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]}[/COLOR][/FONT]
      
      
      
      [FONT=Courier New][COLOR=black]#region Properties[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]:[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]:[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]:[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black][Display(ResourceType = typeof(Custom.Resource), Name = "TextFont", Description = "Default setting for Text Font", Order=6, GroupName="Parameters")][/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]public NinjaTrader.Gui.Tools.SimpleFont TextFont[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]{ get; set; }[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]:[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]:[/COLOR][/FONT]
      [FONT=Courier New][COLOR=black]#endregion[/COLOR][/FONT]

      Comment


        #4
        Hello rafo05,

        Thanks for your note.

        I am happy to hear you got the issue resolved.

        If you ever need to reference sample code to see how to create a SimpleFont and use it with Draw.Text, you could view the SimpleFont help guide page linked in post #2.

        From the help guide:

        Code:
        // create custom Courier New, make it big and bold
        NinjaTrader.Gui.Tools.SimpleFont [B]myFont[/B] = new Ninj aTrader.Gui.Tools.SimpleFont("Courier New", 12) { Size = 50, Bold = true };
        
        Draw.Text(this, "myTag", false, "Hi There!", 0, Low[0], 5, Brushes.Blue,[B] myFont[/B], TextAlignment.Center, B rushes.Black, null, 1);
        Let me know if I may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        637 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        366 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        107 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        569 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        572 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X