Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Oscillator crosses zero line to generate arrow

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

    Oscillator crosses zero line to generate arrow

    I am still very new to C# and have a simple mind question. I model from UniversalOsc from this forum, try to display UP arrow when the indicator crosses zero from below and display DOWN arrow when the indicator crosses zero from above.

    When the indicator is implemented on using historical data, the arrow logic is reversed. (See diagram) When the indicator is implemented while replay data is running, the arrow is not displayed. Both hint that the syntax [-1] referring to the previous bar is not working. Do not know how to fix. Appreciate if someone can help.


    namespace NinjaTrader.Indicator
    {
    public class MyUniversalOsc : Indicator
    {
    #region Variables
    private int bandEdge = 20;
    private double a1, b1, c1, c2, c3;
    private DataSeries WhiteNoise, Filt, Peak;
    private double universal;
    #endregion

    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "MyUniversalOscValue"));
    Add(new Plot(Color.FromKnownColor(KnownColor.LightBlue), PlotStyle.Line, "ZeroLine"));
    Overlay = false;
    WhiteNoise = new DataSeries(this);
    Filt = new DataSeries(this);
    Peak = new DataSeries(this);
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 2)
    {
    Peak.Set(0.0000001);
    return;
    }
    if (UniversalOsc(Close, bandEdge)[0] > 0 && UniversalOsc(Close, bandEdge)[-1] < 0)
    DrawArrowUp("myArrow" + CurrentBar, 0, Low[0] - (TickSize), Color.Blue);
    else
    if (UniversalOsc(Close, bandEdge)[0] < 0 && UniversalOsc(Close, bandEdge)[-1] > 0)
    DrawArrowDown("myArrow" + CurrentBar, 0, High[0] + (TickSize), Color.Red);
    else
    {RemoveDrawObject("tag"+CurrentBar);}
    }
    Attached Files

    #2
    Hello Benluk,

    Thank you for your post.

    Please try the following code instead:
    Code:
    if (CrossAbove(UniversalOsc(Close, bandEdge), 0, 1))
    DrawArrowUp("myArrow" + CurrentBar, 0, Low[0] - (TickSize), Color.Blue);
    else 
    if (CrossBelow(UniversalOsc(Close, bandEdge), 0, 1))
    DrawArrowDown("myArrow" + CurrentBar, 0, High[0] + (TickSize), Color.Red);

    Comment


      #3
      Wonderful!!! Thank you Patrick H.

      Comment


        #4
        How to refer previous bar

        Following the same question, how to refer to the value of previous bar in this case? Or direct me to some reading, manual reference, or existing coding examples.

        For example, when I paint the price bar by CCI value, using the values of current minute and previous minute, I have no issue.

        When I paint the price bar by RSI value, using the same technique, the chart is working on historical data. When I implement the indicator by running replay market data, the RSI value reflected by the bar color is always low. This hints that the syntax of referring to previous minutes [-1], [-2], etc. is not working. It may always set to 0. (I do not have debugger) The attached diagram shows that, when replay market data is run, the color always stay at purple between 10:30 to 10:40. I have this issue when I apply this idea in other indicators.

        How to fix? When can I use [-1], [-2] to refer previous bars? When I should not use this sytax to refer previous bars? Appreciate very much if someone can help.


        namespace NinjaTrader.Indicator
        {
        public class MyBarColorRSIstrength : Indicator
        {
        #region Variables
        private int period = 20;
        #endregion

        protected override void Initialize()
        {
        Overlay = true;
        CalculateOnBarClose = false;
        }

        protected override void OnBarUpdate()
        {
        if (CurrentBar < Period) return;

        // Values of previous bar does not seem to work, resulting value is always low.

        double value = (RSI(Period,1)[0] + RSI(Period,1)[-1] + RSI(Period,1)[-1])/3;

        if (value > 70)
        {
        BarColor = Color.DarkGreen;
        }
        if (value > 60 && value <= 70)
        {
        BarColor = Color.OliveDrab;
        }
        if (value > 50 && value <= 60)
        {
        BarColor = Color.Yellow;
        }
        if (value > 40 && value <= 50)
        {
        BarColor = Color.White;
        }
        if (value > 30 && value <= 40)
        {
        BarColor = Color.Violet;
        }
        if (value <= 30)
        {
        BarColor = Color.Purple;
        }

        }
        Attached Files

        Comment


          #5
          Hello Benluk,

          Thank you for your response.

          The current bar is 0, the previous bar is 1, the bar before that is 2, and so on. So instead of -1, you would use 1.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          607 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          353 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          105 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          560 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          561 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X