Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Coloring 2 lines on cross over

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

    Coloring 2 lines on cross over

    Hello,
    Can someone give me a code example of how to change the color of 2 moving averages when they cross?
    Thanks

    #2
    Hello CaptainAmericaXX,

    You may view the following thread that goes over changing an indicator(s) color.


    Let us know if we can be of further assistance.
    JCNinjaTrader Customer Service

    Comment


      #3
      JC,
      Thanks for the reply, however I need to change the colors dynamically, on a crossover. For instance, when the top line crosses the bottom I'd like color both lines red, then when the bottom line crosses the top turn both lines green. Any ideas?

      Comment


        #4
        Okay, here is what I have so far. It's working to change the MA color, but it only lasts for one bar. I'd like it to last until the next cross over. I know I can figure this out eventually, but if someone can ease my pain and give me a quick answer,I'd be grateful.
        protected override void Initialize()
        {
        Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Slow"));
        Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Fast"));
        Overlay = true;
        }


        protected override void OnBarUpdate()
        {

        if(CrossAbove(HMA(slowperiod), HMA(fastperiod), 1))
        {
        PlotColors[0][0] = Color.Green;
        PlotColors[1][0] = Color.Green;
        }
        if(CrossBelow(HMA(slowperiod), HMA(fastperiod), 1))
        {
        PlotColors[0][0] = Color.Red;
        PlotColors[1][0] = Color.Red;
        }
        Slow.Set(HMA(slowperiod)[0]);
        Fast.Set(HMA(fastperiod)[0]);

        Comment


          #5
          All right I figured it out. Now I need to get it to overlay on the price. For some reason the indicator in in a new panel. Here's the code.
          protected override void Initialize()
          {
          Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Slow"));
          Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Fast"));
          Overlay = true;
          }


          protected override void OnBarUpdate()
          {

          if(HMA(slowperiod)[0] > HMA(fastperiod)[0])
          {
          PlotColors[0][0] = Color.Green;
          PlotColors[1][0] = Color.Green;
          }
          else
          {
          PlotColors[0][0] = Color.Red;
          PlotColors[1][0] = Color.Red;
          }

          Slow.Set(HMA(slowperiod)[0]);
          Fast.Set(HMA(fastperiod)[0]);
          }

          Comment


            #6
            Well I'm answering my own questions. Maybe this post will be good for someone as clueless as me. Anyway, originally I had Overlay = false; I changed it to true, but only refreshed the indicator with F5 on the chart instead of removing it and reinstalling it when I changed the code to Overlay = true. All is good now.

            Comment


              #7
              Hello CaptainAmericaXX,

              Glad that you were able to get it all figured out, and thanks for posting the resolutions that you found as well.
              JCNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

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