Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Live bar indicator

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

    Live bar indicator

    Hi, I am trying to create an indicator, that when the current live bar updates, it places some text on the chart to show the colour of the live bar before it closes, so if the bar is green the text shows it is green, but if it turns to red before it closes the text changes to red.

    Below is the code I have been trying, without success

    Thanks for any help


    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class LiveBarText : Indicator
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Displays text on live bars based on bar color.";
    Name = "LiveBarText";
    Calculate = Calculate.OnEachTick;
    IsOverlay = true;
    }
    }

    protected override void OnBarUpdate()
    {
    // Ensure this runs only on the live bar
    if (CurrentBar < BarsRequiredToPlot || IsFirstTickOfBar)
    return;

    // Determine bar color and display corresponding text
    string displayText = "";
    Brush barBrush = Close[0] > Open[0] ? Brushes.Green : Brushes.Red;

    // Set text based on bar direction
    if (Close[0] > Open[0])
    displayText = "Green Bar";
    else if (Close[0] < Open[0])
    displayText = "Red Bar";

    // Draw text on the chart
    Draw.TextFixed(this, "tag1", "Text to draw", TextPosition.TopRight);
    //Draw.TextFixed(this, "LiveBarInfo", displayText, TextPosition.TopRight, barBrush, new SimpleFont("Arial", 12), Brushes.Transparent, Brushes.Transparent, 0);
    }
    }
    }​

    #2
    Hello sweeper2024,

    What specific problem are you having when running the script? The draw.TextFixd you have now is not using your variable so that will just say text to draw. The commented out Text Fixed does use your variable, you would need to use the variable for the text like that line to have it be dynamic.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Sorry, with trying different things I didn't put the correct code with the dynamic text!

      The thing I cannot get working, is I want there to be some text (above or below, doesn't matter) when the latest bar (which hasn't closed yet) shows in the text the colour of the bar.

      Because the bar is live, the text will change depending on whether the bar is green or red and updates every second.

      Thanks for any advice

      Comment


        #4
        Hello sweeper2024,

        If you want the text above or below the bar you have to use Draw.Text instead of TextFixed, TextFixed puts the text in the corner of the chart.
        JesseNinjaTrader Customer Service

        Comment


          #5
          thanks it worked

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by kujista, 04-25-2023, 02:22 AM
          4 responses
          66 views
          0 likes
          Last Post zeller427  
          Started by KaizenNU, Today, 06:28 PM
          0 responses
          9 views
          0 likes
          Last Post KaizenNU  
          Started by bertochi, Today, 05:25 PM
          0 responses
          14 views
          0 likes
          Last Post bertochi  
          Started by ntbone, 02-12-2025, 01:51 AM
          4 responses
          27 views
          0 likes
          Last Post ntbone
          by ntbone
           
          Started by XanderT, Today, 03:38 PM
          4 responses
          13 views
          0 likes
          Last Post XanderT
          by XanderT
           
          Working...
          X